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

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

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.uas.platform.b2b.dao.EnterpriseDao;
import com.uas.platform.b2b.dao.ProductDao;
import com.uas.platform.b2b.dao.PurchaseProofingDao;
import com.uas.platform.b2b.dao.UserDao;
import com.uas.platform.b2b.dao.VendorDao;
import com.uas.platform.b2b.erp.model.ProductSample;
import com.uas.platform.b2b.erp.model.ProductSampleApproval;
import com.uas.platform.b2b.erp.service.PurchaseSampleService;
import com.uas.platform.b2b.model.Enterprise;
import com.uas.platform.b2b.model.Product;
import com.uas.platform.b2b.model.PurchaseProofing;
import com.uas.platform.b2b.model.PurchaseProofingApproval;
import com.uas.platform.b2b.model.PurchaseProofingItem;
import com.uas.platform.b2b.model.User;
import com.uas.platform.b2b.model.Vendor;
import com.uas.platform.b2b.support.SystemSession;

@Service
public class PurchaseSampleServiceImpl implements PurchaseSampleService {
	
	@Autowired
	private PurchaseProofingDao purchaseProofingDao;
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private ProductDao productDao;
	
	@Autowired
	private EnterpriseDao enterpriseDao;
	
	@Autowired
	private VendorDao vendorDao;

	@Override
	public List<PurchaseProofingItem> convertProofing(
			List<ProductSample> samples) {
		List<PurchaseProofingItem> proofingItems = new ArrayList<PurchaseProofingItem>();
		long enUU = SystemSession.getUser().getEnterprise().getUu();
		for(ProductSample sample : samples) {
			List<PurchaseProofing> proofingList = purchaseProofingDao.findByEnUUAndCode(enUU, sample.getPs_code());
			List<Product> products = productDao.findByEnUUAndCode(enUU, sample.getPs_prodcode());
			if(proofingList.size() == 0 && products.size() > 0) {//平台中不存在的单据&&物料已经上传到平台
				PurchaseProofing proofing = sample.convert();
				if (!CollectionUtils.isEmpty(proofing.getProofingItems())) {
					User user = proofing.getUser();
					if(user.getUserUU() != null) {
						List<User> users = userDao.findByEnUUAndUserUU(enUU, user.getUserUU());
						if(users.size() > 0) user = users.get(0);
						else user = SystemSession.getUser();
					} else user = SystemSession.getUser();
					proofing.setUser(user);//单据中能获取到申请人的uu号，并且有效则为申请人，否则为企业管理员
					proofing.setUserUU(user.getUserUU());
					proofing.setProduct(products.get(0));
					proofing.setProductId(products.get(0).getId());
					for(PurchaseProofingItem proofingItem : proofing.getProofingItems()) {//对明细遍历
						proofingItem.setProofing(proofing);
						List<Vendor> vendors = vendorDao.findByMyEnUUAndVendUU(enUU, proofingItem.getVendUU());
						Vendor vendor = null;
						if(vendors.size() == 0) {//供应商列表中不存在供应商关系
							vendor = addVendor(proofingItem);//增加供应商关系
						} else {
							vendor = vendors.get(0);
						}
						if(vendor != null) {//供应商uu号不为非法的值
							//如果明细中客户联系人UU号为空或者UU号不存在对应的用户，则设为供应商资料中的供应商联系人（可能为空）
							if(proofingItem.getVendUserUU() != null) {
								List<User> vendUsers = userDao.findByEnUUAndUserUU(vendor.getVendEnUU(), proofingItem.getVendUserUU());
								if(vendUsers.size() == 0) proofingItem.setVendUserUU(vendor.getVendUserUU());
							} else proofingItem.setVendUserUU(vendor.getVendUserUU());
							proofingItems.add(proofingItem);
						}
					}
				}
			}
			
		}
		return proofingItems;
	}

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

	@Override
	public List<PurchaseProofingApproval> convertApproval(
			List<ProductSampleApproval> sampleApprovals) {
		List<PurchaseProofingApproval> proofingApprovals = new ArrayList<PurchaseProofingApproval>();
		for(ProductSampleApproval sampleApproval : sampleApprovals) {
			if(sampleApproval.getPa_venduu() != null){
				List<Vendor> vendors = vendorDao.findByMyEnUUAndVendUU(SystemSession.getUser().getEnterprise().getUu(),
						sampleApproval.getPa_venduu());
				if(vendors.size() > 0) 
					proofingApprovals.add(sampleApproval.convert());
			}
		}
		return proofingApprovals;
	}

}
