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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.uas.platform.b2b.erp.model.SaleForecastDown;
import com.uas.platform.b2b.erp.model.SaleForecastDownDet;
import com.uas.platform.b2b.erp.service.SaleForecastDownService;
import com.uas.platform.b2b.model.PurchaseForecastAllItem;
@Service
public class SaleForecastDownServiceImpl implements SaleForecastDownService {

	@Override
	public List<SaleForecastDown> convertPurchaseForecastDown(
			List<PurchaseForecastAllItem> items ) {
		Map<Long, SaleForecastDown> forecastMap = new HashMap<Long, SaleForecastDown>();
		for(PurchaseForecastAllItem item : items) {
			if(!forecastMap.containsKey(item.getOrder().getId())) {
				forecastMap.put(item.getOrder().getId(), new SaleForecastDown(item));
			} else {
				SaleForecastDown forecastDown = forecastMap.get(item.getOrder().getId());
				List<SaleForecastDownDet> dets = forecastDown.getSaleForecastDownDets();
				dets.add(new SaleForecastDownDet(item));
				forecastDown.setSaleForecastDownDets(dets);
			}
		}
		List<SaleForecastDown> sales = new ArrayList<SaleForecastDown>(forecastMap.values());
		return sales;
	}

}
