package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.busi.jx;

import cn.estsh.i3plus.ext.mes.api.busi.jx.IJxCenterWorkingBoardService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesFourMDetail;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesShiftProdCenterRecord;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkCenterExt;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkCenterNotificationBar;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesFourMDetailRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesWorkCenterNotificationBarRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author : wangjie
 * @CreateDate : 2022-02-15
 * @Modify:
 **/
@Slf4j
public class JxCenterWorkingBoardCenterOneService implements IJxCenterWorkingBoardService {

    private IJxCenterWorkingBoardService proxyService0;

    private IJxCenterWorkingBoardService proxyService;

    private MesFourMDetailRepository fourMDetailRepository;

    private MesWorkCenterNotificationBarRepository workCenterNotificationBarRepository;

    public JxCenterWorkingBoardCenterOneService(IJxCenterWorkingBoardService proxyService0, IJxCenterWorkingBoardService proxyService) {
        this.proxyService0 = proxyService0;
        this.proxyService = proxyService;
        this.fourMDetailRepository = (MesFourMDetailRepository) SpringContextsUtil.getBean("mesFourMDetailRepository");
        this.workCenterNotificationBarRepository = (MesWorkCenterNotificationBarRepository) SpringContextsUtil.getBean("mesWorkCenterNotificationBarRepository");
    }

    @Override
    public Map<String, Object> queryCenterWorkingBoardData(Map<String, Object> dataMap) {

        dataMap = proxyService0.queryCenterWorkingBoardData(dataMap);

        MesWorkCenterExt workCenterExtDb = (MesWorkCenterExt) dataMap.get(MesWorkCenterExt.class.getSimpleName());

        String curTime = TimeTool.getNowTime(true);

        packProxyCondition(dataMap, workCenterExtDb, curTime);

        dataMap = proxyService.queryCenterWorkingBoardData(dataMap);

        List<MesShiftProdCenterRecord> shiftProdCenterRecordList = (List<MesShiftProdCenterRecord>) dataMap.get(MesShiftProdCenterRecord.class.getSimpleName());

        dataMap.put("BOARD_TITLE", workCenterExtDb.getWorkCenterName() + "产线运行状态看板");

        dataMap.put("BOARD_BOTTOM", "PKATJX·松下厨电科技（嘉兴）有限公司");

        dataMap.put("生产状态", dataMap.get(MesExtConstWords.STATUS));

        dataMap.put("异常停工时间", getUnnormalStopCenterData(shiftProdCenterRecordList, curTime));

        dataMap.put("4M变更信息", getFourMChangeData(workCenterExtDb, curTime));

        dataMap.put("公告信息", getWorkCenterNotificationBarData(workCenterExtDb));

        return clearUselessData(dataMap);
    }

    @Override
    public Map<String, Object> clearUselessData(Map<String, Object> dataMap) {
        dataMap.remove(MesWorkCenterExt.class.getSimpleName());
        dataMap.remove(MesShiftProdCenterRecord.class.getSimpleName());
        dataMap.remove(MesExtConstWords.WORK_TIME);
        dataMap.remove(MesExtConstWords.SHIFT_CODE);
        dataMap.remove(MesExtConstWords.WORK_CENTER_CODE);
        dataMap.remove(MesExtConstWords.STATUS);
        dataMap.remove("curTime");
        return dataMap;
    }

    private void packProxyCondition(Map<String, Object> dataMap, MesWorkCenterExt workCenterExtDb, String curTime) {
        if (StringUtils.isEmpty(workCenterExtDb.getWorkTime()) || StringUtils.isEmpty(workCenterExtDb.getShiftCode())) return;
        dataMap.put(MesExtConstWords.WORK_TIME, workCenterExtDb.getWorkTime());
        dataMap.put(MesExtConstWords.SHIFT_CODE, workCenterExtDb.getShiftCode());
        dataMap.put(MesExtConstWords.WORK_CENTER_CODE, workCenterExtDb.getWorkCenterCode());
        dataMap.put("curTime", curTime);
    }

    private List<MesShiftProdCenterRecord> getUnnormalStopCenterData(List<MesShiftProdCenterRecord> shiftProdCenterRecordList, String curTime) {
        List<MesShiftProdCenterRecord> filterList = CollectionUtils.isEmpty(shiftProdCenterRecordList) ? null :
                shiftProdCenterRecordList.stream().filter(o -> (null != o && MesExtEnumUtil.SHIFT_PROD_TYPE.UNNORMAL_STOP_CENTER.getValue() == o.getProdType() && o.getStartTime().compareTo(curTime) <= 0)).collect(Collectors.toList());
        filterList = CollectionUtils.isEmpty(filterList) ? null : filterList.stream().filter(o -> null != o).sorted(Comparator.comparing(MesShiftProdCenterRecord::getStartTime).reversed()).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(filterList) && filterList.size() > 3) filterList = filterList.subList(0, 3);
        return filterList;
    }

    private List<MesFourMDetail> getFourMChangeData(MesWorkCenterExt workCenterExtDb, String curTime) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(workCenterExtDb.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(workCenterExtDb.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getStringRightLikerPack(curTime.substring(0, 10), MesExtConstWords.CREATE_DATE_TIME, packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{MesExtConstWords.CREATE_DATE_TIME}, packBean);
        packBean.setHqlAppend(" limit 3 ");
        List<MesFourMDetail> fourMDetailList = fourMDetailRepository.findByHqlWhere(packBean);
        fourMDetailList.forEach(a -> {
            a.setManageTypeName(MesExtEnumUtil.MANAGE_TYPE.valueOfDescription(a.getManageType()));
        });
        return fourMDetailList;
    }

    private String getWorkCenterNotificationBarData(MesWorkCenterExt workCenterExtDb) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(workCenterExtDb.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(workCenterExtDb.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{MesExtConstWords.MODIFY_DATE_TIME}, packBean);
        MesWorkCenterNotificationBar workCenterNotificationBar = workCenterNotificationBarRepository.getByProperty(packBean);
        return null == workCenterNotificationBar ? "暂无公告" : workCenterNotificationBar.getContent();
    }

}
