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

import com.uas.platform.b2b.dao.EnterpriseDao;
import com.uas.platform.b2b.dao.ProductDao;
import com.uas.platform.b2b.dao.PurchaseApBillDao;
import com.uas.platform.b2b.erp.model.PurchaseAPBill;
import com.uas.platform.b2b.erp.model.PurchaseAPBillDetail;
import com.uas.platform.b2b.erp.service.PurchaseAPBillService;
import com.uas.platform.b2b.model.*;
import com.uas.platform.b2b.support.SystemSession;
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.Iterator;
import java.util.List;

@Service
public class PurchaseAPBillServiceImpl implements PurchaseAPBillService {

	@Autowired
	private PurchaseApBillDao purchaseApBillDao;

	@Autowired
	private ProductDao productDao;

	@Autowired
	private EnterpriseDao enterpriseDao;

	@Override
	public List<PurchaseApBillItem> convertApBills(List<PurchaseAPBill> apBills) {
		List<PurchaseApBillItem> apBillItems = new ArrayList<PurchaseApBillItem>();
		long enUU = SystemSession.getUser().getEnterprise().getUu();
		for (PurchaseAPBill apBill : apBills) {
			List<PurchaseApBill> apBills2 = purchaseApBillDao.findByEnUUAndCode(enUU, apBill.getAb_code());
			Enterprise vendor = enterpriseDao.findEnterpriseByUu(apBill.getAb_vendoruu());
			if (apBills2.size() == 0 && vendor != null) {
				PurchaseApBill purchaseApBill = apBill.convert();
				Iterator<PurchaseApBillItem> items = purchaseApBill.getItems().iterator();
				while (items.hasNext()) {
					PurchaseApBillItem apBillItem = items.next();
					List<Product> products = productDao.findByEnUUAndCode(enUU, apBillItem.getProduct().getCode());
					if (products.size() > 0) {
						apBillItem.setProduct(products.get(0));
						apBillItem.setProductId(products.get(0).getId());
						apBillItem.setaPBill(purchaseApBill);
						apBillItem.setErpDate(new Date());
						apBillItems.add(apBillItem);
					} else {
						items.remove();
					}
				}
			}
		}
		return apBillItems;
	}

	@Override
	public List<PurchaseApBill> convertNonPostingApBills(List<PurchaseAPBill> apBills) {
		List<PurchaseApBill> purchaseApBills = new ArrayList<PurchaseApBill>();
		if (!CollectionUtils.isEmpty(apBills)) {
			Long enUU = SystemSession.getUser().getEnterprise().getUu();
			for (PurchaseAPBill apBill : apBills) {
				List<PurchaseApBill> list = purchaseApBillDao.findByEnUUAndCode(enUU, apBill.getAb_code());
				if (list.size() > 0) {
					purchaseApBills.add(list.get(0));
				}
			}
		}
		return purchaseApBills;
	}

	@Override
	public List<ApbillAdjustment> convertApbillAdjustment(List<PurchaseAPBill> apBills) {
		List<ApbillAdjustment> adjusts = new ArrayList<ApbillAdjustment>();
		if (!CollectionUtils.isEmpty(apBills)) {
			Long enUU = SystemSession.getUser().getEnterprise().getUu();
			for (PurchaseAPBill apBill : apBills) {
				if (!CollectionUtils.isEmpty(apBill.getDetails())) {
					for (PurchaseAPBillDetail detail : apBill.getDetails()) {
						ApbillAdjustment adjust = new ApbillAdjustment(apBill);
						Product product = new Product();
						product.setCode(detail.getAbd_prodcode());
						adjust.setPrid(product.getId());
						adjust.setDetno(detail.getAbd_detno());
						adjust.setProdcode(product.getCode());
						adjust.setQty(detail.getAbd_qty());
						adjust.setThischeckqty(detail.getAbd_thisvoqty());
						adjust.setOrderprice(detail.getAbd_price());
						adjust.setOrdercode(detail.getAbd_ordercode());
						adjust.setOrderdetno(detail.getAbd_orderdetno());
						adjust.setTaxrate(detail.getAbd_taxrate());
						if (detail.getAbd_yqty() != null) {
							adjust.setYcheckqty(detail.getAbd_yqty());
						} else {
							adjust.setYcheckqty((double) 0);
						}
						Enterprise enterprise = enterpriseDao.findEnterpriseByUu(adjust.getEnuu());
						adjust.setReceivename(enterprise.getEnName());
						List<Product> products = productDao.findByEnUUAndCode(enUU, adjust.getProdcode());
						if (!CollectionUtils.isEmpty(products)) {
							Product productInfo = products.get(0);
							adjust.setPrid(productInfo.getId());
							adjust.setProdcode(productInfo.getCode());
							adjust.setProdtitle(productInfo.getTitle());
							adjust.setProdspec(productInfo.getSpec());
						}
						adjusts.add(adjust);
					}
				}
			}

		}
		return adjusts;
	}

	/**
	 * 获取应付发票对应的平台的ID
	 *
	 * @param purchaseAPBills erp应付发票
	 * @return
	 */
	@Override
	public List<PurchaseAPBill> getB2bId(List<PurchaseAPBill> purchaseAPBills) {
	    for (PurchaseAPBill apBill : purchaseAPBills) {
	        List<PurchaseApBill> apBills = purchaseApBillDao.findByEnUUAndErpId(SystemSession.getUser().getEnterprise().getUu(), apBill.getAb_id());
	        if (!CollectionUtils.isEmpty(apBills)) {
	            apBill.setAb_b2bid(apBills.get(0).getId());
            }
        }
		return purchaseAPBills;
	}
}
