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

import com.alibaba.fastjson.JSONArray;
import com.uas.platform.b2b.dao.VendorDao;
import com.uas.platform.b2b.erp.model.InquiryVendorInfo;
import com.uas.platform.b2b.erp.model.VendorInfo;
import com.uas.platform.b2b.erp.model.VendorRate;
import com.uas.platform.b2b.erp.service.VendorService;
import com.uas.platform.b2b.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.model.Vendor;
import com.uas.platform.core.logging.BufferedLoggerManager;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/erp/vendor")
public class VendorRateController {

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

	@Autowired
	private VendorDao vendorDao;

	@Autowired
	private VendorService vendorService;

	/**
	 * 上传供应商费率
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/rate", method = RequestMethod.POST)
	@ResponseBody
	public void uploadVendorRate(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<VendorRate> rates = JSONArray.parseArray(jsonStr, VendorRate.class);
		logger.log("供应商", "更改供应商税率", rates.size());
		if(!CollectionUtils.isEmpty(rates)){
			for (VendorRate rate : rates) {
				List<Vendor> vendors = vendorDao.findByMyEnUUAndVendUU(rate.getCustuu(), rate.getVenduu());
				if (!CollectionUtils.isEmpty(vendors)) {
					for (Vendor vendor : vendors) {
						vendor.setRate(rate.getRate());
						vendorDao.save(vendor);
					}
				} else {// 如果没有，则插入一条关系
					Vendor vend = new Vendor();
					vend.setMyEnUU(rate.getCustuu());
					vend.setVendEnUU(rate.getVenduu());
					vend.setRate(rate.getRate());
					vendorDao.save(vend);
				}
			}
		}
	}

	/**
	 * 判断是否启用b2b对账功能
	 * 
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/apcheck", method = RequestMethod.POST)
	@ResponseBody
	public void uploadB2BApcheck(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<VendorInfo> infos = JSONArray.parseArray(jsonStr, VendorInfo.class);
		logger.log("供应商资料", "设置是否启用平台对账功能", infos.size());
		if (!CollectionUtils.isEmpty(infos)) {
			for (VendorInfo info : infos) {
				List<Vendor> vendors = vendorDao.findByMyEnUUAndVendUU(info.getCustuu(), info.getVenduu());
				if (!CollectionUtils.isEmpty(vendors)) {
					for (Vendor vendor : vendors) {
						vendor.setApcheck((short) Math.abs(info.getApcheck()));
						vendorDao.save(vendor);
						if(vendor.getApcheck() == 0) {
							logger.log("供应商资料", "关闭平台对账功能:" + vendor.getId(),   1);
						} else {
							logger.log("供应商资料", "开启平台对账功能:" + vendor.getId(), 1);
						}
					}
				}
			}
		}
	}


	/**
	 * 同步erp的未注册平台的供应商进行邀请注册
	 *
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/invite", method = RequestMethod.POST)
	@ResponseBody
	public void uploadVendorInfo(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<InquiryVendorInfo> infos = JSONArray.parseArray(jsonStr, InquiryVendorInfo.class);
		vendorService.saveInvitation(infos);
		logger.log("供应商资料", "邀请供应商开通平台", infos.size());
	}

	/**
	 * 获取未同步UU号到ERP的供应商信息
	 *
	 * @return
	 */
	@RequestMapping(value = "/downloadUU", method = RequestMethod.GET)
	@ResponseBody
	public List<InquiryVendorInfo> getVendUU() {
		return vendorService.getInfo();
	}

	/**
	 * erp更新供应商uu后回传到平台更新下载状态
	 *
	 * @param data
	 */
	@RequestMapping(value = "/check", method = RequestMethod.POST)
	@ResponseBody
	public void checkSuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		vendorService.updateStatus(URLDecoder.decode(data, "UTF-8"));
	}
}
