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

import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkCellPrintQueue;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WorkCellPrintQueueModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesWorkCellPrintQueueRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseModuleService;
import cn.estsh.i3plus.mes.pcn.util.LocaleUtil;
import cn.estsh.i3plus.mes.pcn.util.PojoAttrUtil;
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.MesStateMachineStatus;
import cn.estsh.i3plus.pojo.mes.model.AttrBean;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description : 展示组件：打印
 * @Reference :
 * @Author :wangjie
 * @CreateDate : 2020-06-26 11:18 上午
 * @Modify:
 **/
@Slf4j
@Service("sxOuterPackagePrintModuleService")
public class SxOuterPackagePrintModuleService extends BaseModuleService {

    @Autowired
    private MesWorkCellPrintQueueRepository workCellPrintQueueRepository;

    @Override
    public void init(StationRequestBean reqBean) {
        String info = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "正在监听打印队列...");
        this.sendMessage(reqBean,  null, info, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);

        StationResultBean resultBean = new StationResultBean();
        resultBean.setBusiType(MesPcnEnumUtil.STATION_BUSI_TYPE.MODULE_CONTENT.getValue());
        resultBean.setDataType(MesPcnEnumUtil.STATION_DATA_TYPE.CUSTOM.getValue());
        resultBean.setCustomPageName(MesPcnExtConstWords.SINGLE_PRINT_MODULE);
        resultBean.setDataAttrList(packDataAttrList(reqBean));
        resultBean.setSpecialDisplayData(getStepColIndent(reqBean));

