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

import cn.estsh.i3plus.ext.mes.api.base.ep.IEpPartDivulgeCheckCfgService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPartDivulgeCheckCfg;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPartDivulgeCheckRecord;
import cn.estsh.i3plus.ext.mes.pojo.model.ep.EpPartDivulgeCheckResultModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesPartDivulgeCheckCfgRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesPartDivulgeCheckRecordRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
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.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
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.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2021-12-20 14:16
 */
@Service
public class EpPartDivulgeCheckCfgService implements IEpPartDivulgeCheckCfgService {

    @Autowired
    private MesPartDivulgeCheckCfgRepository partDivulgeCheckCfgRepository;

    @Autowired
    private MesPartDivulgeCheckRecordRepository partDivulgeCheckRecordRepository;

    @Override
    public MesPartDivulgeCheckCfg insert(MesPartDivulgeCheckCfg partDivulgeCheckCfg) {
        MesPartDivulgeCheckCfg partDivulgeCheckCfgMeta = partDivulgeCheckCfgRepository.getByProperty(new String[]{MesExtConstWords.PART_NO, MesExtConstWords.PART_NAME}, new Object[]{partDivulgeCheckCfg.getPartNo(), partDivulgeCheckCfg.getPartName()});
        if(partDivulgeCheckCfgMeta != null){
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode(), "已存在该物料信息");
        }
        ConvertBean.modelInitialize(partDivulgeCheckCfg, AuthUtil.getSessionUser());
        partDivulgeCheckCfg.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
        partDivulgeCheckCfg.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        partDivulgeCheckCfg.setSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        return partDivulgeCheckCfgRepository.insert(partDivulgeCheckCfg);
    }

    @Override
    public MesPartDivulgeCheckCfg update(MesPartDivulgeCheckCfg partDivulgeCheckCfg) {
        MesPartDivulgeCheckCfg partDivulgeCheckCfgMeta = partDivulgeCheckCfgRepository.getById(partDivulgeCheckCfg.getId());
        if(partDivulgeCheckCfgMeta == null){
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(),ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(),"不存在该物料信息");
        }

        BeanUtils.copyProperties(partDivulgeCheckCfg,partDivulgeCheckCfgMeta,MesExtConstWords.ID);
        partDivulgeCheckCfgMeta.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        ConvertBean.modelUpdate(partDivulgeCheckCfgMeta,AuthUtil.getSessionUser());
        partDivulgeCheckCfgMeta.setSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        partDivulgeCheckCfgMeta.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        return partDivulgeCheckCfgRepository.save(partDivulgeCheckCfgMeta);
    }

    @Override
    public ListPager queryByPager(MesPartDivulgeCheckCfg partDivulgeCheckCfg, Pager pager) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getStringRightLikerPack(partDivulgeCheckCfg.getPartNo(),MesExtConstWords.PART_NO,packBean);
        DdlPreparedPack.getStringRightLikerPack(partDivulgeCheckCfg.getPartName(),MesExtConstWords.PART_NAME,packBean);
        DdlPreparedPack.getNumEqualPack(partDivulgeCheckCfg.getIsValid(), MesExtConstWords.IS_VALID, packBean);
        pager = PagerHelper.getPager(pager,partDivulgeCheckCfgRepository.findByHqlWhereCount(packBean));
        return new ListPager(partDivulgeCheckCfgRepository.findByHqlWherePage(packBean,pager),pager);
    }

    @Override
    public int updateIsValidById(long id, Integer status) {
        MesPartDivulgeCheckCfg happenStationMeta = partDivulgeCheckCfgRepository.getById(id);
        if(StringUtils.isEmpty(happenStationMeta)){
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(),ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(),"没有该记录");
        }

        return partDivulgeCheckCfgRepository.updateByProperties(new String[]{MesExtConstWords.ID}, new Object[]{MesExtConstWords.ID},
                new String[]{MesExtConstWords.IS_VALID, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME, MesExtConstWords.SYSTEM_SYNC_STATUS},
                new Object[]{status, AuthUtil.getSessionUser().getUserName(), TimeTool.getNowTime(true), MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue()});
    }

    @Override
    public EpPartDivulgeCheckResultModel queryPartDivulgeCheckCfg(String organizeCode, String partNo, String packageNo) {

        if(StringUtils.isEmpty(partNo) || StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(packageNo)){
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION.getCode(), "传入参数不全");
        }

        MesPartDivulgeCheckCfg partDivulgeCheckCfg = partDivulgeCheckCfgRepository.getByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_VALID, MesExtConstWords.PART_NO},
                new Object[]{organizeCode, MesExtEnumUtil.IS_VAILD.VAILD.getValue(), partNo});
        EpPartDivulgeCheckResultModel partDivulgeCheckResultModel = new EpPartDivulgeCheckResultModel();
        partDivulgeCheckResultModel.setPartNo(partNo);
        partDivulgeCheckResultModel.setOrganizeCode(organizeCode);
        partDivulgeCheckResultModel.setPackageNo(packageNo);
        if(partDivulgeCheckCfg == null){
            partDivulgeCheckResultModel.setIsNeedCheck(false);
            partDivulgeCheckResultModel.setIsChecked(false);
            return partDivulgeCheckResultModel;
        }

        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(organizeCode, MesExtConstWords.ORGANIZE_CODE,packBean);
        DdlPreparedPack.getStringEqualPack(packageNo, MesExtConstWords.PACKAGENO, packBean);
        DdlPreparedPack.getStringEqualPack(partNo, MesExtConstWords.PART_NO ,packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{MesExtConstWords.CREATE_DATE_TIME}, packBean);
        MesPartDivulgeCheckRecord mesPartDivulgeCheckRecord = partDivulgeCheckRecordRepository.getByProperty(packBean);

        partDivulgeCheckResultModel.setIsNeedCheck(true);
        //24小时泄露检测记录不存在
        if(mesPartDivulgeCheckRecord == null){
            partDivulgeCheckResultModel.setIsChecked(false);
            return partDivulgeCheckResultModel;
        }
        ///24小时泄露检测记录存在
        partDivulgeCheckResultModel.setIsChecked(true);
        partDivulgeCheckResultModel.setCheckResult(mesPartDivulgeCheckRecord.getCheckResult());
        partDivulgeCheckResultModel.setDivulgeValue(mesPartDivulgeCheckRecord.getDivulgeValue());

        return partDivulgeCheckResultModel;
    }

    /**
     * 异常
     * @param softTypeCode softTypeCode
     * @param errorDetailString 错误消息字符串
     * @param imppExceptionEnumCode 异常代码
     * @param args 参数
     */
    private void throwImppException(String softTypeCode, String errorDetailString, String imppExceptionEnumCode, Object... args) {
        throw ImppExceptionBuilder.newInstance()
                .setSystemID(softTypeCode)
                .setErrorCode(imppExceptionEnumCode)
                .setErrorDetail(errorDetailString,args)
                .build();
    }
}
