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.prepare.harmonic.pojo.param.LineParam;
import com.njcn.prepare.harmonic.service.mysql.line.LimitTargetService;
import com.njcn.prepare.harmonic.utils.PublicUtil;
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.validation.annotation.Validated;
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;

/**
 * @author xiaoyao
 * @version 1.0.0
 * @createTime 2022/10/26 13:32
 */

@Slf4j
@Api(tags = "监测点T相数据超限处理")
@RestController
@RequestMapping("/limitTarget")
@RequiredArgsConstructor
public class LimitTargetController extends BaseController {

    private final LimitTargetService limitTargetService;

    @Deprecated
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getLimitTargetData")
    @ApiOperation("生成limit_target表")
    @ApiImplicitParam(name = "lineParam", value = "参数", required = true)
    public HttpResult<String> getLimitTargetData(@RequestBody @Validated LineParam lineParam){
        String methodDescribe = getMethodDescribe("getLimitTargetData");
        limitTargetService.getLimitTargetData(lineParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/limitTargetData")
    @ApiOperation("生成limit_target表(MySQL入库)")
    @ApiImplicitParam(name = "lineParam", value = "参数", required = true)
    public HttpResult<String> limitTargetData(@RequestBody @Validated LineParam lineParam){
        log.info(LocalDateTime.now()+"limitTargetData开始执行");
        String methodDescribe = getMethodDescribe("limitTargetData");
        String time;
        if (lineParam.getType() == 3){
            int year = Integer.parseInt(lineParam.getDataDate().split("-")[0]);
            int month = Integer.parseInt(lineParam.getDataDate().split("-")[1]);
            List<String> dayList = PublicUtil.getDayByMonth(year,month);
            for (String item : dayList) {
                log.info(item+"-->开始执行");
                time = item + " 00:00:00";
                limitTargetService.limitTargetData(lineParam,time);
            }
        } else {
            time = lineParam.getDataDate() + " 00:00:00";
            limitTargetService.limitTargetData(lineParam,time);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }
}
