package com.ruoyi.ems.controller;

import com.huashe.common.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.ems.domain.InspectionReport;
import com.ruoyi.ems.domain.ManualReportSubmit;
import com.ruoyi.ems.service.IInspectionReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 巡检报告Controller
 */
@RestController
@RequestMapping("/inspection/report")
@Api(value = "InspectionReportController", description = "巡检报告管理")
public class InspectionReportController extends BaseController {

    @Autowired
    private IInspectionReportService reportService;

    /**
     * 查询巡检报告列表
     */
    @RequiresPermissions("ems:inspection:report:list")
    @GetMapping("/list")
    @ApiOperation("查询巡检报告列表")
    public TableDataInfo list(InspectionReport report) {
        startPage();
        List<InspectionReport> list = reportService.selectList(report);
        return getDataTable(list);
    }

    /**
     * 导出巡检报告列表
     */
    @RequiresPermissions("ems:inspection:report:export")
    @Log(title = "巡检报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出巡检报告")
    public void export(HttpServletResponse response, InspectionReport report) {
        List<InspectionReport> list = reportService.selectList(report);
        ExcelUtil<InspectionReport> util = new ExcelUtil<>(InspectionReport.class);
        util.exportExcel(response, list, "巡检报告数据");
    }

    /**
     * 获取巡检报告详细信息
     */
    @RequiresPermissions("ems:inspection:report:query")
    @GetMapping("/{id}")
    @ApiOperation("获取巡检报告详情")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(reportService.selectById(id));
    }

    /**
     * 根据报告代码获取详情
     */
    @RequiresPermissions("ems:inspection:report:query")
    @GetMapping("/code/{reportCode}")
    @ApiOperation("根据报告代码获取详情")
    public AjaxResult getByCode(@PathVariable("reportCode") String reportCode) {
        return success(reportService.selectByReportCode(reportCode));
    }

    /**
     * 修改巡检报告(手动备注)
     */
    @RequiresPermissions("ems:inspection:report:edit")
    @Log(title = "巡检报告", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改巡检报告")
    public AjaxResult edit(@RequestBody InspectionReport report) {
        return toAjax(reportService.update(report));
    }

    /**
     * 更新手动巡检报告
     * 用于编辑已提交的手动巡检报告，支持更新巡检结果、报告内容、图片等
     */
    @RequiresPermissions("ems:inspection:report:edit")
    @Log(title = "更新手动巡检报告", businessType = BusinessType.UPDATE)
    @PutMapping("/updateManual")
    @ApiOperation("更新手动巡检报告")
    public AjaxResult updateManualReport(@RequestBody ManualReportSubmit submit) {
        try {
            submit.setSubmitter(SecurityUtils.getUsername());
            int result = reportService.updateManualReport(submit);
            return toAjax(result);
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 删除巡检报告
     */
    @RequiresPermissions("ems:inspection:report:remove")
    @Log(title = "巡检报告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除巡检报告")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(reportService.deleteByIds(ids));
    }

    /**
     * 提交手动巡检结果
     */
    @RequiresPermissions("ems:inspection:report:edit")
    @Log(title = "提交手动巡检", businessType = BusinessType.UPDATE)
    @PostMapping("/submit")
    @ApiOperation("提交手动巡检结果")
    public AjaxResult submitManual(@RequestBody InspectionReport report) {
        return toAjax(reportService.submitManualReport(report));
    }
}