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

import com.uas.platform.b2b.erp.model.ProductSample;
import com.uas.platform.b2b.erp.model.ProductSampleApproval;
import com.uas.platform.b2b.erp.model.SaleSampleSend;
import com.uas.platform.b2b.erp.service.PurchaseSampleService;
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;
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.List;
import java.util.Map;

/**
 * 对买家ERP的数据接口<br>
 * 打样申请处理
 * 
 * @author yingp
 * 
 */
@Controller
@RequestMapping("/erp/purchase/sample")
public class ProductSampleController {

	@Autowired
	private AttachService attachService;
	
	@Autowired
	private PurchaseSampleService purchaseSampleService;
	
	@Autowired
	private PurchaseProofingService purchaseProofingService;
	
	@Autowired
	private SaleSampleDownService saleSampleDownService;

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

	/**
	 * 将买家ERP的打样申请写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public List<ProductSample> saveProductSamples(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<ProductSample> productSamples = FlexJsonUtils.fromJsonArray(jsonStr, ProductSample.class);
		purchaseProofingService.save(purchaseSampleService.convertProofing(productSamples));
		logger.log("打样申请", "买家上传打样申请", productSamples.size());
		return purchaseSampleService.getB2bId(productSamples);
	}

	/**
	 * 买家ERP的打样申请附件
	 * 
	 * @return
	 */
	@RequestMapping(value = "/attach", method = RequestMethod.POST)
	@ResponseBody
	public void uploadAttach(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.saveAttach(attachs);
		logger.log("打样申请", "买家上传打样申请附件", attachs.size());
	}

	/**
	 * 买家ERP的认定单附件
	 * 
	 * @return
	 */
	@RequestMapping(value = "/approval/attach", method = RequestMethod.POST)
	@ResponseBody
	public void uploadApprovalAttach(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, "purchaseApproval", "客户送样认定单附件", fileList);
		purchaseProofingService.saveApprovalAttach(attachs);
		logger.log("认定单", "买家上传认定单附件", attachs.size());
	}

	/**
	 * 买家ERP的认定单prd附件
	 * 
	 * @return
	 */
	@RequestMapping(value = "/approval/prdattach", method = RequestMethod.POST)
	@ResponseBody
	public void uploadApprovalPrdAttach(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, "purchaseApproval", "客户送样认定单附件", fileList);
		purchaseProofingService.saveApprovalPrdattach(attachs);
		logger.log("认定单", "买家上传认定单prd附件", attachs.size());
	}

	/**
	 * 买家ERP的认定单pad附件
	 * 
	 * @return
	 */
	@RequestMapping(value = "/approval/padattach", method = RequestMethod.POST)
	@ResponseBody
	public void uploadApprovalPadAttach(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, "purchaseApproval", "客户送样认定单附件", fileList);
		purchaseProofingService.saveApprovalPadattach(attachs);
		logger.log("认定单", "买家上传认定单pad附件", attachs.size());
	}

	/**
	 * 买家ERP的认定单ppd附件
	 * 
	 * @return
	 */
	@RequestMapping(value = "/approval/ppdattach", 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, "purchaseApproval", "客户送样认定单附件", fileList);
		purchaseProofingService.saveApprovalPpdattach(attachs);
		logger.log("认定单", "买家上传认定单ppd附件", attachs.size());
	}
	

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

	/**
	 * 平台的送样单传到供应商ERP之后，修改平台里面的送样单的上传状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/sampleSend/back", method = RequestMethod.POST)
	@ResponseBody
	public void onSampleSendDownSuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		purchaseProofingService.sendSampleSendSuccess(URLDecoder.decode(data, "UTF-8").split(","));
	}
	
	/**
	 * 将买家ERP的认定单写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/approval", method = RequestMethod.POST)
	@ResponseBody
	public List<ProductSampleApproval> saveProductSampleApprovals(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<ProductSampleApproval> sampleApprovals = FlexJsonUtils.fromJsonArray(jsonStr, ProductSampleApproval.class);
		purchaseProofingService.saveSampleApproval(purchaseSampleService.convertApproval(sampleApprovals));
		logger.log("认定单", "买家上传认定单", sampleApprovals.size());
		return purchaseSampleService.getApprovalB2bId(sampleApprovals);
	}

	/**
	 * erp作废打样申请单之后，平台修改对应状态
	 *
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/invalidate", method = RequestMethod.POST)
	@ResponseBody
	public void invalidateProductSample(@RequestParam("data") String data) throws UnsupportedEncodingException {
		purchaseProofingService.invalidateProductSample(URLDecoder.decode(data, "UTF-8").split(","));
	}

}
