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.EnterpriseDao;
import com.uas.platform.b2b.dao.ProductDao;
import com.uas.platform.b2b.dao.PurchaseForecastAllDao;
import com.uas.platform.b2b.dao.UserDao;
import com.uas.platform.b2b.dao.VendorDao;
import com.uas.platform.b2b.erp.service.PurchaseForecastService;
import com.uas.platform.b2b.model.Enterprise;
import com.uas.platform.b2b.model.Product;
import com.uas.platform.b2b.model.PurchaseForecastAll;
import com.uas.platform.b2b.model.PurchaseForecastAllItem;
import com.uas.platform.b2b.model.User;
import com.uas.platform.b2b.model.Vendor;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.b2b.erp.model.PurchaseForecast;

@Service
public class PurchaseForecastServiceImpl implements PurchaseForecastService {

	@Autowired
	private ProductDao productDao;
	@Autowired
	private VendorDao vendorDao;
	@Autowired
	private EnterpriseDao enterpriseDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private PurchaseForecastAllDao purchaseForecastDao;
	@Override
	public List<PurchaseForecastAllItem> convertPurchaseForecast(List<PurchaseForecast> purchaseForecasts) {
		List<PurchaseForecastAllItem> items = new ArrayList<PurchaseForecastAllItem>();
		long enUU = SystemSession.getUser().getEnterprise().getUu();
		for (PurchaseForecast purchaseForecast : purchaseForecasts) {
			// 先查看是否已存在
			List<PurchaseForecastAll> forecasts = purchaseForecastDao.findByEnUUAndCode(enUU, purchaseForecast.getPf_code());
			if (forecasts.size() == 0) {
				PurchaseForecastAll forecast = purchaseForecast.convert();
				if (!CollectionUtils.isEmpty(forecast.getForecastItems())) {
					//设置User
					if(forecast.getUser().getUserUU() != null) {
						List<User> users = userDao.findByEnUUAndUserUU(enUU, forecast.getUser().getUserUU());
						if(users.size() > 0) {
							forecast.setUser(users.get(0));
							forecast.setUserUU(users.get(0).getUserUU());
						} else {
							forecast.setUser(SystemSession.getUser());
							forecast.setUserUU(SystemSession.getUser().getUserUU());
						}
					} else {
						forecast.setUser(SystemSession.getUser());
						forecast.setUserUU(SystemSession.getUser().getUserUU());
					}
					//对明细遍历
					for (PurchaseForecastAllItem item : forecast.getForecastItems()) {
						item.setOrder(forecast);
						if (item.getVendUU() != null) {//明细中有供应商uu号
							List<Vendor> vendors = vendorDao.findByMyEnUUAndVendUU(enUU, item.getVendUU());
							Vendor vendor = null;
							if(vendors.size() == 0) {
								vendor = addVendor(item);
							} else {
								vendor = vendors.get(0);
							}
							List<Product> products = productDao.findByEnUUAndCode(enUU, item.getProduct().getCode());
							if (products.size() > 0 && vendor != null) {
								item.setVendUU(vendor.getVendEnUU());
								if(item.getVendUserUU() != null && item.getVendUserUU() != 0) {
									List<User> vendUsers = userDao.findByEnUUAndUserUU(vendor.getVendEnUU(), item.getVendUserUU());
									if(vendUsers.size() == 0) {
										//平台中的供应商企业不存在供应商联系人uu的用户
										//则设为平台中供应商列表中的供应商联系人UU
										item.setVendUserUU(vendors.get(0).getVendUserUU());
									}
								}else {
									//买方ERP中供应商资料没有设置供应商联系人uu
									//则设为平台中供应商列表中的供应商联系人UU
									item.setVendUserUU(vendors.get(0).getVendUserUU());
								}
								item.setProduct(products.get(0));
								item.setProductId(products.get(0).getId());
								items.add(item);
							}
						}
					}
				}
			}
		}
		return items;
	}

	/**
	 * 添加供应商资料
	 * 
	 * @param forecast
	 */
	private Vendor addVendor(PurchaseForecastAllItem forecastItem) {
		Enterprise enterprise = enterpriseDao.findOne(forecastItem.getVendUU());
		if (enterprise != null) {
			Vendor vendor = new Vendor();
			vendor.setMyEnUU(forecastItem.getOrder().getEnUU());
			if(forecastItem.getOrder().getUserUU() != null && userDao.findOne(forecastItem.getOrder().getUserUU()) != null)
				vendor.setMyUserUU(forecastItem.getOrder().getUserUU());
			else {
				vendor.setMyUserUU(SystemSession.getUser().getUserUU());
			}
			vendor.setVendorEnterprise(enterprise);
			vendor.setVendEnUU(enterprise.getUu());
			if (forecastItem.getVendUserUU() != null) {
				List<User> users = userDao.findByEnUUAndUserUU(enterprise.getUu(), forecastItem.getVendUserUU());
				if (users.size() > 0) {
					vendor.setVendorUser(users.get(0));
					vendor.setVendUserUU(forecastItem.getVendUserUU());
				}
			}
			return vendorDao.save(vendor);
		}
		return null;
	}
	
}
