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

import org.jeecg.modules.task.entity.CsPtTask;
import org.jeecg.modules.task.entity.CsPtTaskmx;
import org.jeecg.modules.task.mapper.CsPtTaskmxMapper;
import org.jeecg.modules.task.mapper.CsPtTaskMapper;
import org.jeecg.modules.task.service.ICsPtTaskService;
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:   2023-08-19
 * @Version: V1.0
 */
@Service
public class CsPtTaskServiceImpl extends ServiceImpl<CsPtTaskMapper, CsPtTask> implements ICsPtTaskService {

	@Autowired
	private CsPtTaskMapper csPtTaskMapper;
	@Autowired
	private CsPtTaskmxMapper csPtTaskmxMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CsPtTask csPtTask, List<CsPtTaskmx> csPtTaskmxList) {
		csPtTaskMapper.insert(csPtTask);
		if(csPtTaskmxList!=null && csPtTaskmxList.size()>0) {
			for(CsPtTaskmx entity:csPtTaskmxList) {
				//外键设置
				entity.setFromid(csPtTask.getId());
				csPtTaskmxMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CsPtTask csPtTask,List<CsPtTaskmx> csPtTaskmxList) {
		csPtTaskMapper.updateById(csPtTask);
		
		//1.先删除子表数据
		csPtTaskmxMapper.deleteByMainId(csPtTask.getId());
		
		//2.子表数据重新插入
		if(csPtTaskmxList!=null && csPtTaskmxList.size()>0) {
			for(CsPtTaskmx entity:csPtTaskmxList) {
				//外键设置
				entity.setFromid(csPtTask.getId());
				csPtTaskmxMapper.insert(entity);
			}
		}
	}

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

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