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

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

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.PurchaseAcceptDao;
import com.uas.platform.b2b.dao.PurchaseBadInDao;
import com.uas.platform.b2b.dao.PurchaseBadInItemDao;
import com.uas.platform.b2b.dao.PurchaseBadOutDao;
import com.uas.platform.b2b.dao.PurchaseBadOutItemDao;
import com.uas.platform.b2b.dao.PurchaseOrderDao;
import com.uas.platform.b2b.dao.PurchaseOrderItemDao;
import com.uas.platform.b2b.dao.PurchaseReturnDao;
import com.uas.platform.b2b.dao.PurchaseReturnItemDao;
import com.uas.platform.b2b.erp.model.PurchaseProdInOut;
import com.uas.platform.b2b.erp.service.PurchaseProdInOutService;
import com.uas.platform.b2b.model.Enterprise;
import com.uas.platform.b2b.model.PurchaseAccept;
import com.uas.platform.b2b.model.PurchaseAcceptItem;
import com.uas.platform.b2b.model.PurchaseBadIn;
import com.uas.platform.b2b.model.PurchaseBadInItem;
import com.uas.platform.b2b.model.PurchaseBadOut;
import com.uas.platform.b2b.model.PurchaseBadOutItem;
import com.uas.platform.b2b.model.PurchaseOrderItem;
import com.uas.platform.b2b.model.PurchaseReturn;
import com.uas.platform.b2b.model.PurchaseReturnItem;
import com.uas.platform.b2b.support.SystemSession;

@Service
public class PurchaseProdInOutServiceImpl implements PurchaseProdInOutService{

	@Autowired
	private PurchaseOrderDao purchaseOrderDao;
	
	@Autowired
	private PurchaseOrderItemDao purchaseOrderItemDao;
	
	@Autowired
	private PurchaseAcceptDao purchaseAcceptDao;
	
	@Autowired
	private PurchaseReturnDao purchaseReturnDao;
	
	@Autowired
	private PurchaseReturnItemDao purchaseReturnItemDao;
	
	@Autowired
	private PurchaseBadInDao purchaseBadInDao;
	
	@Autowired
	private PurchaseBadInItemDao purchaseBadInItemDao;

	@Autowired
	private PurchaseBadOutDao purchaseBadOutDao;
	
	@Autowired
	private PurchaseBadOutItemDao purchaseBadOutItemDao;
	
	@Autowired
	private EnterpriseDao enterpriseDao;
	
	@Override
	public List<PurchaseAcceptItem> convertProdInOuts(
			List<PurchaseProdInOut> prodInOuts) {
		List<PurchaseAcceptItem> acceptItems = new ArrayList<PurchaseAcceptItem>();
		long enUU = SystemSession.getUser().getEnterprise().getUu();
		for(PurchaseProdInOut prodInOut : prodInOuts) {
			PurchaseAccept accept = prodInOut.convent();
			List<PurchaseAccept> accepts2 = purchaseAcceptDao.findPurchaseAcceptByEnUUAndCode(enUU, accept.getCode());
			Enterprise vendor = enterpriseDao.findEnterpriseByUu(accept.getVendUU());
			//采购验收单不存在，供应商UU号存在对应供应商
			if(accepts2.size() == 0 && vendor != null) {
				for(PurchaseAcceptItem item : accept.getAcceptItems()) {//设置明细对应的采购单明细
					List<PurchaseOrderItem> purchaseOrderItems = purchaseOrderItemDao.findByEnUUAndOrderCodeAndNumber(enUU, 
							item.getOrderItem().getOrder().getCode(), item.getOrderItem().getNumber());
					if(purchaseOrderItems.size() > 0) {//对应的采购单存在
						item.setOrderItem(purchaseOrderItems.get(0));//对应的采购单明细set进去
						item.setOrderItemId(purchaseOrderItems.get(0).getId());
						item.setAccept(accept);//设置主记录
						acceptItems.add(item);
					}
				}
			}
		}
		return acceptItems;
	}

