/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sg.financial.settlement.service;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.filing.basicInformation.entity.BasicInformation;
import com.jeeplus.modules.filing.basicInformation.entity.BasicInformationChild;
import com.jeeplus.modules.sg.financial.settlement.entity.Corresponding;
import com.jeeplus.modules.sg.financial.settlement.mapper.CorrespondingMapper;
import com.jeeplus.modules.test.one.dialog.entity.Leave1;
import com.jeeplus.modules.test.one.dialog.mapper.Leave1Mapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 物料对应配置Service
 * @author user3
 * @version 2021-04-22
 */
@Service
@Transactional(readOnly = true)
public class CorrespondingService extends CrudService<CorrespondingMapper, Corresponding> {

	public Corresponding get(String id) {
		Corresponding corresponding = super.get(id);
		corresponding.setChildList(mapper.findChildList(corresponding));
		return corresponding;
	}
	
	public List<Corresponding> findList(Corresponding entity) {
		return super.findList(entity);
	}
	
	public Page<Corresponding> findPage(Page<Corresponding> page, Corresponding entity) {
		return super.findPage(page, entity);
	}

	public List<Corresponding> findChildList(Corresponding entity) {
		dataRuleFilter(entity);
		return mapper.findChildList(entity);
	}

	public Page<Corresponding> findChildPage(Page<Corresponding> page, Corresponding entity) {
		dataRuleFilter(entity);
		entity.setPage(page);
		page.setList(mapper.findChildList(entity));
		return page;
	}

	@Transactional(readOnly = false)
	public void insert(Corresponding entity) {
		entity.preInsert();
		mapper.insert(entity);
	}

	@Transactional(readOnly = false)
	public void save(Corresponding entity) {
		if (entity.getIsNewRecord()){
			entity.preInsert();
			mapper.insert(entity);
		}else{
			entity.preUpdate();
			mapper.update(entity);
		}
		List<Corresponding> childList = entity.getChildList();
		for (Corresponding c :
				childList) {
			c.setSerialNumber(entity.getSerialNumber());
			if (BasicInformationChild.DEL_FLAG_NORMAL.equals(c.getDelFlag())) {
				if (c.getIsNewRecord()) {
					c.preInsert();
					mapper.insertChild(c);
				} else {
					c.preUpdate();
					mapper.updateChild(c);
				}
			} else {
				mapper.deleteChild(c);
			}
		}
	}

	@Transactional(readOnly = false)
	public void insertChild(Corresponding entity) {
		entity.preInsert();
		mapper.insertChild(entity);
	}

	@Transactional(readOnly = false)
	public void delete(Corresponding entity) {
		super.delete(entity);
		mapper.deleteAllChild(entity);
	}

	@Transactional(readOnly = false)
	public void deleteAllChild(Corresponding entity) {
		mapper.deleteAllChild(entity);
	}

	public Corresponding isNull(Corresponding entity) {
		return mapper.isNull(entity);
	}
}