package com.engine.salary.wrapper;

import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.salarysob.bo.SalarySobAdjustRuleBO;
import com.engine.salary.entity.salarysob.dto.SalaryItemBaseDTO;
import com.engine.salary.entity.salarysob.dto.SalarySobAdjustRuleListDTO;
import com.engine.salary.entity.salarysob.param.SalarySobAdjustRuleItemQueryParam;
import com.engine.salary.entity.salarysob.param.SalarySobAdjustRuleQueryParam;
import com.engine.salary.entity.salarysob.param.SalarySobAdjustRuleSaveParam;
import com.engine.salary.entity.salarysob.po.SalarySobAdjustRulePO;
import com.engine.salary.entity.salarysob.po.SalarySobItemPO;
import com.engine.salary.service.SalaryItemService;
import com.engine.salary.service.SalarySobAdjustRuleService;
import com.engine.salary.service.SalarySobItemService;
import com.engine.salary.service.impl.SalaryItemServiceImpl;
import com.engine.salary.service.impl.SalarySobAdjustRuleServiceImpl;
import com.engine.salary.service.impl.SalarySobItemServiceImpl;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.valid.ValidUtil;
import weaver.hrm.User;

import java.util.List;
import java.util.Set;

/**
 * 薪资账套的调薪计薪规则
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SalarySobAdjustRuleWrapper extends Service {

    private SalarySobAdjustRuleService getSalarySobAdjustRuleService(User user) {
        return (SalarySobAdjustRuleService) ServiceUtil.getService(SalarySobAdjustRuleServiceImpl.class, user);
    }

    private SalaryItemService getSalaryItemService(User user) {
        return (SalaryItemService) ServiceUtil.getService(SalaryItemServiceImpl.class, user);
    }

    private SalarySobItemService getSalarySobItemService(User user) {
        return (SalarySobItemService) ServiceUtil.getService(SalarySobItemServiceImpl.class, user);
    }


    /**
     * 薪资账套的调薪计薪规则列表
     *
     * @param queryParam 查询参数
     * @return
     */
    public List<SalarySobAdjustRuleListDTO> list(SalarySobAdjustRuleQueryParam queryParam) {

        ValidUtil.doValidator(queryParam);

        // 查询调薪计薪规则po
        List<SalarySobAdjustRulePO> salarySobAdjustRulePOS = getSalarySobAdjustRuleService(user).listBySalarySobId(queryParam.getSalarySobId());
        // 查询薪资项目
        Set<Long> salaryItemIds = SalaryEntityUtil.properties(salarySobAdjustRulePOS, SalarySobAdjustRulePO::getSalaryItemId);
        List<SalaryItemPO> salaryItemPOS = getSalaryItemService(user).listByIds(salaryItemIds);
        // 转换成dto
        return SalarySobAdjustRuleBO.convert2ListDTO(salarySobAdjustRulePOS, salaryItemPOS);
    }

    /**
     * 查询调薪计薪规则可选的薪资项目
     *
     * @param queryParam 查询条件
     * @return
     */
    public List<SalaryItemBaseDTO> list4SalarySobItem(SalarySobAdjustRuleItemQueryParam queryParam) {

        ValidUtil.doValidator(queryParam);

        // 查询薪资账套的薪资项目副本
        List<SalarySobItemPO> salarySobItemPOS = getSalarySobItemService(user).listBySalarySobIdAndSalaryItemIdNotIn(queryParam.getSalarySobId(), queryParam.getExcludeSalaryItemIds());
        // 查询薪资项目
        Set<Long> salaryItemIds = SalaryEntityUtil.properties(salarySobItemPOS, SalarySobItemPO::getSalaryItemId);
        List<SalaryItemPO> salaryItemPOS = getSalaryItemService(user).listByIds(salaryItemIds);
        // 转换成dto
        return SalarySobAdjustRuleBO.convertItemBaseDTO(salaryItemPOS);
    }

    /**
     * 保存调薪计薪规则
     *
     * @param saveParam 保存参数
     */
    public void save(SalarySobAdjustRuleSaveParam saveParam) {
        getSalarySobAdjustRuleService(user).save(saveParam);
    }
}