	@Override
	public List<PurchaseReturnItem> convertReturns(
			List<PurchaseProdInOut> prodInOuts) {
		List<PurchaseReturnItem> returnItems = new ArrayList<PurchaseReturnItem>();
		long enUU = SystemSession.getUser().getEnterprise().getUu();
		for(PurchaseProdInOut prodInOut : prodInOuts) {
			PurchaseReturn returnn = prodInOut.conventToReturn();
			List<PurchaseReturn> returns2 = purchaseReturnDao.findPurchaseReturnByEnUUAndCode(enUU, returnn.getCode());
			Enterprise vendor = enterpriseDao.findEnterpriseByUu(returnn.getVendUU());
			//采购验收单不存在，供应商UU号存在对应供应商
			if(returns2.size() == 0 && vendor != null) {
				for(PurchaseReturnItem returnItem : returnn.getReturnItems()) {//设置明细对应的采购单明细
					List<PurchaseOrderItem> purchaseOrderItems = purchaseOrderItemDao.findByEnUUAndOrderCodeAndNumber(enUU, 
							returnItem.getOrderItem().getOrder().getCode(), returnItem.getOrderItem().getNumber());
					if(purchaseOrderItems.size() > 0) {//对应的采购单存在
						returnItem.setOrderItem(purchaseOrderItems.get(0));//对应的采购单明细set进去
						returnItem.setOrderItemId(purchaseOrderItems.get(0).getId());
						returnItem.setPurchaseReturn(returnn);//设置主记录
						returnItems.add(returnItem);
					}
				}
			}
		}
		return returnItems;
	}
	
	@Override
	public List<PurchaseBadInItem> convertBadIns(
			List<PurchaseProdInOut> prodInOuts) {
		List<PurchaseBadInItem> badInItems = new ArrayList<PurchaseBadInItem>();
		long enUU = SystemSession.getUser().getEnterprise().getUu();
		for(PurchaseProdInOut prodInOut : prodInOuts) {
			PurchaseBadIn badIn = prodInOut.conventToBadIn();
			List<PurchaseBadIn> badIns = purchaseBadInDao.findPurchaseBadInByEnUUAndCode(enUU, badIn.getCode());
			Enterprise vendor = enterpriseDao.findEnterpriseByUu(badIn.getVendUU());
			//采购验收单不存在，供应商UU号存在对应供应商
			if(badIns.size() == 0 && vendor != null) {
				for(PurchaseBadInItem badInItem : badIn.getBadInItems()) {//设置明细对应的采购单明细
					List<PurchaseOrderItem> purchaseOrderItems = purchaseOrderItemDao.findByEnUUAndOrderCodeAndNumber(enUU, 
							badInItem.getOrderItem().getOrder().getCode(), badInItem.getOrderItem().getNumber());
					if(purchaseOrderItems.size() > 0) {//对应的采购单存在
						badInItem.setOrderItem(purchaseOrderItems.get(0));//对应的采购单明细set进去
						badInItem.setOrderItemId(purchaseOrderItems.get(0).getId());
						badInItem.setBadIn(badIn);//设置主记录
						badInItems.add(badInItem);
					}
				}
			}
		}
		return badInItems;
	}

	@Override
	public List<PurchaseBadOutItem> convertBadOuts(
			List<PurchaseProdInOut> prodInOuts) {
		List<PurchaseBadOutItem> badOutItems = new ArrayList<PurchaseBadOutItem>();
		long enUU = SystemSession.getUser().getEnterprise().getUu();
		for(PurchaseProdInOut prodInOut : prodInOuts) {
			PurchaseBadOut badOut = prodInOut.conventToBadOut();
			List<PurchaseBadOut> badOuts = purchaseBadOutDao.findPurchaseBadOutByEnUUAndCode(enUU, badOut.getCode());
			Enterprise vendor = enterpriseDao.findEnterpriseByUu(badOut.getVendUU());
			//不良品出库单不存在，供应商UU号存在对应供应商
			if(badOuts.size() == 0 && vendor != null) {
				for(PurchaseBadOutItem badOutItem : badOut.getBadOutItems()) {//设置明细对应的采购单明细
					List<PurchaseOrderItem> purchaseOrderItems = purchaseOrderItemDao.findByEnUUAndOrderCodeAndNumber(enUU, 
							badOutItem.getOrderItem().getOrder().getCode(), badOutItem.getOrderItem().getNumber());
					if(purchaseOrderItems.size() > 0) {//对应的采购单存在
						badOutItem.setOrderItem(purchaseOrderItems.get(0));//对应的采购单明细set进去
						badOutItem.setOrderItemId(purchaseOrderItems.get(0).getId());
						badOutItem.setBadOut(badOut);//设置主记录
						badOutItems.add(badOutItem);
					}
				}
			}
		}
		return badOutItems;
	}

}
