package com.engine.salary.timer;

import cn.hutool.core.date.DateUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.salary.service.AddUpDeductionService;
import com.engine.salary.service.impl.AddUpDeductionServiceImpl;
import com.engine.salary.util.SalaryDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import weaver.hrm.User;
import weaver.interfaces.schedule.BaseCronJob;

import java.time.LocalDate;
import java.util.Collections;
import java.util.Date;

/**
 * 定时自动累计专项附加扣除
 */
@Slf4j
public class AutoAddAllSpecialAddDeductionJob extends BaseCronJob {

    private AddUpDeductionService getAddUpDeductionService(User user) {
        return ServiceUtil.getService(AddUpDeductionServiceImpl.class, user);
    }

    private String salaryTaxMonth;

    @Override
    public void execute() {
        LocalDate localDate = SalaryDateUtil.dateToLocalDate(new Date());
        if(StringUtils.isNotBlank(salaryTaxMonth)){
            localDate = localDate.plusMonths(Integer.valueOf(salaryTaxMonth.trim()));
        }
        getAddUpDeductionService(null).autoAddAll(DateUtil.beginOfMonth(SalaryDateUtil.localDateToDate(localDate)), Boolean.TRUE, Collections.emptyList());
    }
}
