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

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.uas.platform.b2b.dao.ProductDao;
import com.uas.platform.b2b.dao.PurchaseOrderChangeDao;
import com.uas.platform.b2b.dao.PurchaseOrderDao;
import com.uas.platform.b2b.dao.PurchaseOrderItemDao;
import com.uas.platform.b2b.erp.model.PurchaseChange;
import com.uas.platform.b2b.erp.model.PurchaseChangeReply;
import com.uas.platform.b2b.erp.service.PurchaseChangeService;
import com.uas.platform.b2b.model.Product;
import com.uas.platform.b2b.model.PurchaseOrder;
import com.uas.platform.b2b.model.PurchaseOrderChange;
import com.uas.platform.b2b.model.PurchaseOrderChangeItem;
import com.uas.platform.b2b.model.PurchaseOrderItem;
import com.uas.platform.b2b.support.SystemSession;

@Service
public class PurchaseChangeServiceImpl implements PurchaseChangeService {

	@Autowired
	private PurchaseOrderDao purchaseOrderDao;

	@Autowired
	private PurchaseOrderItemDao purchaseOrderItemDao;

	@Autowired
	private ProductDao productDao;

	@Autowired
	private PurchaseOrderChangeDao purchaseOrderChangeDao;

	@Override
	public List<PurchaseOrderChangeItem> convertPurchaseChange(List<PurchaseChange> changes) {
		List<PurchaseOrderChangeItem> changeItems = new ArrayList<PurchaseOrderChangeItem>();
		long enUU = SystemSession.getUser().getEnterprise().getUu();
		for (PurchaseChange change : changes) {
			PurchaseOrderChange orderChange = change.convert();
			List<PurchaseOrder> orders = purchaseOrderDao.findByEnUUAndCode(enUU, change.getPc_purccode());
			List<PurchaseOrderChange> orderChanges = purchaseOrderChangeDao.findByEnUUAndCode(enUU, change.getPc_code());
			// 原采购单需存在，变更单不存在
			if (orders.size() > 0 && orderChanges.size() == 0) {
				orderChange.setOrder(orders.get(0));
				orderChange.setOrderId(orders.get(0).getId());
				if (!CollectionUtils.isEmpty(orderChange.getOrderChangeItems())) {
					for (PurchaseOrderChangeItem item : orderChange.getOrderChangeItems()) {
						// 取订单明细ID
						List<PurchaseOrderItem> orderItems = purchaseOrderItemDao.findByEnUUAndOrderCodeAndNumber(enUU, orderChange
								.getOrder().getCode(), item.getOrderItem().getNumber());
						if (orderItems.size() > 0) {
							item.setOrderItem(orderItems.get(0));
							item.setOrderItemId(orderItems.get(0).getId());
						}
						// 取平台的产品ID
						Product newProduct = item.getNewProduct();
						List<Product> products = productDao.findByEnUUAndCode(newProduct.getEnUU(), newProduct.getCode());
						if (products.size() > 0) {
							item.setNewProduct(products.get(0));
							item.setNewProductId(products.get(0).getId());
						}
						item.setOrderChange(orderChange);
					}
					changeItems.addAll(orderChange.getOrderChangeItems());
				}
			}
		}
		return changeItems;
	}

	@Override
	public List<PurchaseChangeReply> convertPurchaseOrderChangeReply(List<PurchaseOrderChange> changes) {
		List<PurchaseChangeReply> replies = new ArrayList<PurchaseChangeReply>();
		for (PurchaseOrderChange change : changes)
			replies.add(new PurchaseChangeReply(change));
		return replies;
	}

}
