/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.warehouse.goods.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.goods.entity.Goods;
import com.jeeplus.modules.warehouse.goods.mapper.GoodsMapper;
import com.jeeplus.modules.warehouse.goods.entity.GoodsInfo;
import com.jeeplus.modules.warehouse.goods.mapper.GoodsInfoMapper;

/**
 * 货品模块Service
 * @author huyi
 * @version 2023-02-13
 */
@Service
@Transactional(readOnly = true)
public class GoodsService extends CrudService<GoodsMapper, Goods> {

	@Autowired
	private GoodsInfoMapper goodsInfoMapper;
	
	public Goods get(String id) {
		Goods goods = super.get(id);
		goods.setGoodsInfoList(goodsInfoMapper.findList(new GoodsInfo(goods)));
		return goods;
	}
	
	public List<Goods> findList(Goods goods) {
		return super.findList(goods);
	}
	
	public Page<Goods> findPage(Page<Goods> page, Goods goods) {
		return super.findPage(page, goods);
	}
	
	@Transactional(readOnly = false)
	public void save(Goods goods) {
		super.save(goods);
		for (GoodsInfo goodsInfo : goods.getGoodsInfoList()){
			if (goodsInfo.getId() == null){
				continue;
			}
			if (GoodsInfo.DEL_FLAG_NORMAL.equals(goodsInfo.getDelFlag())){
				if (StringUtils.isBlank(goodsInfo.getId())){
					goodsInfo.setGoods(goods);
					goodsInfo.preInsert();
					goodsInfoMapper.insert(goodsInfo);
				}else{
					goodsInfo.preUpdate();
					goodsInfoMapper.update(goodsInfo);
				}
			}else{
				goodsInfoMapper.delete(goodsInfo);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Goods goods) {
		super.delete(goods);
		goodsInfoMapper.delete(new GoodsInfo(goods));
	}
	
}