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.Label;
import com.uas.platform.b2b.erp.model.LabelParameter;
import com.uas.platform.b2b.erp.model.LabelPrintSetting;
import com.uas.platform.b2b.erp.service.BarLPSetingService;
import com.uas.platform.b2b.erp.service.BarLParameterService;
import com.uas.platform.b2b.erp.service.BarLabelService;
import com.uas.platform.b2b.erp.support.ErpBufferedLogger;
import com.uas.platform.b2b.service.B2bBarLPSetingService;
import com.uas.platform.b2b.service.B2bBarLParameterService;
import com.uas.platform.b2b.service.B2bBarLabelService;
import com.uas.platform.core.logging.BufferedLoggerManager;
import com.uas.platform.core.util.serializer.FlexJsonUtils;

/**
 * B2B端数据接口，处理erp端Label数据
 * @author aof
 * @date 2015年10月29日
 */
@Controller
@RequestMapping("/erp/label")
public class LabelInfoController {

	@Autowired
	private BarLabelService barLabelService;
	
	@Autowired
	private BarLParameterService barLParameterService;
	
	@Autowired
	private B2bBarLabelService b2bBarLabelService;
	
	@Autowired
	private B2bBarLParameterService b2bBarLParameterService;
	
	@Autowired
	private BarLPSetingService barLPSetingService;
	
	@Autowired
	private B2bBarLPSetingService b2bBarLPSetingService;
	
	private final static ErpBufferedLogger logger = BufferedLoggerManager.getLogger(ErpBufferedLogger.class);
	
	/**
	 * 保存发货单条码 label表
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/label", method = RequestMethod.POST)
	@ResponseBody
	public void saveLabel(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<Label> labels = FlexJsonUtils.fromJsonArray(jsonStr, Label.class);
		b2bBarLabelService.saveOrUpdate(barLabelService.convertBarLabel(labels));
		logger.log("发货单", "保存发货单条码 label表", labels.size());
	}
	
	/**
	 * 保存发货条码 LabelParameter模型
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/labelparameter", method = RequestMethod.POST)
	@ResponseBody
	public void saveLabelParameter(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<LabelParameter> lParameters = FlexJsonUtils.fromJsonArray(jsonStr, LabelParameter.class);
		b2bBarLParameterService.saveOrUpdate(barLParameterService.convertBarLParameter(lParameters));
		logger.log("发货单", "保存发货单条码  LabelParameter模型", lParameters.size());
	}
	
	/**
	 * 保存发货条码 lpSeting表
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/lpSeting", method = RequestMethod.POST)
	@ResponseBody
	public void savelPSeting(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<LabelPrintSetting> lPSetings = FlexJsonUtils.fromJsonArray(jsonStr, LabelPrintSetting.class);
		b2bBarLPSetingService.saveOrUpdate(barLPSetingService.convertBarLSeting(lPSetings));
		logger.log("发货单", "保存发货单条码  lpSeting表", lPSetings.size());
	}
	
}
