package com.njcn.gather.aireport.testreportmulti.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.gather.aireport.testreport.pojo.vo.TestReportVO;
import com.njcn.gather.aireport.testreportmulti.pojo.param.TestReportMultiParam;
import com.njcn.gather.aireport.testreportmulti.pojo.vo.TestReportMultiVO;
import com.njcn.gather.aireport.testreportmulti.pojo.vo.TestReportMultiValidateResultVO;
import com.njcn.gather.aireport.testreportmulti.service.TestReportMultiService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(tags = "多模板普测批任务")
@RestController
@RequestMapping("/api/test-report-multi")
@RequiredArgsConstructor
public class TestReportMultiController extends BaseController {

    private final TestReportMultiService testReportMultiService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询多模板批任务列表")
    @PostMapping("/list")
    public HttpResult<Page<TestReportMultiVO>> list(@RequestBody(required = false) TestReportMultiParam.QueryParam param) {
        String methodDescribe = getMethodDescribe("list");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,
                testReportMultiService.list(param), methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @ApiOperation("校验多模板批任务压缩包")
    @PostMapping("/zip/validate")
    public HttpResult<TestReportMultiValidateResultVO> validateZip(@RequestParam("file") MultipartFile file) {
        String methodDescribe = getMethodDescribe("validateZip");
        TestReportMultiValidateResultVO result = testReportMultiService.validateZip(file);
        return HttpResultUtil.assembleCommonResponseResult(Boolean.TRUE.equals(result.getSuccess())
                        ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL,
                result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @ApiOperation("创建多模板批任务")
    @PostMapping("/create")
    public HttpResult<Boolean> create(@RequestBody @Validated TestReportMultiParam.CreateParam param) {
        String methodDescribe = getMethodDescribe("create");
        boolean result = testReportMultiService.create(param);
        return HttpResultUtil.assembleCommonResponseResult(result ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL,
                result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询多模板批任务详情")
    @ApiImplicitParam(name = "id", value = "多模板批任务ID", required = true)
    @GetMapping("/{id}")
    public HttpResult<TestReportMultiVO> detail(@PathVariable("id") String id) {
        String methodDescribe = getMethodDescribe("detail");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,
                testReportMultiService.detail(id), methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询子报告列表")
    @ApiImplicitParam(name = "id", value = "多模板批任务ID", required = true)
    @GetMapping("/{id}/child-report/list")
    public HttpResult<List<TestReportVO>> listChildReports(@PathVariable("id") String id) {
        String methodDescribe = getMethodDescribe("listChildReports");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,
                testReportMultiService.listChildReports(id), methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("下载多模板批任务结果压缩包")
    @ApiImplicitParam(name = "id", value = "多模板批任务ID", required = true)
    @GetMapping("/{id}/download")
    public ResponseEntity<byte[]> download(@PathVariable("id") String id) {
        return testReportMultiService.download(id);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DELETE)
    @ApiOperation("删除多模板批任务")
    @PostMapping("/delete")
    public HttpResult<Boolean> delete(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delete");
        boolean result = testReportMultiService.delete(ids);
        return HttpResultUtil.assembleCommonResponseResult(result ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL,
                result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @ApiOperation("执行多模板批任务生成")
    @ApiImplicitParam(name = "id", value = "多模板批任务ID", required = true)
    @PostMapping("/{id}/generate")
    public HttpResult<Boolean> generate(@PathVariable("id") String id) {
        String methodDescribe = getMethodDescribe("generate");
        boolean result = testReportMultiService.generate(id);
        return HttpResultUtil.assembleCommonResponseResult(result ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL,
                result, methodDescribe);
    }
}
