package com.engine.salary.cmd.sischeme;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.salary.biz.SISchemeBiz;
import com.engine.salary.enums.sicategory.WelfareTypeEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import org.apache.commons.collections4.CollectionUtils;
import weaver.hrm.User;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author weaver_cl
 * 
 * @Date 2022/3/8
 * @Version V1.0
 **/
@Deprecated
public class SISchemeDeleteCmd extends AbstractCommonCommand<Map<String, Object>> {

    public SISchemeDeleteCmd(Map<String, Object> params, User user) {
        this.user = user;
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> apidatas = new HashMap<String, Object>(16);


        Collection<Long> ids = (Collection<Long>)params.get("ids");
        if (CollectionUtils.isEmpty(ids)) {
            throw new SalaryRunTimeException("参数错误");
        }
        SISchemeBiz siSchemeBiz = new SISchemeBiz();
        //fixme 校验带上类型
        int welfareTypeId = (Integer) params.get("welfareTypeId");
        if(WelfareTypeEnum.SOCIAL_SECURITY.getValue() == welfareTypeId){
            int num = siSchemeBiz.checkBeforeDeleteSocialscheme(params);
            if (num > 0){
                throw new SalaryRunTimeException("该社保方案已被使用");
            }
        }
        if(WelfareTypeEnum.ACCUMULATION_FUND.getValue() == welfareTypeId){
            int accumulationfundNum = siSchemeBiz.checkBeforeDeleteAccumulationfund(params);
            if (accumulationfundNum > 0){
                throw new SalaryRunTimeException("该公积金方案已被使用");
            }
        }
        if(WelfareTypeEnum.OTHER.getValue() == welfareTypeId){
            int otherschemeNum = siSchemeBiz.checkBeforeDeleteOtherscheme(params);
            if (otherschemeNum > 0){
                throw new SalaryRunTimeException("该其他福利方案已被使用");
            }
        }

        int billNum = siSchemeBiz.checkBeforeDeleteBill(params,welfareTypeId);
        if (billNum > 0){
            throw new SalaryRunTimeException("该福利方案已被核算");
        }
        siSchemeBiz.deleteSocialscheme(params);

        return apidatas;
    }
}
