package com.xindazhou.business.controller.admin.member;

import com.xindazhou.business.api.member.MemberGroupApi;
import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 管理后台 - 会员分组（代理 Controller，转发到用户服务）
 *
 * @author xindazhou
 */
@Tag(name = "管理后台 - 会员分组")
@RestController
@RequestMapping("/member/group")
@Validated
public class MemberGroupController {

    @Resource
    private MemberGroupApi memberGroupApi;

    @GetMapping("/page")
    @Operation(summary = "获得用户分组分页")
    @PreAuthorize("@ss.hasPermission('member:group:query')")
    public CommonResult<PageResult<Map<String, Object>>> getGroupPage(@Valid @ModelAttribute Map<String, Object> pageVO) {
        return memberGroupApi.getGroupPage(pageVO);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户分组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:group:query')")
    public CommonResult<Map<String, Object>> getGroup(@RequestParam("id") Long id) {
        return memberGroupApi.getGroup(id);
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获取会员分组精简信息列表", description = "只包含被开启的会员分组，主要用于前端的下拉选项")
    @PreAuthorize("@ss.hasPermission('member:group:query')")
    public CommonResult<List<Map<String, Object>>> getSimpleGroupList() {
        return memberGroupApi.getSimpleGroupList();
    }

    @PostMapping("/create")
    @Operation(summary = "创建用户分组")
    @PreAuthorize("@ss.hasPermission('member:group:create')")
    public CommonResult<Long> createGroup(@Valid @RequestBody Map<String, Object> createReqVO) {
        return memberGroupApi.createGroup(createReqVO);
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户分组")
    @PreAuthorize("@ss.hasPermission('member:group:update')")
    public CommonResult<Boolean> updateGroup(@Valid @RequestBody Map<String, Object> updateReqVO) {
        return memberGroupApi.updateGroup(updateReqVO);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户分组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:group:delete')")
    public CommonResult<Boolean> deleteGroup(@RequestParam("id") Long id) {
        return memberGroupApi.deleteGroup(id);
    }
}

