package com.njcn.gather.plan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.gather.plan.mapper.AdPlanSourceMapper;
import com.njcn.gather.plan.pojo.po.AdPlanSource;
import com.njcn.gather.plan.service.IAdPlanSourceService;
import com.njcn.gather.source.pojo.po.PqSource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author caozehui
 * @date 2024-12-09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdPlanSourceServiceImpl extends ServiceImpl<AdPlanSourceMapper, AdPlanSource> implements IAdPlanSourceService {

    private final AdPlanSourceMapper adPlanSourceMapper;

    @Override
    public List<PqSource> listPqSourceByPlanId(String planId) {
        return adPlanSourceMapper.selectPqSourceByPlanId(planId);
    }

    @Override
    @Transactional
    public boolean addAdPlanSource(String planId, List<String> sourceIds) {
        List<AdPlanSource> adPlanSourceList = new ArrayList<>();
        for (String sourceId : sourceIds) {
            adPlanSourceList.add(new AdPlanSource(planId, sourceId));
        }
        return this.saveBatch(adPlanSourceList);
    }

    @Override
    @Transactional
    public boolean deleteAdPlanSourceByPlanIds(List<String> planIds) {
        if(CollectionUtils.isNotEmpty(planIds)){
            QueryWrapper<AdPlanSource> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("ad_plan_source.Plan_Id", planIds);
            return this.remove(queryWrapper);
        }
        return false;
    }

    @Override
    @Transactional
    public boolean updateAdPlanSource(String planId, List<String> sourceIds) {
        this.deleteAdPlanSourceByPlanIds(Collections.singletonList(planId));
        this.addAdPlanSource(planId, sourceIds);
        return true;
    }
}
