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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IQManageService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOrderJob;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProcessCell;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProcessQManage;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesQManageTaskDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOrderJobRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesProcessCellRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesProcessQManageRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesQManageTaskDetailRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
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.sqlpack.MesPcnHqlPack;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
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.List;
import java.util.stream.Collectors;

/**
 * @Author: jessica.chen
 * @CreateDate: 2021/01/18 16:00
 * @Description:
 **/
@Service
public class QManagService implements IQManageService {

    @Autowired
    private MesQManageTaskDetailRepository mesQManageTaskDetailRDao;

    @Autowired
    private MesProcessQManageRepository mesProcessQManageRDao;

    @Autowired
    private MesOrderJobRepository mesOrderJobRDao;

    @Autowired
    private MesProcessCellRepository processCellRepository;

    @Override
    public List<MesQManageTaskDetail> findMesQManageTaskDetail(MesQManageTaskDetail mesQManageTaskDetail) {
        DdlPackBean packBean = MesPcnHqlPack.getAllBaseData(mesQManageTaskDetail.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(mesQManageTaskDetail.getOrderNo(), "orderNo", packBean);

        return mesQManageTaskDetailRDao.findByHqlWhere(packBean);
    }

    @Override
    public void updateMesQManageTaskDetail(MesQManageTaskDetail mesQManageTaskDetail, String userName) {
        //根据单号查询任务主表信息
        String orderNo = mesQManageTaskDetail.getOrderNo();
        MesOrderJob mesOrderJob = mesOrderJobRDao.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, "orderNo"},
                new Object[]{mesQManageTaskDetail.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(),
                        MesPcnExtEnumUtil.IS_VAILD.VAILD.getValue(), orderNo});

        if(null == mesOrderJob){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("单据【%s】不存在",orderNo)
                    .build();
        }

        MesQManageTaskDetail oldDetail = mesQManageTaskDetailRDao.getById(mesQManageTaskDetail.getId());
        if(!oldDetail.getScrewCutterNo().equals(mesQManageTaskDetail.getScrewCutterNo())) {
            //根据螺批刀编号字段查询工序Q管理项目信息
            DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(mesQManageTaskDetail.getOrganizeCode());
            DdlPreparedPack.getStringEqualPack(oldDetail.getScrewCutterNo(), "screwCutterNo", ddlPackBean);
            DdlPreparedPack.getStringEqualPack(oldDetail.getProcessCode(), "processCode", ddlPackBean);
            DdlPreparedPack.getStringEqualPack(oldDetail.getManageProjects(), "manageProjects", ddlPackBean);


            DdlPackBean cellCondition = DdlPackBean.getDdlPackBean(mesQManageTaskDetail.getOrganizeCode());
            DdlPreparedPack.getStringEqualPack(oldDetail.getWorkCellCode(), "workCellCode", cellCondition);
            DdlPreparedPack.getStringEqualPack(oldDetail.getProcessCode(), "processCode", cellCondition);
            List<MesProcessCell> processCells = processCellRepository.findByHqlWhere(cellCondition);
            List<MesProcessQManage> mesProcessQManageList = mesProcessQManageRDao.findByHqlWhere(ddlPackBean);
            mesProcessQManageList = CollectionUtils.isEmpty(mesProcessQManageList) ? null : mesProcessQManageList.stream().filter(o -> StringUtils.isEmpty(o.getCheckType()) || o.getCheckType().equals(MesPcnExtEnumUtil.MES_Q_MANAGER_CHECK_TYPE.ORDER_CHECK.getValue())).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(processCells)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("未找到【%s】工序与【%s】工位的关系", oldDetail.getProcessCode(),
                                oldDetail.getWorkCellCode())
                        .build();
            }
            if (!CollectionUtils.isEmpty(mesProcessQManageList) && !CollectionUtils.isEmpty(processCells)) {
                for (MesProcessQManage mesProcessQManage : mesProcessQManageList) {
                    mesProcessQManage.setScrewCutterNo(mesQManageTaskDetail.getScrewCutterNo());
                    ConvertBean.serviceModelUpdate(mesProcessQManage, userName);
                    mesProcessQManageRDao.save(mesProcessQManage);
                }
            }
        }
        ConvertBean.serviceModelUpdate(mesQManageTaskDetail,userName);
        mesQManageTaskDetailRDao.save(mesQManageTaskDetail);


        mesOrderJob.setOrderStatus(MesPcnExtEnumUtil.MES_ORJ_ORDER_STATUS.CHECKING.getValue());
        ConvertBean.serviceModelUpdate(mesOrderJob, userName);

        mesOrderJobRDao.save(mesOrderJob);
    }
}
