package org.dromara.app.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.app.domain.bo.AppDeviceBo;
import org.dromara.app.domain.vo.AppDeviceVo;
import org.dromara.app.domain.vo.DashboardStatsVo;
import org.dromara.app.service.IAppDeviceService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备信息

 *
 * @author Lion Li
 * @date 2026-04-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/device")
public class AppDeviceController extends BaseController {

    private final IAppDeviceService appDeviceService;

    /**
     * 查询设备信息
列表
     */
    @SaCheckPermission("app:device:list")
    @GetMapping("/list")
    public TableDataInfo<AppDeviceVo> list(AppDeviceBo bo, PageQuery pageQuery) {
        return appDeviceService.queryPageList(bo, pageQuery);
    }

    /**
     * 仪表盘统计 — 首页 8 个统计卡片聚合数据。
     */
    @SaCheckPermission("app:device:query")
    @GetMapping("/dashboard")
    public R<DashboardStatsVo> dashboard() {
        return R.ok(appDeviceService.getDashboardStats());
    }

    /**
     * 导出设备信息
列表
     */
    @SaCheckPermission("app:device:export")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppDeviceBo bo, HttpServletResponse response) {
        List<AppDeviceVo> list = appDeviceService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备信息 ", AppDeviceVo.class, response);
    }

    /**
     * 获取设备信息
详细信息
     *
     * @param
     */
    @SaCheckPermission("app:device:query")
    @GetMapping("/{deviceNo}")
    public R<AppDeviceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String deviceNo) {
        return R.ok(appDeviceService.queryById(deviceNo));
    }

    /**
     * 新增设备信息

     */
    @SaCheckPermission("app:device:add")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppDeviceBo bo) {
        bo.setStatus("0");
        bo.setWorkStatus("2");
        return toAjax(appDeviceService.insertByBo(bo));
    }

    /**
     * 修改设备信息

     */
    @SaCheckPermission("app:device:edit")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppDeviceBo bo) {
        return toAjax(appDeviceService.updateByBo(bo));
    }

    /**
     * 生成设备二维码
     *
     * @param ids 设备编号集合
     */
    @SaCheckPermission("app:device:edit")
    @Log(title = "设备二维码", businessType = BusinessType.UPDATE)
    @PostMapping("/generateQrCode/{ids}")
    public R<Void> generateQrCode(@NotEmpty(message = "设备编号不能为空")
                                  @PathVariable String[] ids) {
        return toAjax(appDeviceService.generateQrCode(List.of(ids)));
    }

    /**
     * 删除设备信息

     *
     * @param
     */
    @SaCheckPermission("app:device:remove")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceNos}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] deviceNos) {
        return toAjax(appDeviceService.deleteWithValidByIds(List.of(deviceNos), true));
    }
}
