package com.mcwl.memberCenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.memberCenter.domain.Benefit;
import com.mcwl.memberCenter.domain.MemberBenefit;
import com.mcwl.memberCenter.domain.MemberLevel;
import com.mcwl.memberCenter.domain.vo.MemberBenefitVO;
import com.mcwl.memberCenter.enums.MemberBenefitTypeEnum;
import com.mcwl.memberCenter.mapper.MemberBenefitMapper;
import com.mcwl.memberCenter.service.BenefitService;
import com.mcwl.memberCenter.service.MemberBenefitService;
import com.mcwl.memberCenter.service.MemberLevelService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@RequiredArgsConstructor
public class MemberBenefitServiceImpl extends ServiceImpl<MemberBenefitMapper, MemberBenefit> implements MemberBenefitService {

    private final MemberLevelService memberLevelService;

    private final BenefitService benefitService;

    @Override
    public List<MemberBenefitVO> getMemberBenefitList() {
        List<MemberBenefitVO> memberBenefitVOList = initList();

        List<MemberLevel> memberLevelList = memberLevelService.list();
        for (MemberLevel memberLevel : memberLevelList) {
            MemberBenefitVO memberBenefitVO = new MemberBenefitVO();
            memberBenefitVO.setMemberLevelName(memberLevel.getMemberName());
            // 获取会员等级对应的权益
            List<Benefit> benefitList = benefitService.getBenefitListByMemberLevelId(memberLevel.getId());

            // 获取会员等级没有的权益
            List<Benefit> notBenefitList = benefitService.getNotBenefitList(memberLevel.getId());

            benefitList.addAll(notBenefitList);

            // 排序
            benefitList.sort(Comparator.comparingInt(Benefit::getBenefitOrder));

            memberBenefitVO.setMemberBenefitList(benefitList);
            memberBenefitVOList.add(memberBenefitVO);
        }

        return memberBenefitVOList;
    }

    private List<MemberBenefitVO> initList() {
        List<MemberBenefitVO> memberBenefitVOList = new ArrayList<>();
        MemberBenefitVO memberBenefitVO = new MemberBenefitVO();
        memberBenefitVO.setMemberLevelName("会员权益");
        memberBenefitVO.setMemberBenefitList(benefitService.getBenefitList());
        memberBenefitVOList.add(memberBenefitVO);
        return memberBenefitVOList;
    }
}
