package com.njcn.prepare.harmonic.controller.newalgorithm;

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.NjcnDateUtils;
import com.njcn.device.biz.commApi.CommTerminalGeneralClient;
import com.njcn.prepare.harmonic.pojo.param.LineParam;
import com.njcn.prepare.harmonic.service.mysql.line.ROperatingMonitorMService;
import com.njcn.prepare.harmonic.service.mysql.line.ROperatingMonitorService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2023/4/19  15:45【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/roperatingmonitor")
@Api(tags = "监测点数据质量")
@AllArgsConstructor
public class ROperatingMonitorController extends BaseController {
    private final CommTerminalGeneralClient commTerminalGeneralClient;
    private final ROperatingMonitorService rOperatingMonitorService;
    private final ROperatingMonitorMService rOperatingMonitorMService;

    @ApiOperation("监测点数据质量_日统计(MySQL库)")
    @ApiImplicitParam(value = "jobParam", name = "jobParam", required = true)
    @PostMapping("handlerMonitorOperatingD")
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM)
    public HttpResult<Boolean> handlerMonitorOperatingD(@RequestBody LineParam jobParam) {
        log.info(LocalDateTime.now() + "handlerMonitorOperatingD开始执行");
        String methodDescribe = getMethodDescribe("handlerMonitorOperatingD");
        List<String> indexLists;
        if (CollectionUtils.isEmpty(jobParam.getLineIds())) {
            indexLists = commTerminalGeneralClient.getRunMonitorIds().getData();
        } else {
            indexLists = jobParam.getLineIds();
        }
        if (jobParam.getRepairFlag()) {
            List<String> timeRange = NjcnDateUtils.findEveryDay(jobParam.getBeginTime(), jobParam.getEndTime());
            for (String item : timeRange) {
                log.info(item + "-->开始执行");
                rOperatingMonitorService.handlerMonitorOperatingD(indexLists, item);
            }
        } else {
            rOperatingMonitorService.handlerMonitorOperatingD(indexLists, jobParam.getDataDate());
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
    }

    @ApiOperation("监测点数据质量_月统计(MySQL库)")
    @ApiImplicitParam(value = "jobParam", name = "jobParam", required = true)
    @PostMapping("handlerMonitorOperatingM")
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM)
    public HttpResult<Boolean> handlerMonitorOperatingM(@RequestBody LineParam jobParam) {

        log.info(LocalDateTime.now() + "handlerMonitorOperatingM开始执行");
        String methodDescribe = getMethodDescribe("handlerMonitorOperatingM");
        List<String> indexLists;
        if (CollectionUtils.isEmpty(jobParam.getLineIds())) {
            indexLists = commTerminalGeneralClient.getRunMonitorIds().getData();
        } else {
            indexLists = jobParam.getLineIds();
        }
        rOperatingMonitorMService.handlerMonitorOperatingM(indexLists,jobParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
    }
}
