package com.njcn.gather.steady.checksquare.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
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.steady.checksquare.pojo.param.SteadyChecksquareHistoryQueryParam;
import com.njcn.gather.steady.checksquare.pojo.param.SteadyChecksquareQueryParam;
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareItemDetailVO;
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareQueryVO;
import com.njcn.gather.steady.checksquare.pojo.vo.SteadyChecksquareTaskVO;
import com.njcn.gather.steady.checksquare.service.SteadyChecksquareService;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 数据校验接口。
 */
@Slf4j
@Api(tags = "数据校验")
@RestController
@RequestMapping("/steady/checksquare")
@RequiredArgsConstructor
public class SteadyChecksquareController extends BaseController {

    private final SteadyChecksquareService checksquareService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询数据校验历史记录")
    @PostMapping("/query")
    public HttpResult<Page<SteadyChecksquareTaskVO>> query(@RequestBody @Validated SteadyChecksquareHistoryQueryParam param) {
        String methodDescribe = getMethodDescribe("query");
        LogUtil.njcnDebug(log, "{}，开始查询数据校验历史记录，param={}", methodDescribe, param);
        Page<SteadyChecksquareTaskVO> result = checksquareService.query(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @ApiOperation("新增数据校验记录")
    @PostMapping("/create")
    public HttpResult<SteadyChecksquareTaskVO> create(@RequestBody @Validated SteadyChecksquareQueryParam param) {
        String methodDescribe = getMethodDescribe("create");
        LogUtil.njcnDebug(log, "{}，开始新增数据校验记录，param={}", methodDescribe, param);
        SteadyChecksquareTaskVO result = checksquareService.create(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @ApiOperation("Restart failed checksquare task")
    @PostMapping("/restart")
    public HttpResult<SteadyChecksquareTaskVO> restart(@RequestParam("taskId") String taskId) {
        String methodDescribe = getMethodDescribe("restart");
        LogUtil.njcnDebug(log, "{} restart checksquare task, taskId={}", methodDescribe, taskId);
        SteadyChecksquareTaskVO result = checksquareService.restart(taskId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DELETE)
    @ApiOperation("删除数据校验任务")
    @PostMapping("/delete")
    public HttpResult<Boolean> delete(@RequestBody List<String> taskIds) {
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log, "{}，开始删除数据校验任务，taskIds={}", methodDescribe, taskIds);
        boolean result = checksquareService.delete(taskIds);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询数据校验任务详情")
    @GetMapping("/detail")
    public HttpResult<SteadyChecksquareQueryVO> detail(@RequestParam("taskId") String taskId) {
        String methodDescribe = getMethodDescribe("detail");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, checksquareService.detail(taskId), methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询数据校验检测项明细")
    @GetMapping("/item-detail")
    public HttpResult<SteadyChecksquareItemDetailVO> itemDetail(@RequestParam("itemId") String itemId,
                                                               @RequestParam("detailType") String detailType,
                                                               @RequestParam(value = "statType", required = false) String statType,
                                                               @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                               @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        String methodDescribe = getMethodDescribe("itemDetail");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,
                checksquareService.itemDetail(itemId, detailType, statType, pageNum, pageSize), methodDescribe);
    }
}
