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

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.permiss.DevicePermissModel;
import com.zhongshu.card.client.model.devices.permiss.DevicePermissParam;
import com.zhongshu.card.client.model.devices.permiss.DevicePermissSearch;
import com.zhongshu.card.client.type.DataState;
import com.zhongshu.card.server.core.service.devices.permiss.DevicePermissService;
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("/devicePermiss")
@Tag(name = "项目-用户设备权限")
public class DevicePermissController {

    @Autowired
    private DevicePermissService devicePermissService;

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "设备绑定用户", description = "设备绑定用户")
    @RequestMapping(value = "saveBindInfo", method = {RequestMethod.POST})
    public ResultContent saveBindInfo(@RequestBody @Valid DevicePermissParam param) {
        return this.devicePermissService.saveBindInfo(param);
    }

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

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "查询详情", description = "查询详情")
    @RequestMapping(value = "getDetail", method = {RequestMethod.POST})
    public ResultContent<DevicePermissModel> getDetail(
            @RequestBody IDParam param) {
        return this.devicePermissService.getDetail(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "启用设备权限", description = "启用设备权限")
    @RequestMapping(value = "enablePermiss", method = {RequestMethod.POST})
    public ResultContent enablePermiss(@RequestBody IDParam param) {
        return this.devicePermissService.changeState(param.getId(), DataState.Enable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "禁用设备权限", description = "禁用设备权限")
    @RequestMapping(value = "disablePermiss", method = {RequestMethod.POST})
    public ResultContent disablePermiss(@RequestBody IDParam param) {
        return this.devicePermissService.changeState(param.getId(), DataState.Disable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "删除设备权限", description = "删除设备权限")
    @RequestMapping(value = "deleteInfo", method = {RequestMethod.POST})
    public ResultContent deleteInfo(@RequestBody IDParam param) {
        IDsParam iDsParam = new IDsParam();
        iDsParam.setIds(List.of(param.getId()));
        return this.devicePermissService.deleteByIds(iDsParam);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "删除设备权限(多个)", description = "删除设备权限（多个）")
    @RequestMapping(value = "deleteByIds", method = {RequestMethod.POST})
    public ResultContent deleteByIds(@RequestBody IDsParam param) {
        return this.devicePermissService.deleteByIds(param);
    }

}
