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


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

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 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.support.ErpBufferedLogger;
import com.uas.platform.b2b.service.SaleARCheckService;
import com.uas.platform.core.logging.BufferedLoggerManager;
import com.uas.platform.core.util.serializer.FlexJsonUtils;

/**
 * 对卖家ERP的数据接口<br>
 * 应收对账单
 * @author suntg
 * @date 2015年10月13日14:03:16
 *
 */
@Controller
@RequestMapping("/erp/sale/ARCheck")
public class ARCheckController {

	@Autowired
	private SaleARCheckService saleARCheckService;

	@Autowired
	private ARCheckService arCheckService;
	
	private final static ErpBufferedLogger logger = BufferedLoggerManager.getLogger(ErpBufferedLogger.class);

	/**
	 * 保存从卖方ERP传来的应收对账单
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public void save(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<ARCheck> arChecks = FlexJsonUtils.fromJsonArray(jsonStr, ARCheck.class);
		saleARCheckService.save(arCheckService.convertARChecks(arChecks));
		logger.log("应付对账单", "上传客户应付对账单", arChecks.size());
	}

	/**
	 * 获取客户在平台的应付对账单回复
	 * 
	 * @return
	 */
	@RequestMapping(value = "/reply", method = RequestMethod.GET)
	@ResponseBody
	public List<ARCheckReply> getReply() {
		List<ARCheckReply> replies = arCheckService.convertReplies(saleARCheckService.getUploadReply());
		logger.log("应付对账单", "下载应付对账客户回复记录", replies.size());
		return replies;
	}

	/**
	 * 客户回复对账不同意后，供应商在ERP针对客户回复的数量作回复
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/reply", method = RequestMethod.POST)
	@ResponseBody
	public void reply(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<ARCheckReply> replies = FlexJsonUtils.fromJsonArray(jsonStr, ARCheckReply.class);
		saleARCheckService.reply(arCheckService.convertSaleReplies(replies));
		logger.log("应付对账单", "上传供应商回复", replies.size());
	}

	/**
	 * 平台的应付对账单回复传到ERP之后，修改平台里面的回复记录的传输状态为已上传
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/reply/back", method = RequestMethod.POST)
	@ResponseBody
	public void onReplySuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		saleARCheckService.uploadReplySuccess(URLDecoder.decode(data, "UTF-8").split(","));
	}

}