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

import cn.estsh.i3plus.ext.mes.api.base.IConsistencyAffirmProjectService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesConsistencyAffirmProject;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesConsistencyAffirmProjectRepository;
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/07 15:46
 * @Description:
 **/
@Service
public class ConsistencyAffirmProjectService implements IConsistencyAffirmProjectService {
    @Autowired
    private MesConsistencyAffirmProjectRepository consistencyAffirmProjectRDao;
    @Override
    public void insertConsistencyAffirmProject(MesConsistencyAffirmProject consistencyAffirmProject) {
        //数据唯一性校验
        StringBuffer whereStr = new StringBuffer();

        // 封装唯一校验查询条件
        HqlPack.getStringEqualPack(consistencyAffirmProject.getOrganizeCode(), MesExtConstWords.ORGANIZE_CODE, whereStr);
        HqlPack.getStringEqualPack(consistencyAffirmProject.getPartNo(), MesExtConstWords.PART_NO, whereStr);
        HqlPack.getStringEqualPack(consistencyAffirmProject.getItemDescription(), MesExtConstWords.ITEM_DESCRIPTION, whereStr);

        if (consistencyAffirmProjectRDao.isExitByHqlWhere(whereStr.toString())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("物料编码【%s】、项目描述【%s】在一致性确认物料信息中已存在"
                            , consistencyAffirmProject.getPartNo(), consistencyAffirmProject.getItemDescription())
                    .setErrorSolution("请更换新增参数")
                    .build();
        }

        ConvertBean.serviceModelInitialize(consistencyAffirmProject, AuthUtil.getSessionUser().getUserName());
        consistencyAffirmProjectRDao.insert(consistencyAffirmProject);
    }

    @Override
    public void updateConsistencyAffirmProject(MesConsistencyAffirmProject consistencyAffirmProject) {
        //数据唯一性校验
        StringBuffer whereStr = new StringBuffer();

        // 封装唯一校验查询条件
        HqlPack.getNumNOEqualPack(consistencyAffirmProject.getId(),MesExtConstWords.ID, whereStr);
        HqlPack.getStringEqualPack(consistencyAffirmProject.getOrganizeCode(), MesExtConstWords.ORGANIZE_CODE, whereStr);
        HqlPack.getStringEqualPack(consistencyAffirmProject.getPartNo(), MesExtConstWords.PART_NO, whereStr);
        HqlPack.getStringEqualPack(consistencyAffirmProject.getItemDescription(), MesExtConstWords.ITEM_DESCRIPTION, whereStr);

        if (consistencyAffirmProjectRDao.isExitByHqlWhere(whereStr.toString())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("物料编码【%s】、项目描述【%s】在一致性确认物料信息中已存在"
                            , consistencyAffirmProject.getPartNo(), consistencyAffirmProject.getItemDescription())
                    .setErrorSolution("请更换新增参数")
                    .build();
        }

        ConvertBean.serviceModelUpdate(consistencyAffirmProject, AuthUtil.getSessionUser().getUserName());
        consistencyAffirmProjectRDao.save(consistencyAffirmProject);
    }

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

        int num = consistencyAffirmProjectRDao.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 updateConsistencyAffirmProjectValidById(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 consistencyAffirmProjectRDao.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<MesConsistencyAffirmProject> queryConsistencyAffirmProjectByPager(MesConsistencyAffirmProject consistencyAffirmProject, Pager pager) {
        if (null == consistencyAffirmProject) {
            pager = PagerHelper.getPager(pager, consistencyAffirmProjectRDao.listCount());
            return new ListPager(consistencyAffirmProjectRDao.listPager(pager), pager);
        } else {
            DdlPackBean hqlPack = MesExtHqlPack.getMesConsistencyAffirmProject(consistencyAffirmProject);
            hqlPack.setOrderByStr(consistencyAffirmProject.orderBy());
            pager = PagerHelper.getPager(pager, consistencyAffirmProjectRDao.findByHqlWhereCount(hqlPack));
            return new ListPager(consistencyAffirmProjectRDao.findByHqlWherePage(hqlPack, pager), pager);
        }
    }
}
