package org.dromara.inspection.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
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.core.validate.QueryGroup;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.inspection.domain.bo.ArDeviceBo;
import org.dromara.inspection.domain.vo.ArDeviceVo;
import org.dromara.inspection.service.IArDeviceService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;

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

/**
 * AR设备Controller
 *
 * @author Lion Li
 * @date 2025-01-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/inspection/device")
public class ArDeviceController extends BaseController {

    private final IArDeviceService arDeviceService;

    /**
     * 查询AR设备列表
     */
    @SaCheckPermission("inspection:device:list")
    @GetMapping("/list")
    public TableDataInfo<ArDeviceVo> list(@Validated(QueryGroup.class) ArDeviceBo bo, PageQuery pageQuery) {
        return arDeviceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出AR设备列表
     */
    @SaCheckPermission("inspection:device:export")
    @Log(title = "AR设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated ArDeviceBo bo, HttpServletResponse response) {
        List<ArDeviceVo> list = arDeviceService.queryList(bo);
        ExcelUtil.exportExcel(list, "AR设备", ArDeviceVo.class, response);
    }

    /**
     * 获取AR设备详细信息
     *
     * @param id 设备ID
     */
    @SaCheckPermission("inspection:device:query")
    @GetMapping("/{id}")
    public R<ArDeviceVo> getInfo(@NotNull(message = "设备ID不能为空")
                                 @PathVariable("id") Long id) {
        return R.ok(arDeviceService.queryById(id));
    }

    /**
     * 新增AR设备
     */
    @SaCheckPermission("inspection:device:add")
    @Log(title = "AR设备", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ArDeviceBo bo) {
        return toAjax(arDeviceService.insertByBo(bo));
    }

    /**
     * 修改AR设备
     */
    @SaCheckPermission("inspection:device:edit")
    @Log(title = "AR设备", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ArDeviceBo bo) {
        return toAjax(arDeviceService.updateByBo(bo));
    }

    /**
     * 删除AR设备
     *
     * @param ids 设备ID串
     */
    @SaCheckPermission("inspection:device:remove")
    @Log(title = "AR设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "设备ID不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(arDeviceService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
