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

import cn.estsh.i3plus.ext.mes.api.busi.bu.IMesEmployeeNatureService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesEmployeeNature;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesEmployeeNatureRepository;
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.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 java.util.List;

@Service
public class MesEmployeeNatureService implements IMesEmployeeNatureService {

    @Autowired
    private MesEmployeeNatureRepository employeeNatureRepository;

    /**
     * 员工性质分页查询
     * @param employeeNature
     * @param pager
     * @param organizeCode
     * @return
     */
    @Override
    public ListPager getEmployeeNaturePage(MesEmployeeNature employeeNature, Pager pager, String organizeCode) {
        if (null == employeeNature) {
            pager = PagerHelper.getPager(pager, employeeNatureRepository.listCount());
            return new ListPager(employeeNatureRepository.listPager(pager), pager);
        } else {
            DdlPackBean packBean = MesExtHqlPack.getEmployeeNatureSelectQuery(employeeNature, organizeCode);

            int count = employeeNatureRepository.findByHqlWhereCount(packBean);
            pager = PagerHelper.getPager(pager, count);
            DdlPreparedPack.getOrderByPack(new Object[]{"2"}, new String[]{MesExtConstWords.MODIFY_DATE_TIME}, packBean);
            List<MesEmployeeNature> employeeNatureList = employeeNatureRepository.findByHqlWherePage(packBean, pager);

            employeeNatureList.forEach(s -> {
                //s.setColorName(StringUtils.isEmpty(s.getColor()) ? null : MesExtEnumUtil.EMPLOYEE_NATURE_COLOR.valueOfDescription(s.getColor()));
                s.setColorName(s.getColor());
            });

            return new ListPager(employeeNatureList, pager);
        }
    }

    /**
     * 新增员工性质
     * @param employeeNature
     * @param organizeCode
     * @param userName
     */
    @Override
    public void insertEmployeeNature(MesEmployeeNature employeeNature, String organizeCode, String userName) {

        checkRepeat(employeeNature, organizeCode);
        employeeNature.setOrganizeCode(organizeCode);
        ConvertBean.serviceModelInitialize(employeeNature, userName);
        employeeNatureRepository.insert(employeeNature);
    }

    /**
     * 更新员工性质
     * @param employeeNature
     * @param organizeCode
     * @param userName
     */
    @Override
    public void updateEmployeeNature(MesEmployeeNature employeeNature, String organizeCode, String userName) {

        employeeNature.setOrganizeCode(organizeCode);
        ConvertBean.serviceModelUpdate(employeeNature, userName);
        employeeNatureRepository.update(employeeNature);
    }

    private void checkRepeat(MesEmployeeNature employeeNature, String organizeCode) {
        DdlPackBean packBean = MesExtHqlPack.getEmployeeNatureSelectQuery(employeeNature, organizeCode);
        MesEmployeeNature employeeNatureCheck = employeeNatureRepository.getByProperty(packBean);

        if (null != employeeNatureCheck) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("已存在相同员工性质")
                    .build();
        }

        employeeNature.setOrganizeCode(organizeCode);
    }
}
