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

import cn.estsh.i3plus.ext.mes.api.busi.bu.IMesSkillsManagementBenchmarkService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesSkillsManagementBenchmark;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesSkillsManagementBenchmarkRepository;
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.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.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class MesSkillsManagementBenchmarkService implements IMesSkillsManagementBenchmarkService {

    @Autowired
    private MesSkillsManagementBenchmarkRepository skillsManagementBenchmarkRepository;

    /**
     * 技能管理基准分页查询
     * @param skillsManagementBenchmark
     * @param pager
     * @param organizeCode
     * @return
     */
    @Override
    public ListPager getSkillsManagementBenchmarkPage(MesSkillsManagementBenchmark skillsManagementBenchmark, Pager pager, String organizeCode) {
        if (null == skillsManagementBenchmark) {
            pager = PagerHelper.getPager(pager, skillsManagementBenchmarkRepository.listCount());
            return new ListPager(skillsManagementBenchmarkRepository.listPager(pager), pager);
        } else {
            DdlPackBean packBean = MesExtHqlPack.getSkillsManagementBenchmarkSelectQuery(skillsManagementBenchmark, organizeCode);

            int count = skillsManagementBenchmarkRepository.findByHqlWhereCount(packBean);
            pager = PagerHelper.getPager(pager, count);
            DdlPreparedPack.getOrderByPack(new Object[]{CommonEnumUtil.ASC_OR_DESC.DESC.getValue()}, new String[]{MesExtConstWords.MODIFY_DATE_TIME}, packBean);
            List<MesSkillsManagementBenchmark> skillsManagementBenchmarkList = skillsManagementBenchmarkRepository.findByHqlWherePage(packBean, pager);

            skillsManagementBenchmarkList.forEach(s -> {
                s.setSkillLevelName(StringUtils.isEmpty(s.getSkillLevel()) ? null : MesExtEnumUtil.SKILLS_MANAGEMENT_BENCHMARK_SKTLLS_LEVEL.valueOfDescription(s.getSkillLevel()));
            });

            return new ListPager(skillsManagementBenchmarkList, pager);
        }
    }

    /**
     * 新增技能管理基准
     * @param skillsManagementBenchmark
     * @param organizeCode
     * @param userName
     */
    @Override
    public void insertSkillsManagementBenchmark(MesSkillsManagementBenchmark skillsManagementBenchmark, String organizeCode, String userName) {

        checkRepeat(skillsManagementBenchmark, organizeCode);
        skillsManagementBenchmark.setOrganizeCode(organizeCode);
        ConvertBean.serviceModelInitialize(skillsManagementBenchmark, userName);
        skillsManagementBenchmarkRepository.insert(skillsManagementBenchmark);
    }

    /**
     * 更新技能管理基准
     * @param skillsManagementBenchmark
     * @param organizeCode
     * @param userName
     */
    @Override
    public void updateSkillsManagementBenchmark(MesSkillsManagementBenchmark skillsManagementBenchmark, String organizeCode, String userName) {

        skillsManagementBenchmark.setOrganizeCode(organizeCode);
        ConvertBean.serviceModelUpdate(skillsManagementBenchmark, userName);
        skillsManagementBenchmarkRepository.update(skillsManagementBenchmark);
    }

    private void checkRepeat(MesSkillsManagementBenchmark skillsManagementBenchmark, String organizeCode) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(skillsManagementBenchmark.getSkillLevel(),"skillLevel",packBean);
        MesSkillsManagementBenchmark skillsManagementBenchmarkCheck = skillsManagementBenchmarkRepository.getByProperty(packBean);

        if (null != skillsManagementBenchmarkCheck) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("已存在相同技能管理基准")
                    .build();
        }

        skillsManagementBenchmark.setOrganizeCode(organizeCode);
    }
}
