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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu3.IBuPersonCheckDetailService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOrderJob;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPersonCheckDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkLicense;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOrderJobRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesPersonCheckDetailRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesWorkLicenseRepository;
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.enumutil.CommonEnumUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-bu
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2023-04-14 10:37
 */
@Service
public class BuPersonCheckDetailService implements IBuPersonCheckDetailService {

    @Autowired
    private MesPersonCheckDetailRepository personCheckDetailRDao;

    @Autowired
    private MesWorkLicenseRepository workLicenseRepository;

    @Autowired
    private MesOrderJobRepository mesOrderJobRDao;

    @Override
    public void updateTaskUserByCell(MesPersonCheckDetail personCheckDetail) {
        // 校验该人员上岗证是否有效
        MesWorkLicense workLicenseDb = workLicenseRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.WORK_LICENSE_CODE},
                new Object[]{personCheckDetail.getOrganizeCode(),
                        CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                        CommonEnumUtil.IS_DEAL.NO.getValue(),
                        personCheckDetail.getWorkLicenseCode()
                });
        try {
            if(workLicenseDb == null){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("作业人员上岗证【%s】未维护,请重新录入", personCheckDetail.getWorkLicenseCode())
                        .build();
            }

            int intervalDays = TimeTool.getSecoundsBetweenTime(0, workLicenseDb.getValidityDate(), TimeTool.getNowTime(false));
            if(intervalDays > 0){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("当前作业人员上岗证【%s（%s）】已超过有效期,请重新录入", workLicenseDb.getWorkLicenseCode(), workLicenseDb.getUserName())
                        .build();
            }
        } catch (ParseException e) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("上岗证编码[%s]有效期格式无效,请重新维护", workLicenseDb.getWorkLicenseCode())
                    .build();
        }

        personCheckDetailRDao.updateByProperties(new String[]{MesPcnExtConstWords.ORDER_NO, MesPcnExtConstWords.WORK_CELL_CODE}, new Object[]{personCheckDetail.getOrderNo(), personCheckDetail.getWorkCellCode()},
                new String[]{MesPcnExtConstWords.TASK_USER, MesPcnExtConstWords.WORK_LICENSE_CODE, MesPcnExtConstWords.REMARK, MesPcnExtConstWords.WORK_LICENSE_VALIDITY_DATE, MesPcnExtConstWords.MODIFY_USER, MesPcnExtConstWords.MODIFY_DATE_TIME},
                new Object[]{workLicenseDb.getUserName(), workLicenseDb.getWorkLicenseCode(), workLicenseDb.getRemark(), workLicenseDb.getValidityDate(), personCheckDetail.getModifyUser(), TimeTool.getNowTime(true)});
    }

    @Override
    public MesWorkLicense getTaskUserByWorkLicenseCode(MesPersonCheckDetail personCheckDetail) {
        return workLicenseRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.WORK_LICENSE_CODE},
                new Object[]{personCheckDetail.getOrganizeCode(),
                        CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                        CommonEnumUtil.IS_DEAL.NO.getValue(),
                        personCheckDetail.getWorkLicenseCode()
        });
    }

    @Override
    public String checkWorkLicenseValidityDate(String workLicenseCode, MesWorkLicense workLicenseDb) {
        try {
            if(workLicenseDb == null){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("作业人员上岗证【%s】未维护,请重新录入", workLicenseCode)
                        .build();
            }

            int intervalDays = TimeTool.getSecoundsBetweenTime(0, workLicenseDb.getValidityDate(), TimeTool.getNowTime(false));
            if(intervalDays > 0){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("当前作业人员上岗证【%s（%s）】已超过有效期,请重新录入", workLicenseDb.getWorkLicenseCode(), workLicenseDb.getUserName())
                        .build();
            }

            if(intervalDays >= -6){
                return String.format("当前上岗证【%s（%s）】有效期即将过期,请及时更新", workLicenseDb.getWorkLicenseCode(), workLicenseDb.getUserName());
            }
        } catch (ParseException e) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("上岗证编码[%s]有效期格式无效,请重新维护", workLicenseDb.getWorkLicenseCode())
                    .build();
        }

        return "";
    }

    private void updateOrderJobStatus(MesPersonCheckDetail personCheckDetail) {
        //单据主表状态改为点检中
        MesOrderJob mesOrderJob = mesOrderJobRDao.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.ORDER_NO},
                new Object[]{personCheckDetail.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(),
                        MesPcnExtEnumUtil.IS_VAILD.VAILD.getValue(), personCheckDetail.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, personCheckDetail.getModifyUser());
            mesOrderJobRDao.save(mesOrderJob);
        }

        /*//查询单据下点检单是否全部完成
        DdlPackBean packBean = MesPcnHqlPack.getAllBaseData(personCheckDetail.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(personCheckDetail.getOrderNo(), MesPcnExtConstWords.ORDER_NO, packBean);
        DdlPreparedPack.getInPackArray(new Object[]{MesPcnExtEnumUtil.MES_ORJ_ORDER_STATUS.CREATE.getValue(), MesPcnExtEnumUtil.MES_ORJ_ORDER_STATUS.CHECKING.getValue()},
                MesPcnExtConstWords.ORDER_STATUS, packBean);
        List<MesPersonCheckDetail> checkDetails = personCheckDetailRDao.findByHqlWhere(packBean);
        if (CollectionUtils.isEmpty(checkDetails)) {
            mesOrderJob.setOrderStatus(MesPcnExtEnumUtil.MES_ORJ_ORDER_STATUS.CHECK_COMPLETE.getValue());
            mesOrderJob.setComplateTime(TimeTool.getNowTime(true));
            ConvertBean.serviceModelUpdate(mesOrderJob, personCheckDetail.getModifyUser());
            mesOrderJobRDao.save(mesOrderJob);
            return true;
        }
        return false;*/
    }
}
