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

import com.uas.sso.util.AccountUtils;
import com.uas.platform.b2b.dao.EnterpriseDao;
import com.uas.platform.b2b.dao.SaleARCheckDao;
import com.uas.platform.b2b.dao.SaleARCheckItemDao;
import com.uas.platform.b2b.dao.VendorDao;
import com.uas.platform.b2b.erp.model.ARCheck;
import com.uas.platform.b2b.erp.model.ARCheckReply;
import com.uas.platform.b2b.erp.service.ARCheckService;
import com.uas.platform.b2b.erp.service.VendorService;
import com.uas.platform.b2b.model.*;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.b2b.temporary.model.PartnershipRecord;
import com.uas.platform.core.model.Constant;
import com.uas.platform.core.model.Status;
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;

@Service
public class ARCheckServiceImpl implements ARCheckService {

	@Autowired
	private EnterpriseDao enterpriseDao;

	@Autowired
	private VendorDao vendorDao;

	@Autowired
	private SaleARCheckDao saleARCheckDao;

	@Autowired
	private SaleARCheckItemDao saleARCheckItemDao;

	@Autowired
	private VendorService vendorService;

	@Override
	public List<SaleARCheckItem> convertARChecks(List<ARCheck> arChecks) {
		List<SaleARCheckItem> arCheckItems = new ArrayList<SaleARCheckItem>();
        final String appId = "uas";// 应用来源
		long vendUU = SystemSession.getUser().getEnterprise().getUu();// 卖方UU号
		if (!CollectionUtils.isEmpty(arChecks)) {
			for (ARCheck arCheck : arChecks) {
				Enterprise customer = enterpriseDao.findOne(arCheck.getAc_custuu());
				List<SaleARCheck> existChecks = saleARCheckDao.findByVendUUAndCode(vendUU, arCheck.getAc_code());
				if (customer != null && CollectionUtils.isEmpty(existChecks)) {// 客户UU正确，否则不保存
					List<Vendor> existVendors = vendorDao.findByMyEnUUAndVendUU(arCheck.getAc_custuu(), vendUU);
					if (CollectionUtils.isEmpty(existVendors)) {// 不存在供应商关系，增加供应商关系
						Vendor vendor = addVendor(customer);
						if (null != vendor) {
							vendorService.addPartnerShipRecord(vendor, appId); // 添加合作伙伴关系
						}
					}
					SaleARCheck saleARCheck = arCheck.convert();
					if (!CollectionUtils.isEmpty(saleARCheck.getItems())) {
						for (SaleARCheckItem item : saleARCheck.getItems()) {
							item.setArCheck(saleARCheck);
							item.setErpDate(new Date());
							arCheckItems.add(item);
						}
					}
				}
			}
		}
		return arCheckItems;
	}

	/**
	 * 在UU号正确但两者之间不存在供应商关系时，添加供应商关系
	 * 
	 * @param customer
	 */
	private Vendor addVendor(Enterprise customer) {
		Vendor vendor = new Vendor();
		vendor.setMyEnUU(customer.getUu());
		vendor.setMyUserUU(customer.getEnAdminuu());// 买方联系人为买方企业管理员
		vendor.setVendEnUU(SystemSession.getUser().getEnterprise().getUu());
		// 卖方联系人为卖方企业管理员
		vendor.setVendUserUU(SystemSession.getUser().getEnterprise().getEnAdminuu());
		vendor.setCustswitch(Constant.YES);
		vendor.setVendswitch(Constant.YES);
		return vendorDao.save(vendor);
	}

	@Override
	public List<ARCheckReply> convertReplies(List<SaleARCheckReply> replies) {
		List<ARCheckReply> arCheckReplies = new ArrayList<ARCheckReply>();
		if (!CollectionUtils.isEmpty(replies)) {
			for (SaleARCheckReply reply : replies) {
				arCheckReplies.add(new ARCheckReply(reply));
			}
		}
		return arCheckReplies;
	}

	@Override
	public List<SaleARCheckReply> convertSaleReplies(List<ARCheckReply> replies) {
		List<SaleARCheckReply> saleARCheckReplies = new ArrayList<SaleARCheckReply>();
		if (!CollectionUtils.isEmpty(replies)) {
			for (ARCheckReply reply : replies) {
				List<SaleARCheckItem> items = saleARCheckItemDao.findByArCheckVendUUAndSourceId(
						SystemSession.getUser().getEnterprise().getUu(), reply.getSourceId());
				if (!CollectionUtils.isEmpty(items)) {
					SaleARCheckReply saleARCheckReply = new SaleARCheckReply();
					saleARCheckReply.setItem(items.get(0));
					saleARCheckReply.setItemId(items.get(0).getId());
					saleARCheckReply.setDate(reply.getReplyDate());
					saleARCheckReply.setQty(reply.getReplyQty());
					saleARCheckReply.setRemark(reply.getReplyRemark());
					saleARCheckReply.setSendStatus((short) Status.DOWNLOADED.value());
					saleARCheckReply.setType("v");
					saleARCheckReplies.add(saleARCheckReply);
				}
			}
		}
		return saleARCheckReplies;
	}

}
