package com.uas.search.dao;

import com.uas.search.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.uas.search.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sunyj
 * @since 2017年7月8日 下午7:45:01
 */
@Repository
public class GoodsDao {

    @Autowired
    private TradeGoodsDao tradeGoodsDao;

    @Autowired
    private ComponentDao componentDao;

    @Autowired
    private StoreDao storeDao;

    @Autowired
    private ProductsDao productsDao;

    /**
     * 根据批次 id 或者器件 id 获取批次
     *
     * @param goods 批次 id 或者器件 id
     * @return 批次
     */
    public List<Goods> find(Goods goods) {
        if (goods == null) {
            return null;
        }
        List<Goods> goodsesList = new ArrayList<>();
        if (goods.getComponent() != null && goods.getComponent().getId() != null) {
            goodsesList.addAll(findByCmpId(goods.getComponent().getId()));
        } else if (goods.getTradeGoods() != null && goods.getTradeGoods().getId() != null) {
            goodsesList.add(findByGoId(goods.getTradeGoods().getId()));
        }
        return goodsesList;
    }

    /**
     * 根据批次 id 获取批次信息
     *
     * @param goId 批次 id
     * @return 批次信息
     */
    private Goods findByGoId(Long goId) {
        TradeGoods tradeGoods = tradeGoodsDao.findOne(goId);
        if (tradeGoods == null) {
            throw new IllegalStateException("批次不存在：" + goId);
        }
        return findByTradeGoods(tradeGoods);
    }

    /**
     * 获取批次信息
     *
     * @param tradeGoods 批次
     * @return 批次信息
     */
    public Goods findByTradeGoods(TradeGoods tradeGoods) {
        Component component = null;
        if(tradeGoods.getCmpUuid() != null){
            component = componentDao.findByUuid(tradeGoods.getCmpUuid());
        }
        Store store = null;
        Products products = null;
        if (tradeGoods.getStoreId() != null) {
            store = storeDao.findByUuid(tradeGoods.getStoreId());
        }
        if (tradeGoods.getProductId() != null) {
            products = productsDao.findOne(tradeGoods.getProductId());
        }
        return new Goods(tradeGoods, store, component, products);
    }

    /**
     * 根据器件 id 获取批次信息
     *
     * @param cmpId 器件id
     * @return 批次信息
     */
    public List<Goods> findByCmpId(Long cmpId) {
        Component component = componentDao.findOne(cmpId);
        if (component == null) {
            throw new IllegalStateException("器件不存在：" + cmpId);
        }
        return findByComponent(component);
    }

    /**
     * 根据器件获取批次信息
     *
     * @param component 器件
     * @return 批次信息
     */
    public List<Goods> findByComponent(Component component) {
        if (component == null || component.getUuid() == null) {
            return null;
        }
        List<TradeGoods> tradeGoodsesList = tradeGoodsDao.findByCmpUuid(component.getUuid());
        List<Goods> goodsesList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(tradeGoodsesList)) {
            for (TradeGoods tradeGoods : tradeGoodsesList) {
                Store store = null;
                Products products = null;
                if (tradeGoods.getStoreId() != null) {
                    store = storeDao.findByUuid(tradeGoods.getStoreId());
                }
                if (tradeGoods.getProductId() != null) {
                    products = productsDao.findOne(tradeGoods.getProductId());
                }
                goodsesList.add(new Goods(tradeGoods, store, component, products));
            }
        } else {
            goodsesList.add(new Goods(null, null, component, null));
        }
        return goodsesList;
    }
}
