package com.tofly.feesapi.task;

import com.tofly.feesapi.bjgl.service.YhCbkxxService;
import com.tofly.feesapi.cbgl.service.IZnbCbLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/**
 * 抄表任务
 */
@Component
@Slf4j
public class ReadTask {

    @Autowired
    private IZnbCbLogService cbLogService;
    @Autowired
    private YhCbkxxService cbkxxService;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    /**
     * 智能表抄表
     * 0 0 0 * * ?
     */
    //@Scheduled(cron = "${read.cron_expression:-}")
    public void batchReadTask() {
        log.info("/////////////////////////////////智能表抄表开始//////////");
        Future<?> future = threadPoolExecutor.submit(() -> {
            try {
                cbLogService.batchRead();
            } catch (Exception e) {
                log.error("cbLogService.batchRead() 执行失败 ==================> ", e);
            }
        });
        try {
            future.get(5, TimeUnit.MINUTES);//避免长时间阻塞
        } catch (Exception e) {
            log.warn("智能表抄表异常:{}", e.getMessage());
        }
        log.info("/////////////////////////////////智能表抄表结束//////////");
    }

    /**
     * 智能表同步
     * 0 0 0 * * ?
     */
    //@Scheduled(cron = "${meterSync.cron_expression:-}")
    public void meterSync() {
        log.info("/////////////////////////////////智能表同步开始//////////");
        Future<?> future = threadPoolExecutor.submit(() -> cbkxxService.znbSync());
        try {
            future.get(5, TimeUnit.MINUTES);//避免长时间阻塞
        } catch (Exception e) {
            log.warn("智能表同步失败 ==================> ", e);
        }
        log.info("/////////////////////////////////智能表同步结束//////////");
    }

}
