package com.uas.platform.b2b.erp.controller;

import com.uas.platform.b2b.dao.EnterpriseDao;
import com.uas.platform.b2b.dao.VendorDao;
import com.uas.platform.b2b.erp.service.VendorService;
import com.uas.platform.b2b.model.Enterprise;
import com.uas.platform.b2b.model.Vendor;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.b2b.support.UsageBufferedLogger;
import com.uas.platform.core.logging.BufferedLoggerManager;
import com.uas.platform.core.model.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/erp/relationship")
public class RelationshipController {

	@Autowired
	private VendorDao vendorDao;
	@Autowired
	private EnterpriseDao enterpriseDao;

	@Autowired
	private VendorService vendorService;

	private final static UsageBufferedLogger logger = BufferedLoggerManager.getLogger(UsageBufferedLogger.class);

	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	@ResponseStatus(value = HttpStatus.OK)
	public Map<String, Object> relationship(Long otheruu) throws UnsupportedEncodingException {
		Map<String, Object> infos = new HashMap<String, Object>();
		boolean ok = true;
		String error = null;
		long enUU = SystemSession.getUser().getEnterprise().getUu();
        final String appId = "uas";// 应用来源
		if (!StringUtils.isEmpty(otheruu)) {
			Enterprise enterprise = enterpriseDao.findEnterpriseByUu(otheruu);
			if (enterprise != null) {
				List<Vendor> vendors = vendorDao.findByMyEnUUAndVendUU(otheruu, enUU);
				if (vendors.size() == 0) {// 供应商资料在平台上不存在
					Vendor vendor = addVendor(otheruu, enUU);// 添加企业的供应商资料
					// 添加企业圈合作伙伴资料
					if (null != vendor) {
						vendorService.addPartnerShipRecord(vendor, appId);
					}
				}
			} else {
				ok = false;
				error = "企业UU号错误，请联系管理员!";
			}
		}
		infos.put("ok", ok);
		infos.put("error", error);
		return infos;
	}

	private Vendor addVendor(Long enuu, Long venduu) {
		Vendor vendor = new Vendor();
		vendor.setMyEnUU(enuu);
		Enterprise enEnterprise = enterpriseDao.findOne(enuu);
		vendor.setMyEnterprise(enEnterprise);
		vendor.setMyUserUU(enEnterprise.getEnAdminuu());
		Enterprise vendEnterprise = enterpriseDao.findOne(enuu);
		vendor.setVendEnUU(venduu);
		vendor.setVendorEnterprise(vendEnterprise);
		vendor.setVendUserUU(vendEnterprise.getEnAdminuu());
		vendor.setVendswitch(Constant.YES);
		vendor.setCustswitch(Constant.YES);
		logger.log("添加客户信息", "来自接口【RelationshipController】,客户UU" + enuu);
		return vendorDao.save(vendor);
	}
}
