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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu3.ISxEsopCenterActionService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesEsopExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProcessCell;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu3.SxEsopCenterDataModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesEsopExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesProcessCellRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
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.i3plus.pojo.mes.bean.MesFile;
import cn.estsh.i3plus.pojo.mes.bean.MesProcess;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrder;
import cn.estsh.i3plus.pojo.mes.repository.MesFileRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesProcessRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesWorkOrderRepository;
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.Iterator;
import java.util.List;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/7/12 9:59
 **/
@Slf4j
public class SxEsopCenterActionCommonService implements ISxEsopCenterActionService {

    @Override
    public SxEsopCenterDataModel exec(SxEsopCenterDataModel data) {

        if (StringUtils.isEmpty(packProcess(data).getProcessCode())) return data;

        MesWorkOrder workOrderDb = getFirstOpenWorkOrderByCenter(data);

        if (StringUtils.isEmpty(packOrderInfo(data, workOrderDb).getWorkOrderNo())) return data;

        List<MesEsopExt> sopList = getSopList(data);

        return packSopList(data, sopList);
    }

    private SxEsopCenterDataModel packProcess(SxEsopCenterDataModel data) {
        if (!StringUtils.isEmpty(data.getProcessCode()) && !StringUtils.isEmpty(data.getProcessName())) return data;

        MesProcessCell processCellDb = getProcessCell(data);
        if (null == processCellDb) return data;

        MesProcess processDb = getProcess(data.getOrganizeCode(), processCellDb.getProcessCode());
        return null == processDb ? data : data.processCode(processDb.getProcessCode()).processName(processDb.getProcessName());
    }

    private MesProcessCell getProcessCell(SxEsopCenterDataModel data) {
        MesProcessCellRepository repository = (MesProcessCellRepository) SpringContextsUtil.getBean("mesProcessCellRepository");
        return repository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.WORK_CENTER_CODE, MesPcnExtConstWords.WORK_CELL_CODE},
                new Object[]{data.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), data.getWorkCenterCode(), data.getWorkCellCode()});
    }

    private MesProcess getProcess(String organizeCode, String processCode) {
        MesProcessRepository repository = (MesProcessRepository) SpringContextsUtil.getBean("mesProcessRepository");
        return repository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.PROCESS_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), processCode});
    }

    private MesWorkOrder getFirstOpenWorkOrderByCenter(SxEsopCenterDataModel data) {
        MesWorkOrderRepository repository = (MesWorkOrderRepository) SpringContextsUtil.getBean("mesWorkOrderRepository");
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(data.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(data.getAreaCode(), MesPcnExtConstWords.AREA_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(data.getWorkCenterCode(), MesPcnExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getNumEqualPack(MesPcnExtEnumUtil.WORK_ORDER_STATUS.OPEN.getValue(), MesPcnExtConstWords.WORK_ORDER_STATUS, packBean);
        if (StringUtils.isEmpty(data.getWorkOrderNo())) {
            DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.ASC.getValue()}, new String[]{MesPcnExtConstWords.SEQ}, packBean);
        } else {
            DdlPreparedPack.getStringEqualPack(data.getWorkOrderNo(), MesPcnExtConstWords.ORDER_NO, packBean);
        }
        return repository.getByProperty(packBean);
    }

    private SxEsopCenterDataModel packOrderInfo(SxEsopCenterDataModel data, MesWorkOrder workOrderDb) {
        if (null == workOrderDb) return data.workOrderNo(null);
        return data.workOrderNo(workOrderDb.getOrderNo()).partNo(workOrderDb.getPartNo()).partNameRdd(workOrderDb.getPartNameRdd());
    }

    private List<MesEsopExt> getSopList(SxEsopCenterDataModel data) {
        MesEsopExtRepository repository = (MesEsopExtRepository) SpringContextsUtil.getBean("mesEsopExtRepository");
        List<MesEsopExt> sopList = repository.findByHqlWhere(getSopListDdlPackBean(data, data.getWorkOrderNo()));
        if (CollectionUtils.isEmpty(sopList)) sopList = repository.findByHqlWhere(getSopListDdlPackBean(data, null));
        return sopList;
    }

    private DdlPackBean getSopListDdlPackBean(SxEsopCenterDataModel data, String workOrderNo) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(data.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(data.getPartNo(), MesPcnExtConstWords.PART_NO, packBean);
        DdlPreparedPack.getStringEqualPack(data.getProcessCode(), MesPcnExtConstWords.PROCESS_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(workOrderNo, MesPcnExtConstWords.WORK_ORDER_NO, packBean);
        if (StringUtils.isEmpty(workOrderNo)) DdlPreparedPack.getIsNull(MesPcnExtConstWords.WORK_ORDER_NO, packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.ASC.getValue()}, new String[]{MesPcnExtConstWords.BC_SEQ}, packBean);
        return packBean;
    }

    private SxEsopCenterDataModel packSopList(SxEsopCenterDataModel data, List<MesEsopExt> sopList) {
        if (CollectionUtils.isEmpty(sopList)) return data;

        MesFileRepository repository = (MesFileRepository) SpringContextsUtil.getBean("mesFileRepository");
        Iterator<MesEsopExt> iterator = sopList.iterator();

        while (iterator.hasNext()) {

            MesEsopExt esopExt = iterator.next();
            MesFile fileDb = (null == esopExt || StringUtils.isEmpty(esopExt.getFileId())) ? null : repository.getById(esopExt.getFileId());

            if (!checkFileIsValid(fileDb)) {
                iterator.remove();
                continue;
            }

            esopExt.setFileUrl(fileDb.getFileUrl());
            esopExt.setFileName(fileDb.getFileName());
            esopExt.setFileOriginName(fileDb.getFileOriginName());
            esopExt.setFileSize(fileDb.getFileSize());
            esopExt.setFileType(fileDb.getFileType());
            esopExt.setGroupName(fileDb.getGroupName());
        }

        data.setEsopExtList(sopList);
        return data;
    }

    private Boolean checkFileIsValid(MesFile fileDb) {
        if (null == fileDb || (CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() ==
                fileDb.getIsDeleted() || CommonEnumUtil.IS_VAILD.INVAILD.getValue() == fileDb.getIsValid())) {
           return false;
        }
        if (StringUtils.isEmpty(fileDb.getFileUrl()) ||
                StringUtils.isEmpty(fileDb.getGroupName()) || StringUtils.isEmpty(fileDb.getFileName())) {
            return false;
        }
        return true;
    }
}
