package com.sifang.modules.task;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.dao.CustomerEntryDao;
import com.sifang.modules.customer.entity.CustomerEntry;
import com.sifang.modules.monitor.dao.MonitorCollectDataDao;
import com.sifang.modules.monitor.service.MonitorCollectDataService;
import com.sifang.modules.youkua.service.NhPowerStationService;
import com.sifang.modules.youkua.vo.PowerStationVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class GetFirstGenerationTimeTask {
    private static final Logger log = LoggerFactory.getLogger(GetFirstGenerationTimeTask.class);
    @Autowired
    private MonitorCollectDataDao collectDataDao;
    @Autowired
    private NhPowerStationService nhPowerStationService;
    //每隔1个小时 28分执行  获取首日发电时间
    @Scheduled(cron = "0 28 * * * ?")
    public void getFirstGenerationTime() {
        List<Record> records = Db.find("select * from crm_customer_entry e where e.status in ('5','f','6','7','10','a','i') and  e.first_generation_time  is null ");
        log.info("getFirstGenerationTime 获取首日发电时间   size:{} ",records.size());
        long begin = System.currentTimeMillis();
        for (Record record : records) {
            String entryId = record.getStr("id");
            log.info("getFirstGenerationTime.id:{}",entryId);
            PowerStationVo powerStationVo = nhPowerStationService.getStationRealTimeInfo(entryId);
            if (powerStationVo != null){
                String fisGenerateTime = powerStationVo.getFisGenerateTime();
                if (StringUtils.isNotBlank(fisGenerateTime)){
                    Db.update("update crm_customer_entry set first_generation_time=? where id=?", fisGenerateTime, entryId);
                }
            }
//            List<String> nbqSnList = collectDataDao.findNbqSnByEntryId(entryId);
//            if (nbqSnList.size()>0){
//                // 查询电站最早发电时间
//                Date firstGenerate = collectDataDao.findFirstGenerate(nbqSnList);
//                if (firstGenerate != null) {
//                    Db.update("update crm_customer_entry set first_generation_time=? where id=?", firstGenerate, entryId);
//                }
//            }
        }
        long end = System.currentTimeMillis();
        log.info("getFirstGenerationTime 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }
}
