package com.njcn.advance.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.advance.pojo.po.PqsRelevanceLog;
import com.njcn.advance.service.EventRelevantAnalysisService;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.constant.PatternRegex;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.PubUtils;
import com.njcn.event.pojo.po.RmpEventDetailAssPO;
import com.njcn.event.pojo.vo.AdvanceEventDetailVO;
import com.njcn.web.constant.ValidMessage;
import com.njcn.web.controller.BaseController;
import com.njcn.web.pojo.param.BaseParam;
import io.swagger.annotations.Api;
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.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * pqs
 * 事件关联分析
 *
 * @author cdf
 * @date 2023/6/30
 */
@Slf4j
@RestController
@RequestMapping("process")
@Api(tags = "暂降事件关联分析")
@RequiredArgsConstructor
public class EventRelevantAnalysisController extends BaseController {

    private final EventRelevantAnalysisService eventRelevantAnalysisService;

    @GetMapping("processEvents")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("启动关联分析")
    public HttpResult<Object> processEvents(@RequestParam("startTime") String startTime, @RequestParam("endTime")String endTime) {
        String methodDescribe = getMethodDescribe("processEvents");
        List<LocalDateTime> timeVal = PubUtils.checkLocalDate(startTime,endTime);
        eventRelevantAnalysisService.processEvents(timeVal.get(0),timeVal.get(1));
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }



    /**
     * 高级算法-暂降事件关联分析主列表
     * @author cdf
     * @date 2023/7/20
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/querySagEventsPage")
    @ApiOperation("高级算法-暂降事件关联分析主列表")
    public HttpResult<Page<AdvanceEventDetailVO>> querySagEventsPage(@RequestBody BaseParam baseParam){
        String methodDescribe = getMethodDescribe("querySagEventsPage");
        Page<AdvanceEventDetailVO> list = eventRelevantAnalysisService.querySagEventsPage(baseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    /**
     * 事件查看
     * @author cdf
     * @date 2023/8/11
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryEventsAssPage")
    @ApiOperation("事件波形分析主列表")
    public HttpResult<Page<RmpEventDetailAssPO>> queryEventsAssPage(@RequestBody BaseParam baseParam){
        String methodDescribe = getMethodDescribe("queryEventsAssPage");
        String searchValue = baseParam.getSearchValue();
//        Pattern pattern = Pattern.compile(PatternRegex.SPECIAL_REGEX);
//        if(pattern.matcher(searchValue).find()){
//            throw new BusinessException(ValidMessage.SPECIAL_REGEX);
//        }
        Page<RmpEventDetailAssPO> page = eventRelevantAnalysisService.queryEventsAssPage(baseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }


    /**
     * 事件详情
     * @author cdf
     * @date 2023/8/11
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/queryEventDetailByAssId")
    @ApiOperation("事件详情查看")
    public HttpResult<Page<AdvanceEventDetailVO>> queryEventDetailByAssId(@RequestParam("id")String id,@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize){
        String methodDescribe = getMethodDescribe("queryEventDetailByAssId");
        Page<AdvanceEventDetailVO> page = eventRelevantAnalysisService.queryEventDetailByAssId(id,pageNum,pageSize);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    /**
     * 新增关联事件主列表
     * @author cdf
     * @date 2023/8/11
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryEventByTimeRange")
    @ApiOperation("新增关联事件主列表")
    public HttpResult<Page<AdvanceEventDetailVO>> queryEventByTimeRange(@RequestBody BaseParam baseParam){
        String methodDescribe = getMethodDescribe("queryEventDetailByAssId");
        Page<AdvanceEventDetailVO> page = eventRelevantAnalysisService.queryEventByTimeRange(baseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    /**
     * 把暂降事件添加到指定事件中去
     * @author cdf
     * @date 2023/8/16
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPLOAD)
    @PostMapping("/updateEventToAss")
    @ApiOperation("把暂降事件添加到指定事件中去")
    public HttpResult<Page<AdvanceEventDetailVO>> updateEventToAss(@RequestBody Map<String,Object> map){
        String methodDescribe = getMethodDescribe("updateEventToAss");
        if(!map.containsKey("eventId") || !map.containsKey("assId")){
            throw new BusinessException("参数异常");
        }
        List<String> eventIds = (List<String>) map.get("eventId");
        if(CollectionUtil.isEmpty(eventIds)){
            throw new BusinessException("暂降事件不可为空");
        }
        eventRelevantAnalysisService.updateEventToAss(eventIds, (String) map.get("assId"));
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 删除指定关联事件中的暂降事件
     * @author cdf
     * @date 2023/8/16
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPLOAD)
    @PostMapping("/delEventToAss")
    @ApiOperation("删除指定关联事件中的暂降事件")
    public HttpResult<Page<AdvanceEventDetailVO>> delEventToAss(@RequestBody List<String> eventId){
        String methodDescribe = getMethodDescribe("delEventToAss");
        eventRelevantAnalysisService.delEventToAss(eventId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 获取指定事件中的暂降事件
     * @author cdf
     * @date 2023/8/16
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryEventList")
    @ApiOperation("查询关联事件中的暂降事件")
    public HttpResult<List<AdvanceEventDetailVO>> queryEventList(@RequestBody List<String> eventId){
        String methodDescribe = getMethodDescribe("queryEventList");
        List<AdvanceEventDetailVO> list = eventRelevantAnalysisService.queryEventList(eventId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }


    /**
     * 操作记录查看
     * @author cdf
     * @date 2023/8/11
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryRelevantLogPage")
    @ApiOperation("事件分析操作记录查看")
    public HttpResult<Page<PqsRelevanceLog>> queryRelevantLogPage(@RequestBody BaseParam baseParam){
        String methodDescribe = getMethodDescribe("queryRelevantLogPage");
        Page<PqsRelevanceLog> page = eventRelevantAnalysisService.queryRelevantLogPage(baseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, page, methodDescribe);
    }

    /**
     * 操作记录删除
     * @author cdf
     * @date 2023/8/11
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/delRelevantLog")
    @ApiOperation("操作记录删除")
    public HttpResult<Boolean> delRelevantLog(@RequestParam("id")String id){
        String methodDescribe = getMethodDescribe("delRelevantLog");
        boolean flag = eventRelevantAnalysisService.delRelevantLog(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
    }








}
