package cn.iocoder.yudao.module.system.job;

import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.dal.dataobject.sys.BusinessParamsConfigDO;
import cn.iocoder.yudao.module.system.enums.common.BooleanEnum;
import cn.iocoder.yudao.module.system.service.biz.ExpenseOrderService;
import cn.iocoder.yudao.module.system.service.sys.BusinessParamsConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * 月度账单定时生成job
 */
@Component
@Slf4j
public class ExpenseOrderJob implements JobHandler {


    @Resource
    private ExpenseOrderService expenseOrderService;
    @Resource
    private BusinessParamsConfigService businessParamsConfigService;

    @Override
    @TenantIgnore
    public String execute(String param) throws Exception {
        List<BusinessParamsConfigDO> businessParamsConfigList = businessParamsConfigService.getMonthlySettlementConfigByStatus(BooleanEnum.TRUE.getValue());
        log.info("月度账单定时任务执行了！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！");
        for (BusinessParamsConfigDO businessParamsConfigDO : businessParamsConfigList) {
            String value = businessParamsConfigDO.getValue();

            //判断是否当前日期
            LocalDate now = LocalDate.now();
            int dayOfMonth = now.getDayOfMonth();
            if (dayOfMonth == Integer.parseInt(value)){
                log.info("月度账单定时任务开始执行！！！！！！！！！！异步执行开始================》");
                //日期相同,生成账单
                new Thread(() ->{
                    expenseOrderService.executeMonthlyBillingStatement(businessParamsConfigDO.getTenantId());
                }).start();
                log.info("月度账单定时任务执行结束！！！！！！！！！！异步执行结束================》");
            }
        }
        return null;
    }
}
