package cn.cslg.pas.service.impl;

import cn.cslg.pas.domain.asso.AssoEventProject;
import cn.cslg.pas.mapper.asso.AssoEventProjectMapper;
import cn.cslg.pas.service.IAssoEventProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * 事件和专题库关联的Service层接口实现类
 *
 * @Author chenyu
 * @Date 2023/4/4
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class AssoEventProjectServiceImpl extends ServiceImpl<AssoEventProjectMapper, AssoEventProject> implements IAssoEventProjectService {
    private final AssoEventProjectMapper assoEventProjectMapper;

    /**
     * 新增事件和专题库关联数据
     *
     * @param eventIds  事件ids
     * @param projectId 专题库id
     */
    @Override
    public void addNew(List<Integer> eventIds, Integer projectId) {
        log.info("开始处理【批量新增事件和专题库关联】的业务，参数为：{}, {}", eventIds, projectId);

        //删除原有相同的关联数据
//        LambdaQueryWrapper<AssoEventProject> wrapper = new LambdaQueryWrapper<>();
//        wrapper.in(AssoEventProject::getProjectId, projectId);
//        this.remove(wrapper);
        assoEventProjectMapper.deleteByEventIdsAndProjectId(eventIds, projectId);

        ArrayList<AssoEventProject> assoEventProjects = new ArrayList<>();
        for (Integer eventId : eventIds) {
            AssoEventProject assoEventProject = new AssoEventProject()
                    .setEventId(eventId)
                    .setProjectId(projectId);
            assoEventProjects.add(assoEventProject);
        }
        //数据入事件和专题库关联表
        this.saveBatch(assoEventProjects);

        log.info("批量新增事件和专题库关联完成");

    }

}
