package com.zhongshu.card.server.core.controller.devices;

import com.github.microservice.auth.security.annotations.ResourceAuth;
import com.github.microservice.auth.security.type.AuthType;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.base.IDParam;
import com.zhongshu.card.client.model.devices.*;
import com.zhongshu.card.client.service.school.DeviceInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 设备
 *
 * @author TRX
 * @date 2024/6/5
 */
@RestController
@RequestMapping("/devices")
@Tag(name = "平台-设备管理")
public class DeviceInfoController {

    @Autowired
    private DeviceInfoService deviceInfoService;

    //----------------------------设备基础信息 start------------------------

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "根据设备数据ID 设备详情", description = "根据设备数据ID 设备详情")
    @RequestMapping(value = "getDeviceById", method = {RequestMethod.POST})
    public ResultContent<DeviceInfoMoreModel> getDeviceById(@RequestBody IDParam param) {
        return this.deviceInfoService.getDeviceDetail(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "根据设备ID 设备详情（精确匹配）", description = "根据设备ID 设备详情（精确匹配）")
    @RequestMapping(value = "getDeviceByDeviceId", method = {RequestMethod.GET})
    public ResultContent<DeviceInfoMoreModel> getDeviceByDeviceId(@RequestParam(name = "deviceId") String deviceId) {
        return this.deviceInfoService.getDeviceDetailByDeviceId(deviceId);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "根据设备ID 设备列表（模糊匹配）", description = "根据设备ID 设备列表（模糊匹配）")
    @RequestMapping(value = "getDeviceDetailByDeviceIdLike", method = {RequestMethod.GET})
    public ResultContent<List<DeviceInfoMoreModel>> getDeviceDetailByDeviceIdLike(@RequestParam(name = "deviceId") String deviceId) {
        return this.deviceInfoService.getDeviceDetailByDeviceIdLike(deviceId);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "添加-编辑设备", description = "添加-编辑设备")
    @RequestMapping(value = "addDeviceInfo", method = {RequestMethod.POST})
    public ResultContent addDeviceInfo(@RequestBody DeviceInfoParam param) {
        return this.deviceInfoService.addDeviceInfo(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "删除设备", description = "删除设备")
    @RequestMapping(value = "deleteDeviceInfo", method = {RequestMethod.POST})
    public ResultContent deleteDeviceInfo(@RequestBody IDParam param) {
        return this.deviceInfoService.deleteDeviceInfo(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "设备列表-分页查询", description = "设备列表-分页查询")
    @RequestMapping(value = {"page"}, method = {RequestMethod.POST})
    public ResultContent<Page<DeviceInfoModel>> page(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) DeviceInfoSearch param) {
        Assert.hasText(param.getProjectOid(), "projectOid不能为空");
        return deviceInfoService.page(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "设备列表-分页查询(商户小程序用)", description = "设备列表-分页查询")
    @RequestMapping(value = {"busPage"}, method = {RequestMethod.POST})
    public ResultContent<Page<DeviceInfoModel>> busPage(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) DeviceInfoSearch param) {
        return deviceInfoService.busPage(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "编辑设备信息", description = "编辑设备信息")
    @RequestMapping(value = "updateDevice", method = {RequestMethod.POST})
    public ResultContent updateDevice(@RequestBody @Valid DeviceBindAreaParam param) {
        return this.deviceInfoService.updateDevice(param);
    }

    //----------------------------设备基础信息 end--------------------------

}
