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

import org.jeecg.modules.studytourActivityDate.entity.StudytourActivityDate;
import org.jeecg.modules.studytourActivityDate.entity.StudytourDatePrice;
import org.jeecg.modules.studytourActivityDate.mapper.StudytourDatePriceMapper;
import org.jeecg.modules.studytourActivityDate.mapper.StudytourActivityDateMapper;
import org.jeecg.modules.studytourActivityDate.service.IStudytourActivityDateService;
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-08
 * @Version: V1.0
 */
@Service
public class StudytourActivityDateServiceImpl extends ServiceImpl<StudytourActivityDateMapper, StudytourActivityDate> implements IStudytourActivityDateService {

	@Autowired
	private StudytourActivityDateMapper studytourActivityDateMapper;
	@Autowired
	private StudytourDatePriceMapper studytourDatePriceMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(StudytourActivityDate studytourActivityDate, List<StudytourDatePrice> studytourDatePriceList) {
		studytourActivityDateMapper.insert(studytourActivityDate);
		if(studytourDatePriceList!=null && studytourDatePriceList.size()>0) {
			for(StudytourDatePrice entity:studytourDatePriceList) {
				//外键设置
				entity.setDateId(studytourActivityDate.getId());
				studytourDatePriceMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(StudytourActivityDate studytourActivityDate,List<StudytourDatePrice> studytourDatePriceList) {
		studytourActivityDateMapper.updateById(studytourActivityDate);
		
		//1.先删除子表数据
		studytourDatePriceMapper.deleteByMainId(studytourActivityDate.getId());
		
		//2.子表数据重新插入
		if(studytourDatePriceList!=null && studytourDatePriceList.size()>0) {
			for(StudytourDatePrice entity:studytourDatePriceList) {
				//外键设置
				entity.setDateId(studytourActivityDate.getId());
				studytourDatePriceMapper.insert(entity);
			}
		}
	}

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

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