package com.engine.salary.wrapper;

import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.sys.entity.param.AppSettingSaveParam;
import com.engine.salary.sys.entity.param.MatchEmployeeModeSaveParam;
import com.engine.salary.sys.entity.param.OrderRuleParam;
import com.engine.salary.sys.entity.param.SalarySysConfQueryParam;
import com.engine.salary.sys.entity.po.SalarySysConfPO;
import com.engine.salary.sys.entity.vo.AppSettingVO;
import com.engine.salary.sys.entity.vo.OrderRuleVO;
import com.engine.salary.sys.enums.TaxDeclarationFunctionEnum;
import com.engine.salary.sys.service.SalarySysConfService;
import com.engine.salary.sys.service.impl.SalarySysConfServiceImpl;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.util.page.SalaryPageUtil;
import com.engine.salary.util.valid.RuntimeTypeEnum;
import com.engine.salary.util.valid.ValidUtil;
import org.apache.commons.lang3.StringUtils;
import weaver.general.BaseBean;
import weaver.hrm.User;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 累计专项附加扣除
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SalarySystemConfigWrapper extends Service {

    private SalarySysConfService getSalarySysConfService(User user) {
        return ServiceUtil.getService(SalarySysConfServiceImpl.class, user);
    }

    public Map<String, Object> info() {
        BaseBean baseBean = new BaseBean();
        String log = baseBean.getPropValue("hrmSalary", "log");
        String version = baseBean.getPropValue("hrmSalary", "version");
        String openFormulaForcedEditing = baseBean.getPropValue("hrmSalary", "openFormulaForcedEditing");
        String showEncryptOperationButton = baseBean.getPropValue("hrmSalary", "showEncryptOperationButton");

        Map<String, Object> o = new HashMap<>();
        o.put("log", log);
        o.put("version", version);
        o.put("openFormulaForcedEditing", openFormulaForcedEditing);
        o.put("showEncryptOperationButton", showEncryptOperationButton);


        List<SalarySysConfPO> confPOS = getSalarySysConfService(user).listSome(SalarySysConfPO.builder().build());
        Map<String, String> map = SalaryEntityUtil.convert2Map(confPOS, SalarySysConfPO::getConfKey, SalarySysConfPO::getConfValue);
        o.putAll(map);
        return o;
    }

    /**
     * 操作是否需要申报功能
     *
     * @param flag 开启 0/关闭 1/重新开启 2
     * @return 执行结果
     */
    public boolean operateTaxDeclarationFunction(TaxDeclarationFunctionEnum flag) {
        return getSalarySysConfService(user).operateTaxDeclarationFunction(flag);
    }

    /**
     * 获取单一配置
     *
     * @param code
     * @return
     */
    public String getOneByCode(String code) {
        SalarySysConfPO one = getSalarySysConfService(user).getOneByCode(code);
        if (one != null) {
            return one.getConfValue();
        } else {
            return StringUtils.EMPTY;
        }
    }


    public PageInfo<SalarySysConfPO> list(SalarySysConfQueryParam param) {
        List<SalarySysConfPO> salarySysConfPOS = getSalarySysConfService(user).listSome(SalarySysConfPO.builder().build());

        List<SalarySysConfPO> list = SalaryPageUtil.subList(param.getCurrent(), param.getPageSize(), salarySysConfPOS);
        PageInfo<SalarySysConfPO> page = new PageInfo<>(list, SalarySysConfPO.class);

        page.setTotal(salarySysConfPOS.size());
        page.setPageNum(param.getCurrent());
        page.setPageSize(param.getPageSize());
        return page;
    }


    public void save(SalarySysConfPO salarySysConfPO) {
        ValidUtil.doValidator(salarySysConfPO);
        getSalarySysConfService(user).save(salarySysConfPO);
    }

    public void update(SalarySysConfPO salarySysConfPO) {
        ValidUtil.doValidator(salarySysConfPO, RuntimeTypeEnum.UPDATE);
        getSalarySysConfService(user).update(salarySysConfPO);
    }

    public SalarySysConfPO detail(Long id) {
        return getSalarySysConfService(user).getById(id);
    }

    public void updateByCode(SalarySysConfPO salarySysConfPO) {
        getSalarySysConfService(user).updateByCode(salarySysConfPO);
    }

    public void updateOrderRule(OrderRuleParam param) {
        getSalarySysConfService(user).updateOrderRule(param);
    }

    public OrderRuleVO orderRule() {
        return getSalarySysConfService(user).orderRule();
    }

    public void saveMatchEmployeeModeRule(MatchEmployeeModeSaveParam param) {
        ValidUtil.doValidator(param);
        getSalarySysConfService(user).saveMatchEmployeeModeRule(param.getRule());
    }

    public void saveAppSetting(AppSettingSaveParam param) {
        ValidUtil.doValidator(param);
        getSalarySysConfService(user).saveAppSetting(param);
    }

    public AppSettingVO appSetting() {

       return getSalarySysConfService(user).appSetting();

    }

    public Map<String, Object> saveEncryptSetting(AppSettingSaveParam param) {
       return getSalarySysConfService(user).saveEncryptSetting(param);
    }

    public Map<String, Object> getEncryptProgress(String progressId) {
        return getSalarySysConfService(user).getEncryptProgress(progressId);
    }

    public void saveSalaryAcctEmployeeRule(String rule) {
        getSalarySysConfService(user).saveSalaryAcctEmployeeRule(rule);
    }

    public void saveWithDrawTaxDeclaration(SalarySysConfPO param) {
        getSalarySysConfService(user).saveWithDrawTaxDeclaration(param);
    }

    public void saveArchiveDelete(String setting) {
        getSalarySysConfService(user).saveArchiveDelete(setting);
    }

    public void operate(SalarySysConfPO salarySysConfPO) {
        getSalarySysConfService(user).operate(salarySysConfPO);
    }

    public void saveSalarySendFeedback(SalarySysConfPO param) {
        getSalarySysConfService(user).saveSalarySendFeedback(param);
    }
}
