package cn.cslg.report.controller;

import cn.cslg.report.common.core.base.Constants;
import cn.cslg.report.common.model.dto.ConclusionDTO;
import cn.cslg.report.common.model.dto.ReportDTO;
import cn.cslg.report.common.model.vo.ReportDeVO;
import cn.cslg.report.common.model.vo.ReportVO;
import cn.cslg.report.common.utils.Response;
import cn.cslg.report.common.utils.StringUtils;
import cn.cslg.report.common.utils.auth.checkAuth;
import cn.cslg.report.entity.ReportReferences;
import cn.cslg.report.service.business.*;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Tag(name = "报告管理")
@RestController
@RequestMapping(Constants.REPORT_API + "/report")
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ReportController {
    private final ReportService reportService;
    private final ReportReferencesService reportReferencesService;

    /**
     * @param report 报告类
     * @author 沈永艺
     * @date 2022-11-4
     * @see ReportService
     */
    @RequestMapping(value = "/addReport", method = RequestMethod.POST)
    @Operation(summary = "新增报告")
    @checkAuth(FunId = "/pcs/report/add")
    @Transactional(rollbackFor = Exception.class)
    public String addReport(String report, List<MultipartFile> files) throws IOException {
        if (report != null) {
            ReportDTO report1 = JSONObject.parseObject(report, ReportDTO.class);
            //新建报告并返回报告Id
            Integer reportId = reportService.addReport(report1, files);
            return Response.success(reportId);
        } else {
            return Response.error("网络异常");
        }
    }

    @RequestMapping(value = "/updateReport", method = RequestMethod.POST)
    @Operation(summary = "修改报告")
    public String updateReport(String report, List<MultipartFile> files) throws IOException {
        if (report != null) {
            ReportDTO report1 = JSONObject.parseObject(report, ReportDTO.class);
            report1.setScenarioId(StringUtils.join(report1.getScenarioList(), ","));
            Boolean flag = reportService.updateReport(report1, files);
            if (flag) {
                return Response.success("修改成功");
            } else {
                return Response.error("修改失败");
            }

        } else {
            return Response.error("未检测到报告信息");
        }
    }

    @RequestMapping(value = "/queryReport", method = RequestMethod.POST)
    @checkAuth(FunId = "/pcs/report/query")
    @Operation(summary = "查找报告")
    public String queryReport(@RequestBody ReportVO reportVO) throws IOException {
        return reportService.queryReport(reportVO);
    }

    @RequestMapping(value = "/deleReport", method = RequestMethod.GET)
    @Operation(summary = "删除报告")
    public String deleReport(Integer id) throws IOException {
        return reportService.reportDele(id);
    }

    @RequestMapping(value = "/addFile", method = RequestMethod.POST)
    @Operation(summary = "文件上传")
    public String addFile(String jsons, List<MultipartFile> files) throws IOException {
        ReportReferences reportReferences = JSONObject.parseObject(jsons, ReportReferences.class);

        return reportReferencesService.add(reportReferences, files);
    }

    @PostMapping(value = "selectfile")
    @Operation(summary = "文件查询")
    public String queryLitigationHistory(@RequestBody ReportReferences reportReferences) throws IOException {
        return reportReferencesService.queryReportReferences(reportReferences);
    }

    @GetMapping("/deletefile")
    @Operation(summary = "删除文件")
    public String deleFile(int id) throws IOException {
        return reportReferencesService.delete(id);
    }

    @PostMapping(value = "updatefiles")
    @Operation(summary = "文件修改")
    @Transactional(rollbackFor = Exception.class)
    public String updatefiles(String jsons, List<MultipartFile> files) throws IOException {
        ReportReferences reportReferences = JSONObject.parseObject(jsons, ReportReferences.class);
        Boolean ja = reportReferencesService.updateReportReferences(reportReferences, files);
        if (ja) {
            return Response.success();
        }
        return Response.error();
    }

    @GetMapping(value = "updateStatus")
    @Operation(summary = "修改报告状态")
    @Transactional(rollbackFor = Exception.class)
    public String updateStatus(Integer reportId, Integer status) {
        reportService.updateStatus(reportId, status);
        return Response.success();
    }

    @PostMapping(value = "reportVisual")
    @Operation(summary = "报告可视化")
    @Transactional(rollbackFor = Exception.class)
    public String reportVisual(@RequestBody ReportVO reportVO) throws IOException {
        return Response.success(reportService.reportVisual(reportVO));
    }

    @GetMapping(value = "getPermission")
    @Operation(summary = "获得报告权限")
    @Transactional(rollbackFor = Exception.class)
    public String getPermission() throws IOException {
        return Response.success(reportService.getPermession());
    }

    @PostMapping(value = "addConclusions")
    @Operation(summary = "添加核心结论")
    @Transactional(rollbackFor = Exception.class)
    public String addConclusions(@RequestBody ConclusionDTO conclusionDTO) throws IOException {
        Boolean flag = reportService.addConclusions(conclusionDTO);
        if (flag) {
            return Response.success("添加成功");
        }
        return Response.error("添加失败");
    }


    @GetMapping(value = "/reReportDetail")
    @Operation(summary = "返回报告详情")
    @Transactional(rollbackFor = Exception.class)
    public String addConclusions(Integer reportId) throws IOException {
        ReportDeVO reportDeDTO = reportService.reReportDetail(reportId);
        if (reportDeDTO == null) {
            return Response.error("报告不存在");
        }
        return Response.success(reportDeDTO);
    }
}
