package com.ho.xxljob;

import com.alibaba.fastjson.JSONObject;
import com.ho.business.vo.req.MonitorQuery;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.util.RedisCommon;
import com.ho.xxljob.feignclient.BusinessFeignClient;
import com.ho.xxljob.feignclient.FileFeignClient;
import com.ho.xxljob.feignclient.FlowFeignClient;
import com.ho.xxljob.handlers.TimeGenerateService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.core.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author fancl
 * @desc: 执行器:所有执行任务都是以Bean的方式定义
 * @date 2022/10/19
 */

@Component
@Slf4j
public class JobHandler {

    @Autowired
    RedisCommon redisUtil;

    @Autowired
    TimeGenerateService timeGenerateService;

    @Autowired
    BusinessFeignClient businessFeignClient;

    @Autowired
    FlowFeignClient flowFeignClient;

    @Autowired
    FileFeignClient fileFeignClient;

    @Autowired
    RedisService redisService;

    /**
     * 生成监控页面需要的时间缓存数据（Redis）
     */
    @XxlJob("generateMonitorRedisTimeHandler")
    public ReturnT<String> generateMonitorRedisTimeHandler(String param) throws Exception {
        XxlJobLogger.log("generateMonitorRedisTimeHandler ");
        log.info("generateMonitorRedisTimeHandler start !");
        timeGenerateService.buildTimeMinutes();
        timeGenerateService.buildTimeDays();
        //每年的各个月
        timeGenerateService.buildYearMonth();
        log.info("generateMonitorRedisTimeHandler end !");
        return ReturnT.SUCCESS;
    }

    //光伏逆变器每15分钟根据发电量 和 电价表 产生的收益,入库到income_total表
    @XxlJob("jobIntervalGene4RealTimeDataBySrc")
    public ReturnT<String> jobIntervalGene4RealTimeDataBySrc(String param) throws Exception {
        XxlJobLogger.log("jobIntervalGene4RealTimeDataBySrc ");
        log.info("jobIntervalGeneAndProfitByDay start !");
        businessFeignClient.intervalGene4RealTimeDataBySrc();
        log.info("jobIntervalGeneAndProfitByDay end !");
        return ReturnT.SUCCESS;
    }

    /**
     * 定时检测电站的接入点是否在线或离线()
     */
    @XxlJob("accessPointEvent")
    public ReturnT<String> accessPointEvent(String param) throws Exception {
        XxlJobLogger.log("accessPointEvent ");
        log.info("accessPointEvent start !");
        flowFeignClient.accessPointEvent();
        log.info("accessPointEvent end !");
        return ReturnT.SUCCESS;
    }

    /**
     * 定时删除多余的图片及文件数据
     */
    @XxlJob("deletedPicture")
    public ReturnT<String> deletedPicture(String param) throws Exception {
        XxlJobLogger.log("deletedPicture ");
        log.info("deletedPicture start !");
        flowFeignClient.deletedPicture();
        businessFeignClient.deletedPicture();
        log.info("deletedPicture end !");
        return ReturnT.SUCCESS;
    }

    /**
     * 加工一体柜 充电量，放电量,当月充电量,当月放电量
     * 暂时用不到
     */
    @XxlJob("jobIntegratedCabinet")
    public ReturnT<String> jobIntegratedCabinet(String param) throws Exception {
        XxlJobLogger.log("jobIntegratedCabinet,param: {} ", param);
        //参数转换为MonitorQuery对象
        MonitorQuery monitorQuery = new MonitorQuery();
        if (!StringUtils.isEmpty(param)) {
            monitorQuery = JSONObject.parseObject(param, MonitorQuery.class);
        }
        log.info("jobIntegratedCabinet start !");
        //建议改成异步调用,这个方法会比较耗时
        businessFeignClient.jobIntegratedCabinet(monitorQuery);
        log.info("jobIntegratedCabinet end !");
        return ReturnT.SUCCESS;
    }

    /**
     * 加工一体柜 充电量，放电量,当月充电量,当月放电量(小时)
     * 暂时用不到
     */
    @XxlJob("jobIntegratedCabinetHour")
    public ReturnT<String> jobIntegratedCabinetHour(String param) throws Exception {
        XxlJobLogger.log("jobIntegratedCabinetHour,param: {} ", param);
        //参数转换为MonitorQuery对象
        MonitorQuery monitorQuery = new MonitorQuery();
        if (!StringUtils.isEmpty(param)) {
            monitorQuery = JSONObject.parseObject(param, MonitorQuery.class);
        }
        log.info("jobIntegratedCabinetHour start !");
        //建议改成异步调用,这个方法会比较耗时
        businessFeignClient.jobIntegratedCabinetHour(monitorQuery);
        log.info("jobIntegratedCabinetHour end !");
        return ReturnT.SUCCESS;
    }

