package com.fuyuanshen.app.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.app.domain.vo.AppDeviceBindRecordVo;
import com.fuyuanshen.app.domain.bo.AppDeviceBindRecordBo;
import com.fuyuanshen.app.service.IAppDeviceBindRecordService;
import com.fuyuanshen.common.mybatis.core.page.TableDataInfo;

/**
 * 设备绑定关系
 *
 * @author Lion Li
 * @date 2025-07-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/deviceBindRecord")
public class AppDeviceBindRecordController extends BaseController {

    private final IAppDeviceBindRecordService appDeviceBindRecordService;

    /**
     * 查询设备绑定关系列表
     */
    @SaCheckPermission("app:deviceBindRecord:list")
    @GetMapping("/list")
    public TableDataInfo<AppDeviceBindRecordVo> list(AppDeviceBindRecordBo bo, PageQuery pageQuery) {
        return appDeviceBindRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出设备绑定关系列表
     */
    @SaCheckPermission("app:deviceBindRecord:export")
    @Log(title = "设备绑定关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppDeviceBindRecordBo bo, HttpServletResponse response) {
        List<AppDeviceBindRecordVo> list = appDeviceBindRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备绑定关系", AppDeviceBindRecordVo.class, response);
    }

    /**
     * 获取设备绑定关系详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("app:deviceBindRecord:query")
    @GetMapping("/{id}")
    public R<AppDeviceBindRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(appDeviceBindRecordService.queryById(id));
    }

    /**
     * 新增设备绑定关系
     */
    @SaCheckPermission("app:deviceBindRecord:add")
    @Log(title = "设备绑定关系", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppDeviceBindRecordBo bo) {
        return toAjax(appDeviceBindRecordService.insertByBo(bo));
    }

    /**
     * 修改设备绑定关系
     */
    @SaCheckPermission("app:deviceBindRecord:edit")
    @Log(title = "设备绑定关系", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppDeviceBindRecordBo bo) {
        return toAjax(appDeviceBindRecordService.updateByBo(bo));
    }

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