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.RMpMonitorEvaluateDService;
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("/rmpmonitorevaluate")
@Api(tags = "监测点评价_日统计")
@AllArgsConstructor
public class RMpMonitorEvaluateDController extends BaseController {

    private final CommTerminalGeneralClient commTerminalGeneralClient;
    private final RMpMonitorEvaluateDService rMpMonitorEvaluateDService;


    @ApiOperation("监测点评价_日统计(MySQL库)")
    @ApiImplicitParam(value = "jobParam", name = "jobParam", required = true)
    @PostMapping("handlerMonitorEvaluate")
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM)
    public HttpResult<Boolean> handlerMonitorEvaluate(@RequestBody LineParam jobParam) {
        log.info(LocalDateTime.now() + "handlerMonitorEvaluate开始执行");
        String methodDescribe = getMethodDescribe("handlerMonitorEvaluate");
        String startTime,endTime;
        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+"-->开始执行");
                startTime = item+" "+"00:00:00";
                endTime = item+" "+"23:59:59";
                rMpMonitorEvaluateDService.dayDataJobHandler(indexLists,startTime, endTime);
            }
        } else {
            rMpMonitorEvaluateDService.dayDataJobHandler(indexLists, jobParam.getBeginTime(), jobParam.getEndTime());
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
    }
}
