package com.engine.salary.wrapper;

import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.entity.salaryformula.ExpressFormula;
import com.engine.salary.entity.salarysob.bo.SalarySobCheckRuleBO;
import com.engine.salary.entity.salarysob.dto.SalarySobCheckRuleFormDTO;
import com.engine.salary.entity.salarysob.dto.SalarySobCheckRuleListDTO;
import com.engine.salary.entity.salarysob.param.SalarySobCheckRuleQueryParam;
import com.engine.salary.entity.salarysob.param.SalarySobCheckRuleSaveParam;
import com.engine.salary.entity.salarysob.param.UpdateCheckRuleFormulaParam;
import com.engine.salary.entity.salarysob.po.SalarySobCheckRulePO;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.service.SalaryFormulaService;
import com.engine.salary.service.SalarySobCheckRuleService;
import com.engine.salary.service.impl.SalarySobCheckRuleServiceImpl;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.page.PageInfo;
import weaver.hrm.User;

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

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


    private SalarySobCheckRuleService getSalarySobCheckRuleService(User user) {
        return (SalarySobCheckRuleService) ServiceUtil.getService(SalarySobCheckRuleServiceImpl.class, user);
    }


    private SalaryFormulaService salaryFormulaService;

    /**
     * 薪资账套的校验规则列表
     *
     * @param queryParam 列表查询条件
     * @return
     */
    public PageInfo<SalarySobCheckRuleListDTO> listPage(SalarySobCheckRuleQueryParam queryParam) {
        // 分页查询薪资账套的校验规则
        PageInfo<SalarySobCheckRulePO> page = getSalarySobCheckRuleService(user).listPageByParam(queryParam);
        // todo 查询公式详情
//        Set<Long> formulaIds = SalaryEntityUtil.properties(page.getList(), SalarySobCheckRulePO::getFormulaId);
//        List<ExpressFormula> expressFormulas = salaryFormulaService.listExpressFormula(formulaIds);
        // 转换成dto
        List<SalarySobCheckRuleListDTO> salarySobCheckRuleListDTOS = SalarySobCheckRuleBO.convert2ListDTO(page.getList(), new ArrayList<>());
        // 转换成前端所需的数据格式
        PageInfo<SalarySobCheckRuleListDTO> dtoPage = new PageInfo<>(salarySobCheckRuleListDTOS, SalarySobCheckRuleListDTO.class);
        dtoPage.setPageSize(page.getPageSize());
        dtoPage.setPageNum(page.getPageNum());
        dtoPage.setTotal(page.getTotal());
        return dtoPage;
    }

    /**
     * 薪资账套的校验规则详情
     *
     * @param id 校验规则的id
     * @return
     */
    public SalarySobCheckRuleFormDTO getForm(Long id) {
        SalarySobCheckRuleFormDTO checkRuleFormDTO = new SalarySobCheckRuleFormDTO();
        if (!Objects.isNull(id)) {
            // 查询校验规则
            SalarySobCheckRulePO salarySobCheckRulePO = getSalarySobCheckRuleService(user).getById(id);
            if (Objects.isNull(salarySobCheckRulePO)) {
                throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98636, "校验规则不存在或者已被删除"));
            }
            // 查询公式详情
            ExpressFormula expressFormula = salaryFormulaService.getExpressFormula(salarySobCheckRulePO.getFormulaId());
            // 转换成详情dto
            checkRuleFormDTO = SalarySobCheckRuleBO.convert2FormDTO(salarySobCheckRulePO, expressFormula);
        }
        // 转换成前端所需的数据格式
        return checkRuleFormDTO;
    }

    /**
     * 保存
     *
     * @param saveParam 保存参数
     */
    public void save(SalarySobCheckRuleSaveParam saveParam) {
        getSalarySobCheckRuleService(user).save(saveParam);
    }

    /**
     * 更新
     *
     * @param updateParam 更新参数
     */
    public void update(SalarySobCheckRuleSaveParam updateParam) {
        getSalarySobCheckRuleService(user).update(updateParam);
    }

    /**
     * 更新校验规则的公式
     *
     * @param updateParam 更新参数
     */
    public void updateFormulaId(UpdateCheckRuleFormulaParam updateParam) {
        getSalarySobCheckRuleService(user).updateFormulaId(updateParam);
    }

    /**
     * 删除
     *
     * @param ids 校验规则的id
     */
    public void delete(Collection<Long> ids) {
        getSalarySobCheckRuleService(user).deleteByIds(ids);
    }
}
