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

import org.jeecg.modules.studytourOrder.entity.StudytourOrder;
import org.jeecg.modules.studytourOrder.entity.StudytourOrderPerson;
import org.jeecg.modules.studytourOrder.mapper.StudytourOrderPersonMapper;
import org.jeecg.modules.studytourOrder.mapper.StudytourOrderMapper;
import org.jeecg.modules.studytourOrder.service.IStudytourOrderService;
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-10
 * @Version: V1.0
 */
@Service
public class StudytourOrderServiceImpl extends ServiceImpl<StudytourOrderMapper, StudytourOrder> implements IStudytourOrderService {

	@Autowired
	private StudytourOrderMapper studytourOrderMapper;
	@Autowired
	private StudytourOrderPersonMapper studytourOrderPersonMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(StudytourOrder studytourOrder, List<StudytourOrderPerson> studytourOrderPersonList) {
		studytourOrderMapper.insert(studytourOrder);
		if(studytourOrderPersonList!=null && studytourOrderPersonList.size()>0) {
			for(StudytourOrderPerson entity:studytourOrderPersonList) {
				//外键设置
				entity.setOrderId(studytourOrder.getId());
				studytourOrderPersonMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(StudytourOrder studytourOrder,List<StudytourOrderPerson> studytourOrderPersonList) {
		studytourOrderMapper.updateById(studytourOrder);
		
		//1.先删除子表数据
		studytourOrderPersonMapper.deleteByMainId(studytourOrder.getId());
		
		//2.子表数据重新插入
		if(studytourOrderPersonList!=null && studytourOrderPersonList.size()>0) {
			for(StudytourOrderPerson entity:studytourOrderPersonList) {
				//外键设置
				entity.setOrderId(studytourOrder.getId());
				studytourOrderPersonMapper.insert(entity);
			}
		}
	}

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

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