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

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.uas.sso.util.AccountUtils;
import com.uas.platform.b2b.dao.*;
import com.uas.platform.b2b.erp.model.InquiryMould;
import com.uas.platform.b2b.erp.model.InquiryMouldDecide;
import com.uas.platform.b2b.erp.service.InquiryMouldService;
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.core.model.Constant;
import com.uas.platform.core.model.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class InquiryMouldServiceImpl implements InquiryMouldService {

	@Autowired
	private PurchaseInquiryMouldDao purchaseInquiryMouldDao;

	@Autowired
	private EnterpriseDao enterpriseDao;

	@Autowired
	private VendorDao vendorDao;

	@Autowired
	private ProductDao productDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private VendorService vendorService;

	@Override
	public List<PurchaseInquiryMould> convertInquiryMould(List<InquiryMould> inquiries) {
		List<PurchaseInquiryMould> purcMould = new ArrayList<PurchaseInquiryMould>();
		final String appId = "uas";// 应用来源
		long enUU = SystemSession.getUser().getEnterprise().getUu();
		for (InquiryMould inquiry : inquiries) {
			// 先进行判断是否存在
			List<PurchaseInquiryMould> purcMoulds = purchaseInquiryMouldDao.findByEnuuAndCode(enUU, inquiry.getIn_code());
			if (purcMoulds.size() == 0) {// 不存在新增数据
				PurchaseInquiryMould purchaseInquiryMould = inquiry.convert();
				// 设置状态为有效
				Vendor vendor = null;
				List<Vendor> vendors = vendorDao.findByMyEnUUAndVendUU(purchaseInquiryMould.getEnuu(), purchaseInquiryMould.getVenduu());
				if (vendors.size() > 0) {
					vendor = vendors.get(0);
					if (purchaseInquiryMould.getVendUserUU() == null || purchaseInquiryMould.getVendUserUU() == 0) {
						purchaseInquiryMould.setVendUserUU(vendor.getVendUserUU());
					}
					if (purchaseInquiryMould.getUseruu() == null || purchaseInquiryMould.getUseruu() == 0) {
						purchaseInquiryMould.setUseruu(SystemSession.getUser().getUserUU());
					}
				} else {
					vendor = addVendor(purchaseInquiryMould);
					if (null != vendor) {
						vendorService.addPartnerShipRecord(vendor, appId); // 添加合作伙伴关系
					}
				}
				if (vendor != null) {
					for (PurchaseInquiryMouldItem item : purchaseInquiryMould.getInquiryMouldItems()) {
						item.setInquiryMould(purchaseInquiryMould);
						if (!CollectionUtils.isEmpty(item.getInquiryMouldDetails())) {
							for (PurchaseInquiryMouldDetail detail : item.getInquiryMouldDetails()) {
								List<Product> products = productDao.findByEnUUAndCode(enUU, detail.getProdCode());
								if (!CollectionUtils.isEmpty(products)) {
									detail.setProdTitle(products.get(0).getTitle());
									detail.setProdSpec(products.get(0).getSpec());
								}
								detail.setImid(purchaseInquiryMould.getId());
								detail.setInquiryMouldItem(item);
							}
						}
					}
					purcMould.add(purchaseInquiryMould);
				}
			} else {
				if (null != inquiry.getB2b_im_id()) {
					PurchaseInquiryMould mould = purchaseInquiryMouldDao.findOne(inquiry.getB2b_im_id());
					if (mould != null) {
						if (inquiry.getIn_adoptstatus().equals("已采纳")) {
							mould.setAdoptStatus(Constant.YES);
						} else if (inquiry.getIn_adoptstatus().equals("未采纳")) {
							mould.setAdoptStatus(Constant.NO);
						}
						purchaseInquiryMouldDao.save(mould);
					}
				}
			}
		}
		return purcMould;
	}

	@Override
	public List<InquiryMould> convertPurchaseInquiryMouldReply(List<PurchaseInquiryMould> purchaseInquiryMoulds) {
		List<InquiryMould> inquiryMoulds = new ArrayList<InquiryMould>();
		if (!CollectionUtils.isEmpty(purchaseInquiryMoulds)) {
			for (PurchaseInquiryMould mould : purchaseInquiryMoulds) {
				if (mould.getAdoptStatus() == null) {
					inquiryMoulds.add(new InquiryMould(mould));
				}
			}
		}
		return inquiryMoulds;
	}

	@Override
	public List<PurchaseInquiryMould> convertInquiryMouldDecide(List<InquiryMouldDecide> decides) {
		List<PurchaseInquiryMould> inquiryMoulds = new ArrayList<PurchaseInquiryMould>();
		if (!CollectionUtils.isEmpty(decides)) {
			for (InquiryMouldDecide decide : decides) {
				if (decide.getB2b_im_id() != null) {
					PurchaseInquiryMould mould = purchaseInquiryMouldDao.findOne(decide.getB2b_im_id());
					if (mould != null) {
						if (decide.getIn_adoptstatus().equals("已采纳")) {
							mould.setAdoptStatus(Constant.YES);
						} else if (decide.getIn_adoptstatus().equals("未采纳")) {
							mould.setAdoptStatus(Constant.NO);
						}
						mould.setDecideStatus((short) Status.NOT_UPLOAD.value());
						inquiryMoulds.add(mould);
					}
				}
			}
		}
		return inquiryMoulds;
	}

	/**
	 * 添加供应商资料
	 * 
	 * @param purchaseInquiryMould
	 * @return
	 */
	private Vendor addVendor(PurchaseInquiryMould purchaseInquiryMould) {
		Vendor vendor = new Vendor();
		vendor.setMyEnUU(SystemSession.getUser().getEnterprise().getUu());
		vendor.setMyEnterprise(SystemSession.getUser().getEnterprise());
		vendor.setCustswitch(Constant.YES);
		vendor.setVendswitch(Constant.YES);
		if (purchaseInquiryMould.getUseruu() != null && purchaseInquiryMould.getUseruu() != 0) {
			vendor.setMyUserUU(purchaseInquiryMould.getUseruu());
		} else {
			vendor.setMyUserUU(SystemSession.getUser().getUserUU());
		}
		Enterprise vendEnterprise = enterpriseDao.findOne(purchaseInquiryMould.getVenduu());
		if (vendEnterprise != null) {
			vendor.setVendorEnterprise(vendEnterprise);
			vendor.setVendEnUU(purchaseInquiryMould.getVenduu());
			if (purchaseInquiryMould.getVendUserUU() != null) {
				List<User> users = userDao.findByEnUUAndUserUU(vendEnterprise.getUu(), purchaseInquiryMould.getVendUserUU());
				if (users.size() > 0) {
					vendor.setVendUserUU(purchaseInquiryMould.getVendUserUU());
				} else {
					vendor.setVendUserUU(vendEnterprise.getEnAdminuu());
					purchaseInquiryMould.setVendUserUU(vendEnterprise.getEnAdminuu());
				}
			} else {
				vendor.setVendUserUU(vendEnterprise.getEnAdminuu());
				purchaseInquiryMould.setVendUserUU(vendEnterprise.getEnAdminuu());
			}
			return vendorDao.save(vendor);
		}
		return null;
	}
}
