package com.xindazhou.user.controller.admin.level;

import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.user.controller.admin.level.vo.level.*;
import com.xindazhou.user.convert.level.MemberLevelConvert;
import com.xindazhou.user.dal.dataobject.level.MemberLevelDO;
import com.xindazhou.user.service.level.MemberLevelService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

import static com.xindazhou.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 会员等级")
@RestController
@RequestMapping("/admin/member/level")
@Validated
public class MemberLevelController {

    @Resource
    private MemberLevelService levelService;

    @PostMapping("/create")
    @Operation(summary = "创建会员等级")
    public CommonResult<Long> createLevel(@Valid @RequestBody MemberLevelCreateReqVO createReqVO) {
        return success(levelService.createLevel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员等级")
    public CommonResult<Boolean> updateLevel(@Valid @RequestBody MemberLevelUpdateReqVO updateReqVO) {
        levelService.updateLevel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员等级")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteLevel(@RequestParam("id") Long id) {
        levelService.deleteLevel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员等级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<MemberLevelRespVO> getLevel(@RequestParam("id") Long id) {
        MemberLevelDO level = levelService.getLevel(id);
        return success(MemberLevelConvert.INSTANCE.convert(level));
    }

    @GetMapping("/listAllSimple")
    @Operation(summary = "获取会员等级精简信息列表", description = "只包含被开启的会员等级，主要用于前端的下拉选项")
    public CommonResult<List<MemberLevelSimpleRespVO>> getSimpleLevelList() {
        // 获用户列表，只要开启状态的
        List<MemberLevelDO> list = levelService.getEnableLevelList();
        // 排序后，返回给前端
        return success(MemberLevelConvert.INSTANCE.convertSimpleList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员等级分页")
    public CommonResult<com.xindazhou.framework.common.pojo.PageResult<MemberLevelRespVO>> getLevelPage(@Valid MemberLevelListReqVO listReqVO) {
        com.xindazhou.framework.common.pojo.PageResult<MemberLevelDO> pageResult = levelService.getLevelPage(listReqVO);
        return success(MemberLevelConvert.INSTANCE.convertPage(pageResult));
    }

    @PutMapping("/setDefault")
    @Operation(summary = "设置默认会员等级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<Boolean> setDefaultLevel(@RequestParam("id") Long id) {
        levelService.setDefaultLevel(id);
        return success(true);
    }

}
