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.base.IDsParam;
import com.zhongshu.card.client.model.devices.deviceGroup.*;
import com.zhongshu.card.client.type.DataState;
import com.zhongshu.card.server.core.service.devices.DeviceGroupService;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 设备分组
 *
 * @author TRX
 * @date 2024/6/5
 */
@RestController
@RequestMapping("/deviceGroup")
@Tag(name = "设备管理-设备分组")
public class DeviceGroupController {

    @Autowired
    private DeviceGroupService deviceGroupService;

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "数据详情", description = "数据详情")
    @RequestMapping(value = "getDetailInfo", method = {RequestMethod.POST})
    public ResultContent<DeviceGroupModel> getDetailInfo(@RequestBody IDParam param) {
        return this.deviceGroupService.getDetailInfo(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "添加-编辑数据", description = "添加-编辑数据")
    @RequestMapping(value = "saveInfo", method = {RequestMethod.POST})
    public ResultContent saveInfo(@RequestBody DeviceGroupParam param) {
        return this.deviceGroupService.saveInfo(param);
    }

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

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

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "数据列表-分页查询（供选择使用）", description = "数据列表-分页查询")
    @RequestMapping(value = {"pageForSelect"}, method = {RequestMethod.POST})
    public ResultContent<Page<DeviceGroupModel>> pageForSelect(
            @Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable,
            @Parameter(required = false) DeviceGroupSearch param) {
        Assert.hasText(param.getProjectOid(), "projectOid不能为空");
        return deviceGroupService.page(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "启用数据", description = "启用数据")
    @RequestMapping(value = "enableData", method = {RequestMethod.POST})
    public ResultContent enableData(@RequestBody IDParam param) {
        return this.deviceGroupService.changeState(param.getId(), DataState.Enable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "禁用数据", description = "禁用数据")
    @RequestMapping(value = "disableData", method = {RequestMethod.POST})
    public ResultContent disableData(@RequestBody IDParam param) {
        return this.deviceGroupService.changeState(param.getId(), DataState.Disable);
    }

    //-----------------------------分组关联的设备信息 start--------------------

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "设备分组绑定设备", description = "设备分组绑定设备")
    @RequestMapping(value = "groupBindDevices", method = {RequestMethod.POST})
    public ResultContent groupBindDevices(@RequestBody DeviceGroupToDeviceAddParam param) {
        return this.deviceGroupService.groupBindDevices(param);
    }

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

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "删除分组下的设备(多个)", description = "删除分组下的设备(多个)")
    @RequestMapping(value = "deleteGroupDevicesInfo", method = {RequestMethod.POST})
    public ResultContent deleteGroupDevicesInfo(@RequestBody IDsParam param) {
        return this.deviceGroupService.deleteGroupDevicesInfo(param);
    }

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

    //-----------------------------分组关联的设备信息 end----------------------

}
