package cn.cslg.report.controller;

import cn.cslg.report.common.core.base.Constants;
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.entity.Report;
import cn.cslg.report.service.business.*;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
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 AssoTaskPersonelService assoTaskPersonelService;
    private final ReportFileService reportFileService;
    private final AssoReportFileService assoReportFileService;

    /**
     * @param report 报告类
     * @author 沈永艺
     * @date 2022-11-4
     * @see ReportService
     */
    @RequestMapping(value = "/addReport", method = RequestMethod.POST)
    @Operation(summary = "新增报告")
    @Transactional(rollbackFor = Exception.class)
    public String addReport(Report report, List<MultipartFile> file) {
        // TODO: 2022/11/4 关于错误提示与对应的错误码还需要制定 避免前台看到失败字样 用网络异常来代替说明
        // 目前的错误说明有
        //  添加成功 是正常添加成功
        //  网络异常,添加失败 是数据库的操作或返回的数据有问题但没抛出异常
        //  请求失败 是Service抛出了异常
        //  服务器无响应 是未知情况 需要重点查看
        //  网络异常 是传入参数出现了问题
        // 以上的说明可以参考或者改变，按照实际情况使用，另外这些说明我没有设置CODE码，建议加上
        if (report != null) {
            //新建报告并返回报告Id
            Integer reportId = reportService.addReport(report,file);

            return Response.success("添加成功");

        } else {
            return Response.error("网络异常");
        }
    }

    @RequestMapping(value = "/updateReport", method = RequestMethod.POST)
    @Operation(summary = "修改报告")
    public String updateReport(@RequestBody Report report) {
        if (report != null) {
            report.setScenarioId(StringUtils.join(report.getScenarioList(), ","));
            Boolean flag = reportService.updateById(report);
            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 {
        assoTaskPersonelService.deleAssoTaskPersonel(id);
        return reportService.reportDele(id);
    }


}
