package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.utils.DateUtils;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.mapper.BasElecProCepMapMapper;
import cn.cecep.talroad.service.ElectricityMeterService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import cn.cecep.talroad.domain.BasElecProCepMap;
/**
 * @className: ElectricityMeterController
 * @description: 分表计电定时任务
 * @author: Mr.Yanghongtao
 * @date: 2023/03/30 5:21 PM
 * @Company: Copyright© [2023/03/30 5:21 PM] by [Mr.Yanghongtao]
 **/
@RestController
@RequestMapping("electricityMeterController")
@Slf4j
public class ElectricityMeterController {

    @Autowired
    ElectricityMeterService electricityMeterServiceImpl;

    @Autowired
    private BasElecProCepMapMapper basElecProCepMapMapper;

    /**
     * @Author: Mr.Yanghongtao
     * @data: 2023/4/2 6:32 PM
     * @Deseription: 按照规则，15分钟执行一次，但是数据接入可能会慢一些，所以定时任务执行时间往后延长2分钟 02 17 32 47 执行，
     * @Params: dateTime  yyyy-MM-dd HH-02:00 |  yyyy-MM-dd HH-17:00 | yyyy-MM-dd HH-32:00 | yyyy-MM-dd HH-47:00
     * @Params: deviceType 1-生产设施，2-治理设施
     * @Return: cn.cecep.talroad.common.core.web.domain.AjaxResults
     * @Throws:
     */
    @ApiOperation(value = "统计设备运行状态")
    @GetMapping("/statisticsDeviceRunningState")
    @Log(title = "统计设备运行状态-每15分钟一次", businessType = BusinessType.UPDATE)
    public AjaxResults statisticsDeviceRunningState(@RequestParam(name = "dateTime") String dateTime) {
        try {

            // 查询一下生产设施和治理设施的对应关系
            List<BasElecProCepMap> isStatus = basElecProCepMapMapper.selectList(new QueryWrapper<BasElecProCepMap>().eq("is_status", 1));
            // key生产设施ID
            Map<String, List<String>> cepAndProIds = isStatus.stream()
                    .collect(Collectors.groupingBy(BasElecProCepMap::getCepDeviceId,
                            Collectors.mapping(BasElecProCepMap::getProDeviceId, Collectors.toList())));

            electricityMeterServiceImpl.statisticsDeviceRunningState(dateTime,"0",cepAndProIds);
            electricityMeterServiceImpl.statisticsDeviceRunningState(dateTime,"1",cepAndProIds);
        } catch (ParseException e) {
            throw new RuntimeException("传入时间格式错误！");
        } catch (Exception e) {
            log.error("统计设备运行状态-每15分钟一次执行失败,异常日志：{}",e);
        }
        return AjaxResults.success();
    }

    @ApiOperation(value = "统计设备运行状态-每日一次")
    @GetMapping("/statisticsDeviceRunningTime")
    @Log(title = "统计设备运行状态-每日一次", businessType = BusinessType.UPDATE)
    public AjaxResults statisticsDeviceRunningTime(
            @RequestParam(value = "startTime",required = false) String startTime,
            @RequestParam(value = "endTime",required = false) String endTime
    ) {
       try {
           electricityMeterServiceImpl.statisticsDeviceRunningTime(startTime,endTime);
       } catch (Exception e) {
           log.error("统计设备运行状态-每日一次执行失败,异常日志：{}",e);
       }
        return AjaxResults.success();
    }

    @ApiOperation(value = "统计设备运行状态-每月一次")
    @GetMapping("/statisticsDeviceRunningTimeByMonth")
    @Log(title = "统计设备运行状态-每月一次", businessType = BusinessType.UPDATE)
    public AjaxResults statisticsDeviceRunningTimeByMonth(
            @RequestParam(value = "startTime",required = false) String startTime,
            @RequestParam(value = "endTime",required = false) String endTime
    ) {
        try {
            electricityMeterServiceImpl.statisticsDeviceRunningTimeByMonth(startTime,endTime);
        } catch (Exception e) {
            log.error("统计设备运行状态-每月一次执行失败,异常日志：{}",e);
        }
        return AjaxResults.success();
    }

    @ApiOperation(value = "统计设备用电数据，每小时一次")
    @GetMapping("/statisticsDeviceUsePowerCount")
    @Log(title = "统计设备用电数据-每小时一次", businessType = BusinessType.UPDATE)
    public AjaxResults statisticsDeviceUsePowerCount(
            @RequestParam(value = "startTime",required = false) String startTime,
            @RequestParam(value = "endTime",required = false) String endTime
    ) {
        try {
            electricityMeterServiceImpl.statisticsDeviceUsePowerCount(startTime,endTime);
        } catch (Exception e) {
            log.error("统计设备用电数据-每小时一次执行失败,异常日志：{}",e);
        }
        return AjaxResults.success();
    }

    @ApiOperation(value = "统计设备用电数据，每日统计一次")
    @GetMapping("/statisticsDeviceUsePowerCountByDay")
    @Log(title = "统计设备用电数据-每日一次", businessType = BusinessType.UPDATE)
    public AjaxResults statisticsDeviceUsePowerCountByDay(
            @RequestParam(value = "startTime",required = false) String startTime,
            @RequestParam(value = "endTime",required = false) String endTime
    ) {
      try {
            electricityMeterServiceImpl.statisticsDeviceUsePowerCountByDay(startTime,endTime);
        } catch (Exception e) {
            log.error("统计设备用电数据-每日一次执行失败,异常日志：{}",e);
        }
        return AjaxResults.success();
    }


}
