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

import cn.estsh.i3plus.ext.mes.api.base.jx.IJxRepairJudgeTypeService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesRepairJudgeType;
import cn.estsh.i3plus.ext.mes.pojo.model.BatchIdModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesRepairJudgeTypeRepository;
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.DdlPreparedPack;
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 java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-jx
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2023-11-13 11:53
 */
@Service
public class JxMesRepairJudgeTypeService implements IJxRepairJudgeTypeService {

    @Autowired
    private MesRepairJudgeTypeRepository repairJudgeTypeRepository;

    @Override
    public ListPager<MesRepairJudgeType> queryRepairJudgeTypePager(MesRepairJudgeType model, Pager pager) {
        DdlPackBean hqlPack = MesExtHqlPack.getRepairJudgeType(model);
        pager = PagerHelper.getPager(pager, repairJudgeTypeRepository.findByHqlWhereCount(hqlPack));
        hqlPack.setOrderByStr(" order by seq asc ");
        return new ListPager(repairJudgeTypeRepository.findByHqlWherePage(hqlPack, pager), pager);
    }

    @Override
    public MesRepairJudgeType insertRepairJudgeType(MesRepairJudgeType model) {
        //数据唯一性校验
        StringBuffer whereStr = new StringBuffer();

        // 封装唯一校验查询条件
        HqlPack.getStringEqualPack(model.getOrganizeCode(), MesExtConstWords.ORGANIZE_CODE, whereStr);
        HqlPack.getStringEqualPack(model.getRepairJudgeCode(), MesExtConstWords.REPAIR_JUDGE_CODE, whereStr);

        if (repairJudgeTypeRepository.isExitByHqlWhere(whereStr.toString())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("维修判定类型代码【%s】在维修判定类型信息中已存在"
                            , model.getRepairJudgeCode())
                    .setErrorSolution("请更换新增参数")
                    .build();
        }

        ConvertBean.serviceModelInitialize(model, AuthUtil.getSessionUser().getUserName());
        return repairJudgeTypeRepository.insert(model);
    }

    @Override
    public void updateRepairJudgeType(MesRepairJudgeType model) {
        MesRepairJudgeType repairJudgeTypeDb = repairJudgeTypeRepository.getById(model.getId());

        if (null == repairJudgeTypeDb)
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("ID[%s]记录不存在", model.getId())
                    .build();

        // 封装唯一校验查询条件
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(model.getRepairJudgeCode(), MesExtConstWords.REPAIR_JUDGE_CODE, packBean);
        MesRepairJudgeType repairJudgeTypeDbIsExist = repairJudgeTypeRepository.getByProperty(packBean);

        if (null != repairJudgeTypeDbIsExist && repairJudgeTypeDbIsExist.getId().compareTo(repairJudgeTypeDbIsExist.getId()) != 0) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("维修判定类型代码【%s】在维修判定类型启用或禁用数据中已存在"
                            , model.getRepairJudgeCode())
                    .setErrorSolution("请更换新增参数")
                    .build();
        }

        repairJudgeTypeDb.setRepairJudgeCode(model.getRepairJudgeCode());
        repairJudgeTypeDb.setRepairJudgeName(model.getRepairJudgeName());
        repairJudgeTypeDb.setSeq(model.getSeq());
        repairJudgeTypeDb.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());

        ConvertBean.serviceModelUpdate(repairJudgeTypeDb, model.getModifyUser());
        repairJudgeTypeRepository.save(repairJudgeTypeDb);
    }

    @Override
    public int updateRepairJudgeTypeValidById(String[] ids, int status, String userName) {
        StringBuffer wheres = new StringBuffer();
        HqlPack.getInPack(String.join(",", ids), MesExtConstWords.ID, wheres);

        int num = repairJudgeTypeRepository.updateByHqlWhere(
                wheres.toString(),
                new String[]{MesExtConstWords.SYSTEM_SYNC_STATUS, MesExtConstWords.IS_VALID, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                new Object[]{MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue(), status, userName, TimeTool.getNowTime(true)}
        );
        return num;
    }

    @Override
    public int deleteRepairJudgeTypeValidById(BatchIdModel model, String userName) {
        StringBuffer wheres = new StringBuffer();
        HqlPack.getInPack(String.join(",", model.getIds()), MesExtConstWords.ID, wheres);

        int num = repairJudgeTypeRepository.updateByHqlWhere(
                wheres.toString(),
                new String[]{MesExtConstWords.SYSTEM_SYNC_STATUS, MesExtConstWords.IS_VALID, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                new Object[]{MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue(), CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), userName, TimeTool.getNowTime(true)}
        );
        return num;
    }

    @Override
    public List<MesRepairJudgeType> queryRepairJudgeList(String organizeCode) {

        return repairJudgeTypeRepository.findByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue()});
    }

    @Override
    public Map<String, String> queryRepairJudgeMap(String organizeCode) {
        List<MesRepairJudgeType> mesRepairJudgeTypes = queryRepairJudgeList(organizeCode);
        return CollectionUtils.isEmpty(mesRepairJudgeTypes) ? null :
                mesRepairJudgeTypes.stream().collect(Collectors.toMap(
                        MesRepairJudgeType::getRepairJudgeCode, MesRepairJudgeType::getRepairJudgeName, (x,y) -> y));
    }


}
