package com.ruoyi.system.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresLogin;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.UDevice;
import com.ruoyi.system.service.IUDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * App设备管理（u_device）
 */
@RestController
@RequestMapping("/app/device")
public class UDeviceController extends BaseController {

    @Autowired
    private IUDeviceService uDeviceService;

    /**
     * 查询当前登录用户设备列表
     */
    @RequiresLogin
    @GetMapping("/list")
    public TableDataInfo myList(UDevice query) {
        Long ownerUserId = SecurityUtils.getUserId();
        startPage();
        List<UDevice> list = uDeviceService.selectList(ownerUserId, query);
        return getDataTable(list);
    }

    /**
     * 查询当前登录用户设备详情
     */
    @RequiresLogin
    @GetMapping("/{deviceId}")
    public AjaxResult myInfo(@PathVariable Long deviceId) {
        Long ownerUserId = SecurityUtils.getUserId();
        return success(uDeviceService.selectById(deviceId, ownerUserId));
    }

    /**
     * 新增当前登录用户设备
     */
    @RequiresLogin
    @PostMapping
    public AjaxResult add(@RequestBody UDevice device) {
        Long ownerUserId = SecurityUtils.getUserId();
        String operator = SecurityUtils.getUsername();
        return toAjax(uDeviceService.addDevice(ownerUserId, operator, device));
    }

    /**
     * 修改当前登录用户设备
     */
    @RequiresLogin
    @PutMapping
    public AjaxResult edit(@RequestBody UDevice device) {
        Long ownerUserId = SecurityUtils.getUserId();
        String operator = SecurityUtils.getUsername();
        return toAjax(uDeviceService.editDevice(ownerUserId, operator, device));
    }

    /**
     * 删除当前登录用户设备
     */
    @RequiresLogin
    @DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds) {
        Long ownerUserId = SecurityUtils.getUserId();
        String operator = SecurityUtils.getUsername();
        return toAjax(uDeviceService.removeDevices(ownerUserId, operator, deviceIds));
    }

    /**
     * 内部服务调用：按 ownerUserId 查询列表
     */
    @InnerAuth
    @GetMapping("/inner/list/{ownerUserId}")
    public List<UDevice> innerList(@PathVariable Long ownerUserId, UDevice query) {
        return uDeviceService.selectList(ownerUserId, query);
    }
}
