package cn.iocoder.yudao.module.member.service.impl;

import cn.iocoder.yudao.module.member.entity.ActiveRuleCoupon;
import cn.iocoder.yudao.module.member.entity.MemberCoupon;
import cn.iocoder.yudao.module.member.mapper.ActiveRuleCouponMapper;
import cn.iocoder.yudao.module.member.service.ActiveRuleCouponService;
import cn.iocoder.yudao.module.member.service.MemberCouponService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

/**
 * 营销活动、规则及会员等级与基础卡券关联关系 Service 实现类
 *
 * @author pqz
 */
@Service
@Validated
public class ActiveRuleCouponServiceImpl extends ServiceImpl<ActiveRuleCouponMapper, ActiveRuleCoupon> implements ActiveRuleCouponService {

    @Resource
    private MemberCouponService couponService;


    /**
     * 根据主表id删除关联关系
     *
     * @param id 主表id
     * @author PQZ
     * @date 17:56 2024/9/19
     **/
    @Override
    public void deleteByMainId(String id) {
        LambdaQueryWrapper<ActiveRuleCoupon> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ActiveRuleCoupon::getMainId, id);
        remove(lambdaQueryWrapper);
    }

    /**
     * 保存关联关系
     *
     * @param list      卡券id集合
     * @param mainId    主表id
     * @param mainTable 主表名称
     * @author PQZ
     * @date 17:57 2024/9/19
     **/
    @Override
    public void saveActiveRuleCoupon(List<MemberCoupon> list, String mainId, String mainTable) {
        //删除原有关联关系
        deleteByMainId(mainId);
        List<ActiveRuleCoupon> couponList = new ArrayList<>();
        //组装现有关联关系
        list.forEach(item -> {
            ActiveRuleCoupon couponItem = new ActiveRuleCoupon();
            couponItem.setCouponId(item.getId());
            couponItem.setMainTable(mainTable);
            couponItem.setMainId(mainId);
            couponList.add(couponItem);
        });
        saveBatch(couponList);
    }

    /**
     * 根据主表id查询关联关系
     *
     * @param id      主表id
     * @param isChild 是否查询出关联的附属卡券信息
     * @return java.util.List<cn.iocoder.yudao.module.member.entity.ActiveRuleCoupon>
     * @author PQZ
     * @date 17:59 2024/9/19
     **/
    @Override
    public List<MemberCoupon> queryByMainId(String id, boolean isChild) {
        //查询关联关系
        LambdaQueryWrapper<ActiveRuleCoupon> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ActiveRuleCoupon::getMainId, id).eq(ActiveRuleCoupon::getDeleted, "0");
        List<ActiveRuleCoupon> list = list(lambdaQueryWrapper);
        List<String> idList = list.stream().map(ActiveRuleCoupon::getCouponId).collect(Collectors.toList());
        //查询关联卡券信息
        List<MemberCoupon> result = new ArrayList<>();
        if (!idList.isEmpty()) {
            result = couponService.listByIds(idList,isChild);
        }
        return result;
    }
}