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.Inquiry;
import com.uas.platform.b2b.erp.model.InquiryDecide;
import com.uas.platform.b2b.erp.model.InquiryDetail;
import com.uas.platform.b2b.erp.service.InquiryService;
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.PurchaseInquiryService;
import com.uas.platform.core.util.serializer.FlexJsonUtils;

/**
 * 对买家ERP的数据接口<br>
 * 买家询价单处理
 * 
 * @author yingp
 * 
 */
@Controller
@RequestMapping("/erp/purchase/inquiry")
public class InquiryController {

	@Autowired
	private InquiryService inquiryService;

	@Autowired
	private PurchaseInquiryService purchaseInquiryService;

	@Autowired
	private AttachService attachService;

	/**
	 * 将买家ERP的采购询价写到平台
	 * 
	 * @param data
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public void saveInquiries(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<Inquiry> inquiries = FlexJsonUtils.fromJsonArray(jsonStr, Inquiry.class);
		purchaseInquiryService.save(inquiryService.convertInquiry(inquiries));
	}

	/**
	 * 买家ERP获取平台的报价信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/reply", method = RequestMethod.GET)
	@ResponseBody
	public List<InquiryDetail> getReply() {
		return inquiryService.convertPurchaseInquiryReply(purchaseInquiryService.findNotUploadReply());
	}

	/**
	 * 平台的报价信息传到买家ERP之后，修改平台里面的询价单明细的上传状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/reply/back", method = RequestMethod.POST)
	@ResponseBody
	public void onReplySuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		purchaseInquiryService.onReplyUploadSuccess(URLDecoder.decode(data, "UTF-8").split(","));
	}

	/**
	 * 买家ERP（不）采纳了价格之后，修改平台里面的报价信息的状态
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/reply/decide", method = RequestMethod.POST)
	@ResponseBody
	public void onReplyDecide(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<InquiryDecide> decides = FlexJsonUtils.fromJsonArray(jsonStr, InquiryDecide.class);
		purchaseInquiryService.onReplyDecide(inquiryService.convertInquiryDecide(decides));
	}

	/**
	 * 买家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, "inquiry", "客户采购询价单附件", fileList);
		purchaseInquiryService.saveAttach(attachs);
	}

}
