package com.njcn.gather.report.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.common.utils.LogUtil;
import com.njcn.gather.report.pojo.DevReportParam;
import com.njcn.gather.report.pojo.param.ReportParam;
import com.njcn.gather.report.pojo.vo.PqReportVO;
import com.njcn.gather.report.service.IPqReportService;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @author hongawen
 * @version 1.0
 * @data 2025/1/9 14:02
 */
@Slf4j
@Api(tags = "报告管理")
@RestController
@RequestMapping("/report")
@RequiredArgsConstructor
public class  ReportController extends BaseController {

    private final IPqReportService pqReportService;

    /**
     * 此方法临时的，给楼下使用，实际需要优化
     * 1、不同的设备需要不同的模板；
     * 2、数据页的内容暂时是固定的，后期可能是动态 的；
     */
    @OperateInfo
    @PostMapping("/generateReport")
    @ApiOperation("生成报告")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public HttpResult<Object> generateReport(@RequestBody DevReportParam devReportParam) {
        String methodDescribe = getMethodDescribe("generateReport");
        LogUtil.njcnDebug(log, "{}，终端参数为：{}", methodDescribe, devReportParam);
        pqReportService.generateReport(devReportParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 下载报告
     */
    @OperateInfo
    @PostMapping("/downloadReport")
    @ApiOperation("下载报告")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public void downloadReport(@RequestBody DevReportParam devReportParam, HttpServletResponse response) {
        String methodDescribe = getMethodDescribe("downloadReport");
        LogUtil.njcnDebug(log, "{}，终端参数为：{}", methodDescribe, devReportParam);
        pqReportService.downloadReport(devReportParam, response);
    }

    @OperateInfo
    @PostMapping("/list")
    @ApiOperation("分页查询报告模板")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public HttpResult<Page<PqReportVO>> list(@RequestBody @Validated ReportParam.QueryParam queryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询参数为：{}", methodDescribe, queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, pqReportService.list(queryParam), methodDescribe);
    }

    @OperateInfo
    @GetMapping("/getById")
    @ApiOperation("查询报告模板详情")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public HttpResult<PqReportVO> getById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getById");
        LogUtil.njcnDebug(log, "{}，查询参数为：{}", methodDescribe, id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, pqReportService.getByReportId(id), methodDescribe);
    }

    @OperateInfo(operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增报告模板")
    @ApiImplicitParam(name = "reportParam", value = "报告模板参数", required = true)
    public HttpResult<Boolean> add(ReportParam reportParam) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，新增参数为：{}", methodDescribe, reportParam);
        boolean result = pqReportService.add(reportParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(operateType = OperateType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("更新报告模板")
    @ApiImplicitParam(name = "reportParam", value = "报告模板参数", required = true)
    public HttpResult<Boolean> update(ReportParam.UpdateParam reportParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，修改参数为：{}", methodDescribe, reportParam);
        boolean result = pqReportService.update(reportParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(operateType = OperateType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("删除报告模板")
    @ApiImplicitParam(name = "param", value = "报告模板参数", required = true)
    public HttpResult<Boolean> delete(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log, "{}，删除参数为：{}", methodDescribe, ids);
        boolean result = pqReportService.delete(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo
    @GetMapping("/listOptions")
    @ApiOperation("查询所有报告模板名称")
    public HttpResult<List<PqReportVO.OptionVO>> listOptions(@RequestParam("pattern") String pattern) {
        String methodDescribe = getMethodDescribe("listOptions");
        LogUtil.njcnDebug(log, "{}锛屾煡璇㈠弬鏁颁负锛歿}", methodDescribe, pattern);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, pqReportService.listOptions(pattern), methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/documented")
    @ApiOperation("设备归档")
    @ApiImplicitParam(name = "ids", value = "设备id", required = true)
    public HttpResult<Boolean> documented(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("documented");
        LogUtil.njcnDebug(log, "{}，设备id为：{}", methodDescribe, ids);
        boolean result = pqReportService.documented(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo
    @PostMapping("/uploadReportToCloud")
    @ApiOperation("批量上传检测报告到云端")
    @ApiImplicitParam(name = "deviceIds", value = "被检设备ID列表，为空时上传所有已生成报告的设备")
    public HttpResult<Object> uploadReportToCloud(@RequestBody(required = false) List<String> deviceIds) {
        String methodDescribe = getMethodDescribe("uploadReportToCloud");
        LogUtil.njcnDebug(log, "{}，设备ID列表为：{}", methodDescribe, deviceIds);
        pqReportService.uploadReportToCloud(deviceIds);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }
}
