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.ProductDao;
import com.uas.platform.b2b.dao.PurchaseInquiryDao;
import com.uas.platform.b2b.dao.PurchaseInquiryItemDao;
import com.uas.platform.b2b.dao.SaleQuotationItemDao;
import com.uas.platform.b2b.dao.VendorDao;
import com.uas.platform.b2b.erp.model.Inquiry;
import com.uas.platform.b2b.erp.model.InquiryDecide;
import com.uas.platform.b2b.erp.model.InquiryDetail;
import com.uas.platform.b2b.erp.service.InquiryService;
import com.uas.platform.b2b.model.Product;
import com.uas.platform.b2b.model.PurchaseInquiry;
import com.uas.platform.b2b.model.PurchaseInquiryItem;
import com.uas.platform.b2b.model.SaleQuotation;
import com.uas.platform.b2b.model.SaleQuotationItem;
import com.uas.platform.b2b.model.Vendor;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.core.model.Status;

@Service
public class InquiryServiceImpl implements InquiryService {

	@Autowired
	private ProductDao productDao;

	@Autowired
	private VendorDao vendorDao;

	@Autowired
	private PurchaseInquiryDao purchaseInquiryDao;

	@Autowired
	private PurchaseInquiryItemDao purchaseInquiryItemDao;

	@Autowired
	private SaleQuotationItemDao saleQuotationItemDao;

	@Override
	public List<PurchaseInquiryItem> convertInquiry(List<Inquiry> inquiries) {
		List<PurchaseInquiryItem> inquiryItems = new ArrayList<PurchaseInquiryItem>();
		long enUU = SystemSession.getUser().getEnterprise().getUu();
		for (Inquiry inquiry : inquiries) {
			// 先查看是否已存在
			List<PurchaseInquiry> inquiries2 = purchaseInquiryDao.findByEnUUAndCode(enUU, inquiry.getIn_code());
			if (inquiries2.size() == 0) {// 不存在才继续，存在不作操作
				PurchaseInquiry purchaseInquiry = inquiry.convert();
				if (!CollectionUtils.isEmpty(purchaseInquiry.getInquiryItems())) {
					for (PurchaseInquiryItem item : purchaseInquiry.getInquiryItems()) {
						// 如果询价单明细行没有指定供应商联系人，取平台中供应商资料中的联系人
						if (item.getVendUserUU() == null || item.getVendUserUU() == 0) {
							List<Vendor> vendors = vendorDao.findByMyEnUUAndVendUU(purchaseInquiry.getEnUU(), item.getVendUU());
							if (vendors.size() > 0)
								item.setVendUserUU(vendors.get(0).getVendUserUU());
						}
						item.setInquiry(purchaseInquiry);
						// 取平台的产品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());
						}
					}
					inquiryItems.addAll(purchaseInquiry.getInquiryItems());
				}
			}
		}
		return inquiryItems;
	}

	@Override
	public List<InquiryDetail> convertPurchaseInquiryReply(List<PurchaseInquiryItem> inquiryItems) {
		List<InquiryDetail> details = new ArrayList<InquiryDetail>();
		if (!CollectionUtils.isEmpty(inquiryItems)) {
			for (PurchaseInquiryItem item : inquiryItems) {
				details.add(new InquiryDetail(item));
			}
		}
		return details;
	}

	@Override
	public List<PurchaseInquiryItem> convertInquiryDecide(List<InquiryDecide> decides) {
		List<PurchaseInquiryItem> inquiryItems = new ArrayList<PurchaseInquiryItem>();
		if (!CollectionUtils.isEmpty(decides)) {
			long enUU = SystemSession.getUser().getEnterprise().getUu();
			for (InquiryDecide decide : decides) {
				List<PurchaseInquiryItem> items = purchaseInquiryItemDao.findByEnUUAndCodeAndNumber(enUU, decide.getIn_code(),
						decide.getId_detno());
				if (!CollectionUtils.isEmpty(items)) {
					PurchaseInquiryItem item = items.get(0);
					item.setAgreed(decide.getId_agreed());
					item.setDecideStatus((short) Status.NOT_UPLOAD.value());
					inquiryItems.add(item);
				}
			}
		}
		return inquiryItems;
	}

	@Override
	public List<Inquiry> convertSaleQuotation(List<SaleQuotation> quotations) {
		List<Inquiry> inquiries = new ArrayList<Inquiry>();
		if (!CollectionUtils.isEmpty(quotations)) {
			for (SaleQuotation quotation : quotations) {
				inquiries.add(new Inquiry(quotation));
			}
		}
		return inquiries;
	}

	@Override
	public List<SaleQuotationItem> convertQuotationDecide(List<InquiryDecide> decides) {
		List<SaleQuotationItem> quotationItems = new ArrayList<SaleQuotationItem>();
		if (!CollectionUtils.isEmpty(decides)) {
			for (InquiryDecide decide : decides) {
				List<SaleQuotationItem> items = saleQuotationItemDao
						.findByQuotationIdAndNumber(decide.getB2b_qu_id(), decide.getId_detno());
				if (!CollectionUtils.isEmpty(items)) {
					SaleQuotationItem item = items.get(0);
					item.setAgreed(decide.getId_agreed());
					item.setDecideStatus((short) Status.NOT_UPLOAD.value());
					quotationItems.add(item);
				}
			}
		}
		return quotationItems;
	}

}
