package com.aegis.web.controller.emergency.report;

import com.aegis.common.annotation.Log;
import com.aegis.common.core.controller.BaseController;
import com.aegis.common.core.domain.AjaxResult;
import com.aegis.common.core.page.TableDataInfo;
import com.aegis.common.enums.BusinessType;
import com.aegis.emergency.report.domain.ReportTemplate;
import com.aegis.emergency.report.service.IReportTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 上报模板管理 Controller
 * 
 * @author aegis
 */
@RestController
@RequestMapping("/api/emergency/report-templates")
public class ReportTemplateController extends BaseController {

    @Autowired
    private IReportTemplateService reportTemplateService;

    /**
     * 查询模板列表
     */
    @PreAuthorize("@ss.hasPermi('emergency:report:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReportTemplate condition) {
        startPage();
        List<ReportTemplate> list = reportTemplateService.listTemplates(condition);
        return getDataTable(list);
    }

    /**
     * 获取模板详情
     */
    @PreAuthorize("@ss.hasPermi('emergency:report:template:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        ReportTemplate template = reportTemplateService.getById(id);
        if (template == null) {
            return error("模板不存在或已删除");
        }
        return success(template);
    }

    /**
     * 新增模板
     */
    @PreAuthorize("@ss.hasPermi('emergency:report:template:add')")
    @Log(title = "上报模板管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReportTemplate template) {
        return toAjax(reportTemplateService.save(template));
    }

    /**
     * 修改模板
     */
    @PreAuthorize("@ss.hasPermi('emergency:report:template:edit')")
    @Log(title = "上报模板管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public AjaxResult edit(@PathVariable Long id, @RequestBody ReportTemplate template) {
        template.setId(id);
        return toAjax(reportTemplateService.updateById(template));
    }

    /**
     * 删除模板
     */
    @PreAuthorize("@ss.hasPermi('emergency:report:template:remove')")
    @Log(title = "上报模板管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(reportTemplateService.removeByIds(Arrays.asList(ids)));
    }
}