package org.jeecg.modules.studytourImage.service.impl;

import org.jeecg.modules.studytourImage.entity.StudytourImage;
import org.jeecg.modules.studytourImage.entity.StudytourImageContent;
import org.jeecg.modules.studytourImage.mapper.StudytourImageContentMapper;
import org.jeecg.modules.studytourImage.mapper.StudytourImageMapper;
import org.jeecg.modules.studytourImage.service.IStudytourImageService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 图片直播表
 * @Author: jeecg-boot
 * @Date:   2025-09-12
 * @Version: V1.0
 */
@Service
public class StudytourImageServiceImpl extends ServiceImpl<StudytourImageMapper, StudytourImage> implements IStudytourImageService {

	@Autowired
	private StudytourImageMapper studytourImageMapper;
	@Autowired
	private StudytourImageContentMapper studytourImageContentMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(StudytourImage studytourImage, List<StudytourImageContent> studytourImageContentList) {
		studytourImageMapper.insert(studytourImage);
		if(studytourImageContentList!=null && studytourImageContentList.size()>0) {
			for(StudytourImageContent entity:studytourImageContentList) {
				//外键设置
				entity.setImageId(studytourImage.getId());
				studytourImageContentMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(StudytourImage studytourImage,List<StudytourImageContent> studytourImageContentList) {
		studytourImageMapper.updateById(studytourImage);
		
		//1.先删除子表数据
		studytourImageContentMapper.deleteByMainId(studytourImage.getId());
		
		//2.子表数据重新插入
		if(studytourImageContentList!=null && studytourImageContentList.size()>0) {
			for(StudytourImageContent entity:studytourImageContentList) {
				//外键设置
				entity.setImageId(studytourImage.getId());
				studytourImageContentMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		studytourImageContentMapper.deleteByMainId(id);
		studytourImageMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			studytourImageContentMapper.deleteByMainId(id.toString());
			studytourImageMapper.deleteById(id);
		}
	}
	
}