    /**
     * 每天加工天气数据
     * 暂时用不到
     */
    @XxlJob("jobStationWeathers")
    public ReturnT<String> jobStationWeathers(String param) throws Exception {
        XxlJobLogger.log("jobStationWeathers,param: {} ", param);
        log.info("jobStationWeathers start !");
        //建议改成异步调用,这个方法会比较耗时
        fileFeignClient.jobStationWeathers();
        log.info("jobStationWeathers end !");
        return ReturnT.SUCCESS;
    }

    /**
     * 收益统计定时任务
     */
    @XxlJob("jobIncomeDataManage")
    public ReturnT<String> jobIncomeDataManage(String param) throws Exception {
        XxlJobLogger.log("jobIncomeDataManage ");
        log.info("jobIncomeDataManage start !");
        businessFeignClient.jobIncomeDataManage();
        log.info("jobIncomeDataManage end !");
        return ReturnT.SUCCESS;
    }

    /**
     * 计算储能-收益（昨日）
     * @param param
     * @return
     */
    @XxlJob("jobEarningsCalculate")
    public ReturnT<String> jobEarningsCalculate(String param) {
        XxlJobLogger.log("jobEarningsCalculate");
        log.info("jobMaintenanceInsert start !");
        Date date = DateUtil.addDays(new Date(), -1);
        businessFeignClient.jobEarningsCalculate(DateUtil.formatDate(date));
        log.info("jobMaintenanceInsert end !");
        return ReturnT.SUCCESS;
    }

    /**
     * 计算光伏-Mppt收益(昨日)
     * @param param
     * @return
     */
    @XxlJob("jobEarningsCalculateMppt")
    public ReturnT<String> jobEarningsCalculateMppt(String param) {
        XxlJobLogger.log("jobEarningsCalculateMppt");
        log.info("jobEarningsCalculateMpptInsert start !");
        Date date = DateUtil.addDays(new Date(), -1);
        businessFeignClient.jobEarningsCalculateMppt(DateUtil.formatDate(date));
        log.info("jobEarningsCalculateMpptInsert end !");
        return ReturnT.SUCCESS;
    }

    /**
     * 计算光伏-Mppt收益(当日)
     * @param param
     * @return
     */
    @XxlJob("jobEarningsCalculateMpptToDay")
    public ReturnT<String> jobEarningsCalculateMpptToDay(String param) {
        XxlJobLogger.log("jobEarningsCalculateMpptToDay");
        log.info("jobEarningsCalculateMpptToDayInsert start !");
        Date date = new Date();
        businessFeignClient.jobEarningsCalculateMpptToDay(DateUtil.formatDate(date));
        log.info("jobEarningsCalculateMpptToDayInsert end !");
        return ReturnT.SUCCESS;
    }

    /**
     * 计算收益(当日)
     *
     * @param param
     * @return
     */
    @XxlJob("jobEarningsCalculateHour")
    public ReturnT<String> jobEarningsCalculateHour(String param) {
        XxlJobLogger.log("jobEarningsCalculateHour");
        log.info("jobEarningsCalculateHour start !");
        businessFeignClient.jobEarningsCalculate(DateUtil.formatDate(new Date()));
        log.info("jobEarningsCalculateHour end !");
        return ReturnT.SUCCESS;
    }

    @XxlJob("regularlySendNotifications")
    public ReturnT<String> regularlySendNotifications(String param) {
        XxlJobLogger.log("regularlySendNotifications,param: {} ", param);
        //参数转换为MonitorQuery对象
        MonitorQuery monitorQuery = new MonitorQuery();
        if (!StringUtils.isEmpty(param)) {
            monitorQuery = JSONObject.parseObject(param, MonitorQuery.class);
        }
        log.info("regularlySendNotifications start !");
        flowFeignClient.regularlySendNotifications(monitorQuery);
        log.info("regularlySendNotifications end !");
        return ReturnT.SUCCESS;
    }


    @XxlJob("jobFreezeMonthTotalEle")
    public ReturnT<String> jobFreezeMonthTotalEle(String param) {
        XxlJobLogger.log("jobEarningsCalculate");
        log.info("jobMaintenanceInsert start !");
        businessFeignClient.jobFreezeMonthTotalEle();
        log.info("jobMaintenanceInsert end !");
        return ReturnT.SUCCESS;
    }

    //定时获取每个电站的日充日放
    @XxlJob("jobGetDayDate")
    public ReturnT<String> jobGetDayDate(String param) {
        XxlJobLogger.log("jobGetDayDate");
        log.info("jobGetDayDate start !");
        businessFeignClient.jobGetDayDate();
        log.info("jobGetDayDate end !");
        return ReturnT.SUCCESS;
    }

    /**
     * 定时任务，根据时间刷新消息广播发送状态及阅读状态
     * @return
     */
    @XxlJob("jobUpdateMessageInfo")
    public ReturnT<String> jobUpdateMessageInfo(String param){
        log.info("jobUpdateMessageInfo start !");
        businessFeignClient.jobUpdateMessageInfo();
        log.info("jobUpdateMessageInfo end !");
        return ReturnT.SUCCESS;
    }
}
