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

import com.xindazhou.business.api.member.MemberLevelApi;
import com.xindazhou.framework.common.pojo.CommonResult;
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/level")
@Validated
public class MemberLevelController {

    @Resource
    private MemberLevelApi memberLevelApi;

    @GetMapping("/list")
    @Operation(summary = "获得会员等级列表")
    @PreAuthorize("@ss.hasPermission('member:level:query')")
    public CommonResult<List<Map<String, Object>>> getLevelList(@Valid @ModelAttribute Map<String, Object> listReqVO) {
        return memberLevelApi.getLevelList(listReqVO);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得会员等级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:level:query')")
    public CommonResult<Map<String, Object>> getLevel(@RequestParam("id") Long id) {
        return memberLevelApi.getLevel(id);
    }

    @PostMapping("/create")
    @Operation(summary = "创建会员等级")
    @PreAuthorize("@ss.hasPermission('member:level:create')")
    public CommonResult<Long> createLevel(@Valid @RequestBody Map<String, Object> createReqVO) {
        return memberLevelApi.createLevel(createReqVO);
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员等级")
    @PreAuthorize("@ss.hasPermission('member:level:update')")
    public CommonResult<Boolean> updateLevel(@Valid @RequestBody Map<String, Object> updateReqVO) {
        return memberLevelApi.updateLevel(updateReqVO);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员等级")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:level:delete')")
    public CommonResult<Boolean> deleteLevel(@RequestParam("id") Long id) {
        return memberLevelApi.deleteLevel(id);
    }
}

