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

import com.uas.platform.b2b.dao.ProductDao;
import com.uas.platform.b2b.dao.PurchaseApBillOutDao;
import com.uas.platform.b2b.dao.PurchaseApBillOutInfoDao;
import com.uas.platform.b2b.erp.model.BillOut;
import com.uas.platform.b2b.erp.model.BillOutDetail;
import com.uas.platform.b2b.model.Product;
import com.uas.platform.b2b.model.PurchaseApBillOut;
import com.uas.platform.b2b.model.PurchaseApBillOutInfo;
import com.uas.platform.b2b.model.PurchaseApBillOutItem;
import com.uas.platform.b2b.support.SystemSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

@Service
public class BillOutServiceImpl implements BillOutService {

	@Autowired
	private PurchaseApBillOutDao purchaseApBillOutDao;

	@Autowired
	private ProductDao productDao;

	@Autowired
	private PurchaseApBillOutInfoDao purchaseApBillOutInfoDao;


	@Override
	public List<PurchaseApBillOutItem> convertBillOuts(List<BillOut> billOuts) {
		List<PurchaseApBillOutItem> apBillItems = new ArrayList<PurchaseApBillOutItem>();
		long enUU = SystemSession.getUser().getEnterprise().getUu();
		for (BillOut billOut : billOuts) {
			List<PurchaseApBillOut> billOuts2 = purchaseApBillOutDao.findByEnUuAndCode(enUU, billOut.getBi_code());
			//Enterprise vendor = enterpriseDao.findEnterpriseByUu(billOut.getAb_vendoruu());
			if (billOuts2.size() == 0) {
				Long custuu = billOut.getBi_custuu();
				PurchaseApBillOutInfo customerApBillOutInfo = null;
				PurchaseApBillOutInfo apBillOutInfo = purchaseApBillOutInfoDao.findByUu(enUU);
				if (custuu != null) {
					customerApBillOutInfo = purchaseApBillOutInfoDao.findByUu(custuu);
				}

				//获取物料
				List<BillOutDetail> billOutDetails = billOut.getDetails();
				List<Product> billOutProducts = new ArrayList<>();
				if (!CollectionUtils.isEmpty(billOutDetails)) {
					for (BillOutDetail billOutDetail : billOutDetails) {
						List<Product> products = productDao.findByEnUUAndCode(enUU, billOutDetail.getArd_prodcode());
						if (!CollectionUtils.isEmpty(products)) {
							billOutProducts.add(products.get(0));
						}
					}
				}


				PurchaseApBillOut purchaseApBillOut = billOut.convert(customerApBillOutInfo, apBillOutInfo, billOutProducts);
				Iterator<PurchaseApBillOutItem> items = purchaseApBillOut.getItems().iterator();
				while (items.hasNext()) {
					PurchaseApBillOutItem purchaseApBillOutItem = items.next();
					List<Product> products = productDao.findByEnUUAndCode(enUU, purchaseApBillOutItem.getProduct().getCode());
					if (products.size() > 0) {
						purchaseApBillOutItem.setProduct(products.get(0));
						purchaseApBillOutItem.setPrid(products.get(0).getId());
						purchaseApBillOutItem.setApBillOut(purchaseApBillOut);
						//TODO purchaseApBillOutItem.setErpDate(new Date());
						apBillItems.add(purchaseApBillOutItem);
					} else {
						items.remove();
					}
				}
			}
		}
		return apBillItems;
	}
}
