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.dto.MemberBenefitDto;
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.*;
import java.util.stream.Collectors;

@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 = new ArrayList<>();

        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::getCreateTime));

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

        return memberBenefitVOList;
    }

    @Override
    public MemberBenefitVO getMemberBenefit(Long memberLevelId) {

        // 查询会员权益关联表
        List<MemberBenefit> memberBenefitList = baseMapper.selectList(new LambdaQueryWrapper<MemberBenefit>()
                .eq(MemberBenefit::getMemberLevelId, memberLevelId));

        // 封装数据
        MemberBenefitVO memberBenefitVO = new MemberBenefitVO();
        memberBenefitVO.setMemberLevelName(memberLevelService.getById(memberLevelId).getMemberName());

        // 查询权益
        List<Benefit> benefitList = benefitService.listByIds(memberBenefitList
                .stream()
                .map(MemberBenefit::getBenefitId)
                .collect(Collectors.toList()));

        memberBenefitVO.setMemberBenefitList(benefitList);

        return memberBenefitVO;
    }

    @Override
    public void addMemberBenefit(MemberBenefitDto memberBenefitDto) {
        Long memberLevelId = memberBenefitDto.getMemberLevelId();
        List<Long> benefitIds = memberBenefitDto.getBenefitIds();
        baseMapper.deleteByMemberLevelId(memberLevelId);

        for (Long benefitId : benefitIds) {
            MemberBenefit memberBenefit = new MemberBenefit();
            memberBenefit.setMemberLevelId(memberLevelId);
            memberBenefit.setBenefitId(benefitId);
            baseMapper.insert(memberBenefit);
        }

    }

    @Override
    public void deleteByMemberLevelId(Long memberLevelId) {
        baseMapper.deleteByMemberLevelId(memberLevelId);
    }

}
