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

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.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.PurchaseInquiryService;
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.*;

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

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

	@Autowired
	private InquiryService inquiryService;

	@Autowired
	private PurchaseInquiryService purchaseInquiryService;

	@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 List<Inquiry> saveInquiries(@RequestParam("data") String data) throws UnsupportedEncodingException {
        String jsonStr = URLDecoder.decode(data, "UTF-8");
        List<Inquiry> inquiries = FlexJsonUtils.fromJsonArray(jsonStr, Inquiry.class);
        purchaseInquiryService.saveList(inquiryService.convertInquiry(inquiries));
        logger.log("询价单", "上传询价单", inquiries.size());
		return inquiryService.getB2bId(inquiries);
    }

	/**
	 * 将买家ERP的采购询价写到平台（新方法）
	 *
	 * @param inquiries
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/v2", method = RequestMethod.POST)
	@ResponseBody
	public void saveInquiries(@RequestBody List<Inquiry> inquiries) {
//        purchaseInquiryService.save(inquiryService.convertInquiry(inquiries));
		logger.log("询价单", "上传询价单", inquiries.size());
	}


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

	/**
	 * 买家反提交询价单后，状态传回平台，待报价单据重新回到待报价列表且可以进行报价，已报价单据可以修改报价
	 *
	 * @param data
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/reCheck", method = RequestMethod.POST)
	@ResponseBody
	public void reCheckInquiry(@RequestParam("data") String data) throws UnsupportedEncodingException {
		purchaseInquiryService.reCheck(ArrayUtils.convertStringToNumberArray(URLDecoder.decode(data, "UTF-8"), Long.class));
	}

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

	/**
	 * 平台的报价信息传到买家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));
		logger.log("询价单", "修改询价单报价信息状态", decides.size());
	}

	/**
	 * 买家ERP作废询价单后，修改平台里面的状态
	 *
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/invalid", method = RequestMethod.POST)
	@ResponseBody
	public void onReplyInvalid(@RequestParam("data") String data) throws UnsupportedEncodingException {
		String jsonStr = URLDecoder.decode(data, "UTF-8");
		List<Inquiry> inquiries = FlexJsonUtils.fromJsonArray(jsonStr, Inquiry.class);
		purchaseInquiryService.onReplyInvalid(inquiries);
		logger.log("询价单", "作废询价单", inquiries.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, "inquiry", "客户采购询价单附件", fileList);
		purchaseInquiryService.saveAttach(attachs);
		logger.log("询价单", "询价单附件上传", attachs.size());
	}

    /**
     * 买家上传自动询价单明细，因为存在客户后来才注册平台的情况（重新上传）
     *
     * @param data
     * @throws UnsupportedEncodingException
     */
	@RequestMapping(value = "/qutoInquiry", method = RequestMethod.POST)
    @ResponseBody
    public void uploadQutoInquiry(@RequestParam("data") String data) throws UnsupportedEncodingException {
        String jsonStr = URLDecoder.decode(data, "UTF-8");
        List<InquiryDetail> details = FlexJsonUtils.fromJsonArray(jsonStr, InquiryDetail.class);
        inquiryService.save(details);
    }
}
