package com.njcn.event.controller.majornetwork;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;
import com.njcn.event.mapper.majornetwork.EventDetailMapper;
import com.njcn.event.pojo.dto.EventDeatilDTO;
import com.njcn.event.pojo.param.EventCountParam;
import com.njcn.event.pojo.po.EventDetail;
import com.njcn.event.pojo.po.RmpEventDetailPO;
import com.njcn.event.pojo.vo.GeneralVO;
import com.njcn.event.common.service.EventDetailService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author denghuajun
 * @version 1.0.0
 * @date 2022/3/16
 */
@Validated
@Slf4j
@Api(tags = "暂降事件相关")
@RestController
@RequestMapping("/event")
@RequiredArgsConstructor
public class EventDetailController extends BaseController {

    private final EventDetailService eventDetailService;
    private final EventDetailMapper eventDetailMapper;

    /**
     * 获取暂降事件
     * @param id 监测点id
     * @param startTime 起始时间
     * @param endTime 结束时间
     * @return 结果
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getEventDetailData")
    @ApiOperation("获取暂降事件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "监测点id", required = true),
            @ApiImplicitParam(name = "startTime", value = "起始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
    })
    public HttpResult<List<EventDetail>> getEventDetailData(@RequestParam("id") String id, @RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime) {
        String methodDescribe = getMethodDescribe("getEventDetailData");
        LogUtil.njcnDebug(log, "{}，监测点id为：{}", methodDescribe, id);
        List<EventDetail> result = eventDetailService.getEventDetailData(id, startTime, endTime);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 获取暂降事件
     * @param id 监测点id
     * @param time 时间id
     * @return 结果
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getEventDetailByTime")
    @ApiOperation("获取暂降事件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "监测点id", required = true),
            @ApiImplicitParam(name = "time", value = "时间id", required = true),
    })
    public HttpResult<EventDetail> getEventDetailByTime(@RequestParam("id") String id, @RequestParam("time") String time) {
        String methodDescribe = getMethodDescribe("getEventDetailByTime");
        LogUtil.njcnDebug(log, "{}，监测点id为：{}", methodDescribe, id);
        EventDetail result = eventDetailService.getEventDetailByTime(id, time);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 根据监测点集合获取暂态列表
     * @param lineIndexes
     * @param startTime
     * @param endTime
     * @return
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getEventDetail")
    @ApiOperation("获取暂降列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineIndexes", value = "监测点集合", required = true),
            @ApiImplicitParam(name = "startTime", value = "起始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
    })
    public HttpResult<List<EventDetail>> getEventDetail(@RequestBody List<String> lineIndexes, @RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime) {
        String methodDescribe = getMethodDescribe("getEventDetail");
        LogUtil.njcnDebug(log, "{}，监测点id为：{}", methodDescribe, lineIndexes);
        List<EventDetail> result = eventDetailService.getEventDetail(lineIndexes, startTime, endTime);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 根据监测点集合以及分页信息获取暂降事件
     * @param lineIndexes
     * @param startTime
     * @param endTime
     * @return
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getEventDetailLimit")
    @ApiOperation("分页获取暂降列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineIndexes", value = "监测点集合", required = true),
            @ApiImplicitParam(name = "startTime", value = "起始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "pageSize", value = "暂降页面大小", required = true),
            @ApiImplicitParam(name = "pageNum", value = "暂降页数", required = true),
    })
    public HttpResult<List<EventDetail>> getEventDetailLimit(@RequestBody List<String> lineIndexes, @RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime, @RequestParam("pageSize") Integer pageSize, @RequestParam("pageNum") Integer pageNum) {
        String methodDescribe = getMethodDescribe("getEventDetailLimit");
        LogUtil.njcnDebug(log, "{}，监测点id为：{}", methodDescribe, lineIndexes);
        List<EventDetail> result = eventDetailService.getEventDetailLimit(lineIndexes, startTime, endTime, pageSize, pageNum, new ArrayList<>());
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 根据监测点集合获取短时终端，电压暂升，电压暂降
     * @return
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getEventDetailCount")
    @ApiOperation("根据监测点集合获取各个监测点的统计类型数量")
    public HttpResult<List<GeneralVO>> getEventDetail(@RequestBody Map<String, Object> condMap) {
        String methodDescribe = getMethodDescribe("getEventDetail");
        List<GeneralVO> generalVOS = eventDetailMapper.ListEventType(condMap);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, generalVOS, methodDescribe);
    }

    /**
     * 根据暂态id集合获取暂降列表
     * @return
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getEventDetailByIdsList")
    @ApiOperation("根据暂态id集合获取暂降列表")
    public HttpResult<List<RmpEventDetailPO>> getEventDetailByIdsList(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("getEventDetailByIdsList");
        List<RmpEventDetailPO> eventDetailPOS = eventDetailService.listByIds(ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, eventDetailPOS, methodDescribe);
    }

    /**
     * 根据监测点id集合,统计时间范围内暂态超标次数（灿能云）
     * @return
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getEventDetailByLineCount")
    @ApiOperation("根据监测点id集合,统计时间范围内暂态超标次数")
    public HttpResult<Integer> getEventDetailByLineCount(@RequestBody EventCountParam param) {
        String methodDescribe = getMethodDescribe("getEventDetailByLineCount");
        int count = eventDetailService.count(new LambdaQueryWrapper<RmpEventDetailPO>()
                .in(CollUtil.isNotEmpty(param.getIds()), RmpEventDetailPO::getLineId, param.getIds())
                .ge(StrUtil.isNotBlank(param.getStartTime()), RmpEventDetailPO::getStartTime, DateUtil.beginOfDay(DateUtil.parse(param.getStartTime())))
                .le(StrUtil.isNotBlank(param.getEndTime()), RmpEventDetailPO::getStartTime, DateUtil.endOfDay(DateUtil.parse(param.getEndTime())))
        );
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, count, methodDescribe);
    }

    /**
     * 根据监测点id集合和统计类型,查询暂态信息
     * @return
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getEventDetailByEventType")
    @ApiOperation("根据监测点id集合和统计类型,查询暂态信息")
    public HttpResult<List<RmpEventDetailPO>> getEventDetailByEventType(@RequestBody EventCountParam param) {
        String methodDescribe = getMethodDescribe("getAppEventDetailLtAmplitude");
        List<RmpEventDetailPO> list = eventDetailService.list(new LambdaQueryWrapper<RmpEventDetailPO>()
                .in(CollUtil.isNotEmpty(param.getIds()), RmpEventDetailPO::getLineId, param.getIds())
                .in(CollUtil.isNotEmpty(param.getEventType()), RmpEventDetailPO::getEventType, param.getEventType())
                .ge(StrUtil.isNotBlank(param.getStartTime()), RmpEventDetailPO::getStartTime,DateUtil.beginOfDay(DateUtil.parse(param.getStartTime())))
                .le(StrUtil.isNotBlank(param.getEndTime()), RmpEventDetailPO::getStartTime, DateUtil.endOfDay(DateUtil.parse(param.getEndTime())))
        );
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    /**
     * 根据监测点id集合,统计时间范围内特征增幅小于0.9的暂态信息（灿能云）
     * @return
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getAppEventDetailLtAmplitude")
    @ApiOperation("根据监测点id集合,统计时间范围内特征增幅小于0.9的暂态信息")
    public HttpResult<List<RmpEventDetailPO>> getAppEventDetailLtAmplitude(@RequestBody EventCountParam param) {
        String methodDescribe = getMethodDescribe("getAppEventDetailLtAmplitude");
        List<RmpEventDetailPO> list = eventDetailService.list(new LambdaQueryWrapper<RmpEventDetailPO>()
                .in(CollUtil.isNotEmpty(param.getIds()), RmpEventDetailPO::getLineId, param.getIds())
                .ge(StrUtil.isNotBlank(param.getStartTime()), RmpEventDetailPO::getStartTime, param.getStartTime())
                .le(StrUtil.isNotBlank(param.getEndTime()), RmpEventDetailPO::getStartTime, param.getEndTime())
                .lt(RmpEventDetailPO::getFeatureAmplitude,0.9)
        );
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    /**
     * 根据开始时间及结束时间获取暂态事件信息
     * @return
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getNewEventDetailByTime")
    @ApiOperation("根据开始时间及结束时间获取暂态事件信息")
    public HttpResult<List<RmpEventDetailPO>> getNewEventDetailByTime(@RequestBody EventCountParam param) {
        String methodDescribe = getMethodDescribe("getNewEventDetailByTime");
        LambdaQueryWrapper<RmpEventDetailPO> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.in(RmpEventDetailPO::getMeasurementPointId,param.getIds());
        lambdaQueryWrapper.le(RmpEventDetailPO::getStartTime,param.getEndTime());
        if(param.getStartTime() != null){
            lambdaQueryWrapper.gt(RmpEventDetailPO::getStartTime,param.getStartTime());
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, eventDetailService.list(lambdaQueryWrapper), methodDescribe);
    }

    /**
     * 暂降事件记录
     * @return
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/addEventDetail")
    @ApiOperation("暂降事件记录")
    public HttpResult<Boolean> addEventDetail(@RequestBody EventDeatilDTO deatilDTO) {
        String methodDescribe = getMethodDescribe("addEventDetail");
        Boolean flag = eventDetailService.addEventDetail(deatilDTO);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
    }


}
