package com.mall.member.controller;

import com.mall.member.domain.dto.MemberDto;
import com.mall.member.domain.entity.Member;
import com.mall.member.domain.vo.MemberVo;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import com.mall.member.service.MemberService;
import com.mall.common.domain.vo.result.PageResult;
import com.mall.common.domain.vo.result.Result;
import com.mall.common.domain.vo.result.ResultCodeEnum;

import java.util.List;

/**
 * <p>
 * 会员 前端控制器
 * </p>
 *
 * @author Bunny
 * @since 2025-07-05 19:30:06
 */
@Tag(name = "会员", description = "会员相关接口")
@RestController
@RequestMapping("/api/member/member")
@RequiredArgsConstructor
public class MemberController {

    private final MemberService memberService;

    @Operation(summary = "分页查询会员", description = "分页会员")
    @GetMapping("{page}/{limit}")
    public Result<PageResult<MemberVo>> getMemberPage(
            @Parameter(name = "page", description = "当前页", required = true)
            @PathVariable("page") Integer page,
            @Parameter(name = "limit", description = "每页记录数", required = true)
            @PathVariable("limit") Integer limit,
            MemberDto dto) {
        Page<Member> pageParams = new Page<>(page, limit);
        PageResult<MemberVo> pageResult = memberService.getMemberPage(pageParams, dto);
        return Result.success(pageResult);
    }

    @Operation(summary = "添加会员", description = "添加会员")
    @PostMapping()
    public Result<String> addMember(@Valid @RequestBody MemberDto dto) {
            memberService.addMember(dto);
        return Result.success(ResultCodeEnum.ADD_SUCCESS);
    }

    @Operation(summary = "更新会员", description = "更新会员")
    @PutMapping()
    public Result<String> updateMember(@Valid @RequestBody MemberDto dto) {
            memberService.updateMember(dto);
        return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
    }

    @Operation(summary = "删除会员", description = "删除会员")
    @DeleteMapping()
    public Result<String> deleteMember(@RequestBody List<Long> ids) {
            memberService.deleteMember(ids);
        return Result.success(ResultCodeEnum.DELETE_SUCCESS);
    }
}