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

import com.uas.platform.b2b.erp.model.InquiryMould;
import com.uas.platform.b2b.erp.model.InquiryMouldDecide;
import com.uas.platform.b2b.erp.service.InquiryMouldService;
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.PurchaseInquiryMouldService;
import com.uas.platform.core.logging.BufferedLoggerManager;
import com.uas.platform.core.util.ArrayUtils;
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 hejq
 * @time 创建时间：2016年12月8日
 */
@Controller
@RequestMapping("/erp/purchase/inquiryMould")
public class InquiryMouldController {

	@Autowired
	private InquiryMouldService inquiryMouldService;

	@Autowired
	private PurchaseInquiryMouldService purchaseInquiryMouldService;

	@Autowired
	private AttachService attachService;

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

	/**
	 * 将买家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<InquiryMould> inquirieymoulds = FlexJsonUtils.fromJsonArray(jsonStr, InquiryMould.class);
		purchaseInquiryMouldService.save(inquiryMouldService.convertInquiryMould(inquirieymoulds));
		logger.log("模具询价单", "上传模具询价单", inquirieymoulds.size());
	}

	/**
	 * 买家在收到模具报价信息后，点击提交按钮后，状态传至平台，至此erp的供应商不能再对这张模具询价单进行报价
	 *
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/checking", method = RequestMethod.POST)
	@ResponseBody
	public void checkInquiryMould(@RequestParam("data") String data) throws UnsupportedEncodingException {
		purchaseInquiryMouldService
				.check(ArrayUtils.convertStringToNumberArray(URLDecoder.decode(data, "UTF-8"), Long.class));
	}

	/**
	 * 买家ERP反提交
	 *
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/reCheck", method = RequestMethod.POST)
	@ResponseBody
	public void reCheckInquiryMould(@RequestParam("data") String data) throws UnsupportedEncodingException {
		purchaseInquiryMouldService
				.reCheck(ArrayUtils.convertStringToNumberArray(URLDecoder.decode(data, "UTF-8"), Long.class));
	}

	/**
	 * 买家ERP获取平台的报价信息
	 *
	 * @return
	 */
	@RequestMapping(value = "/reply", method = RequestMethod.GET)
	@ResponseBody
	public List<InquiryMould> getReply() {
		List<InquiryMould> moulds = inquiryMouldService
				.convertPurchaseInquiryMouldReply(purchaseInquiryMouldService.findNotUploadReply());
		if (moulds != null) {
			logger.log("模具询价单", "下载模具询价单报价结果", moulds.size());
		}
		return moulds;
	}

	/**
	 * 平台的报价信息传到买家ERP之后，修改平台里面的询价单的上传状态
	 *
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/reply/back", method = RequestMethod.POST)
	@ResponseBody
	public void onReplySuccess(@RequestParam("data") String data) throws UnsupportedEncodingException {
		purchaseInquiryMouldService.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<InquiryMouldDecide> decides = FlexJsonUtils.fromJsonArray(jsonStr, InquiryMouldDecide.class);
		purchaseInquiryMouldService.onReplyDecide(inquiryMouldService.convertInquiryMouldDecide(decides));
		logger.log("模具询价单", "修改模具询价单报价信息状态", decides.size());
	}

	/**
	 * 买家erp作废模具询价单，修改平台内单据有效状态
	 * 
	 * @param data
	 * @throws UnsupportedOperationException
	 */
	@RequestMapping(value = "/invalidate", method = RequestMethod.POST)
	@ResponseBody
	public void onInvalid(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<InquiryMould> inquiryMoulds = FlexJsonUtils.fromJsonArray(jsonStr, InquiryMould.class);
		purchaseInquiryMouldService.onInvalid(inquiryMoulds);
		logger.log("模具询价单", "作废模具询价单", inquiryMoulds.size());
	}

	/**
	 * 买家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, "inquirymould", "客户模具询价单附件", fileList);
		purchaseInquiryMouldService.saveAttach(attachs);
		logger.log("模具询价单", "模具询价单附件上传", attachs.size());
	}

}
