package com.ruoyi.web.controller.system;

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

import com.ruoyi.common.constant.UserConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ReportDetail;
import com.ruoyi.system.service.IReportDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报告信息Controller
 * 
 * @author boman
 * @date 2021-12-27
 */
@RestController
@RequestMapping("/system/reportDetail")
public class ReportDetailController extends BaseController
{
    @Autowired
    private IReportDetailService reportDetailService;

    /**
     * 查询报告信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:reportDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReportDetail reportDetail)
    {
        startPage();
        List<ReportDetail> list = reportDetailService.selectReportDetailList(reportDetail);
        return getDataTable(list);
    }

    /**
     * 导出报告信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:reportDetail:export')")
    @Log(title = "报告信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReportDetail reportDetail)
    {
        List<ReportDetail> list = reportDetailService.selectReportDetailList(reportDetail);
        ExcelUtil<ReportDetail> util = new ExcelUtil<ReportDetail>(ReportDetail.class);
        util.exportExcel(response, list, "报告信息数据");
    }

    /**
     * 获取报告信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:reportDetail:query')")
    @GetMapping(value = "/{reportId}")
    public AjaxResult getInfo(@PathVariable("reportId") Long reportId)
    {
        return AjaxResult.success(reportDetailService.selectReportDetailByReportId(reportId));
    }

    /**
     * 新增报告信息
     */
    @PreAuthorize("@ss.hasPermi('system:reportDetail:add')")
    @Log(title = "报告信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReportDetail reportDetail)
    {
        if (UserConstants.NOT_UNIQUE.equals(reportDetailService.checkReportNumberUnique(reportDetail)))
        {
            return AjaxResult.error("新增报告信息'" + reportDetail.getReportNumber() + "'失败，报告编号已存在");
        }
        reportDetail.setCreateBy(getUsername());
        return toAjax(reportDetailService.insertReportDetail(reportDetail));
    }

    /**
     * 修改报告信息
     */
    @PreAuthorize("@ss.hasPermi('system:reportDetail:edit')")
    @Log(title = "报告信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReportDetail reportDetail)
    {
        if (UserConstants.NOT_UNIQUE.equals(reportDetailService.checkReportNumberUnique(reportDetail)))
        {
            return AjaxResult.error("修改报告信息'" + reportDetail.getReportNumber() + "'失败，报告编号已存在");
        }
        reportDetail.setUpdateBy(getUsername());
        return toAjax(reportDetailService.updateReportDetail(reportDetail));
    }

    /**
     * 删除报告信息
     */
    @PreAuthorize("@ss.hasPermi('system:reportDetail:remove')")
    @Log(title = "报告信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{reportIds}")
    public AjaxResult remove(@PathVariable Long[] reportIds)
    {
        return toAjax(reportDetailService.deleteReportDetailByReportIds(reportIds));
    }
}
