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

import com.uas.platform.b2b.core.util.StringUtil;
import com.uas.platform.b2b.dao.*;
import com.uas.platform.b2b.erp.exception.ExceptionNote;
import com.uas.platform.b2b.erp.model.PurchaseChange;
import com.uas.platform.b2b.erp.model.PurchaseChangeReply;
import com.uas.platform.b2b.erp.service.PurchaseChangeService;
import com.uas.platform.b2b.model.*;
import com.uas.platform.b2b.service.NotExistProductService;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.b2b.temporary.model.*;
import com.uas.platform.b2b.temporary.model.OrderType;
import com.uas.platform.core.model.Constant;
import org.apache.axis.utils.StringUtils;
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;

/**
 * 采购变更新增变更物料信息判断
 *
 * @author hejq
 * @date 2018-07-05 17:43
 */
@Service
public class PurchaseChangeServiceImpl implements PurchaseChangeService {

	@Autowired
	private PurchaseOrderDao purchaseOrderDao;

	@Autowired
	private PurchaseOrderItemDao purchaseOrderItemDao;

	@Autowired
	private ProductDao productDao;

	@Autowired
	private PurchaseOrderChangeDao purchaseOrderChangeDao;

	@Autowired
	private PurchaseOrderChangeItemDao purchaseOrderChangeItemDao;

	@Autowired
    private NotExistProductService productService;

	@Override
	public List<PurchaseOrderChangeItem> convertPurchaseChange(List<PurchaseChange> changes) {
		List<PurchaseOrderChangeItem> changeItems = new ArrayList<PurchaseOrderChangeItem>();
		long enUU = SystemSession.getUser().getEnterprise().getUu();
		for (PurchaseChange change : changes) {
			PurchaseOrderChange orderChange = change.convert();
			List<PurchaseOrder> orders = purchaseOrderDao.findByEnUUAndCode(enUU, change.getPc_purccode());
			List<PurchaseOrderChange> orderChanges = purchaseOrderChangeDao.findByEnUUAndCode(enUU,
					change.getPc_code());
			// 原采购单需存在，变更单不存在
			if (orders.size() > 0 && orderChanges.size() == 0) {
				// 赋值原单据信息字段
				convertOrderInfo(orderChange, orders.get(0));
				if (!CollectionUtils.isEmpty(orderChange.getOrderChangeItems())) {
				    List<NotExistProduct> productList = new ArrayList<>();
				    String uuid = "";
					for (PurchaseOrderChangeItem item : orderChange.getOrderChangeItems()) {
						item.setErpDate(new Date());
						// 取订单明细ID
						List<PurchaseOrderItem> orderItems = purchaseOrderItemDao.findByEnUUAndOrderCodeAndNumber(enUU,
								orderChange.getOrderCode(), item.getOrderItemNumber());
						if (orderItems.size() > 0) {
							// 赋值原单据信息字段
							convertOrderItemInfo(item, orderItems.get(0));
						}
						// 取平台的产品ID
						Product newProduct = item.getNewProduct();
						List<Product> products = productDao.findByEnUUAndCode(newProduct.getEnUU(),
								newProduct.getCode());
						if (products.size() > 0) {
							item.setNewProduct(products.get(0));
							item.setNewProductId(products.get(0).getId());
						} else {
						    if (StringUtils.isEmpty(uuid)) {
						        uuid = StringUtil.uuid();
                            }
                            productList.add(new NotExistProduct(newProduct.getCode(), enUU, OrderType.purcorderChange.name(),
                                    change.getPc_code(), Constant.NO, uuid));
                        }
						item.setOrderChange(orderChange);
					}
					if (!CollectionUtils.isEmpty(productList)) {
                        productService.batchSave(productList, ExceptionNote.PURC_CHANGE_PRODUCT_NOTFOUND.getPhrase(), uuid);
                    }
					changeItems.addAll(orderChange.getOrderChangeItems());
				}
			}
		}
		return changeItems;
	}

	@Override
	public List<PurchaseChangeReply> convertPurchaseOrderChangeReply(List<PurchaseOrderChange> changes) {
		List<PurchaseChangeReply> replies = new ArrayList<PurchaseChangeReply>();
		for (PurchaseOrderChange change : changes) {
            replies.add(new PurchaseChangeReply(change));
        }
		return replies;
	}

	/**
	 * 向变更单赋值原单据信息字段
	 * 
	 * @param change
	 * @param order
	 */
	private void convertOrderInfo(PurchaseOrderChange change, PurchaseOrder order) {
		change.setEnUU(order.getEnUU());
		change.setUserUU(order.getUserUU());
		change.setVendUU(order.getVendUU());
		change.setVendUserUU(order.getVendUserUU());
		change.setOrderId(order.getId());
		change.setOldCurrency(order.getCurrency());
		change.setOldPayments(order.getPayments());
		change.setOldRate(order.getRate());
		change.setOrderCode(order.getCode());
	}

	/**
	 * 向变更单明细赋值原单据信息字段
	 * 
	 * @param changeItem
	 * @param orderItem
	 */
	private void convertOrderItemInfo(PurchaseOrderChangeItem changeItem, PurchaseOrderItem orderItem) {
		changeItem.setOldDelivery(orderItem.getDelivery());
		changeItem.setOldPrice(orderItem.getPrice());
		changeItem.setOldProductId(orderItem.getProductId());
		changeItem.setOldProduct(orderItem.getProduct());
		changeItem.setOldQty(orderItem.getQty());
		changeItem.setOldTaxrate(orderItem.getTaxrate());
		changeItem.setOrderItemId(orderItem.getId());
		changeItem.setOrderItemNumber(orderItem.getNumber());
	}

	@Override
	public Long getChangeCount(Long uu) {
		return purchaseOrderChangeDao.getChangeCountByEnUU(uu);
	}

	@Override
	public Long getChangeItemCount(Long uu) {
		return purchaseOrderChangeItemDao.getChangeItemByEnUU(uu);
	}

}
