package com.njcn.gather.aireport.testreport.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.param.TestReportGroupSaveParam;
import com.njcn.gather.aireport.testreport.pojo.param.TestReportParam;
import com.njcn.gather.aireport.testreport.pojo.vo.TestReportGroupReportVO;
import com.njcn.gather.aireport.testreport.pojo.vo.TestReportLedgerImportResultVO;
import com.njcn.gather.aireport.testreport.pojo.vo.TestReportPointVO;
import com.njcn.gather.aireport.testreport.pojo.vo.TestReportVO;
import com.njcn.gather.aireport.testreport.service.TestReportService;
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.ModelAttribute;
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")
@RequiredArgsConstructor
public class TestReportController extends BaseController {

    private final TestReportService testReportService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询普测报告列表")
    @PostMapping("/list")
    public HttpResult<Page<TestReportVO>> list(@RequestBody(required = false) TestReportParam.QueryParam param) {
        String methodDescribe = getMethodDescribe("list");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,
                testReportService.listTestReports(param), methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @ApiOperation("新增普测报告")
    @PostMapping("/add")
    public HttpResult<Boolean> add(@RequestBody @Validated TestReportParam.AddParam param) {
        String methodDescribe = getMethodDescribe("add");
        boolean result = testReportService.addTestReport(param);
        return HttpResultUtil.assembleCommonResponseResult(result ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL,
                result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @ApiOperation("编辑普测报告")
    @PostMapping("/update")
    public HttpResult<Boolean> update(@RequestBody @Validated TestReportParam.UpdateParam param) {
        String methodDescribe = getMethodDescribe("update");
        boolean result = testReportService.updateTestReport(param);
        return HttpResultUtil.assembleCommonResponseResult(result ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL,
                result, methodDescribe);
    }

    @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 = testReportService.deleteTestReports(ids);
        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<TestReportVO> detail(@PathVariable("id") String id) {
        String methodDescribe = getMethodDescribe("detail");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,
                testReportService.getTestReport(id), methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("预览普测报告关联模板文件")
    @ApiImplicitParam(name = "id", value = "普测报告ID", required = true)
    @GetMapping("/{id}/preview")
    public ResponseEntity<byte[]> preview(@PathVariable("id") String id) {
        return testReportService.previewTemplate(id);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("导出台账导入模板")
    @GetMapping("/ledger/template")
    public ResponseEntity<byte[]> downloadLedgerTemplate() {
        return testReportService.downloadLedgerTemplate();
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("导出普测报告")
    @GetMapping("/export")
    public void export(@ModelAttribute TestReportParam.QueryParam param) {
        testReportService.exportTestReports(param);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @ApiOperation("提交普测报告审核")
    @PostMapping("/submit-audit")
    public HttpResult<Boolean> submitAudit(@RequestBody @Validated TestReportParam.SubmitAuditParam param) {
        String methodDescribe = getMethodDescribe("submitAudit");
        boolean result = testReportService.submitAudit(param);
        return HttpResultUtil.assembleCommonResponseResult(result ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL,
                result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @ApiOperation("校验台账汇总与监测点附件")
    @PostMapping("/{id}/ledger/validate")
    public HttpResult<TestReportLedgerImportResultVO> validateLedger(@PathVariable("id") String id,
                                                                     @RequestParam("files") MultipartFile[] files) {
        String methodDescribe = getMethodDescribe("validateLedger");
        TestReportLedgerImportResultVO result = testReportService.validateLedger(id, files);
        return HttpResultUtil.assembleCommonResponseResult(Boolean.TRUE.equals(result.getSuccess())
                        ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL,
                result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @ApiOperation("导入已校验通过的台账批次")
    @PostMapping("/{id}/ledger/import")
    public HttpResult<TestReportLedgerImportResultVO> importLedger(@PathVariable("id") String id,
                                                                   @RequestBody @Validated TestReportParam.LedgerImportParam param) {
        String methodDescribe = getMethodDescribe("importLedger");
        TestReportLedgerImportResultVO result = testReportService.importLedger(id, param);
        return HttpResultUtil.assembleCommonResponseResult(Boolean.TRUE.equals(result.getSuccess())
                        ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL,
                result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询监测点列表")
    @GetMapping("/{id}/point/list")
    public HttpResult<List<TestReportPointVO>> listPoints(@PathVariable("id") String id) {
        String methodDescribe = getMethodDescribe("listPoints");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,
                testReportService.listPoints(id), methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @ApiOperation("保存监测点分组")
    @PostMapping("/{id}/group/save")
    public HttpResult<Boolean> saveGroups(@PathVariable("id") String id,
                                          @RequestBody @Validated TestReportGroupSaveParam param) {
        String methodDescribe = getMethodDescribe("saveGroups");
        boolean result = testReportService.saveGroups(id, param);
        return HttpResultUtil.assembleCommonResponseResult(result ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL,
                result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("查询分组报告结果")
    @GetMapping("/{id}/group-report/list")
    public HttpResult<List<TestReportGroupReportVO>> listGroupReports(@PathVariable("id") String id) {
        String methodDescribe = getMethodDescribe("listGroupReports");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,
                testReportService.listGroupReports(id), methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @ApiOperation("按分组生成报告")
    @PostMapping("/{id}/generate")
    public HttpResult<Boolean> generateGroupedReports(@PathVariable("id") String id) {
        String methodDescribe = getMethodDescribe("generateGroupedReports");
        boolean result = testReportService.generateGroupedReports(id);
        return HttpResultUtil.assembleCommonResponseResult(result ? CommonResponseEnum.SUCCESS : CommonResponseEnum.FAIL,
                result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("下载分组报告")
    @ApiImplicitParam(name = "groupReportId", value = "分组报告结果ID", required = true)
    @GetMapping("/group-report/{groupReportId}/download")
    public ResponseEntity<byte[]> downloadGroupReport(@PathVariable("groupReportId") String groupReportId) {
        return testReportService.downloadGroupReport(groupReportId);
    }
}
