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.HarmonicGeneralService;
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/11/3 9:00
 */
@Slf4j
@Api(tags = "稳态综合评估")
@RestController
@RequestMapping("/general")
@RequiredArgsConstructor
public class HarmonicGeneralController extends BaseController {

    private final HarmonicGeneralService harmonicGeneralService;

    @Deprecated
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/generalData")
    @ApiOperation("稳态综合评估日数据计算")
    @ApiImplicitParam(name = "lineParam", value = "参数", required = true)
    public HttpResult<String> generalData(@RequestBody @Validated LineParam lineParam){
        String methodDescribe = getMethodDescribe("generalData");
        harmonicGeneralService.generalData(lineParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/generalDataToMysql")
    @ApiOperation("稳态综合评估日数据计算(MySQL入库)")
    @ApiImplicitParam(name = "lineParam", value = "参数", required = true)
    public HttpResult<String> generalDataToMysql(@RequestBody @Validated LineParam lineParam){
        log.info(LocalDateTime.now()+"generalDataToMysql开始执行");
        String methodDescribe = getMethodDescribe("generalDataToMysql");
        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+"-->开始执行");
                harmonicGeneralService.generalDataToMysql(lineParam,item);
            }
        } else {
            harmonicGeneralService.generalDataToMysql(lineParam,lineParam.getDataDate());
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }
}
