package com.muyu.server.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.annotation.RequiresPermissions;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.domain.WarnLogs;
import com.muyu.server.service.WarnLogsService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import static com.muyu.common.core.domain.Result.error;
import static com.muyu.common.core.domain.Result.success;
import static com.muyu.common.core.utils.PageUtils.startPage;

/**
 * 预警日志控制层
 * @Author：蓬叁
 * @Package：com.muyu.warn.controller
 * @Project：cloud-server-8
 * @name：WarnLogsController
 * @Date：2024/9/20 下午7:29
 */
@RestController
@RequestMapping("/logs")
@Tag(name = "预警日志")
public class WarnLogsController extends BaseController {

    @Autowired private WarnLogsService warnLogsService;

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

    /**
     * 导出预警日志列表
     */
    @RequiresPermissions("platform: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("platform:logs:query")
    @GetMapping(value = "/{id}")
    public Result<List<WarnLogs>> getInfo(@PathVariable("id") Long id)
    {
        return success(warnLogsService.selectWarnLogsById(id));
    }

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

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

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

}
