package com.njcn.csharmonic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.csdevice.pojo.vo.DataGroupEventVO;
import com.njcn.csharmonic.param.CldEventParam;
import com.njcn.csharmonic.param.CsEventUserQueryPage;
import com.njcn.csharmonic.param.CsEventUserQueryParam;
import com.njcn.csharmonic.param.DataParam;
import com.njcn.csharmonic.pojo.param.EventStatisticParam;
import com.njcn.csharmonic.pojo.po.CsEventPO;
import com.njcn.csharmonic.pojo.vo.CsEventLogsVo;
import com.njcn.csharmonic.pojo.vo.CsEventReasonAndTypeVo;
import com.njcn.csharmonic.pojo.vo.CsEventVO;
import com.njcn.csharmonic.pojo.vo.EventDetailVO;
import com.njcn.csharmonic.service.CsEventPOService;
import com.njcn.event.file.pojo.dto.WaveDataDTO;
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.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Description:
 * Date: 2023/8/29  20:03【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/event")
@Api(tags = "暂降事件")
@AllArgsConstructor
public class CsEventController extends BaseController {

    private final CsEventPOService csEventPOService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryEventList")
    @ApiOperation("暂降事件列表查询")
    @ApiImplicitParam(name = "csEventUserQueryParam", value = "暂降事件查询参数", required = true)
    public HttpResult<List<EventDetailVO>> queryEventList(@RequestBody CsEventUserQueryParam csEventUserQueryParam) {
        String methodDescribe = getMethodDescribe("queryEventList");
        List<EventDetailVO> list = csEventPOService.queryEventList(csEventUserQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/pageQueryByLineId")
    @ApiOperation("根据监测点分页查询暂降事件")
    @ApiImplicitParam(name = "csEventUserQueryPage", value = "暂降事件查询参数", required = true)
    public HttpResult<Page<DataGroupEventVO>> pageQueryByLineId(@RequestBody CsEventUserQueryPage csEventUserQueryPage) {
        String methodDescribe = getMethodDescribe("queryEventList");
        Page<DataGroupEventVO> page = csEventPOService.pageQueryByLineId(csEventUserQueryPage);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/pageEvent")
    @ApiOperation("分页查询暂降事件")
    public HttpResult<Page<DataGroupEventVO>> pageEvent(@RequestBody EventStatisticParam baseParam) {
        String methodDescribe = getMethodDescribe("pageEvent");
        Page<DataGroupEventVO> page = csEventPOService.pageEvent(baseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/analyseWave")
    @ApiOperation("暂态事件波形分析")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "eventId", value = "暂态事件索引"),
            @ApiImplicitParam(name = "isApp", value = "是否是移动端查询")
    })
    public HttpResult<WaveDataDTO> analyseWave(@RequestParam(value = "eventId") String eventId,@RequestParam(value = "isApp", required = false) Boolean isApp) {
        String methodDescribe = getMethodDescribe("analyseWave");
        WaveDataDTO wave = csEventPOService.analyseWave(eventId,2,isApp);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, wave, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getWavePics")
    @ApiOperation("获取事件波形图")
    @ApiImplicitParam(name = "eventId", value = "暂态事件索引", required = true)
    public HttpResult<CsEventVO> getWavePics(String eventId) {
        String methodDescribe = getMethodDescribe("getWavePics");
        CsEventVO eventVO = csEventPOService.getWavePics(eventId,2);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, eventVO, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getFileZip")
    @ApiOperation("下载事件原始文件压缩包")
    @ApiImplicitParam(name = "eventId", value = "暂态事件索引", required = true)
    public void getFileZip(String eventId, HttpServletResponse response) {
        String methodDescribe = getMethodDescribe("getFileZip");
        csEventPOService.getFileZip(eventId,response);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/saveBatchEventList")
    @ApiOperation("批量新增事件")
    @ApiImplicitParam(name = "csEventPOS", value = "事件集合", required = true)
    public HttpResult<String> saveBatchEventList(@RequestBody List<CsEventPO> csEventPOS) {
        String methodDescribe = getMethodDescribe("saveBatchEventList");
        csEventPOService.saveBatchEventList(csEventPOS);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryByIndex")
    @ApiOperation("根据表唯一索引查询(用于校验是否存在该事件)")
    public HttpResult<CsEventPO> queryByIndex(@RequestBody CsEventPO csEventPO) {
        String methodDescribe = getMethodDescribe("queryByIndex");
        CsEventPO po = csEventPOService.lambdaQuery().eq(CsEventPO::getDeviceId,csEventPO.getDeviceId()).eq(CsEventPO::getTag,csEventPO.getTag()).eq(CsEventPO::getStartTime,csEventPO.getStartTime()).one();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, po, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryById")
    @ApiOperation("根据事件id查询数据")
    public HttpResult<CsEventPO> queryById(String eventId) {
        String methodDescribe = getMethodDescribe("queryById");
        CsEventPO po = csEventPOService.lambdaQuery().eq(CsEventPO::getId,eventId).one();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, po, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryByModelId")
    @ApiOperation("获取各模块事件")
    public HttpResult<List<CsEventPO>> queryByModelId(@RequestBody DataParam param) {
        String methodDescribe = getMethodDescribe("queryByModelId");
        List<CsEventPO> list = csEventPOService.queryByModelId(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/addCldEvent")
    @ApiOperation("新增云前置事件")
    @ApiImplicitParam(name = "param", value = "事件信息", required = true)
    public HttpResult<String> addCldEvent(@RequestBody CldEventParam param) {
        String methodDescribe = getMethodDescribe("addCldEvent");
        csEventPOService.addCldEvent(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/updateEventById")
    @ApiOperation("修改事件信息(修改文件)")
    @ApiImplicitParam(name = "id", value = "事件id", required = true)
    public HttpResult<Boolean> updateEventById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("updateEventById");
        Boolean result = csEventPOService.updateEventById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getEventByTime")
    @ApiOperation("根据时间获取无波形的暂态事件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineList", value = "监测点id"),
            @ApiImplicitParam(name = "startTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间")
    })
    public HttpResult<List<CsEventPO>> getEventByTime(@RequestParam(required = false) List<String> lineList,@RequestParam String startTime,@RequestParam String endTime) {
        String methodDescribe = getMethodDescribe("getEventByTime");
        List<CsEventPO> list = csEventPOService.getEventByTime(lineList,startTime,endTime);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getDevAlarmList")
    @ApiOperation("获取设备运行告警事件")
    @ApiImplicitParam(name = "param", value = "事件信息", required = true)
    public HttpResult<List<CsEventPO>> getDevAlarmList(@RequestBody CsEventUserQueryParam param) {
        String methodDescribe = getMethodDescribe("getDevAlarmList");
        List<CsEventPO> list = csEventPOService.getDevAlarmList(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getEventByIdList")
    @ApiOperation("根据事件id获取事件详情")
    @ApiImplicitParam(name = "list", value = "id集合", required = true)
    public HttpResult<List<CsEventPO>> getEventByIdList(@RequestBody List<String> list) {
        String methodDescribe = getMethodDescribe("getEventByIdList");
        List<CsEventPO> result = csEventPOService.listByIds(list);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getFrontDebugLogs")
    @ApiOperation("获取前置调试日志")
    @ApiImplicitParam(name = "param", value = "param", required = true)
    public HttpResult<IPage<CsEventLogsVo>> getEventByIdList(@RequestBody EventStatisticParam param) {
        String methodDescribe = getMethodDescribe("getEventByIdList");
        IPage<CsEventLogsVo> result = csEventPOService.getFrontDebugLogs(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/eventReasonAndTypeStatistics")
    @ApiOperation("暂降原因和暂降类型统计")
    @ApiImplicitParam(name = "param", value = "param", required = true)
    public HttpResult<CsEventReasonAndTypeVo> eventReasonAndTypeStatistics(@RequestBody EventStatisticParam param) {
        String methodDescribe = getMethodDescribe("eventReasonAndTypeStatistics");
        CsEventReasonAndTypeVo result = csEventPOService.eventReasonAndTypeStatistics(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }
}
