package com.engine.salary.service.impl;

import com.engine.core.impl.Service;
import com.engine.salary.biz.SalarySobAdjustRuleBiz;
import com.engine.salary.config.SalaryElogConfig;
import com.engine.hrmelog.entity.dto.LoggerContext;
import com.engine.salary.entity.salarysob.bo.SalarySobAdjustRuleBO;
import com.engine.salary.entity.salarysob.param.SalarySobAdjustRuleSaveParam;
import com.engine.salary.entity.salarysob.po.SalarySobAdjustRulePO;
import com.engine.salary.entity.salarysob.po.SalarySobPO;
import com.engine.salary.enums.OperateTypeEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.mapper.salarysob.SalarySobAdjustRuleMapper;
import com.engine.salary.mapper.salarysob.SalarySobMapper;
import com.engine.salary.service.SalarySobAdjustRuleService;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.db.MapperProxyFactory;
import com.engine.salary.util.valid.ValidUtil;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 调薪计薪规则
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SalarySobAdjustRuleServiceImpl extends Service implements SalarySobAdjustRuleService {

    private SalarySobAdjustRuleBiz salarySobAdjustRuleMapper = new SalarySobAdjustRuleBiz();

    SalarySobMapper getSalarySobMapper(){
        return MapperProxyFactory.getProxy(SalarySobMapper.class);
    }

    SalarySobAdjustRuleMapper getSalarySobAdjustRuleMapper(){
        return MapperProxyFactory.getProxy(SalarySobAdjustRuleMapper.class);
    }


    @Override
    public List<SalarySobAdjustRulePO> listBySalarySobId(Long salarySobId) {

        if (salarySobId == null) {
            return Collections.emptyList();
        }

        return salarySobAdjustRuleMapper.listBySalarySobId(salarySobId);

    }

    @Override
    public void save(SalarySobAdjustRuleSaveParam saveParam) {

        ValidUtil.doValidator(saveParam);

        //开启事务
//        SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession();
//        try {

        // 查询薪资账套
        SalarySobPO salarySobPO = getSalarySobMapper().getById(saveParam.getSalarySobId());
        if (Objects.isNull(salarySobPO)) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98379, "参数错误，薪资账套不存在或者已被删除"));
        }
        // 删除之前的调薪计薪规则
        getSalarySobAdjustRuleMapper().deleteBySalarySobIds(Collections.singleton(saveParam.getSalarySobId()));
        // 保存参数转换成薪资账套的调薪计薪规则po
        List<SalarySobAdjustRulePO> salarySobAdjustRulePOS = SalarySobAdjustRuleBO.convert2PO(saveParam, (long) user.getUID());
        // 保存
        if (CollectionUtils.isNotEmpty(salarySobAdjustRulePOS)) {
            salarySobAdjustRuleMapper.batchInsert(salarySobAdjustRulePOS);
        }
        // 记录日志
        LoggerContext<SalarySobPO> loggerContext = new LoggerContext<>();
        loggerContext.setUser(user);
        loggerContext.setTargetId(String.valueOf(salarySobPO.getId()));
        loggerContext.setTargetName(salarySobPO.getName());
        loggerContext.setOperateType(OperateTypeEnum.UPDATE.getValue());
        loggerContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(0, "保存调薪计薪规则"));
        loggerContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(0, "保存调薪计薪规则"));
        SalaryElogConfig.salarySobLoggerTemplate.write(loggerContext);
    }

    @Override
    public void batchSave(Collection<SalarySobAdjustRulePO> salarySobAdjustRulePOS) {
        salarySobAdjustRuleMapper.batchInsert(salarySobAdjustRulePOS);
    }

    @Override
    public void deleteBySalarySobIds(Collection<Long> salarySobIds) {
        salarySobAdjustRuleMapper.deleteBySalarySobIds(salarySobIds);
    }
}
