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

import cn.estsh.i3plus.ext.mes.api.base.bu.IBuEquipmentCheckService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEquipmentCheck;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesEquipmentCheckRepository;
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.platform.common.util.MesConstWords;
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.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.sqlpack.MesHqlPack;
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.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/2/2 15:19
 **/
@Service
public class BuEquipmentCheckService implements IBuEquipmentCheckService {

    @Autowired
    private MesEquipmentCheckRepository equipmentCheckRepository;

    /**
     * 新增
     *
     * @param equipmentCheck
     * @return
     */
    @Override
    public MesEquipmentCheck insert(MesEquipmentCheck equipmentCheck) {

        ConvertBean.serviceModelInitialize(equipmentCheck, AuthUtil.getSessionUser().getUserName());
        equipmentCheck.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());

        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(equipmentCheck.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getEquNo(), MesExtConstWords.EQU_NO, ddlPackBean);
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getWorkCellCode(), MesExtConstWords.WORK_CELL_CODE, ddlPackBean);
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, ddlPackBean);
            DdlPreparedPack.getStringEqualPack(equipmentCheck.getProjectContent(), MesExtConstWords.PROJECT_CONTENT, ddlPackBean);
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getCheckMethod(), MesExtConstWords.CHECK_METHOD, ddlPackBean);
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getCheckType(), MesExtConstWords.CHECK_TYPE, ddlPackBean);
        MesEquipmentCheck mesEquipmentCheck = equipmentCheckRepository.getByProperty(ddlPackBean);
        if (!ObjectUtils.isEmpty(mesEquipmentCheck)) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode(), "数据已存在");
        }

        //上下限值校验

        if ((!StringUtils.isEmpty(equipmentCheck.getUpLimit()) && StringUtils.isEmpty(equipmentCheck.getDownLimit())) ||
                (StringUtils.isEmpty(equipmentCheck.getUpLimit()) && !StringUtils.isEmpty(equipmentCheck.getDownLimit()))){
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.PARAMETER_EXCEPTION.getCode(), "缺少上下限值,上限值和下限值需同时填写");
        }

        //上限值
        Double upLimit = null;
        //下限值
        Double downLimit = null;

        if(!StringUtils.isEmpty(equipmentCheck.getUpLimit())){
            upLimit =  Double.valueOf(equipmentCheck.getUpLimit());
            String[] standard = equipmentCheck.getUpLimit().split(MesExtConstWords.DECIMAL_POINT);
            if (null != standard && standard.length == 2 && standard[1].length() > 4) {
                throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.PARAMETER_EXCEPTION.getCode(), "最多四位小数");
            }
        }

        if(!StringUtils.isEmpty(equipmentCheck.getDownLimit())){
            downLimit =  Double.valueOf(equipmentCheck.getDownLimit());
            String[] standard = equipmentCheck.getDownLimit().split(MesExtConstWords.DECIMAL_POINT);
            if (null != standard && standard.length == 2 && standard[1].length() > 4) {
                throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.PARAMETER_EXCEPTION.getCode(), "最多四位小数");
            }
        }

        if (!StringUtils.isEmpty(equipmentCheck.getUpLimit()) && !StringUtils.isEmpty(equipmentCheck.getDownLimit())){
            if (upLimit < downLimit){
                throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.PARAMETER_EXCEPTION.getCode(), "上限值不能小于下限值");
            }
        }
        ConvertBean.modelInitialize(equipmentCheck, AuthUtil.getSessionUser());
        equipmentCheck.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
        equipmentCheck.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        return equipmentCheckRepository.insert(equipmentCheck);
    }

    /**
     * 修改
     *
     * @param equipmentCheck
     * @return
     */
    @Override
    public MesEquipmentCheck update(MesEquipmentCheck equipmentCheck) {
        MesEquipmentCheck equipmentCheck2 = equipmentCheckRepository.getById(equipmentCheck.getId());
        if (ObjectUtils.isEmpty(equipmentCheck2)) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "数据不存在");
        }

        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getEquNo(), MesExtConstWords.EQU_NO, ddlPackBean);
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getWorkCellCode(), MesExtConstWords.WORK_CELL_CODE, ddlPackBean);
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, ddlPackBean);
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getProjectContent(), MesExtConstWords.PROJECT_CONTENT, ddlPackBean);
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getCheckMethod(), MesExtConstWords.CHECK_METHOD, ddlPackBean);
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getCheckType(), MesExtConstWords.CHECK_TYPE, ddlPackBean);
        MesEquipmentCheck mesEquipmentCheck = equipmentCheckRepository.getByProperty(ddlPackBean);
        if (!ObjectUtils.isEmpty(mesEquipmentCheck) && mesEquipmentCheck.getId().compareTo(equipmentCheck.getId()) != 0) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode(), "数据已存在");
        }

        //上下限值校验

        if ((!StringUtils.isEmpty(equipmentCheck.getUpLimit()) && StringUtils.isEmpty(equipmentCheck.getDownLimit())) ||
                (StringUtils.isEmpty(equipmentCheck.getUpLimit()) && !StringUtils.isEmpty(equipmentCheck.getDownLimit()))){
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.PARAMETER_EXCEPTION.getCode(), "缺少上下限值,上限值和下限值需同时填写");
        }

        //上限值
        Double upLimit = null;
        //下限值
        Double downLimit = null;

        if(!StringUtils.isEmpty(equipmentCheck.getUpLimit())){
            upLimit =  Double.valueOf(equipmentCheck.getUpLimit());
            String[] standard = equipmentCheck.getUpLimit().split(MesExtConstWords.DECIMAL_POINT);
            if (null != standard && standard.length == 2 && standard[1].length() > 4) {
                throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.PARAMETER_EXCEPTION.getCode(), "最多四位小数");
            }
        }

        if(!StringUtils.isEmpty(equipmentCheck.getDownLimit())){
            downLimit =  Double.valueOf(equipmentCheck.getDownLimit());
            String[] standard = equipmentCheck.getDownLimit().split(MesExtConstWords.DECIMAL_POINT);
            if (null != standard && standard.length == 2 && standard[1].length() > 4) {
                throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.PARAMETER_EXCEPTION.getCode(), "最多四位小数");
            }
        }

        if (!StringUtils.isEmpty(equipmentCheck.getUpLimit()) && !StringUtils.isEmpty(equipmentCheck.getDownLimit())){
            if (upLimit < downLimit){
                throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.PARAMETER_EXCEPTION.getCode(), "上限值不能小于下限值");
            }
        }

        BeanUtils.copyProperties(equipmentCheck, equipmentCheck2);
        ConvertBean.serviceModelUpdate(equipmentCheck2, AuthUtil.getSessionUser().getUserName());
        equipmentCheck.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        return equipmentCheckRepository.save(equipmentCheck2);
    }

    /**
     * 查询
     *
     * @param equipmentCheck
     * @param pager
     * @return
     */
    @Override
    public ListPager<MesEquipmentCheck> queryByPager(MesEquipmentCheck equipmentCheck, Pager pager) {
        DdlPackBean packBean = MesHqlPack.getMesAllData(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getWorkCellCode(), MesExtConstWords.WORK_CELL_CODE, packBean);
        DdlPreparedPack.getNumEqualPack(equipmentCheck.getIsValid(), MesExtConstWords.IS_VALID, packBean);
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(equipmentCheck.getEquNo(), MesExtConstWords.EQU_NO, packBean);
        DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{MesExtConstWords.MODIFY_DATE_TIME}, packBean);
        pager = PagerHelper.getPager(pager, equipmentCheckRepository.findByHqlWhereCount(packBean));
        return new ListPager(equipmentCheckRepository.findByHqlWherePage(packBean, pager), pager);
    }

    /**
     * 根据
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public int updateIsValidById(long id, Integer status) {
        //1、判断单据是否存在
        MesEquipmentCheck equipmentCheck = equipmentCheckRepository.getById(id);
        if (ObjectUtils.isEmpty(equipmentCheck)) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "自动配置不存在");
        }

        return equipmentCheckRepository.updateByProperties(new String[]{MesExtConstWords.ID}, new Object[]{id},
                new String[]{MesConstWords.IS_VALID, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                new Object[]{status, AuthUtil.getSessionUser().getUserName(), TimeTool.getNowTime(true)});
    }

    /**
     * 异常
     *
     * @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();
    }
}
