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

import cn.estsh.i3plus.ext.mes.api.base.bu3.ISxEsopCenterRemainCfgService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEsopCenterRemainCfg;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesEsopCenterRemainCfgRepository;
import cn.estsh.i3plus.ext.mes.pojo.sqlpack.MesExtHqlPack;
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.MathOperation;
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.impp.framework.boot.exception.ImppExceptionBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class SxEsopCenterRemainCfgService implements ISxEsopCenterRemainCfgService {

    @Autowired
    private MesEsopCenterRemainCfgRepository esopCenterRemainCfgRepository;

    @Override
    public ListPager<MesEsopCenterRemainCfg> queryEsopCenterRemainCfgByPager(MesEsopCenterRemainCfg esopCenterRemainCfg, Pager pager) {
        DdlPackBean hqlPack = MesExtHqlPack.getEsopCenterRemainCfg(esopCenterRemainCfg);
        pager = PagerHelper.getPager(pager, esopCenterRemainCfgRepository.findByHqlWhereCount(hqlPack));
        hqlPack.setOrderByStr(esopCenterRemainCfg.orderBy());
        List<MesEsopCenterRemainCfg> esopCenterRemainCfgList = esopCenterRemainCfgRepository.findByHqlWherePage(hqlPack, pager);
        return new ListPager(esopCenterRemainCfgList, pager);
    }

    @Override
    public MesEsopCenterRemainCfg insertEsopCenterRemainCfg(MesEsopCenterRemainCfg esopCenterRemainCfg) {
        checkRemainQty(esopCenterRemainCfg);
        MesEsopCenterRemainCfg esopCenterRemainCfgDb = esopCenterRemainCfgRepository.getByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.WORK_CENTER_CODE},
                new Object[]{esopCenterRemainCfg.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), esopCenterRemainCfg.getWorkCenterCode()});
        if (null != esopCenterRemainCfgDb && CommonEnumUtil.IS_VAILD.VAILD.getValue() == esopCenterRemainCfgDb.getIsValid())
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("作业指导书生产线切替数量配置生产线代码[%s]记录已存在", esopCenterRemainCfg.getWorkCenterCode())
                    .build();
        if (null == esopCenterRemainCfgDb) esopCenterRemainCfgDb = new MesEsopCenterRemainCfg();
        esopCenterRemainCfgDb.setWorkCenterCode(esopCenterRemainCfg.getWorkCenterCode());
        esopCenterRemainCfgDb.setWorkCenterName(esopCenterRemainCfg.getWorkCenterName());
        esopCenterRemainCfgDb.setRemainQty(esopCenterRemainCfg.getRemainQty());
        esopCenterRemainCfgDb.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        if (StringUtils.isEmpty(esopCenterRemainCfgDb.getId())) {
            esopCenterRemainCfgDb.setOrganizeCode(esopCenterRemainCfg.getOrganizeCode());
            ConvertBean.serviceModelInitialize(esopCenterRemainCfgDb, esopCenterRemainCfg.getCreateUser());
            esopCenterRemainCfgDb = esopCenterRemainCfgRepository.insert(esopCenterRemainCfgDb);
        }
        else {
            esopCenterRemainCfgDb.setIsValid(CommonEnumUtil.IS_VAILD.VAILD.getValue());
            ConvertBean.serviceModelUpdate(esopCenterRemainCfgDb, esopCenterRemainCfg.getCreateUser());
            esopCenterRemainCfgDb = esopCenterRemainCfgRepository.save(esopCenterRemainCfgDb);
        }
        return esopCenterRemainCfgDb;
    }

    private void checkRemainQty(MesEsopCenterRemainCfg esopCenterRemainCfg) {
        String[] remainArr = esopCenterRemainCfg.getRemainQty().toString().split(MesExtConstWords.DECIMAL_POINT);
        if (remainArr.length == 2 && Integer.valueOf(remainArr[1]).compareTo(0) > 0)
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("作业指导书生产线切替数量[%s]必须是0或正整数", esopCenterRemainCfg.getRemainQty())
                    .build();
        if (MathOperation.compareTo(esopCenterRemainCfg.getRemainQty(), new Double(0)) < 0)
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("作业指导书生产线切替数量[%s]必须是0或正整数", esopCenterRemainCfg.getRemainQty())
                    .build();
    }

    @Override
    public MesEsopCenterRemainCfg updateEsopCenterRemainCfg(MesEsopCenterRemainCfg esopCenterRemainCfg) {
        checkRemainQty(esopCenterRemainCfg);
        MesEsopCenterRemainCfg esopCenterRemainCfgDb = esopCenterRemainCfgRepository.getByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.WORK_CENTER_CODE},
                new Object[]{esopCenterRemainCfg.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), esopCenterRemainCfg.getWorkCenterCode()});
        if (null == esopCenterRemainCfgDb)
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("作业指导书生产线切替数量配置生产线代码[%s]记录不存在", esopCenterRemainCfg.getWorkCenterCode())
                    .build();
        esopCenterRemainCfgDb.setRemainQty(esopCenterRemainCfg.getRemainQty());
        esopCenterRemainCfgDb.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        esopCenterRemainCfgDb.setIsValid(CommonEnumUtil.IS_VAILD.VAILD.getValue());
        ConvertBean.serviceModelUpdate(esopCenterRemainCfgDb, esopCenterRemainCfg.getModifyUser());
        return esopCenterRemainCfgRepository.save(esopCenterRemainCfgDb);
    }

    @Override
    public void updateEsopCenterRemainCfgValidById(String idStr, Integer status, String userName) {
        if (status != CommonEnumUtil.IS_VAILD.INVAILD.getValue() && status != CommonEnumUtil.IS_VAILD.VAILD.getValue()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("启用，禁用枚举状态值【%s】未被定义", status)
                    .build();
        }
        MesEsopCenterRemainCfg esopCenterRemainCfgDb = esopCenterRemainCfgRepository.getById(Long.parseLong(idStr));
        if (null == esopCenterRemainCfgDb)
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("作业指导书生产线切替数量配置ID[%s]对应的记录不存在", idStr)
                    .build();
        esopCenterRemainCfgDb.setIsValid(status);
        esopCenterRemainCfgDb.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        ConvertBean.serviceModelUpdate(esopCenterRemainCfgDb, userName);
        esopCenterRemainCfgRepository.save(esopCenterRemainCfgDb);
    }

    @Override
    public void deleteEsopCenterRemainCfgById(String idStr, String userName) {
        MesEsopCenterRemainCfg esopCenterRemainCfgDb = esopCenterRemainCfgRepository.getById(Long.parseLong(idStr));
        if (null == esopCenterRemainCfgDb)
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("作业指导书生产线切替数量配置ID[%s]对应的记录不存在", idStr)
                    .build();
        esopCenterRemainCfgDb.setIsDeleted(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        esopCenterRemainCfgDb.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        ConvertBean.serviceModelUpdate(esopCenterRemainCfgDb, userName);
        esopCenterRemainCfgRepository.save(esopCenterRemainCfgDb);
    }

    @Override
    public List<MesEsopCenterRemainCfg> getEsopCenterRemainCfgAllList(String organizeCode) {
        return esopCenterRemainCfgRepository.findByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue()});
    }
}
