package com.fuyuanshen.web.controller.device;

import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.common.core.validate.AddGroup;
import com.fuyuanshen.common.core.validate.EditGroup;
import com.fuyuanshen.common.excel.utils.ExcelUtil;
import com.fuyuanshen.common.idempotent.annotation.RepeatSubmit;
import com.fuyuanshen.common.log.annotation.Log;
import com.fuyuanshen.common.log.enums.BusinessType;
import com.fuyuanshen.common.mybatis.core.page.PageQuery;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;
import com.fuyuanshen.common.web.core.BaseController;
import com.fuyuanshen.equipment.domain.bo.DeviceAlarmBo;
import com.fuyuanshen.equipment.domain.vo.DeviceAlarmVo;
import com.fuyuanshen.equipment.service.IDeviceAlarmService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备告警
 *
 * @author Lion Li
 * @date 2025-08-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("api/equipment/alarm")
public class DeviceAlarmController extends BaseController {

    private final IDeviceAlarmService deviceAlarmService;


    /**
     * 查询设备告警列表
     */
    // @SaCheckPermission("equipment:alarm:list")
    @GetMapping("/list")
    public TableDataInfo<DeviceAlarmVo> list(DeviceAlarmBo bo, PageQuery pageQuery) {
        return deviceAlarmService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出设备告警列表
     */
    // @SaCheckPermission("equipment:alarm:export")
    @Log(title = "导出设备告警列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DeviceAlarmBo bo, HttpServletResponse response) {
        List<DeviceAlarmVo> list = deviceAlarmService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备告警", DeviceAlarmVo.class, response);
    }

    /**
     * 获取设备告警详细信息
     *
     * @param id 主键
     */
    // @SaCheckPermission("equipment:alarm:query")
    @GetMapping("/{id}")
    public R<DeviceAlarmVo> getInfo(@NotNull(message = "主键不能为空")
                                    @PathVariable Long id) {
        return R.ok(deviceAlarmService.queryById(id));
    }

    /**
     * 新增设备告警
     */
    // @SaCheckPermission("equipment:alarm:add")
    @Log(title = "设备告警", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DeviceAlarmBo bo) {
        return toAjax(deviceAlarmService.insertByBo(bo));
    }

    /**
     * 修改设备告警
     */
    // @SaCheckPermission("equipment:alarm:edit")
    @Log(title = "设备告警", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DeviceAlarmBo bo) {
        return toAjax(deviceAlarmService.updateByBo(bo));
    }

    /**
     * 删除设备告警
     *
     * @param ids 主键串
     */
    // @SaCheckPermission("equipment:alarm:remove")
    @Log(title = "设备告警", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(deviceAlarmService.deleteWithValidByIds(List.of(ids), true));
    }

}
