package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.base;

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesProdOrgExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.api.iservice.base.IConfigService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.ButtonDynamicService;
import cn.estsh.i3plus.mes.pcn.util.PcnHttpUtil;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesConfig;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkCell;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkModule;
import cn.estsh.i3plus.pojo.mes.model.ButtonDynamicModel;
import cn.estsh.i3plus.pojo.mes.repository.MesWorkModuleRepository;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * @Description: 动态按钮
 * @Author: wangjie
 * @Date: 2020/3/17 3:53 下午
 * @Modify:
 */
@Slf4j
@Primary
@Service
public class ButtonDynamicExtService extends ButtonDynamicService {

    @Autowired
    private MesWorkModuleRepository workModuleRepository;

    @Autowired
    private IMesProdOrgExtService prodOrgExtService;

    @Autowired
    private IConfigService configService;

    @Override
    public List<ButtonDynamicModel> filterButtonDynamicModelList(String organizeCode, String workCenterCode, String workCellCode, String windowNo, List<ButtonDynamicModel> buttonDynamicModelList, Integer moduleType) {
        if (CollectionUtils.isEmpty(buttonDynamicModelList) && !StringUtils.isEmpty(moduleType) && moduleType.compareTo(MesEnumUtil.WORK_MODULE_TYPE.BUTTON_MODULE.getValue()) == 0) {
            MesWorkCell workCell = prodOrgExtService.getWorkCellDb(organizeCode, workCenterCode, workCellCode);
            if (null != workCell && !StringUtils.isEmpty(workCell.getGrade()) && workCell.getGrade().compareTo(MesExtEnumUtil.WORK_CELL_GRADE.SCAN.getValue()) == 0) {
                if (doAutoInitWorkCellModuleData(organizeCode, workCenterCode, workCellCode, workCell.getId())) buttonDynamicModelList = findWorkModuleList(organizeCode, windowNo);
            }
        }
        return super.filterButtonDynamicModelList(organizeCode, workCenterCode, workCellCode, windowNo, buttonDynamicModelList, moduleType);
    }

    private List<ButtonDynamicModel> findWorkModuleList(String organizeCode, String windowNo) {
        List<MesWorkModule> workModuleList = getWorkModuleButtonList(organizeCode);
        if (CollectionUtils.isEmpty(workModuleList)) return null;
        List<ButtonDynamicModel> buttonDynamicModelList = new ArrayList<>();
        workModuleList.forEach(o -> buttonDynamicModelList.add(new ButtonDynamicModel(o.getId(), o.getModuleCode(), o.getModuleName(), windowNo, null, o.getSeq(), o.getColor(), o.getMenuCode())));
        return buttonDynamicModelList;
    }

    private List<MesWorkModule> getWorkModuleButtonList(String organizeCode) {
        return workModuleRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.MODULE_TYPE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), MesEnumUtil.WORK_MODULE_TYPE.BUTTON_MODULE.getValue()});
    }

    private Boolean doAutoInitWorkCellModuleData(String organizeCode, String workCenterCode, String workCellCode, Long id) {
        MesConfig configGateWay = configService.getMesConfigByCfgCode(MesPcnEnumUtil.CONFIG_TYPE.GATEWAY_IP.getCode(), organizeCode);
        MesConfig apiCfg = configService.getMesConfigByCfgCode(MesPcnExtConstWords.AUTO_INIT_WORK_CELL_MODULE_URL, organizeCode);
        if (null == configGateWay || StringUtils.isEmpty(configGateWay.getCfgValue())) return false;
        if (null == apiCfg || StringUtils.isEmpty(apiCfg.getCfgValue())) return false;
        log.info("工厂{}生产线{}工位{}: --- 初始化工位按钮 --- START", organizeCode, workCenterCode, workCellCode);
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put(MesPcnExtConstWords.ORGANIZE_CODE, organizeCode);
        paramsMap.put(MesPcnExtConstWords.ID, id.toString());
        String data = PcnHttpUtil.doPost(configGateWay.getCfgValue() + apiCfg.getCfgValue(), paramsMap);
        log.info("工厂{}生产线{}工位{}: --- 初始化工位按钮 --- END --- 执行结果:{}", organizeCode, workCenterCode, workCellCode, data);
        return true;
    }

}
