package com.njcn.gather.tool.adddata.controller;

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.LogUtil;
import com.njcn.gather.tool.adddata.pojo.param.AddDataTaskRequestParam;
import com.njcn.gather.tool.adddata.pojo.vo.AddDataPreviewVO;
import com.njcn.gather.tool.adddata.pojo.vo.AddDataTaskCreateVO;
import com.njcn.gather.tool.adddata.pojo.vo.AddDataTaskStatusVO;
import com.njcn.gather.tool.adddata.service.AddDataTaskService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.Api;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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;

/**
 * 数据补录任务接口。
 */
@Validated
@Slf4j
@Api(tags = "数据补录任务")
@RestController
@RequestMapping("/addData/task")
@RequiredArgsConstructor
public class AddDataTaskController extends BaseController {

    /** 数据补录任务服务。 */
    private final AddDataTaskService addDataTaskService;

    /**
     * 预估本次补数规模。
     *
     * @param param 补数参数
     * @return 预估结果
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("预估电能质量批量补数规模")
    @PostMapping("/preview")
    public HttpResult<AddDataPreviewVO> preview(@RequestBody @Validated AddDataTaskRequestParam param) {
        String methodDescribe = getMethodDescribe("preview");
        LogUtil.njcnDebug(log, "{}，开始预估补数规模，lineCount={}, intervalMinutes={}",
                methodDescribe, param.getLineIds() == null ? 0 : param.getLineIds().size(), param.getIntervalMinutes());
        AddDataPreviewVO result = addDataTaskService.preview(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 创建后台补数任务。
     *
     * @param param 补数参数
     * @return 任务编号
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("创建电能质量批量补数任务")
    @PostMapping("/create")
    public HttpResult<AddDataTaskCreateVO> create(@RequestBody @Validated AddDataTaskRequestParam param) {
        String methodDescribe = getMethodDescribe("create");
        LogUtil.njcnDebug(log, "{}，开始创建补数任务，lineCount={}, intervalMinutes={}",
                methodDescribe, param.getLineIds() == null ? 0 : param.getLineIds().size(), param.getIntervalMinutes());
        AddDataTaskCreateVO result = addDataTaskService.create(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 查询任务状态。
     *
     * @param taskId 任务编号
     * @return 当前任务状态
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询电能质量批量补数任务状态")
    @GetMapping("/status/{taskId}")
    public HttpResult<AddDataTaskStatusVO> status(@PathVariable("taskId") String taskId) {
        String methodDescribe = getMethodDescribe("status");
        LogUtil.njcnDebug(log, "{}，开始查询补数任务状态，taskId={}", methodDescribe, taskId);
        AddDataTaskStatusVO result = addDataTaskService.getStatus(taskId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }
}
