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

import cn.estsh.i3plus.ext.mes.api.base.IConsistencyAffirmPartService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesConsistencyAffirmPart;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesConsistencyAffirmPartRepository;
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 ConsistencyAffirmPartService implements IConsistencyAffirmPartService {
    @Autowired
    private MesConsistencyAffirmPartRepository consistencyAffirmPartRDao;
    @Override
    public void insertConsistencyAffirmPart(MesConsistencyAffirmPart consistencyAffirmPart) {
        //数据唯一性校验
        StringBuffer whereStr = new StringBuffer();

        // 封装唯一校验查询条件
        HqlPack.getStringEqualPack(consistencyAffirmPart.getOrganizeCode(), MesExtConstWords.ORGANIZE_CODE, whereStr);
        HqlPack.getStringEqualPack(consistencyAffirmPart.getPartNo(), MesExtConstWords.PART_NO, whereStr);
        //HqlPack.getNumEqualPack(consistencyAffirmPart.getEssences(), MesExtConstWords.ESSENCES, whereStr);

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

        ConvertBean.serviceModelInitialize(consistencyAffirmPart, AuthUtil.getSessionUser().getUserName());
        consistencyAffirmPartRDao.insert(consistencyAffirmPart);
    }

    @Override
    public void updateConsistencyAffirmPart(MesConsistencyAffirmPart consistencyAffirmPart) {
        //数据唯一性校验
        StringBuffer whereStr = new StringBuffer();

        // 封装唯一校验查询条件
        HqlPack.getNumNOEqualPack(consistencyAffirmPart.getId(),MesExtConstWords.ID, whereStr);
        HqlPack.getStringEqualPack(consistencyAffirmPart.getOrganizeCode(), MesExtConstWords.ORGANIZE_CODE, whereStr);
        HqlPack.getStringEqualPack(consistencyAffirmPart.getPartNo(), MesExtConstWords.PART_NO, whereStr);
        //HqlPack.getNumEqualPack(consistencyAffirmPart.getEssences(), MesExtConstWords.ESSENCES, whereStr);

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

        ConvertBean.serviceModelUpdate(consistencyAffirmPart, AuthUtil.getSessionUser().getUserName());
        consistencyAffirmPartRDao.save(consistencyAffirmPart);
    }

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

        int num = consistencyAffirmPartRDao.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 updateConsistencyAffirmPartValidById(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 consistencyAffirmPartRDao.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<MesConsistencyAffirmPart> queryConsistencyAffirmPartByPager(MesConsistencyAffirmPart consistencyAffirmPart, Pager pager) {
        if (null == consistencyAffirmPart) {
            pager = PagerHelper.getPager(pager, consistencyAffirmPartRDao.listCount());
            return new ListPager(consistencyAffirmPartRDao.listPager(pager), pager);
        } else {
            DdlPackBean hqlPack = MesExtHqlPack.getMesConsistencyAffirmPart(consistencyAffirmPart);
            hqlPack.setOrderByStr(consistencyAffirmPart.orderBy());
            pager = PagerHelper.getPager(pager, consistencyAffirmPartRDao.findByHqlWhereCount(hqlPack));
            return new ListPager(consistencyAffirmPartRDao.findByHqlWherePage(hqlPack, pager), pager);
        }
    }
}
