package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.station.function;

import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.BaseSwsService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.function.IFsmModuleFunctionService;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesWcEquipment;
import cn.estsh.i3plus.pojo.mes.model.ButtonDynamicModel;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
import cn.estsh.i3plus.pojo.mes.repository.MesWcEquipmentRepository;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description : 工位参数按钮事件接口实现【站点绑定容器】
 **/
@Service
public class MesFunctionStationContainerSnBindService extends BaseSwsService implements IFsmModuleFunctionService {
    @Autowired
    private MesWcEquipmentRepository wcEquipmentRDao;

    @Override
    public Object doCustomApiDispatch(StationRequestBean reqBean, StationResultBean resultBean, ButtonDynamicModel buttonDynamicModel) {

        //页面渲染数据集
        Map<String, Object> resultMap = new HashMap<>();

        //验证参数
        Map<String, String> paramMap = null;
        try {
            paramMap = StringUtils.isEmpty(buttonDynamicModel.getFunctionValue()) ? null : JSONObject.parseObject(buttonDynamicModel.getFunctionValue(), Map.class);
        } catch (Exception e) {
        }
        if (CollectionUtils.isEmpty(paramMap) || StringUtils.isEmpty(MesFunctionStationContainerSnBindService.BUSI_TYPE.valueOfDescription(String.valueOf(paramMap.get(MesPcnExtConstWords.BUSI_TYPE))))) {
            resultMap.put(MesPcnExtConstWords.DATA_TYPE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT.getValue());
            return packResultMap(reqBean, resultBean, resultMap, String.format("[%s]参数异常!", buttonDynamicModel.getButtonName()));
        }
        final String busiType = String.valueOf(paramMap.get(MesPcnExtConstWords.BUSI_TYPE));

        //查询
        if (MesFunctionStationContainerSnBindService.BUSI_TYPE.ONE.value.equals(busiType)) return queryAssemblyData(reqBean, resultMap, paramMap);

        this.sendMessage(reqBean, new StationResultBean().writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PICK.getValue()),
                String.format("[%s]%s: %s", buttonDynamicModel.getButtonName(), MesFunctionAssemblyReplaceService.BUSI_TYPE.valueOfDescription(busiType), JSONObject.toJSONString(paramMap)),
                MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);

        resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PICK.getValue());
        resultMap.put(MesPcnExtConstWords.DATA_TYPE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT.getValue());

        if (!resultMap.containsKey(MesPcnExtConstWords.MESSAGE)) {
            resultMap.put(MesPcnExtConstWords.DATA_TYPE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT.getValue());
            resultMap.put(MesPcnExtConstWords.MESSAGE, String.format("[%s]%s成功!", buttonDynamicModel.getButtonName(), MesFunctionAssemblyReplaceService.BUSI_TYPE.valueOfDescription(busiType)));
        }

        return resultMap;
    }

    //查询
    private Map<String, Object> queryAssemblyData(StationRequestBean reqBean, Map<String, Object> resultMap, Map<String, String> paramMap) {
        final String organizeCode = reqBean.getOrganizeCode();
        final String workCenterCode = paramMap.get(MesPcnExtConstWords.WORK_CENTER_CODE);
        final String workCellCode = paramMap.get(MesPcnExtConstWords.WORK_CELL_CODE);

        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(workCenterCode, MesPcnExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(workCellCode, MesPcnExtConstWords.WORK_CELL_CODE, packBean);
        DdlPreparedPack.getOrderBy("createDatetime",CommonEnumUtil.ASC_OR_DESC.DESC.getValue(), packBean);
        List<MesWcEquipment> wcEquipmentList = wcEquipmentRDao.findByHqlWhere(packBean);
        if (CollectionUtils.isEmpty(wcEquipmentList)) {
            MesPcnException.throwFlowException("工位【%s】未绑定设备!", workCellCode);
        }
        MesWcEquipment equipment = wcEquipmentList.get(0);
        paramMap.put(MesPcnExtConstWords.ORGANIZE_CODE, reqBean.getOrganizeCode());
        //根据配置查询生产队列工位推送信息
        resultMap.put(MesPcnExtConstWords.DATA, equipment.getEquipmentCode());
        return resultMap;
    }

    //返回提示信息
    private Map<String, Object> packResultMap(StationRequestBean reqBean, StationResultBean resultBean, Map<String, Object> resultMap, String message) {
        this.sendMessage(reqBean, resultBean, message, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
        resultMap.put(MesPcnExtConstWords.MESSAGE, message);
        return resultMap;
    }

    //业务操作
    enum BUSI_TYPE {
        ONE("1", "查询设备代码");

        private String value;

        private String description;

        BUSI_TYPE(String value, String description) {

            this.value = value;

            this.description = description;

        }

        public String getValue() {
            return value;
        }

        public String getDescription() {
            return description;
        }

        public static String valueOfDescription(String val) {
            if (StringUtils.isEmpty(val)) return null;
            String tmp = null;
            for (int i = 0; i < values().length; i++) {
                if (val.equals(values()[i].value)) {
                    tmp = values()[i].description;
                }
            }
            return tmp;
        }

    }
}
