package com.engine.salary.timer;

import cn.hutool.core.util.StrUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.salary.common.SalaryContext;
import com.engine.salary.entity.siaccount.param.AccountParam;
import com.engine.salary.entity.taxagent.po.TaxAgentPO;
import com.engine.salary.formlua.util.RegularUtil;
import com.engine.salary.service.SIAccountService;
import com.engine.salary.service.TaxAgentService;
import com.engine.salary.service.impl.SIAccountServiceImpl;
import com.engine.salary.service.impl.TaxAgentServiceImpl;
import lombok.extern.slf4j.Slf4j;
import weaver.hrm.User;
import weaver.interfaces.schedule.BaseCronJob;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: sy
 * @Description: 福利台账核算并归档任务
 * @Date: 2024/1/15
 **/
@Slf4j
public class AutoSiAccountAndFileJob extends BaseCronJob {

    private String diffToCurrentMonth;

    public String getDiffToCurrentMonth() {
        return diffToCurrentMonth;
    }

    public void setDiffToCurrentMonth(String diffToCurrentMonth) {
        this.diffToCurrentMonth = diffToCurrentMonth;
    }

    private String taxAgentNames;

    public String getTaxAgentNames() {
        return taxAgentNames;
    }

    public void setTaxAgentNames(String taxAgentNames) {
        this.taxAgentNames = taxAgentNames;
    }

    private String fileFlag;

    public String getFileFlag() {
        return fileFlag;
    }

    public void setFileFlag(String fileFlag) {
        this.fileFlag = fileFlag;
    }

    private TaxAgentService getTaxAgentService(User user) {
        SalaryContext.get().setValue("user",user);
        return ServiceUtil.getService(TaxAgentServiceImpl.class, user);
    }

    public SIAccountService getSIAccountService(User user) {
        SalaryContext.get().setValue("user",user);
        return ServiceUtil.getService(SIAccountServiceImpl.class, user);
    }

    @Override
    public void execute() {
        if (StrUtil.isNotBlank(diffToCurrentMonth) && (RegularUtil.isInteger(diffToCurrentMonth) || "0".equals(diffToCurrentMonth))) {
            User user = new User();
            user.setUid(1);
            user.setLoginid("sysadmin");
            user.setLastname("sysadmin");

            Calendar accountTime= Calendar.getInstance();
            accountTime.set(Calendar.MONTH, accountTime.get(Calendar.MONTH) + Integer.parseInt(diffToCurrentMonth));
            SimpleDateFormat s=new SimpleDateFormat("yyyy-MM");

            String accountMonth = s.format(accountTime.getTime());
            boolean isFile = false;
            if (StrUtil.isNotBlank(fileFlag) && "true".equals(fileFlag)) {
                isFile = true;
            }
            //核算并归档
            List<TaxAgentPO> taxAgentList = getTaxAgentService(user).listAll();
            //判断是否过滤个税扣缴义务人
            if (StrUtil.isNotBlank(taxAgentNames)) {
                List<String> taxAgentNameList = Arrays.stream(taxAgentNames.split(",")).map(String::new).collect(Collectors.toList());
                taxAgentList = taxAgentList.stream().filter(f -> taxAgentNameList.contains(f.getName())).collect(Collectors.toList());
            }
            for (TaxAgentPO po : taxAgentList) {
                try {
                    getSIAccountService(user).saveAndFile(AccountParam.builder().paymentOrganization(po.getId()).billMonth(accountMonth).flag(true).fileFlag(isFile).build());
                } catch (Exception e) {
                    log.info("个税扣缴义务人-" + po.getName() + "，新建账单月份" + accountMonth + "的福利核算（并归档）过程失败，原因：" + e.getMessage());
                }
            }

        }

    }
}
