package com.srs.web.controller.diagnostic;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.diagnostic.domain.DiaReportItems;
import com.srs.diagnostic.service.IDiaReportItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;

import com.srs.diagnostic.domain.DiaReportRelease;
import com.srs.diagnostic.service.IDiaReportReleaseService;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.utils.poi.ExcelUtil;
import com.srs.common.enums.BusinessType;
import com.srs.common.annotation.Log;
import com.srs.common.core.page.TableDataInfo;

/**
 * 报表发布Controller
 *
 * @author srs
 * @date 2025-04-23
 */
@RestController
@RequestMapping("/diagnostic/DiaReportRelease")
@Api(value = "报表发布管理", tags = "报表发布管理")
public class DiaReportReleaseController extends BaseController {
    @Autowired
    private IDiaReportReleaseService diaReportReleaseService;

    @Autowired
    private IDiaReportItemsService diaReportItemsService;

    @ApiOperation("列出所有报表")
    @GetMapping("/listAllRelease")
    public AjaxResult listAllRelease() {
        List<DiaReportRelease> list = diaReportReleaseService.list(
                new QueryWrapper<DiaReportRelease>().orderByDesc("relId")
        );
        return AjaxResult.success(list);
    }

    /**
     * 查询报表发布列表
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:DiaReportRelease:list')")
    @GetMapping("/list")
    @ApiOperation("查询报表发布列表")
    public TableDataInfo list(DiaReportRelease diaReportRelease) {
        startPage();
        List<DiaReportRelease> list = diaReportReleaseService.selectDiaReportReleaseList(diaReportRelease);
        return getDataTable(list);
    }

    /**
     * 导出报表发布列表
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:DiaReportRelease:export')")
    @Log(title = "报表发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出报表发布列表")
    public void export(HttpServletResponse response, DiaReportRelease diaReportRelease) {
        List<DiaReportRelease> list = diaReportReleaseService.selectDiaReportReleaseList(diaReportRelease);
        ExcelUtil<DiaReportRelease> util = new ExcelUtil<DiaReportRelease>(DiaReportRelease.class);
        util.exportExcel(response, list, "报表发布数据");
    }

    /**
     * 获取报表发布详细信息
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:DiaReportRelease:query')")
    @GetMapping(value = "/{relId}")
    @ApiOperation("获取报表发布详细信息")
    public AjaxResult getInfo(@PathVariable("relId") Long relId) {
        return success(diaReportReleaseService.selectDiaReportReleaseByRelId(relId));
    }

    /**
     * 新增报表发布
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:DiaReportRelease:add')")
    @Log(title = "报表发布", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增报表发布")
    public AjaxResult add(@RequestBody DiaReportRelease diaReportRelease) {
        return toAjax(diaReportReleaseService.insertDiaReportRelease(diaReportRelease));
    }
    /**
    *新增诊断报告指标项
    * */
    @Log(title = "诊断报告指标项", businessType = BusinessType.INSERT)
    @PostMapping("/addItems/{reportId}")
    @ApiOperation("新增诊断报告指标项")
    public void addItems(@PathVariable int reportId)
    {
        diaReportItemsService.convertReportsToItems(reportId);
    }

    /**
     * 修改报表发布
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:DiaReportRelease:edit')")
    @Log(title = "报表发布", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改报表发布")
    public AjaxResult edit(@RequestBody DiaReportRelease diaReportRelease) {
        return toAjax(diaReportReleaseService.updateDiaReportRelease(diaReportRelease));
    }

    /**
     * 删除报表发布
     */
    @PreAuthorize("@ss.hasPermi('diagnostic:DiaReportRelease:remove')")
    @Log(title = "报表发布", businessType = BusinessType.DELETE)
    @PostMapping("/{relIds}")
    @ApiOperation("删除报表发布")
    public AjaxResult remove(@PathVariable Long[] relIds) {
        return toAjax(diaReportReleaseService.deleteDiaReportReleaseByRelIds(relIds));
    }

}
