package com.jeesharp.business.member.controller;

import com.jeesharp.business.modules.member.application.MemberApplicationService;
import com.jeesharp.business.modules.member.vo.MemberFeatureVO;
import com.jeesharp.framework.memberauth.utils.MemberAuthUtils;
import com.jeesharp.framework.memberauth.web.BaseMobileController;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.utils.base.ExceptionUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 移动端会员功能权限接口
 * <p>
 * 获取当前会员可用功能列表，供前端控制展示；校验功能权限。
 * </p>
 *
 * @author jeesharp
 * @version 2024-01-24
 */
@Slf4j
@RestController
@RequestMapping("${jeesharp.web.mobilePath}/member/feature")
@Tag(name = "移动端-会员功能权限", description = "会员等级功能权限相关接口")
@AllArgsConstructor
public class MemberFeatureMobileController extends BaseMobileController {

    private final MemberApplicationService memberApplicationService;

    @Operation(summary = "获取当前会员可用功能列表")
    @GetMapping("/myFeatures")
    public AjaxResult<List<MemberFeatureVO>> myFeatures() {
        String memberId = MemberAuthUtils.getLoginId();
        if (memberId == null) {
            ExceptionUtil.error("请先登录");
        }
        List<MemberFeatureVO> features = memberApplicationService.getMemberFeatures(memberId);
        return AjaxResult.okData(features);
    }

    @Operation(summary = "校验当前会员是否拥有某功能")
    @GetMapping("/check")
    public AjaxResult<Map<String, Object>> check(
            @Parameter(description = "功能权限码", required = true)
            @RequestParam("code") String code) {
        String memberId = MemberAuthUtils.getLoginId();
        if (memberId == null) {
            ExceptionUtil.error("请先登录");
        }
        boolean has = memberApplicationService.hasFeature(memberId, code);
        Map<String, Object> data = new HashMap<>();
        data.put("hasFeature", has);
        data.put("featureCode", code);
        return AjaxResult.okData(data);
    }
}
