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

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.Impl.line.LimitRateServiceImpl;
import com.njcn.prepare.harmonic.service.mysql.line.LimitRateService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
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: 2022/10/19  9:58【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Slf4j
@Api(tags = "监测点数据超限处理limitRate")
@RestController
@RequestMapping("limitrate")
@RequiredArgsConstructor
public class LimitrateController extends BaseController {


    private final LimitRateService limitRateService;

    private final CommTerminalGeneralClient commTerminalGeneralClient;

   /* @Deprecated
    @ApiOperation("limitrate表定时任务")
    @ApiImplicitParam(value = "limitRateHanlderParam",name = "limitRateHanlderParam",required = true)
    @PostMapping("LimitRateHanlder")
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM)
    public HttpResult<Boolean> limitRateHanlder(@RequestBody LineParam limitRateHanlderParam ){
        String methodDescribe = getMethodDescribe("limitRateHanlder");
        Boolean result = true;
        List<String> indexLists = new ArrayList<> ();
        if(CollectionUtils.isEmpty (limitRateHanlderParam.getLineIds ())){
            indexLists = lineFeignClient.getLineList ( ).getData ( );
        }else{
            indexLists = limitRateHanlderParam.getLineIds ();
        }
        String startTime = limitRateHanlderParam.getDataDate ()+" "+"00:00:00";
        String endTime = limitRateHanlderParam.getDataDate ()+" "+"23:59:59";

        limitRateService.limitRateJobHandler (indexLists,startTime,endTime);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }*/

    @ApiOperation("生成limitRate表(MySQL入库)")
    @ApiImplicitParam(value = "jobParam",name = "jobParam",required = true)
    @PostMapping("LimitRate")
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM)
    public HttpResult<Boolean> limitRate(@RequestBody LineParam jobParam){
        log.info(LocalDateTime.now()+"limitRate开始执行");
        String startTime,endTime;
        String methodDescribe = getMethodDescribe("limitRate");
        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";
//                limitRateService.limitRate(indexLists,startTime,endTime);
            }
        } else {
//            limitRateService.limitRate(indexLists,jobParam.getBeginTime(),jobParam.getEndTime());
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
    }
}
