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

import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.member.entity.ActiveRule;
import cn.iocoder.yudao.module.member.mapper.ActiveRuleMapper;
import cn.iocoder.yudao.module.member.service.ActiveRuleService;
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 java.util.List;

/**
 * 营销活动规则 Service 实现类
 *
 * @author pqz
 */
@Service
@Validated
public class ActiveRuleServiceImpl extends ServiceImpl<ActiveRuleMapper, ActiveRule> implements ActiveRuleService {


    /**
     * 保存活动规则
     *
     * @param list     活动规则集合
     * @param activeId 活动id
     * @author PQZ
     * @date 18:15 2024/9/19
     **/
    @Override
    public void saveActiveRule(List<ActiveRule> list, String activeId) {
        //根据活动id删除活动规则
        deleteByActiveId(activeId);
        //保存
        list.forEach(item -> {
            item.setId(null);
            item.setActiveId(activeId);
        });
        saveBatch(list);
    }

    /**
     * 根据活动id删除活动规则
     *
     * @param activeId 活动id
     * @author PQZ
     * @date 18:16 2024/9/19
     **/
    @Override
    public void deleteByActiveId(String activeId) {
        LambdaQueryWrapper<ActiveRule> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ActiveRule::getActiveId,activeId);
        remove(lambdaQueryWrapper);
    }

    /**
     * 根据活动id查询活动规则
     *
     * @param activeId 活动id
     * @return java.util.List<cn.iocoder.yudao.module.member.entity.ActiveRule>
     * @author PQZ
     * @date 18:17 2024/9/19
     **/
    @Override
    public List<ActiveRule> listByActiveId(String activeId) {
        LambdaQueryWrapper<ActiveRule> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ActiveRule::getActiveId,activeId).eq(BaseDO::getDeleted,'0');
        return list(lambdaQueryWrapper);
    }
}