package com.seer.teach.iot.admin.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.seer.teach.iot.admin.resp.*;
import com.seer.teach.iot.admin.service.AdminDevicesService;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.LogPrint;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 *
 * </p >
 *
 * @author Captain
 * @since 2025-08-13 10:29
 */
@RequestMapping("/iot/device")
@RestController
@Tag(name = "设备数据")
@Validated
@RequiredArgsConstructor
@SaCheckLogin
public class DeviceController {

    private final AdminDevicesService adminDevicesService;

    @GetMapping("/info")
    @LogPrint
    @Operation(summary = "在线设备")
    public ResultBean<DeviceDashboardResp> info() {
        DeviceDashboardResp result = adminDevicesService.info();
        return ResultBean.success(result);
    }

    @GetMapping("/modelSalesRatio")
    @LogPrint
    @Operation(summary = "产品型号销售占比")
    public ResultBean<List<DeviceModelSaleResp>> modelSalesRatio() {
        List<DeviceModelSaleResp> result = adminDevicesService.modelSalesRatio();
        return ResultBean.success(result);
    }

    @GetMapping("/deviceDistribution/{provinceId}")
    @LogPrint
    @Operation(summary = "设备分布")
    public ResultBean<List<DeviceDistributionResp>> deviceDistribution(@PathVariable("provinceId") @NotNull(message = "省份ID不可为空") Integer provinceId) {
        List<DeviceDistributionResp> result = adminDevicesService.deviceDistribution(provinceId);
        return ResultBean.success(result);
    }

    @GetMapping("/list/{page}/{size}")
    @LogPrint
    @Operation(summary = "设备列表分页查询")
    public ResultBean<PageListBean<DeviceListResp>> list(@Parameter(description = "页码") @PathVariable("page") Integer page,
                                                         @Parameter(description = "每页数量") @PathVariable("size") Integer size,
                                                         @Parameter(description = "学生账号") @RequestParam(required = false) String studentAccount) {
        PageListBean<DeviceListResp> result = adminDevicesService.listPage(page, size, studentAccount);
        return ResultBean.success(result);
    }

    @GetMapping("/{id}")
    @LogPrint
    @Operation(summary = "获取设备详情")
    public ResultBean<DeviceDetailResp> getDeviceDetailById(@PathVariable("id") @NotNull(message = "设备ID不可为空") Integer id) {
        DeviceDetailResp result = adminDevicesService.getDeviceDetailById(id);
        return ResultBean.success(result);
    }


}
