package cn.iocoder.yudao.module.member.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.entity.MemberLevel;
import cn.iocoder.yudao.module.member.service.MemberLevelService;
import cn.iocoder.yudao.module.member.vo.MemberLevelPageReqVO;
import cn.iocoder.yudao.module.member.vo.MemberLevelRespVO;
import cn.iocoder.yudao.module.member.vo.MemberLevelSaveReqVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


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

    @Resource
    private MemberLevelService levelService;

    /**
     * 创建会员等级
     *
     * @param createReqVO MemberLevelSaveReqVO实体
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.String>
     * @author PQZ
     * @date 13:37 2024/9/19
     **/
    @PostMapping("/create")
    @Operation(summary = "创建会员等级")
    @PreAuthorize("@ss.hasPermission('member:level:create')")
    public CommonResult<Boolean> createLevel(@Valid @RequestBody MemberLevelSaveReqVO createReqVO) {
        levelService.saveLevel(createReqVO);
        return success(true);
    }

    /**
     * 更新会员等级
     *
     * @param updateReqVO MemberLevelSaveReqVO实体
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.Boolean>
     * @author PQZ
     * @date 13:37 2024/9/19
     **/
    @PutMapping("/update")
    @Operation(summary = "更新会员等级")
    @PreAuthorize("@ss.hasPermission('member:level:update')")
    public CommonResult<Boolean> updateLevel(@Valid @RequestBody MemberLevelSaveReqVO updateReqVO) {
        levelService.saveLevel(updateReqVO);
        return success(true);
    }

    /**
     * 删除会员等级
     *
     * @param id 会员等级id
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<java.lang.Boolean>
     * @author PQZ
     * @date 13:38 2024/9/19
     **/
    @DeleteMapping("/delete")
    @Operation(summary = "删除会员等级")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:level:delete')")
    public CommonResult<Boolean> deleteLevel(@RequestParam("id") String id) {
        levelService.deleteLevel(id);
        return success(true);
    }

    /**
     * 获取会员等级
     *
     * @param id 会员等级id
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<cn.iocoder.yudao.module.member.vo.MemberLevelRespVO>
     * @author PQZ
     * @date 13:38 2024/9/19
     **/
    @GetMapping("/get")
    @Operation(summary = "获得会员等级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:level:query')")
    public CommonResult<MemberLevelRespVO> getLevel(@RequestParam("id") String id) {
        MemberLevel level = levelService.getLevel(id);
        return success(BeanUtils.toBean(level, MemberLevelRespVO.class));
    }

    /**
     * 分页获取会员等级
     *
     * @param pageReqVO MemberLevelPageReqVO实体
     * @return cn.iocoder.yudao.framework.common.pojo.CommonResult<cn.iocoder.yudao.framework.common.pojo.PageResult < cn.iocoder.yudao.module.member.vo.MemberLevelRespVO>>
     * @author PQZ
     * @date 13:38 2024/9/19
     **/
    @GetMapping("/page")
    @Operation(summary = "获得会员等级分页")
    @PreAuthorize("@ss.hasPermission('member:level:query')")
    public CommonResult<IPage<?>> getLevelPage(MemberLevelPageReqVO pageReqVO,
                                               @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<MemberLevelRespVO> page = new Page<>(pageNo, pageSize);
        return success(levelService.queryListPage(pageReqVO,page));
    }
    /**
     * 获取全部会员
     *
     **/
    @GetMapping("/all")
    @Operation(summary = "获得全部会员")
    @PreAuthorize("@ss.hasPermission('member:level:query')")
    public CommonResult<List<MemberLevel>> getLevelAll(MemberLevelPageReqVO pageReqVO) {
        return success(levelService.queryListAll(pageReqVO));
    }

}