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

import cn.estsh.i3plus.ext.mes.api.base.IProcessCheckService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProcessCheck;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProcessCheckRepository;
import cn.estsh.i3plus.ext.mes.pojo.sqlpack.MesExtHqlPack;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
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.tool.HqlPack;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/02/03 14:43
 * @Description:
 **/
@Service
public class ProcessCheckService implements IProcessCheckService {

    @Autowired
    private MesProcessCheckRepository mesProcessCheckRDao;

    @Override
    public void insertProcessCheck(MesProcessCheck processCheck) {
        //数据唯一性校验
        StringBuffer whereStr = new StringBuffer();

        // 封装唯一校验查询条件
        HqlPack.getStringEqualPack(processCheck.getOrganizeCode(), MesExtConstWords.ORGANIZE_CODE, whereStr);
        HqlPack.getStringEqualPack(processCheck.getProcessCode(), MesExtConstWords.PROCESS_CODE, whereStr);
        HqlPack.getStringEqualPack(processCheck.getCheckInfo(), MesExtConstWords.CHECK_INFO, whereStr);
        HqlPack.getStringEqualPack(processCheck.getPartNo(), MesExtConstWords.PART_NO, whereStr);

        if (mesProcessCheckRDao.isExitByHqlWhere(whereStr.toString())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("物料编号【%s】、点击信息【%s】、工序编号【%s】在工序点检项目信息中已存在"
                            , processCheck.getPartNo(),processCheck.getCheckInfo(), processCheck.getProcessCode())
                    .setErrorSolution("请更换新增参数")
                    .build();
        }

        ConvertBean.serviceModelInitialize(processCheck, AuthUtil.getSessionUser().getUserName());
        mesProcessCheckRDao.insert(processCheck);
    }

    @Override
    public void updateProcessCheck(MesProcessCheck processCheck) {
//数据唯一性校验
        StringBuffer whereStr = new StringBuffer();

        // 封装唯一校验查询条件
        HqlPack.getNumNOEqualPack(processCheck.getId(), MesExtConstWords.ID, whereStr);
        HqlPack.getStringEqualPack(processCheck.getProcessCode(), MesExtConstWords.PROCESS_CODE, whereStr);
        HqlPack.getStringEqualPack(processCheck.getCheckInfo(), MesExtConstWords.CHECK_INFO, whereStr);
        HqlPack.getStringEqualPack(processCheck.getPartNo(), MesExtConstWords.PART_NO, whereStr);

        if (mesProcessCheckRDao.isExitByHqlWhere(whereStr.toString())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("物料编号【%s】、点击信息【%s】、工序编号【%s】在工序点检项目信息中已存在"
                            , processCheck.getPartNo(),processCheck.getCheckInfo(), processCheck.getProcessCode())
                    .setErrorSolution("请更换新增参数")
                    .build();
        }
        ConvertBean.serviceModelUpdate(processCheck, AuthUtil.getSessionUser().getUserName());

        mesProcessCheckRDao.save(processCheck);
    }

    @Override
    public int updateProcessCheckValidByIds(String[] ids, Integer status) {
        StringBuffer wheres = new StringBuffer();
        HqlPack.getInPack(String.join(",", ids), "id", wheres);

        int num = mesProcessCheckRDao.updateByHqlWhere(
                wheres.toString(),
                new String[]{MesExtConstWords.IS_VALID, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                new Object[]{status, AuthUtil.getSessionUser().getUserName(), TimeTool.getNowTime(true)}
        );
        return num;
    }

    @Override
    public int updateProcessCheckValidById(Long id, 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();
        }
        return mesProcessCheckRDao.updateByProperties(new String[]{MesExtConstWords.ID}, new Object[]{id},
                new String[]{MesExtConstWords.IS_VALID, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                new Object[]{status, userName, TimeTool.getNowTime(true)});
    }

    @Override
    public ListPager<MesProcessCheck> queryProcessCheckByPager(MesProcessCheck processCheck, Pager pager) {
        if (null == processCheck) {
            pager = PagerHelper.getPager(pager, mesProcessCheckRDao.listCount());
            return new ListPager(mesProcessCheckRDao.listPager(pager), pager);
        } else {
            DdlPackBean hqlPack = MesExtHqlPack.getMesProcessCheck(processCheck);
            hqlPack.setOrderByStr(processCheck.orderBy());
            pager = PagerHelper.getPager(pager, mesProcessCheckRDao.findByHqlWhereCount(hqlPack));
            return new ListPager(mesProcessCheckRDao.findByHqlWherePage(hqlPack, pager), pager);
        }
    }
}
