package com.ruoyi.web.controller.flood;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
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 org.springframework.web.multipart.MultipartFile;
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.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.FloodCase;
import com.ruoyi.system.service.IFloodCaseService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 洪水案例库Controller
 *
 * @author jackeyhuang
 * @version 1.0.0
 * @date 2025-01-27
 */
@RestController
@RequestMapping("/flood/case")
public class FloodCaseController extends BaseController
{
    @Autowired
    private IFloodCaseService floodCaseService;

    /**
     * 查询洪水案例库列表
     */
    @PreAuthorize("@ss.hasPermi('flood:case:list')")
    @GetMapping("/list")
    public TableDataInfo list(FloodCase floodCase)
    {
        startPage();
        List<FloodCase> list = floodCaseService.selectFloodCaseList(floodCase);
        return getDataTable(list);
    }

    /**
     * 导出洪水案例库列表
     */
    @PreAuthorize("@ss.hasPermi('flood:case:export')")
    @Log(title = "洪水案例库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FloodCase floodCase)
    {
        List<FloodCase> list = floodCaseService.selectFloodCaseList(floodCase);
        ExcelUtil<FloodCase> util = new ExcelUtil<FloodCase>(FloodCase.class);
        util.exportExcel(response, list, "洪水案例库数据");
    }

    /**
     * 获取洪水案例库详细信息
     */
    @PreAuthorize("@ss.hasPermi('flood:case:query')")
    @GetMapping(value = "/{caseId}")
    public AjaxResult getInfo(@PathVariable("caseId") Long caseId)
    {
        return success(floodCaseService.selectFloodCaseByCaseId(caseId));
    }

    /**
     * 新增洪水案例库
     */
    @PreAuthorize("@ss.hasPermi('flood:case:add')")
    @Log(title = "洪水案例库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FloodCase floodCase)
    {
        floodCase.setCreateBy(getUsername());
        return toAjax(floodCaseService.insertFloodCase(floodCase));
    }

    /**
     * 修改洪水案例库
     */
    @PreAuthorize("@ss.hasPermi('flood:case:edit')")
    @Log(title = "洪水案例库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FloodCase floodCase)
    {
        floodCase.setUpdateBy(getUsername());
        return toAjax(floodCaseService.updateFloodCase(floodCase));
    }

    /**
     * 删除洪水案例库
     */
    @PreAuthorize("@ss.hasPermi('flood:case:remove')")
    @Log(title = "洪水案例库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{caseIds}")
    public AjaxResult remove(@PathVariable Long[] caseIds)
    {
        return toAjax(floodCaseService.deleteFloodCaseByCaseIds(caseIds));
    }

    /**
     * 导入洪水案例数据
     */
    @PreAuthorize("@ss.hasPermi('flood:case:import')")
    @Log(title = "洪水案例库", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        String operName = getUsername();
        String message = floodCaseService.importFloodCase(file, updateSupport, operName);
        return success(message);
    }

    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<FloodCase> util = new ExcelUtil<FloodCase>(FloodCase.class);
        util.importTemplateExcel(response, "洪水案例库数据");
    }
}
