package com.backendsys.controller.B2c;

import com.backendsys.aspect.QueryNullCheck;
import com.backendsys.aspect.QueryNullCheckAspect;
import com.backendsys.entity.B2c.B2cMemberDTO;
import com.backendsys.entity.PageDTO;
import com.backendsys.service.B2c.B2cMemberService;
import com.backendsys.utils.response.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 会员
 */
@Validated
@RestController
public class B2cMemberController {

    @Autowired
    private B2cMemberService b2cMemberService;

    @PreAuthorize("@sr.hasPermission('3.3')")
    @GetMapping("/api/b2c/member/getMember")
    public Result getMember(@Validated PageDTO pageDTO, @Validated B2cMemberDTO b2cMemberDTO) {
        return Result.success(b2cMemberService.queryMemberList(pageDTO.getPage_num(), pageDTO.getPage_size(), b2cMemberDTO));
    }

    @PreAuthorize("@sr.hasPermission('3.3.1')")
    @QueryNullCheck(serviceClass = B2cMemberService.class, serviceMethod = "queryMemberDetail", argField = "member_id", message = "会员不存在")
    @GetMapping("/api/b2c/member/getMemberDetail")
    public Result getMemberDetail(@Validated(B2cMemberDTO.Detail.class) B2cMemberDTO b2cMemberDTO) {
        return Result.success(QueryNullCheckAspect.getQueryResult());
    }

    @PreAuthorize("@sr.hasPermission('3.3.2')")
    @PostMapping("/api/b2c/createMember")
    public Result createMember(@Validated(B2cMemberDTO.Create.class) @RequestBody B2cMemberDTO b2cMemberDTO) {
        return Result.success(b2cMemberService.insertMember(b2cMemberDTO), "创建成功");
    }

    @PreAuthorize("@sr.hasPermission('3.3.3')")
    @QueryNullCheck(serviceClass = B2cMemberService.class, serviceMethod = "queryMemberDetail", argField = "member_id", message = "会员不存在")
    @PutMapping("/api/b2c/member/updateMember")
    public Result updateMember(@Validated(B2cMemberDTO.Update.class) @RequestBody B2cMemberDTO b2cMemberDTO) {
        return Result.success(b2cMemberService.updateMember(b2cMemberDTO), "更新成功");
    }

    @PreAuthorize("@sr.hasPermission('3.3.4')")
    @QueryNullCheck(serviceClass = B2cMemberService.class, serviceMethod = "queryMemberDetail", argField = "member_id", message = "会员不存在")
    @DeleteMapping("/api/b2c/member/deleteMember")
    public Result deleteMember(@Validated(B2cMemberDTO.Delete.class) @RequestBody B2cMemberDTO b2cMemberDTO) {
        return Result.success(b2cMemberService.deleteMember(b2cMemberDTO), "删除成功");
    }

    @PreAuthorize("@sr.hasPermission('3.3.6')")
    @GetMapping("/api/b2c/member/getMemberLogined")
    public Result getMemberLogined(@Validated PageDTO pageDTO, @Validated B2cMemberDTO b2cMemberDTO) {
        return Result.success(b2cMemberService.queryMemberWithLogined(pageDTO.getPage_num(), pageDTO.getPage_size(), b2cMemberDTO));
    }

    @PostMapping("/api/b2c/member/auth/login")
    public Result memberLogin(HttpServletRequest request, @Validated(B2cMemberDTO.Login.class) @RequestBody B2cMemberDTO b2cMemberDTO) {
        return b2cMemberService.login(request, b2cMemberDTO);
    }


}
