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.FloodEmergencyPlan;
import com.ruoyi.system.service.IFloodEmergencyPlanService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 应急预案库Controller
 *
 * @author jackeyhuang
 * @version 1.0.0
 * @date 2025-01-27
 */
@RestController
@RequestMapping("/flood/emergency")
public class FloodEmergencyPlanController extends BaseController
{
    @Autowired
    private IFloodEmergencyPlanService floodEmergencyPlanService;

    /**
     * 查询应急预案库列表
     */
    @PreAuthorize("@ss.hasPermi('flood:emergency:list')")
    @GetMapping("/list")
    public TableDataInfo list(FloodEmergencyPlan floodEmergencyPlan)
    {
        startPage();
        List<FloodEmergencyPlan> list = floodEmergencyPlanService.selectFloodEmergencyPlanList(floodEmergencyPlan);
        return getDataTable(list);
    }

    /**
     * 导出应急预案库列表
     */
    @PreAuthorize("@ss.hasPermi('flood:emergency:export')")
    @Log(title = "应急预案库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FloodEmergencyPlan floodEmergencyPlan)
    {
        List<FloodEmergencyPlan> list = floodEmergencyPlanService.selectFloodEmergencyPlanList(floodEmergencyPlan);
        ExcelUtil<FloodEmergencyPlan> util = new ExcelUtil<FloodEmergencyPlan>(FloodEmergencyPlan.class);
        util.exportExcel(response, list, "应急预案库数据");
    }

    /**
     * 获取应急预案库详细信息
     */
    @PreAuthorize("@ss.hasPermi('flood:emergency:query')")
    @GetMapping(value = "/{planId}")
    public AjaxResult getInfo(@PathVariable("planId") Long planId)
    {
        return success(floodEmergencyPlanService.selectFloodEmergencyPlanByPlanId(planId));
    }

    /**
     * 新增应急预案库
     */
    @PreAuthorize("@ss.hasPermi('flood:emergency:add')")
    @Log(title = "应急预案库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FloodEmergencyPlan floodEmergencyPlan)
    {
        floodEmergencyPlan.setCreateBy(getUsername());
        return toAjax(floodEmergencyPlanService.insertFloodEmergencyPlan(floodEmergencyPlan));
    }

    /**
     * 修改应急预案库
     */
    @PreAuthorize("@ss.hasPermi('flood:emergency:edit')")
    @Log(title = "应急预案库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FloodEmergencyPlan floodEmergencyPlan)
    {
        floodEmergencyPlan.setUpdateBy(getUsername());
        return toAjax(floodEmergencyPlanService.updateFloodEmergencyPlan(floodEmergencyPlan));
    }

    /**
     * 删除应急预案库
     */
    @PreAuthorize("@ss.hasPermi('flood:emergency:remove')")
    @Log(title = "应急预案库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{planIds}")
    public AjaxResult remove(@PathVariable Long[] planIds)
    {
        return toAjax(floodEmergencyPlanService.deleteFloodEmergencyPlanByPlanIds(planIds));
    }

    /**
     * 导入应急预案数据
     */
    @PreAuthorize("@ss.hasPermi('flood:emergency:import')")
    @Log(title = "应急预案库", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        String operName = getUsername();
        String message = floodEmergencyPlanService.importEmergencyPlan(file, updateSupport, operName);
        return success(message);
    }

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