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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IProjectTourConfirmService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOrderJob;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProjectTourDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOrderJobRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesProjectTourDetailRepository;
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.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.i3plus.pojo.base.util.StringUtil;
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 java.util.ArrayList;
import java.util.List;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/02/05 13:52
 * @Description:
 **/
@Service
public class ProjectTourConfirmService implements IProjectTourConfirmService {
    @Autowired
    private MesProjectTourDetailRepository projectTourDetailRDao;
    @Autowired
    private MesOrderJobRepository mesOrderJobRDao;


    @Override
    public List<MesProjectTourDetail> queryProjectTourConfirm(MesProjectTourDetail projectTourDetail) {
        if (StringUtil.isEmpty(projectTourDetail.getOrderNo())) {
            return new ArrayList<>();
        }
        DdlPackBean packBean = MesPcnHqlPack.getAllBaseData(projectTourDetail.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(projectTourDetail.getOrderNo(), MesPcnExtConstWords.ORDER_NO, packBean);
        DdlPreparedPack.getNumEqualPack(projectTourDetail.getProdCfgTypeClassify(), MesPcnExtConstWords.PROD_CFG_TYPE_CLASSIFY, packBean);
        //packBean.setOrderByStr(projectTourDetail.orderBy());
        return projectTourDetailRDao.findByHqlWhere(packBean);
    }

    @Override
    public void updateProjectTourConfirm(MesProjectTourDetail projectTourDetail) {
        MesProjectTourDetail detailById = projectTourDetailRDao.getById(projectTourDetail.getId());
        if (null == detailById) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("该条数据不存在")
                    .build();
        }
        //单据主表状态改为点检中
        MesOrderJob mesOrderJob = mesOrderJobRDao.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.ORDER_NO},
                new Object[]{projectTourDetail.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(),
                        MesPcnExtEnumUtil.IS_VAILD.VAILD.getValue(), projectTourDetail.getOrderNo()});
        if (mesOrderJob.getOrderStatus() == MesPcnExtEnumUtil.MES_ORJ_ORDER_STATUS.CREATE.getValue()) {
            mesOrderJob.setOrderStatus(MesPcnExtEnumUtil.MES_ORJ_ORDER_STATUS.CHECKING.getValue());
            mesOrderJob.setComplateTime(TimeTool.getNowTime(true));
            ConvertBean.serviceModelUpdate(mesOrderJob, projectTourDetail.getModifyUser());
            mesOrderJobRDao.save(mesOrderJob);
        }

        projectTourDetail.setCheckTime(TimeTool.getNowTime(true));
        projectTourDetail.setCheckUser(projectTourDetail.getModifyUser());
        ConvertBean.serviceModelUpdate(projectTourDetail, projectTourDetail.getModifyUser());
        projectTourDetailRDao.save(projectTourDetail);

    }
}
