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.OnlineRateService;
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/27 9:17
 */

@Slf4j
@Api(tags = "生成在线率数据表")
@RestController
@RequestMapping("/onlineRate")
@RequiredArgsConstructor
public class OnlineRateController  extends BaseController {

    private final OnlineRateService onlineRateService;

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

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/onlineRateData")
    @ApiOperation("生成在线率数据(MySQL入库)")
    @ApiImplicitParam(name = "lineParam", value = "参数", required = true)
    public HttpResult<String> onlineRateData(@RequestBody @Validated LineParam lineParam){
        log.info(LocalDateTime.now()+"onlineRateData开始执行");
        String methodDescribe = getMethodDescribe("onlineRateData");
        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+"-->开始执行");
                onlineRateService.onlineRateData(lineParam,item);
            }
        } else {
            onlineRateService.onlineRateData(lineParam,lineParam.getDataDate());
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

}
