package com.fuyuanshen.equipment.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.fuyuanshen.common.idempotent.annotation.RepeatSubmit;
import com.fuyuanshen.common.log.annotation.Log;
import com.fuyuanshen.common.web.core.BaseController;
import com.fuyuanshen.common.mybatis.core.page.PageQuery;
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.log.enums.BusinessType;
import com.fuyuanshen.common.excel.utils.ExcelUtil;
import com.fuyuanshen.equipment.domain.vo.DeviceLogVo;
import com.fuyuanshen.equipment.domain.bo.DeviceLogBo;
import com.fuyuanshen.equipment.service.IDeviceLogService;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;

/**
 * 设备日志
 *
 * @author Lion Li
 * @date 2025-07-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/equipment/log")
public class DeviceLogController extends BaseController {

    private final IDeviceLogService deviceLogService;

    /**
     * 查询设备日志列表
     */
    @SaCheckPermission("equipment:log:list")
    @GetMapping("/list")
    public TableDataInfo<DeviceLogVo> list(DeviceLogBo bo, PageQuery pageQuery) {
        return deviceLogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出设备日志列表
     */
    @SaCheckPermission("equipment:log:export")
    @Log(title = "设备日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DeviceLogBo bo, HttpServletResponse response) {
        List<DeviceLogVo> list = deviceLogService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备日志", DeviceLogVo.class, response);
    }

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

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

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

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

}