        List<WorkCellPrintQueueModel> resultList = null;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(reqBean.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(reqBean.getWorkCenterCode(), MesPcnExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(reqBean.getWorkCellCode(), MesPcnExtConstWords.WORK_CELL_CODE, packBean);
        DdlPreparedPack.getNumEqualPack(MesPcnEnumUtil.PACKAGE_PRINT_STATUS.UNPRINTED.getValue(), MesPcnExtConstWords.PRINT_STATUS, packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.ASC.getValue()}, new String[]{MesPcnExtConstWords.MODIFY_DATE_TIME}, packBean);
        List<MesWorkCellPrintQueue> workCellPrintQueueList = workCellPrintQueueRepository.findByHqlWhere(packBean);
        Integer count = 10;
        Integer index = 1;
        if (!CollectionUtils.isEmpty(workCellPrintQueueList)) {
            count = count - workCellPrintQueueList.size();

            for (MesWorkCellPrintQueue printQueue : workCellPrintQueueList) {
                if (null == printQueue) {
                    continue;
                }
                if (CollectionUtils.isEmpty(resultList)) {
                    resultList = new ArrayList<>();
                }
                WorkCellPrintQueueModel workCellPrintQueueModel = new WorkCellPrintQueueModel();
                BeanUtils.copyProperties(printQueue, workCellPrintQueueModel);
                workCellPrintQueueModel.setPrintTypeName(MesPcnExtEnumUtil.PRINT_QUEUE_TYPE.valueOfDescription(printQueue.getPrintType()));
                workCellPrintQueueModel.setPrintStatusName(MesPcnEnumUtil.PACKAGE_PRINT_STATUS.valueOfDescription(printQueue.getPrintStatus()));
                workCellPrintQueueModel.setIndex(index);
                resultList.add(workCellPrintQueueModel);
                index ++;
            }
        }

        if (count.compareTo(0) > 0) {
            DdlPackBean packBean1 = DdlPackBean.getDdlPackBean(reqBean.getOrganizeCode());
            DdlPreparedPack.getStringEqualPack(reqBean.getWorkCenterCode(), MesPcnExtConstWords.WORK_CENTER_CODE, packBean1);
            DdlPreparedPack.getStringEqualPack(reqBean.getWorkCellCode(), MesPcnExtConstWords.WORK_CELL_CODE, packBean1);
            DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{MesPcnExtConstWords.MODIFY_DATE_TIME}, packBean1);
            List<MesWorkCellPrintQueue> workCellPrintQueueList1 = workCellPrintQueueRepository.findByHqlTopWhere(packBean1, 20);
            if (!CollectionUtils.isEmpty(workCellPrintQueueList1)) {
                workCellPrintQueueList1 = workCellPrintQueueList1.stream().filter(o -> (null != o && MesPcnExtEnumUtil.PACKAGE_PRINT_STATUS.PRINTED.getValue() == o.getPrintStatus())).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(workCellPrintQueueList1)) {
                    workCellPrintQueueList1 = workCellPrintQueueList1.stream().filter(o -> null != o).sorted(Comparator.comparing(MesWorkCellPrintQueue::getModifyDatetime).reversed()).collect(Collectors.toList());
                    if (workCellPrintQueueList1.size() > count) {
                        workCellPrintQueueList1 = workCellPrintQueueList1.subList(0, count);
                    }
                    for (MesWorkCellPrintQueue printQueue : workCellPrintQueueList1) {
                        if (null == printQueue) {
                            continue;
                        }
                        if (CollectionUtils.isEmpty(resultList)) {
                            resultList = new ArrayList<>();
                        }
                        WorkCellPrintQueueModel workCellPrintQueueModel = new WorkCellPrintQueueModel();
                        BeanUtils.copyProperties(printQueue, workCellPrintQueueModel);
                        workCellPrintQueueModel.setPrintTypeName(MesPcnExtEnumUtil.PRINT_QUEUE_TYPE.valueOfDescription(printQueue.getPrintType()));
                        workCellPrintQueueModel.setPrintStatusName(MesPcnEnumUtil.PACKAGE_PRINT_STATUS.valueOfDescription(printQueue.getPrintStatus()));
                        workCellPrintQueueModel.setColor(MesPcnExtConstWords.GREEN);
                        workCellPrintQueueModel.setIndex(index);
                        resultList.add(workCellPrintQueueModel);
                        index ++;
                    }
                }
            }

        }

        resultBean.setResultList(resultList);
        this.sendMessage(reqBean, resultBean);
    }

    @Override
    public boolean getMonitorData(StationRequestBean reqBean) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(reqBean.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(reqBean.getWorkCenterCode(), MesPcnExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(reqBean.getWorkCellCode(), MesPcnExtConstWords.WORK_CELL_CODE, packBean);
        DdlPreparedPack.getNumEqualPack(MesPcnEnumUtil.PACKAGE_PRINT_STATUS.UNPRINTED.getValue(), MesPcnExtConstWords.PRINT_STATUS, packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.ASC.getValue()}, new String[]{MesPcnExtConstWords.MODIFY_DATE_TIME}, packBean);
        MesWorkCellPrintQueue printQueue = workCellPrintQueueRepository.getByProperty(packBean);
        if (null == printQueue) {
            return false;
        }
        reqBean.setScanInfo(printQueue.getId().toString());
        return true;
    }

    @Override
    public boolean execute(StationRequestBean reqBean) {
        return true;
    }

    @Override
    public boolean execStateModule(StationRequestBean reqBean, List<MesStateMachineStatus> states, Map<String, String> wcpcMap) {
        init(reqBean);
        return true;
    }

    private List<AttrBean> packDataAttrList(StationRequestBean reqBean) {
        List<AttrBean> attrBeanList = new ArrayList<>();
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.INDEX, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "序号"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.PRINT_STATUS_NAME, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "打印状态"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.WORK_ORDER_NO, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "生产工单号"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.PART_NO, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "物料编码"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.PART_NAME, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "物料名称"));
       // PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.SERIAL_NUMBER, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.PACKAGENO, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "大包装条码"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.PRINT_TYPE_NAME, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "打印内容类型"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.WORK_CENTER_CODE_SOURCE, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "来源生产线"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.WORK_CELL_CODE_SOURCE, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "来源工位"));
        return attrBeanList;
    }
}
