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

import org.jeecg.modules.kzzdys.entity.CsPtKzzdys;
import org.jeecg.modules.kzzdys.entity.CsPtKzzdysmx;
import org.jeecg.modules.kzzdys.mapper.CsPtKzzdysmxMapper;
import org.jeecg.modules.kzzdys.mapper.CsPtKzzdysMapper;
import org.jeecg.modules.kzzdys.service.ICsPtKzzdysService;
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-12-07
 * @Version: V1.0
 */
@Service
public class CsPtKzzdysServiceImpl extends ServiceImpl<CsPtKzzdysMapper, CsPtKzzdys> implements ICsPtKzzdysService {

	@Autowired
	private CsPtKzzdysMapper csPtKzzdysMapper;
	@Autowired
	private CsPtKzzdysmxMapper csPtKzzdysmxMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CsPtKzzdys csPtKzzdys, List<CsPtKzzdysmx> csPtKzzdysmxList) {
		csPtKzzdysMapper.insert(csPtKzzdys);
		if(csPtKzzdysmxList!=null && csPtKzzdysmxList.size()>0) {
			for(CsPtKzzdysmx entity:csPtKzzdysmxList) {
				//外键设置
				entity.setFormid(csPtKzzdys.getId());
				csPtKzzdysmxMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CsPtKzzdys csPtKzzdys,List<CsPtKzzdysmx> csPtKzzdysmxList) {
		csPtKzzdysMapper.updateById(csPtKzzdys);
		
		//1.先删除子表数据
		csPtKzzdysmxMapper.deleteByMainId(csPtKzzdys.getId());
		
		//2.子表数据重新插入
		if(csPtKzzdysmxList!=null && csPtKzzdysmxList.size()>0) {
			for(CsPtKzzdysmx entity:csPtKzzdysmxList) {
				//外键设置
				entity.setFormid(csPtKzzdys.getId());
				csPtKzzdysmxMapper.insert(entity);
			}
		}
	}

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

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