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

import cn.estsh.i3plus.ext.mes.api.base.bu3.IBuQManageService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProcessQManage;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProcessQManageRepository;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
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.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/01/18 11:41 上午
 * @Description:
 **/
@Service
public class BuQManageService implements IBuQManageService {

    @Autowired
    private MesProcessQManageRepository mesProcessQManageRDao;
    @Override
    public MesProcessQManage insertMesProcessQManage(MesProcessQManage mesProcessQManage) {
        //数据唯一性校验
        StringBuffer whereStr = new StringBuffer();

        // 封装唯一校验查询条件
        HqlPack.getStringEqualPack(mesProcessQManage.getOrganizeCode(), "organizeCode", whereStr);
        HqlPack.getStringEqualPack(mesProcessQManage.getPartNo(), "partNo", whereStr);
        HqlPack.getStringEqualPack(mesProcessQManage.getProcessCode(), "processCode", whereStr);
        HqlPack.getStringEqualPack(mesProcessQManage.getManageProjects(), "manageProjects", whereStr);
        HqlPack.getStringEqualPack(mesProcessQManage.getScrewType(), "screwType", whereStr);
        HqlPack.getStringEqualPack(mesProcessQManage.getTighteningTorque(), "tighteningTorque", whereStr);
        HqlPack.getStringEqualPack(mesProcessQManage.getCheckType(), "checkType", whereStr);

        if (mesProcessQManageRDao.isExitByHqlWhere(whereStr.toString())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("工序编号【%s】、管理项目【%s】、管理内容【%s】、检查项目【%s】、点检类型【%s】在工序Q管理项目信息中已存在"
                            , mesProcessQManage.getProcessCode(), mesProcessQManage.getManageProjects(), mesProcessQManage.getScrewType(), mesProcessQManage.getTighteningTorque(), mesProcessQManage.getCheckType())
                    .setErrorSolution("请更换新增参数")
                    .build();
        }

        ConvertBean.serviceModelInitialize(mesProcessQManage, AuthUtil.getSessionUser().getUserName());
        return mesProcessQManageRDao.insert(mesProcessQManage);
    }

    @Override
    public void updateMesProcessQManage(MesProcessQManage mesProcessQManage, String userName) {
        //数据唯一性校验
        StringBuffer whereStr = new StringBuffer();

        // 封装唯一校验查询条件
        HqlPack.getNumNOEqualPack(mesProcessQManage.getId(), "id", whereStr);
        HqlPack.getStringEqualPack(mesProcessQManage.getOrganizeCode(), "organizeCode", whereStr);
        HqlPack.getStringEqualPack(mesProcessQManage.getPartNo(), "partNo", whereStr);
        HqlPack.getStringEqualPack(mesProcessQManage.getProcessCode(), "processCode", whereStr);
        HqlPack.getStringEqualPack(mesProcessQManage.getManageProjects(), "manageProjects", whereStr);
        HqlPack.getStringEqualPack(mesProcessQManage.getScrewType(), "screwType", whereStr);
        HqlPack.getStringEqualPack(mesProcessQManage.getTighteningTorque(), "tighteningTorque", whereStr);
        HqlPack.getStringEqualPack(mesProcessQManage.getCheckType(), "checkType", whereStr);

        if (mesProcessQManageRDao.isExitByHqlWhere(whereStr.toString())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("工序编号【%s】、管理项目【%s】、管理内容【%s】、检查项目【%s】、点检类型【%s】在工序Q管理项目信息中已存在"
                            , mesProcessQManage.getProcessCode(), mesProcessQManage.getManageProjects(), mesProcessQManage.getScrewType(), mesProcessQManage.getTighteningTorque(), mesProcessQManage.getCheckType())
                    .setErrorSolution("请更换修改参数")
                    .build();
        }

        MesProcessQManage processQManageDb = mesProcessQManageRDao.getById(mesProcessQManage.getId());
        if (null == processQManageDb) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("数据不存在")
                    .build();
        }

        BeanUtils.copyProperties(mesProcessQManage, processQManageDb);
        ConvertBean.serviceModelUpdate(processQManageDb, userName);
        mesProcessQManageRDao.save(processQManageDb);
    }
}
