package com.jeesharp.business.modules.member.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jeesharp.business.modules.member.application.MemberApplicationService;
import com.jeesharp.business.modules.member.entity.MemberLevelFeature;
import com.jeesharp.business.modules.member.vo.MemberLevelFeatureQueryVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员等级功能权限配置 Controller（后台管理）
 *
 * @author jeesharp
 * @version 2024-01-24
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/member/levelFeature/v1")
@Tag(name = "会员等级功能配置", description = "等级-功能权限映射配置，运营可配置各等级拥有的功能")
@AllArgsConstructor
public class MemberLevelFeatureController extends BaseController {

    private final MemberApplicationService memberApplicationService;

    @ApiOperationSupport(order = 1)
    @Operation(summary = "1、等级功能配置列表", description = "分页查询等级-功能配置")
    @PreAuthorize("@ss.hasPermission('member:levelFeature:list')")
    @PostMapping(value = "list")
    public AjaxResult<Page<MemberLevelFeature>> list(@RequestBody MemberLevelFeatureQueryVO query) {
        Page<MemberLevelFeature> page = new Page<>(query);
        return AjaxResult.okData(memberApplicationService.findLevelFeaturePage(page, query));
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "2、保存等级功能配置", description = "新增或更新等级-功能配置")
    @PreAuthorize("@ss.hasPermission('member:levelFeature:save')")
    @PostMapping(value = "save")
    public AjaxResult<Void> save(@RequestBody @Valid MemberLevelFeature entity) {
        memberApplicationService.saveLevelFeature(entity);
        return AjaxResult.ok("保存成功");
    }

    @ApiOperationSupport(order = 3)
    @Operation(summary = "3、删除等级功能配置", description = "批量删除，多个ID用逗号分隔")
    @PreAuthorize("@ss.hasPermission('member:levelFeature:delete')")
    @GetMapping(value = "delete")
    public AjaxResult<Void> delete(
            @Parameter(description = "ID，多个用逗号分隔", required = true)
            @RequestParam("ids") String ids) {
        memberApplicationService.deleteLevelFeature(ids);
        return AjaxResult.ok("删除成功");
    }

    @ApiOperationSupport(order = 4)
    @Operation(summary = "4、功能码下拉选项", description = "获取已配置的功能码列表，供下拉选择")
    @PreAuthorize("@ss.hasPermission('member:levelFeature:list')")
    @GetMapping(value = "options")
    public AjaxResult<List<MemberLevelFeature>> options() {
        List<MemberLevelFeature> list = memberApplicationService.getLevelFeatureOptions();
        // 去重按 feature_code，返回用于下拉
        List<MemberLevelFeature> distinct = list.stream()
                .collect(Collectors.toMap(MemberLevelFeature::getFeatureCode, f -> f, (a, b) -> a))
                .values().stream()
                .collect(Collectors.toList());
        return AjaxResult.okData(distinct);
    }
}
