package com.tofly.feesapi.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.tofly.feesapi.bmgl.entity.BmYmjzConfig;
import com.tofly.feesapi.bmgl.service.BmYmjzConfigService;
import com.tofly.feesapi.cbgl.service.YyCblrService;
import com.tofly.feesapi.common.util.ConfigManager;
import com.tofly.feesapi.common.znj.ZnjCal;
import com.tofly.feesapi.sfgl.service.YyQfmxService;
import com.tofly.feesapi.sfgl.service.YySspjxxService;
import com.tofly.feesapi.xtgl.service.ISysGdcsbService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.Month;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 算费任务
 */
@Component
@Slf4j
public class SfTask {

    @Autowired
    private YyQfmxService qfmxService;
    @Autowired
    private YyCblrService cblrService;
    @Autowired
    private YySspjxxService yySspjxxService;
    @Autowired
    private ZnjCal znjCal;
    @Autowired
    private BmYmjzConfigService bmYmjzConfigService;

    /**
     * 自动更新财务日期
     */
    //@Scheduled(cron = "${gd.cron_expression:0 0 0 * * ?}")
    //public void gdcsTask() {
    //gdcsbService.scgdcs();
    //}

    /**
     * 算费任务（每天23点执行一次）
     */
    @Scheduled(cron = "${suanfeiTask.cron_expression:-}")
    public void suanfeiTask() {
        qfmxService.sf();
    }

    /**
     * 预付费扣减（每天2点执行一次）
     */
    @Scheduled(cron = "${yyfkj.cron_expression:-}")
    //@Scheduled(cron = "${cron_expression:0 18 17 * * ?}")
    public void yyfkjTask() {
        yySspjxxService.yffzdkj();
    }

    /**
     * 财务周期-抄表结算  [莱阳每月26日]
     */
    @Scheduled(cron = "${cbymjz.cron_expression:-}")
    //@Scheduled(cron = "${cron_expression:0 02 12 * * ?}")
    public void cbymjzTask() {
        LocalDateTime now = LocalDateTime.now();
        int monthValue = now.getMonthValue();  // 获取月份数值
        int day = now.getDayOfMonth();  // 获取日
        String time = String.format("%02d:%02d", now.getHour(), now.getMinute());
        //执行定时月末结转
        if (bmYmjzConfigService.IsExist(Integer.toString(monthValue), Integer.toString(day), time)) {
            cblrService.cbymjz();
        }
    }

    /**
     * 年阶梯清理 (每月27号2点执行一次)
     */
    @Scheduled(cron = "${jtljlql.cron_expression:-}")
    public void jtljlqlTask() {
        cblrService.jtljlql();
    }

//    /**
//     * 滞纳金自动计算
//     * 0 0 1 * * ?  每天1点执行一次
//     */
//    @Scheduled(cron = "${wyjjs.cron_expression:-}")
//    public void ZnjJsTask() {
//        znjCal.ZnjJsTask();
//    }
}
