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.line.IntegrityService;
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;
import java.util.Objects;

/**
 * @author xiaoyao
 * @version 1.0.0
 * @createTime 2022/10/24 20:03
 */

@Slf4j
@Api(tags = "数据完整性统计")
@RestController
@RequestMapping("/integrity")
@RequiredArgsConstructor
public class IntegrityController extends BaseController {

    private final IntegrityService integrityService;

    private final CommTerminalGeneralClient commTerminalGeneralClient;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/dataIntegrity")
    @ApiOperation("数据完整性统计(MySQL库)")
    @ApiImplicitParam(name = "lineParam", value = "参数", required = true)
    public HttpResult<String> dataIntegrity(@RequestBody @Validated LineParam lineParam){
        log.info(LocalDateTime.now()+"dataIntegrity开始执行");
        String methodDescribe = getMethodDescribe("dataIntegrity");
        String startTime,endTime;
        if (Objects.nonNull(lineParam.getRepairFlag()) && lineParam.getRepairFlag()){
            List<String> timeRange = NjcnDateUtils.findEveryDay(lineParam.getBeginTime(),lineParam.getEndTime());
            for (String item : timeRange) {
                log.info(item+"-->开始执行");
                startTime = item+" "+"00:00:00";
                endTime = item+" "+"23:59:59";
                integrityService.dataIntegrity(lineParam,startTime,endTime);
            }
        } else {
            integrityService.dataIntegrity(lineParam,lineParam.getBeginTime(),lineParam.getEndTime());
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, CommonResponseEnum.SUCCESS.getMessage(), methodDescribe);
    }
}
