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.PurchaseOrderItemDao;
import com.uas.platform.b2b.erp.model.SaleDown;
import com.uas.platform.b2b.erp.model.SaleDownDetailEnd;
import com.uas.platform.b2b.erp.model.SaleReply;
import com.uas.platform.b2b.erp.service.SaleDownService;
import com.uas.platform.b2b.model.PurchaseOrderAll;
import com.uas.platform.b2b.model.PurchaseOrderItem;
import com.uas.platform.b2b.model.PurchaseOrderReply;

@Service
public class SaleDownServiceImpl implements SaleDownService {

	@Autowired
	private PurchaseOrderItemDao purchaseOrderItemDao;

	@Override
	public List<SaleDown> convertPurchaseOrder(List<PurchaseOrderAll> orders) {
		List<SaleDown> sales = new ArrayList<SaleDown>();
		for (PurchaseOrderAll order : orders) {
            sales.add(new SaleDown(order));
        }
		return sales;
	}

	@Override
	public List<PurchaseOrderReply> convertSaleReply(List<SaleReply> replies) {
		List<PurchaseOrderReply> orderReplies = new ArrayList<PurchaseOrderReply>();
		for (SaleReply reply : replies) {
			List<PurchaseOrderItem> items = purchaseOrderItemDao.findByEnUUAndOrderCodeAndNumber(reply.getCu_uu(), reply.getSr_sacode(),
					reply.getSr_sddetno());
			if (!CollectionUtils.isEmpty(items)) {
				PurchaseOrderReply orderReply = reply.convert();
				orderReply.setOrderItem(items.get(0));
				orderReplies.add(orderReply);
			}
		}
		return orderReplies;
	}

	@Override
	public List<SaleReply> convertOrderReply(List<PurchaseOrderReply> replies) {
		List<SaleReply> saleReplies = new ArrayList<SaleReply>();
		for (PurchaseOrderReply reply : replies) {
			saleReplies.add(new SaleReply(reply));
		}
		return saleReplies;
	}

	@Override
	public List<SaleDownDetailEnd> convertOrderItemEnd(List<PurchaseOrderItem> orderItems) {
		List<SaleDownDetailEnd> ends = new ArrayList<SaleDownDetailEnd>();
		if (!CollectionUtils.isEmpty(orderItems)) {
			for (PurchaseOrderItem orderItem : orderItems) {
                ends.add(new SaleDownDetailEnd(orderItem));
            }
		}
		return ends;
	}

}
