package com.ruoyi.base.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.base.domain.BaseCard;
import com.ruoyi.base.domain.BaseRights;
import com.ruoyi.base.mapper.BaseRightsMapper;
import com.ruoyi.base.service.IBaseCardService;
import com.ruoyi.base.service.IBaseRightsService;
import com.ruoyi.base.vo.CardRightsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 会员权益Service业务层处理
 * 
 * @author vinjor-m
 * @date 2025-03-17
 */
@Service
public class BaseRightsServiceImpl extends ServiceImpl<BaseRightsMapper,BaseRights>  implements IBaseRightsService
{
    @Autowired
    private BaseRightsMapper baseRightsMapper;
    @Resource
    private IBaseCardService cardService;

    @Override
    public IPage<BaseRights> queryListPage(BaseRights pageReqVO, Page<BaseRights> page) {
        return baseRightsMapper.queryListPage(pageReqVO, page);
    }

    /**
     * 查询会员权益
     *
     * @param userType 用户类型
     * @return java.util.List<com.ruoyi.base.vo.CardRightsVO>
     * @author PQZ
     * @date 11:33 2025/4/7
     **/
    @Override
    public JSONObject rightList(String userType) {
        JSONObject resultObj = new JSONObject();
        //根据用户类型查询会员卡
        List<BaseCard> cards = cardService.listByUserType(userType);
        JSONArray cardList = new JSONArray();
        for (BaseCard card : cards) {
            JSONObject cardObj = new JSONObject();
            cardObj.put("id",card.getId());
            cardObj.put("cardName",card.getCardName());
            cardList.add(cardObj);
        }
        resultObj.put("cardList",cardList);
        //查询权益
        List<CardRightsVO> list = baseRightsMapper.rightList(userType);
        // 按照 id 分组
        Map<String, List<CardRightsVO>> groupedById = list.stream()
                .collect(Collectors.groupingBy(CardRightsVO::getId));
        // 按照 id 去重
        List<CardRightsVO> uniqueList = new ArrayList<>(list.stream()
                .distinct()
                .collect(Collectors.toMap(
                        CardRightsVO::getId,
                        cardRights -> cardRights,
                        (existing, replacement) -> existing
                ))
                .values());
        JSONArray array = new JSONArray();
        for (CardRightsVO item : uniqueList) {
            JSONObject itemObj = new JSONObject();
            JSONObject cardRights = new JSONObject();
            itemObj.put("name",item.getName());
            itemObj.put("rightsType",item.getRightsType());
            List<CardRightsVO> rightsList = groupedById.get(item.getId());
            for (CardRightsVO rightsItem : rightsList) {
                cardRights.put(rightsItem.getCardId(),rightsItem.getRightsValue());
            }
            itemObj.put("cardRights",cardRights);
            array.add(itemObj);
        }
        resultObj.put("rightsList",array);
        return resultObj;
    }
}
