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

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

import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import com.uas.platform.b2b.mobile.model.Purchase;
import com.uas.platform.b2b.mobile.service.PurchaseService;
import com.uas.platform.b2b.model.PurchaseOrderAll;
import com.uas.platform.core.model.PageInfo;

@Service("mobile.PurchaseService")
public class PurchaseServiceImpl implements PurchaseService {

	@Override
	public Page<Purchase> convertPurchaseOrder(final Page<PurchaseOrderAll> orderPage) {
		List<Purchase> purchases = new ArrayList<Purchase>();
		List<PurchaseOrderAll> orders = orderPage.getContent();
		if (!CollectionUtils.isEmpty(orders)) {
			for (PurchaseOrderAll order : orders) {
				purchases.add(new Purchase(order));
			}
		}
		return new PageImpl<Purchase>(purchases, new PageInfo(orderPage.getNumber(), orderPage.getSize()), orderPage.getTotalElements());
	}

	@Override
	public Purchase convertPurchaseOrder(PurchaseOrderAll order) {
		return new Purchase(order);
	}

}
