package com.muyu.warning.controller;

import java.util.Arrays;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
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.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.warning.domain.WarnLogs;
import com.muyu.warning.service.IWarnLogsService;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.muyu.common.core.web.page.TableDataInfo;

/**
 * 预警日志Controller
 *
 * @author muyu
 * @date 2024-09-20
 */
@RestController
@RequestMapping("/logs")
public class WarnLogsController extends BaseController
{
    @Resource
    private IWarnLogsService warnLogsService;

    /**
     * 查询预警日志列表
     */
    @RequiresPermissions("warning:logs:list")
    @GetMapping("/list")
    public Result<TableDataInfo<WarnLogs>> list(WarnLogs warnLogs)
    {
        startPage();
        List<WarnLogs> list = warnLogsService.selectWarnLogsList(warnLogs);
        return getDataTable(list);
    }

    /**
     * 导出预警日志列表
     */
    @RequiresPermissions("warning:logs:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WarnLogs warnLogs)
    {
        List<WarnLogs> list = warnLogsService.selectWarnLogsList(warnLogs);
        ExcelUtil<WarnLogs> util = new ExcelUtil<WarnLogs>(WarnLogs.class);
        util.exportExcel(response, list, "预警日志数据");
    }

    /**
     * 获取预警日志详细信息
     */
    @RequiresPermissions("warning:logs:query")
    @GetMapping(value = "/{id}")
    public Result<List<WarnLogs>> getInfo(@PathVariable("id") Long id)
    {
        return success(warnLogsService.selectWarnLogsById(id));
    }

    /**
     * 新增预警日志
     */
    @RequiresPermissions("warning:logs:add")
    @PostMapping
    public Result<Integer> add(
            @Validated @RequestBody WarnLogs warnLogs)
    {
        if (warnLogsService.checkIdUnique(warnLogs)) {
            return error("新增 预警日志 '" + warnLogs + "'失败，预警日志已存在");
        }
        return toAjax(warnLogsService.save(warnLogs));
    }

    /**
     * 修改预警日志
     */
    @RequiresPermissions("warning:logs:edit")
    @PutMapping
    public Result<Integer> edit(
            @Validated @RequestBody WarnLogs warnLogs)
    {
        if (!warnLogsService.checkIdUnique(warnLogs)) {
            return error("修改 预警日志 '" + warnLogs + "'失败，预警日志不存在");
        }
        return toAjax(warnLogsService.updateById(warnLogs));
    }

    /**
     * 删除预警日志
     */
    @RequiresPermissions("warning:logs:remove")
	@DeleteMapping("/{ids}")
    public Result<Integer> remove(@PathVariable("ids") Long[] ids)
    {
        warnLogsService.removeBatchByIds(Arrays.asList(ids));
        return success();
    }
}
