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

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

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.PurchaseOrderDao;
import com.uas.platform.b2b.dao.PurchaseOrderItemDao;
import com.uas.platform.b2b.dao.UserDao;
import com.uas.platform.b2b.dao.VendorDao;
import com.uas.platform.b2b.erp.model.Purchase;
import com.uas.platform.b2b.erp.model.PurchaseDetailEnd;
import com.uas.platform.b2b.erp.model.PurchaseReply;
import com.uas.platform.b2b.erp.service.PurchaseService;
import com.uas.platform.b2b.model.Enterprise;
import com.uas.platform.b2b.model.Product;
import com.uas.platform.b2b.model.PurchaseOrder;
import com.uas.platform.b2b.model.PurchaseOrderAll;
import com.uas.platform.b2b.model.PurchaseOrderAllItem;
import com.uas.platform.b2b.model.PurchaseOrderItem;
import com.uas.platform.b2b.model.PurchaseOrderReply;
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.core.model.Constant;
import com.uas.platform.core.model.Status;

@Service
public class PurchaseServiceImpl implements PurchaseService {

	@Autowired
	private ProductDao productDao;
	@Autowired
	private VendorDao vendorDao;
	@Autowired
	private EnterpriseDao enterpriseDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private PurchaseOrderDao purchaseOrderDao;
	@Autowired
	private PurchaseOrderItemDao purchaseOrderItemDao;

	@Override
	public List<PurchaseOrderAllItem> convertPurchase(List<Purchase> purchases) {
		List<PurchaseOrderAllItem> orderItems = new ArrayList<PurchaseOrderAllItem>();
		long enUU = SystemSession.getUser().getEnterprise().getUu();
		for (Purchase purchase : purchases) {
			// 先查看是否已存在
			List<PurchaseOrder> orders = purchaseOrderDao.findByEnUUAndCode(enUU, purchase.getPu_code());
			if (orders.size() == 0) {
				PurchaseOrderAll order = purchase.convert();
				List<Vendor> vendors = vendorDao.findByMyEnUUAndVendUU(order.getEnUU(), order.getVendUU());
				Vendor vendor = null;
				if (vendors.size() == 0) {
					vendor = addVendor(order);
				} else {
					vendor = vendors.get(0);
				}
				if (vendor != null && !CollectionUtils.isEmpty(order.getOrderItems())) {
					if (order.getVendUserUU() == null && vendors.get(0).getVendorUser() != null)
						order.setVendUserUU(vendor.getVendorUser().getUserUU());
					User user = order.getUser();
					if (order.getUserUU() != null) {
						List<User> users = userDao.findUserByUserUU(order.getUserUU());
						if (users.size() == 0)
							user = addUser(user);
						else
							user = users.get(0);
					} else {
						user = addUser(user);
					}
					order.setUser(user);
					order.setUserUU(user.getUserUU());
					for (PurchaseOrderAllItem item : order.getOrderItems()) {
						item.setOrder(order);
						// 取平台的产品ID
						Product product = item.getProduct();
						List<Product> products = productDao.findByEnUUAndCode(product.getEnUU(), product.getCode());
						if (products.size() > 0) {
							item.setProduct(products.get(0));
							item.setProductId(products.get(0).getId());
						}
					}
					orderItems.addAll(order.getOrderItems());
				}
			}
		}
		return orderItems;
	}

	@Override
	public List<PurchaseReply> convertPurchaseOrderReply(List<PurchaseOrderReply> orderReplies) {
		List<PurchaseReply> replies = new ArrayList<PurchaseReply>();
		for (PurchaseOrderReply orderReply : orderReplies)
			replies.add(new PurchaseReply(orderReply));
		return replies;
	}

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

	/**
	 * 添加用户
	 * 
	 * @param user
	 * @return
	 */
	private User addUser(User user) {
		user.setUserUU(null);
		user.setEnable(Constant.NO);
		Set<Enterprise> enterprises = new HashSet<Enterprise>();
		enterprises.add(SystemSession.getUser().getEnterprise());
		user.setEnterprises(enterprises);
		return userDao.save(user);
	}

	@Override
	public List<PurchaseOrderReply> convertPurchaseReply(List<PurchaseReply> replies) {
		List<PurchaseOrderReply> orderReplies = new ArrayList<PurchaseOrderReply>();
		long enUU = SystemSession.getUser().getEnterprise().getUu();
		for (PurchaseReply reply : replies) {
			List<PurchaseOrderItem> items = purchaseOrderItemDao.findByEnUUAndOrderCodeAndNumber(enUU, reply.getPr_pucode(),
					reply.getPr_pddetno());
			if (!CollectionUtils.isEmpty(items)) {
				PurchaseOrderReply orderReply = reply.convert();
				orderReply.setOrderItem(items.get(0));
				orderReplies.add(orderReply);
			}
		}
		return orderReplies;
	}

	@Override
	public List<PurchaseOrderItem> convertPurchaseEnd(List<PurchaseDetailEnd> ends) {
		List<PurchaseOrderItem> orderItems = new ArrayList<PurchaseOrderItem>();
		long enUU = SystemSession.getUser().getEnterprise().getUu();
		for (PurchaseDetailEnd end : ends) {
			List<PurchaseOrderItem> items = purchaseOrderItemDao.findByEnUUAndOrderCodeAndNumber(enUU, end.getPd_code(), end.getPd_detno());
			if (!CollectionUtils.isEmpty(items)) {
				PurchaseOrderItem orderItem = items.get(0);
				orderItem.setEnd(end.getPd_ended());
				orderItem.setEndStatus((short) Status.NOT_UPLOAD.value());
				orderItems.add(orderItem);
			}
		}
		return orderItems;
	}
}
