package com.mcwl.memberCenter.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.BenefitName;
import com.mcwl.memberCenter.domain.dto.AddBenefitDto;
import com.mcwl.memberCenter.domain.vo.BenefitInfoVo;
import com.mcwl.memberCenter.domain.vo.BenefitVo;
import com.mcwl.memberCenter.enums.MemberBenefitTypeEnum;
import com.mcwl.memberCenter.mapper.BenefitMapper;
import com.mcwl.memberCenter.service.BenefitNameService;
import com.mcwl.memberCenter.service.BenefitService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class BenefitServiceImpl extends ServiceImpl<BenefitMapper, Benefit> implements BenefitService {

    private final BenefitNameService benefitNameService;

    @Override
    public List<Benefit> getBenefitList() {
        return baseMapper.getBenefitList();
    }

    @Override
    public List<Benefit> getBenefitListByMemberLevelId(Long memberLevelId) {
        return baseMapper.getBenefitListByMemberLevelId(memberLevelId);
    }

    @Override
    public List<Benefit> getNotBenefitList(Long memberLevelId) {
        return baseMapper.getNotBenefitList(memberLevelId);
    }

    @Override
    public Benefit getBenefitByType(Long memberLevelId, MemberBenefitTypeEnum memberBenefitTypeEnum) {
        return baseMapper.getBenefitByType(memberLevelId, memberBenefitTypeEnum.getValue());
    }

    @Override
    public void addBenefit(AddBenefitDto addBenefitDto) {

        Benefit benefit = BeanUtil.toBean(addBenefitDto, Benefit.class);

        baseMapper.insert(benefit);

    }

    @Override
    public List<BenefitVo> benefitList() {

        List<BenefitVo> benefitVoList = new ArrayList<>();

        List<BenefitName> benefitNameList = benefitNameService.list();

        for (BenefitName benefitName : benefitNameList) {

            BenefitVo benefitVo = BeanUtil.toBean(benefitName, BenefitVo.class);

            List<BenefitInfoVo> benefitInfoVoList = new ArrayList<>();
            List<Benefit> benefitList = baseMapper.selectList(new LambdaQueryWrapper<Benefit>()
                    .eq(Benefit::getBenefitNameId, benefitName.getId()));
            for (Benefit benefit : benefitList) {
                BenefitInfoVo benefitInfoVo1 = BeanUtil.copyProperties(benefit, BenefitInfoVo.class);
                benefitInfoVo1.setBenefitType(benefit.getBenefitType().getValue());
                benefitInfoVoList.add(benefitInfoVo1);
            }
            benefitVo.setBenefitInfoList(benefitInfoVoList);

            benefitVoList.add(benefitVo);
        }
        return benefitVoList;
    }
}
