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

import cn.estsh.i3plus.ext.mes.api.base.IProjectConfirmService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProjectConfirm;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProjectConfirmRepository;
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.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;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/02/07 15:46
 * @Description:
 **/
@Service
public class ProjectConfirmService implements IProjectConfirmService {
    @Autowired
    private MesProjectConfirmRepository projectConfirmRDao;
    @Override
    public void insertProjectConfirm(MesProjectConfirm projectConfirm) {
        //数据唯一性校验
        StringBuffer whereStr = new StringBuffer();

        // 封装唯一校验查询条件
        HqlPack.getStringEqualPack(projectConfirm.getOrganizeCode(), MesExtConstWords.ORGANIZE_CODE, whereStr);
        HqlPack.getNumEqualPack(projectConfirm.getProdCfgTypeClassify(), MesExtConstWords.PROD_CFG_TYPE_CLASSIFY, whereStr);
        HqlPack.getStringEqualPack(projectConfirm.getProdCfgTypeCode(), MesExtConstWords.PROD_CFG_TYPE_CODE, whereStr);

        if (projectConfirmRDao.isExitByHqlWhere(whereStr.toString())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("项目分类【%s】、项目代码【%s】在工程确认项目信息中已存在"
                            , projectConfirm.getProdCfgTypeClassify(), projectConfirm.getProdCfgTypeCode())
                    .setErrorSolution("请更换新增参数")
                    .build();
        }

        ConvertBean.serviceModelInitialize(projectConfirm, AuthUtil.getSessionUser().getUserName());
        projectConfirmRDao.insert(projectConfirm);
    }

    @Override
    public void updateProjectConfirm(MesProjectConfirm projectConfirm) {
        //数据唯一性校验
        StringBuffer whereStr = new StringBuffer();

        // 封装唯一校验查询条件
        HqlPack.getStringEqualPack(projectConfirm.getOrganizeCode(), MesExtConstWords.ORGANIZE_CODE, whereStr);
        HqlPack.getNumEqualPack(projectConfirm.getProdCfgTypeClassify(), MesExtConstWords.PROD_CFG_TYPE_CLASSIFY, whereStr);
        HqlPack.getStringEqualPack(projectConfirm.getProdCfgTypeCode(), MesExtConstWords.PROD_CFG_TYPE_CODE, whereStr);

        if (projectConfirmRDao.isExitByHqlWhere(whereStr.toString())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("项目分类【%s】、项目代码【%s】在工程确认项目信息中已存在"
                            , projectConfirm.getProdCfgTypeClassify(), projectConfirm.getProdCfgTypeCode())
                    .setErrorSolution("请更换新增参数")
                    .build();
        }

        ConvertBean.serviceModelUpdate(projectConfirm, AuthUtil.getSessionUser().getUserName());
        projectConfirmRDao.save(projectConfirm);
    }

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

        int num = projectConfirmRDao.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 updateProjectConfirmValidById(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 projectConfirmRDao.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<MesProjectConfirm> queryProjectConfirmByPager(MesProjectConfirm projectConfirm, Pager pager) {
        if (null == projectConfirm) {
            pager = PagerHelper.getPager(pager, projectConfirmRDao.listCount());
            return new ListPager(projectConfirmRDao.listPager(pager), pager);
        } else {
            DdlPackBean hqlPack = MesExtHqlPack.getMesProjectConfirm(projectConfirm);
            hqlPack.setOrderByStr(projectConfirm.orderBy());
            pager = PagerHelper.getPager(pager, projectConfirmRDao.findByHqlWhereCount(hqlPack));
            List<MesProjectConfirm> projectConfirmList = projectConfirmRDao.findByHqlWherePage(hqlPack, pager);
            if (!CollectionUtils.isEmpty(projectConfirmList)) {
                for (MesProjectConfirm item : projectConfirmList) {
                    if (null == item) {
                        continue;
                    }
                    if (!StringUtils.isEmpty(item.getProdCfgTypeClassify())) {
                        item.setProdCfgTypeClassifyName(MesExtEnumUtil.PROD_CFG_TYPE_CLASSIFY.valueOfDescription(item.getProdCfgTypeClassify()));
                    }
                }
            }
            return new ListPager(projectConfirmList, pager);
        }
    }
}
