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.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.support.SystemSession;
import com.uas.platform.core.model.Status;

@Service
public class InquiryServiceImpl implements InquiryService {

	@Autowired
	private ProductDao productDao;

	@Autowired
	private PurchaseInquiryDao purchaseInquiryDao;

	@Autowired
	private PurchaseInquiryItemDao purchaseInquiryItemDao;

	@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()) {
						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;
	}

}
