package cn.cslg.report.service.impl;

import cn.cslg.report.entity.asso.AssoEventReport;
import cn.cslg.report.mapper.AssoEventReportMapper;
import cn.cslg.report.service.IAssoEventReportService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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
 */
@Slf4j
@Service
public class AssoEventReportServiceImpl extends ServiceImpl<AssoEventReportMapper, AssoEventReport> implements IAssoEventReportService {
    /**
     * 新增事件和报告关联
     *
     * @param eventIds 事件ids
     * @param reportId 报告id
     */
    @Override
    public void addNew(List<Integer> eventIds, Integer reportId) {
        log.info("开始处理【新增事件和报告关联】的业务，参数为：{}， {}", eventIds, reportId);

        //删除原有关联数据
        LambdaQueryWrapper<AssoEventReport> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(AssoEventReport::getReportId, reportId);
        this.remove(wrapper);

        ArrayList<AssoEventReport> assoEventReports = new ArrayList<>();
        for (Integer eventId : eventIds) {
            AssoEventReport assoEventReport = new AssoEventReport()
                    .setEventId(eventId)
                    .setReportId(reportId);
            assoEventReports.add(assoEventReport);
        }

        //数据入事件和报告关联表
        this.saveBatch(assoEventReports);

        log.info("新增事件和报告关联完成");

    }


}
