package cn.cslg.report.controller;

import cn.cslg.report.common.core.base.Constants;
import cn.cslg.report.common.model.vo.FileVO;
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.InvalidRecord;
import cn.cslg.report.entity.LitigationHistory;
import cn.cslg.report.entity.Report;
import cn.cslg.report.entity.ReportReferences;
import cn.cslg.report.service.business.*;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.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(Report report, List<MultipartFile> files) {
        // TODO: 2022/11/4 关于错误提示与对应的错误码还需要制定 避免前台看到失败字样 用网络异常来代替说明
        // 目前的错误说明有
        //  添加成功 是正常添加成功
        //  网络异常,添加失败 是数据库的操作或返回的数据有问题但没抛出异常
        //  请求失败 是Service抛出了异常
        //  服务器无响应 是未知情况 需要重点查看
        //  网络异常 是传入参数出现了问题
        // 以上的说明可以参考或者改变，按照实际情况使用，另外这些说明我没有设置CODE码，建议加上
        if (report != null) {
            //新建报告并返回报告Id
            Integer reportId = reportService.addReport(report,files);
            return Response.success("添加成功");
        } 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) {
            Report report1 =JSONObject.parseObject(report,Report.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)
    @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();
    }

}
