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

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

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.SaleSampleApproval;
import com.uas.platform.b2b.erp.model.SaleSampleDown;
import com.uas.platform.b2b.erp.model.SaleSampleSend;
import com.uas.platform.b2b.erp.service.SaleSampleDownService;
import com.uas.platform.b2b.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.model.Attach;
import com.uas.platform.b2b.model.FileUpload;
import com.uas.platform.b2b.service.AttachService;
import com.uas.platform.b2b.service.PurchaseProofingService;
import com.uas.platform.core.logging.BufferedLoggerManager;
import com.uas.platform.core.util.serializer.FlexJsonUtils;

/**
 * 对卖家ERP的数据接口<br>
 * 客户打样申请
 * 
 * @author suntg
 *
 */
@Controller
@RequestMapping("/erp/sale/sample")
public class SaleSampleDownController {

	@Autowired
	private SaleSampleDownService saleSampleDownService;

	@Autowired
	private PurchaseProofingService purchaseProofingService;

	@Autowired
	private AttachService attachService;

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

	/**
	 * 卖家ERP从平台获取客户送货提醒
	 * 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<SaleSampleDown> getSamples() {
		List<SaleSampleDown> downs = saleSampleDownService.convertPurchaseProofingItems(purchaseProofingService.findNotUploadProofing());
		logger.log("客户送货提醒", "下载客户送货提醒", downs.size());
		return downs;
	}
	
	/**
	 * 平台的客户打样申请传到供应商ERP之后，修改平台里面的客户打样申请的上传状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public void onSampleDownSuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		purchaseProofingService.uploadProofingSuccess(URLDecoder.decode(data, "UTF-8").split(","));
	}
	
	/**
	 * 将卖家ERP的送样单写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/sampleSend", method = RequestMethod.POST)
	@ResponseBody
	public void saveSampleSend(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<SaleSampleSend> sampleSends = FlexJsonUtils.fromJsonArray(jsonStr, SaleSampleSend.class);
		purchaseProofingService.send(saleSampleDownService.convertProofingSends(sampleSends));
		logger.log("送样单", "上传送样单", sampleSends.size());
	}

	/**
	 * 卖家ERP从平台获取在平台送样的送样单
	 * 
	 * @return
	 */
	@RequestMapping(value = "/sampleSend", method = RequestMethod.GET)
	@ResponseBody
	public List<SaleSampleSend> getSampleSend() {
		List<SaleSampleSend> sampleSends = saleSampleDownService.convertSaleSampleSends(purchaseProofingService.findNotBackSampleSend());
		logger.log("送样单", "下载送样单", sampleSends.size());
		return sampleSends;
	}

	/**
	 * 平台的送样单传到供应商ERP之后，修改平台里面的送样单的上传状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/sampleSend/back", method = RequestMethod.POST)
	@ResponseBody
	public void onSampleSendDownSuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		purchaseProofingService.backSampleSendSuccess(URLDecoder.decode(data, "UTF-8").split(","));
	}
	
	/**
	 * 卖家ERP的送样单附件
	 * 
	 * @return
	 */
	@RequestMapping(value = "/sampleSend/attach", method = RequestMethod.POST)
	@ResponseBody
	public void uploadApprovalPpdAttach(String data, FileUpload uploadItem) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		Map<String, Map<String, Object>> fileList = FlexJsonUtils.fromJson(jsonStr);
		List<Attach> attachs = attachService.uploadZipAndSave(uploadItem, "purchaseProofing", "送样单附件", fileList);
		purchaseProofingService.saveSendAttach(attachs);
		logger.log("送样单附件", "上传送样单附件", attachs.size());
	}
	
	/**
	 * 卖家ERP从平台获取在平台的送样认定单
	 * 
	 * @return
	 */
	@RequestMapping(value = "/sampleApproval", method = RequestMethod.GET)
	@ResponseBody
	public List<SaleSampleApproval> getSampleApproval() {
		List<SaleSampleApproval> approvals = saleSampleDownService.covertSampleApprovals(purchaseProofingService.findNotUploadApprovals());
		logger.log("送样认定单", "下载送样认定单", approvals.size());
		return approvals;
	}
	

	/**
	 * 平台的送样认定单传到供应商ERP之后，修改平台里面的送样认定单的上传状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/sampleApproval", method = RequestMethod.POST)
	@ResponseBody
	public void onSampleApprovalDownSuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		purchaseProofingService.uploadApprovalsSuccess(URLDecoder.decode(data, "UTF-8").split(","));
	}

}
