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

import com.uas.platform.b2b.dao.*;
import com.uas.platform.b2b.erp.model.PurchaseForecast;
import com.uas.platform.b2b.erp.service.PurchaseForecastService;
import com.uas.platform.b2b.erp.service.VendorService;
import com.uas.platform.b2b.model.*;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.core.model.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

@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;

	@Autowired
	private VendorService vendorService;

    /**
     * 应用来源
     */
    private final String APPID = "uas";

	@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.getUserUU() != null) {
						List<User> users = userDao.findByEnUUAndUserUU(enUU, forecast.getUser().getUserUU());
						if (CollectionUtils.isEmpty(users)) {
							forecast.setUserUU(SystemSession.getUser().getUserUU());
						}
					} else {
						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);
								if (null != vendor) {
									vendorService.addPartnerShipRecord(vendor, APPID); // 添加合作伙伴关系
								}
							} 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());
								item.setErpDate(new Date());
								items.add(item);
							}
						}
					}
				}
			}
		}
		return items;
	}

	@Override
	public List<PurchaseForecastAllItem> convertEDIPurchaseForecast(List<PurchaseForecast> purchaseForecasts, Long enUU) {
		List<PurchaseForecastAllItem> items = new ArrayList<PurchaseForecastAllItem>();
		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.getUserUU() != null) {
						List<User> users = userDao.findByEnUUAndUserUU(enUU, forecast.getUser().getUserUU());
						if (CollectionUtils.isEmpty(users)) {
							forecast.setUserUU(SystemSession.getUser().getUserUU());
						}
					} else {
						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);
								if (null != vendor) {
                                    vendorService.addPartnerShipRecord(vendor, APPID); // 添加合作伙伴关系
								}
							} 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());
								item.setErpDate(new Date());
								items.add(item);
							}
						}
					}
				}
			}
		}
		return items;
	}

	/**
	 * 添加供应商资料
	 * 
	 * @param forecastItem
	 */
	private Vendor addVendor(PurchaseForecastAllItem forecastItem) {
		Enterprise enterprise = enterpriseDao.findOne(forecastItem.getVendUU());
		if (enterprise != null) {
			Vendor vendor = new Vendor();
			vendor.setMyEnUU(forecastItem.getOrder().getEnUU());
			vendor.setMyEnterprise(SystemSession.getUser().getEnterprise());
			vendor.setCustswitch(Constant.YES);
			vendor.setVendswitch(Constant.YES);
			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.setVendUserUU(forecastItem.getVendUserUU());
				}
			}
			return vendorDao.save(vendor);
		}
		return null;
	}

}
