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.device.pq.api.LineFeignClient;
import com.njcn.prepare.harmonic.pojo.param.LineParam;
import com.njcn.prepare.harmonic.service.mysql.line.DayDataService;
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.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.ArrayList;
import java.util.List;

/**
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2022/10/24  9:26【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Slf4j
@Api(tags = "日表数据处理")
@RestController
@RequestMapping("daydata")
@RequiredArgsConstructor
public class DayDataController extends BaseController {


    private final LineFeignClient lineFeignClient;

    private final DayDataService dayDataService;

    @Deprecated
    @ApiOperation("day表定时任务")
    @ApiImplicitParam(value = "jobParam",name = "jobParam",required = true)
    @PostMapping("dayDataHanlder")
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM)
    public HttpResult<Boolean> dayDataHanlder(@RequestBody LineParam jobParam ){
        log.info(LocalDateTime.now()+"dayDataHanlder开始执行");
        String methodDescribe = getMethodDescribe("dayDataHanlder");
        Boolean result = true;
        List<String> indexLists = new ArrayList<> ();
        if(CollectionUtils.isEmpty (jobParam.getLineIds ())){
            indexLists = lineFeignClient.getLineList ( ).getData ( );
        }else{
            indexLists = jobParam.getLineIds ();
        }
        String startTime = jobParam.getDataDate ()+" "+"00:00:00";
        String endTime = jobParam.getDataDate ()+" "+"23:59:59";

        dayDataService.dayDataJobHandler (indexLists,startTime,endTime);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @ApiOperation("day表定时任务(MySQL库)")
    @ApiImplicitParam(value = "jobParam",name = "jobParam",required = true)
    @PostMapping("dataToDay")
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM)
    public HttpResult<Boolean> dataToDayHandler(@RequestBody LineParam jobParam ){
        String startTime,endTime;
        log.info(LocalDateTime.now()+"dataToDayHandler开始执行");
        String methodDescribe = getMethodDescribe("dataToDayHandler");
        List<String> indexLists = new ArrayList<> ();
        if(CollectionUtils.isEmpty (jobParam.getLineIds())){
            indexLists = lineFeignClient.getLineList().getData();
        } else{
            indexLists = jobParam.getLineIds();
        }
        if (jobParam.getType() == 3){
            int year = Integer.parseInt(jobParam.getDataDate().split("-")[0]);
            int month = Integer.parseInt(jobParam.getDataDate().split("-")[1]);
            List<String> dayList = PublicUtil.getDayByMonth(year,month);
            for (String item : dayList) {
                log.info(item+"-->开始执行");
                startTime = item+" "+"00:00:00";
                endTime = item+" "+"23:59:59";
                dayDataService.dataToDayHandler(indexLists,startTime,endTime);
            }
        } else {
            startTime = jobParam.getDataDate()+" "+"00:00:00";
            endTime = jobParam.getDataDate()+" "+"23:59:59";
            log.info("统计时间范围："+startTime+"----->"+endTime);
            dayDataService.dataToDayHandler(indexLists,startTime,endTime);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
    }
}
