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.MakeOrderDao;
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.OutsourceService;
import com.uas.platform.b2b.model.MakeOrder;
import com.uas.platform.b2b.model.MakeOrderReply;

@Service
public class OutsourceServiceImpl implements OutsourceService {
	
	@Autowired
	private MakeOrderDao makeOrderDao;

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

	@Override
	public List<MakeOrderReply> convertSaleReply(List<SaleReply> replies) {
		List<MakeOrderReply> orderReplies = new ArrayList<MakeOrderReply>();
		for (SaleReply reply : replies) {
			List<MakeOrder> orders = makeOrderDao.findByEnUUAndCode(reply.getCu_uu(), reply.getSr_sacode());
			if (!CollectionUtils.isEmpty(orders)) {
				MakeOrderReply orderReply = reply.convertToMakeReply();
				orderReply.setOrderId(orders.get(0).getId());
				orderReplies.add(orderReply);
			}
		}
		return orderReplies;
	}

	@Override
	public List<SaleReply> convertMakeOrderReply(List<MakeOrderReply> replies) {
		List<SaleReply> saleReplies = new ArrayList<SaleReply>();
		if( ! CollectionUtils.isEmpty(replies)) {
			for (MakeOrderReply reply : replies) {
				saleReplies.add(new SaleReply(reply));
			}
		}
		return saleReplies;
	}

	@Override
	public List<SaleDownDetailEnd> convertMakeOrderEnd(List<MakeOrder> orders) {
		List<SaleDownDetailEnd> ends = new ArrayList<SaleDownDetailEnd>();
		if( ! CollectionUtils.isEmpty(orders)) {
			for(MakeOrder order : orders) {
				ends.add(new SaleDownDetailEnd(order));
			}
		}
		return ends;
	}


}
