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

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.org.userGroup.*;
import com.zhongshu.card.client.type.DataState;
import com.zhongshu.card.server.core.service.org.UserGroupService;
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("/userGroup")
@Tag(name = "用户管理-用户分组")
public class UserGroupController {

    @Autowired
    private UserGroupService userGroupService;

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

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "添加-编辑数据", description = "添加-编辑数据")
    @RequestMapping(value = "saveInfo", method = {RequestMethod.POST})
    public ResultContent saveInfo(@RequestBody UserGroupParam param) {
        return this.userGroupService.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.userGroupService.deleteInfo(param.getId());
    }

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

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

    //-----------------------------分组关联的用户信息 start--------------------

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "用户分组绑定用户", description = "用户分组绑定用户")
    @RequestMapping(value = "groupBindUser", method = {RequestMethod.POST})
    public ResultContent groupBindUser(@RequestBody UserGroupToUserAddParam param) {
        return this.userGroupService.groupBindUser(param);
    }

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

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "删除分组下的用户（多个）", description = "删除分组下的用户（多个）")
    @RequestMapping(value = "deleteGroupUsersInfo", method = {RequestMethod.POST})
    public ResultContent deleteGroupUsersInfo(@RequestBody IDsParam param) {
        return this.userGroupService.deleteGroupUsersInfo(param);
    }

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

    //-----------------------------分组关联的用户信息 end----------------------

}
