package com.njcn.product.advance.eventSource.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.StrUtil;
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.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.product.advance.eventSource.service.EventRelevantAnalysisService;
import com.njcn.product.terminal.mysqlTerminal.pojo.param.LargeScreenCountParam;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

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

    private final EventRelevantAnalysisService eventRelevantAnalysisService;

    @PostMapping("processEvents")
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("启动关联分析")
    public HttpResult<Object> processEvents(@RequestBody LargeScreenCountParam param) {
        String methodDescribe = getMethodDescribe("processEvents");
        List<LocalDateTime> timeVal = checkLocalDate(param.getSearchBeginTime(),param.getSearchEndTime());
        eventRelevantAnalysisService.processEvents(timeVal.get(0),timeVal.get(1),param.getDeptId());
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


    /**
     * 校验字符串起始时间和结束时间并返回时间格式时间
     * @author cdf
     * @date 2023/8/10
     */
    public List<LocalDateTime> checkLocalDate(String startTime,String endTime) {
        List<LocalDateTime> resultList = new ArrayList<>();
        if(StrUtil.isBlank(startTime) || StrUtil.isBlank(endTime)){
            throw new BusinessException(CommonResponseEnum.TIME_ERROR);
        }
        try {
            startTime = startTime+StrUtil.SPACE+"00:00:00";
            endTime = endTime+StrUtil.SPACE+"23:59:59";
            LocalDateTime start = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));
            LocalDateTime end = LocalDateTime.parse(endTime,DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));
            resultList.add(start);
            resultList.add(end);
        } catch (Exception e) {
            throw new BusinessException(CommonResponseEnum.TIME_ERROR);
        }
        return resultList;
    }







}
