package com.ruoyi.web.controller.agent;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.ss.device.domain.DeviceBO;
import com.ruoyi.ss.device.domain.DeviceQuery;
import com.ruoyi.ss.device.domain.vo.DeviceVO;
import com.ruoyi.ss.device.service.DeviceService;
import com.ruoyi.ss.device.service.DeviceValidator;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wjh
 * 2024/10/8
 */
@RestController
@RequestMapping("/agent/device")
public class AgentDeviceController extends BaseController {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private DeviceValidator deviceValidator;

    @ApiOperation("代理商获取设备列表")
    @GetMapping("/list")
    public TableDataInfo list(DeviceQuery query) {
        startPage();
        startOrderBy();
        query.setAgentId(getUserId());
        return getDataTable(deviceService.selectSmDeviceList(query));
    }

    @ApiOperation("代理商解绑设备商户")
    @PutMapping("/unbindMch")
    public AjaxResult unbindMch(@RequestParam Long deviceId) {
        DeviceVO device = deviceService.selectById(deviceId);
        if (!deviceValidator.isAgent(device, getUserId())) {
            return error("您不是该设备的代理商，无权操作");
        }
        return toAjax(deviceService.unbind(deviceId));
    }

    @ApiOperation("代理商修改设备")
    @PutMapping
    public AjaxResult update(@RequestBody DeviceBO data) {
        DeviceVO device = deviceService.selectById(data.getDeviceId());
        if (!deviceValidator.isAgent(device, getUserId())) {
            return error("您不是该设备的代理商，无权操作");
        }
        DeviceBO bo = data.filterUpdateByAgent();
        return toAjax(deviceService.updateSmDevice(bo));
    }
}
