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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesReworkTaskService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesReworkTaskRequestModel;
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.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesProductionAssembly;
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.util.MesExtEnumUtil;
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 MesFunctionAssemblyReplaceService extends BaseSwsService implements IFsmModuleFunctionService {
    @Autowired
    private IMesReworkTaskService mesReworkTaskService;

    @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(MesFunctionAssemblyReplaceService.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 (MesFunctionAssemblyReplaceService.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 (MesFunctionAssemblyReplaceService.BUSI_TYPE.TWO.value.equals(busiType)) doAssemblySnRepeatByScan(reqBean, resultBean, buttonDynamicModel, resultMap, paramMap);
            //装配件替换
        else if (MesFunctionAssemblyReplaceService.BUSI_TYPE.THREE.value.equals(busiType)) doAssemblySnRepeat(reqBean, resultBean, buttonDynamicModel, resultMap, paramMap);

        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 custSn = paramMap.get(MesPcnExtConstWords.CUST_SN);

        MesReworkTaskRequestModel requestModel = new MesReworkTaskRequestModel();
        requestModel.setOrganizeCode(organizeCode);
        requestModel.setCustSn(custSn);
        requestModel.setIsOrigSn(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        List<MesProductionAssembly> mesProductionRecords = mesReworkTaskService.assemblyQuery(requestModel);
        paramMap.put(MesPcnExtConstWords.ORGANIZE_CODE, reqBean.getOrganizeCode());
        //根据配置查询生产队列工位推送信息
        resultMap.put(MesPcnExtConstWords.DATA, mesProductionRecords);
        return resultMap;
    }

    //扫描条码替换装配件
    private Map<String, Object> doAssemblySnRepeatByScan(StationRequestBean reqBean, StationResultBean resultBean,
                                                     ButtonDynamicModel buttonDynamicModel, Map<String, Object> resultMap, Map<String, String> paramMap) {
        final String organizeCode = reqBean.getOrganizeCode();
        final String username = reqBean.getUserInfo();
        final String custSn = paramMap.get(MesPcnExtConstWords.CUST_SN);
        final String sn = paramMap.get(MesPcnExtConstWords.SN);

        if (StringUtils.isEmpty(custSn)) {
            return packResultMap(reqBean, resultBean, resultMap, String.format("[%s]执行装配件替换:客户条码不能为空!", buttonDynamicModel.getButtonName()));
        }
        if (StringUtils.isEmpty(sn)) {
            return packResultMap(reqBean, resultBean, resultMap, String.format("[%s]执行装配件替换:替换条码不能为空!", buttonDynamicModel.getButtonName()));
        }

        MesReworkTaskRequestModel requestModel = new MesReworkTaskRequestModel();
        requestModel.setUserName(username);
        requestModel.setOrganizeCode(organizeCode);
        requestModel.setCustSn(custSn);
        requestModel.setSn(sn);
        requestModel.setIsOrigSn(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        requestModel.setAssemblyPartStatus(MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_10.getValue());
        mesReworkTaskService.doAssemblySnRepeatByScan(requestModel);

        requestModel.setAssemblyPartStatus(null);
        List<MesProductionAssembly> mesProductionRecords = mesReworkTaskService.assemblyQuery(requestModel);
        paramMap.put(MesPcnExtConstWords.ORGANIZE_CODE, reqBean.getOrganizeCode());
        //根据配置查询生产队列工位推送信息
        resultMap.put(MesPcnExtConstWords.DATA, mesProductionRecords);
        return resultMap;
    }

    //装配件替换
    private Map<String, Object> doAssemblySnRepeat(StationRequestBean reqBean, StationResultBean resultBean,
                                                         ButtonDynamicModel buttonDynamicModel, Map<String, Object> resultMap, Map<String, String> paramMap) {
        final String organizeCode = reqBean.getOrganizeCode();
        final String username = reqBean.getUserInfo();
        final String custSn = paramMap.get(MesPcnExtConstWords.CUST_SN);
        final String sn = paramMap.get(MesPcnExtConstWords.SN);
        final String assemblyIds = paramMap.get(MesPcnExtConstWords.ASSEMBLY_IDS);

        if (StringUtils.isEmpty(custSn)) {
            return packResultMap(reqBean, resultBean, resultMap, String.format("[%s]执行装配件替换:客户条码不能为空!", buttonDynamicModel.getButtonName()));
        }
        if (StringUtils.isEmpty(sn)) {
            return packResultMap(reqBean, resultBean, resultMap, String.format("[%s]执行装配件替换:替换条码不能为空!", buttonDynamicModel.getButtonName()));
        }
        if (StringUtils.isEmpty(assemblyIds)) {
            return packResultMap(reqBean, resultBean, resultMap, String.format("[%s]执行装配件替换:替换装配件id不能为空!", buttonDynamicModel.getButtonName()));
        }

        MesReworkTaskRequestModel requestModel = new MesReworkTaskRequestModel();
        requestModel.setUserName(username);
        requestModel.setOrganizeCode(organizeCode);
        requestModel.setCustSn(custSn);
        requestModel.setSn(sn);
        requestModel.setAssemblyIds(assemblyIds);
        requestModel.setIsOrigSn(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        requestModel.setAssemblyPartStatus(MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_10.getValue());
        mesReworkTaskService.doAssemblySnRepeat(requestModel);
        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", "查询装配件"),
        TWO("2", "扫描条码替换装配件"),
        THREE("3", "装配件替换");

        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;
        }

    }
}
