package com.uas.platform.b2b.erp.service.impl;

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

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.uas.platform.b2b.dao.PurchaseNoticeDao;
import com.uas.platform.b2b.dao.PurchaseOrderItemDao;
import com.uas.platform.b2b.erp.model.SaleNotifyDown;
import com.uas.platform.b2b.erp.model.SaleNotifyDownEnd;
import com.uas.platform.b2b.erp.model.SaleOut;
import com.uas.platform.b2b.erp.service.SaleNotifyDownService;
import com.uas.platform.b2b.model.PurchaseNotice;
import com.uas.platform.b2b.model.PurchaseOrderItem;
import com.uas.platform.b2b.model.SaleSend;
import com.uas.platform.b2b.model.SaleSendItem;

@Service
public class SaleNotifyDownServiceImpl implements SaleNotifyDownService {

	@Autowired
	private PurchaseNoticeDao purchaseNoticeDao;

	@Autowired
	private PurchaseOrderItemDao purchaseOrderItemDao;

	@Override
	public List<SaleNotifyDown> convertPurchaseNotice(List<PurchaseNotice> notices) {
		List<SaleNotifyDown> downs = new ArrayList<SaleNotifyDown>();
		if (!CollectionUtils.isEmpty(notices)) {
			for (PurchaseNotice notice : notices) {
                downs.add(new SaleNotifyDown(notice));
            }
		}
		return downs;
	}

	@Override
	public List<SaleSendItem> convertSaleOut(List<SaleOut> outs) {
		List<SaleSendItem> sendItems = new ArrayList<SaleSendItem>();
		// 按卖家发货单明细的订单号，对应的买家的采购单的采购员、币别、付款方式分组
		// 卖家的发货单产生的时候，直接是按币别、付款方式、客户联系人分组了
		if (!CollectionUtils.isEmpty(outs)) {
			for (SaleOut out : outs) {
				SaleSend send = out.convert();
				if (!CollectionUtils.isEmpty(send.getSendItems())) {
					for (SaleSendItem item : send.getSendItems()) {
						item.setSend(send);
						PurchaseOrderItem orderItem = null;
						if (item.getNoticeId() != null) {
							PurchaseNotice notice = purchaseNoticeDao.findOne(item.getNoticeId());
							if (notice != null) {// 按送货提醒发货
								item.setNotice(notice);
								orderItem = notice.getOrderItem();
								item.setOrderItemId(orderItem.getId());
								item.setOrderItem(orderItem);
							}
						} else if (item.getOrderItemId() != null) {// 直接按客户采购订单转的销售单出货
							orderItem = purchaseOrderItemDao.findOne(item.getOrderItemId());
							item.setOrderItem(orderItem);
						}
						if (orderItem != null) {
                            sendItems.add(item);
                        }
					}
				}
			}
		}
		return sendItems;
	}

	@Override
	public List<SaleOut> convertSaleSend(List<SaleSend> sends) {
		List<SaleOut> outs = new ArrayList<SaleOut>();
		if (!CollectionUtils.isEmpty(sends)) {
			for (SaleSend send : sends) {
                outs.add(new SaleOut(send));
            }
		}
		return outs;
	}

	@Override
	public List<SaleNotifyDownEnd> convertPurchaseNoticeEnd(List<PurchaseNotice> notices) {
		List<SaleNotifyDownEnd> ends = new ArrayList<SaleNotifyDownEnd>();
		if (!CollectionUtils.isEmpty(notices)) {
			for (PurchaseNotice notice : notices) {
                ends.add(new SaleNotifyDownEnd(notice));
            }
		}
		return ends;
	}

}
