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

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.warehouse.storage.entity.Storage;
import com.jeeplus.modules.warehouse.storage.mapper.StorageMapper;
import com.jeeplus.modules.warehouse.storage.entity.StorageInfo;
import com.jeeplus.modules.warehouse.storage.mapper.StorageInfoMapper;

/**
 * 入库信息Service
 * @author huyi
 * @version 2023-02-13
 */
@Service
@Transactional(readOnly = true)
public class StorageService extends CrudService<StorageMapper, Storage> {

	@Autowired
	private StorageInfoMapper storageInfoMapper;
	
	public Storage get(String id) {
		Storage storage = super.get(id);
		storage.setStorageInfoList(storageInfoMapper.findList(new StorageInfo(storage)));
		return storage;
	}
	
	public List<Storage> findList(Storage storage) {
		return super.findList(storage);
	}
	
	public Page<Storage> findPage(Page<Storage> page, Storage storage) {
		return super.findPage(page, storage);
	}
	
	@Transactional(readOnly = false)
	public void save(Storage storage) {
		super.save(storage);
		for (StorageInfo storageInfo : storage.getStorageInfoList()){
			if (storageInfo.getId() == null){
				continue;
			}
			if (StorageInfo.DEL_FLAG_NORMAL.equals(storageInfo.getDelFlag())){
				if (StringUtils.isBlank(storageInfo.getId())){
					storageInfo.setStorage(storage);
					storageInfo.preInsert();
					storageInfoMapper.insert(storageInfo);
				}else{
					storageInfo.preUpdate();
					storageInfoMapper.update(storageInfo);
				}
			}else{
				storageInfoMapper.delete(storageInfo);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Storage storage) {
		super.delete(storage);
		storageInfoMapper.delete(new StorageInfo(storage));
	}
	
}