package com.muyu.enterpise.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.domain.WarnLogs;
import com.muyu.enterpise.service.IWarnLogsService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 预警日志Controller
 * @author muyu
 * @date 2024-09-20
 */
@RestController
@RequestMapping("/logs")
@Tag(name = "查询预警日志模块",description = "查询预警日志模块")
public class WarnLogsController extends BaseController
{
    @Resource
    private IWarnLogsService warnLogsService;

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

    /**
     * 导出预警日志列表
     */
    @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, "预警日志数据");
    }

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

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

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

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