package com.chinacreator.video.restful.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;

import org.jboss.logging.Logger;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.restful.util.JsonUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
@RequestMapping(value = "/api")
public class RestfulTestController {
	
	private static Logger logger = Logger.getLogger(RestfulTestController.class);
	
	//post方式传参
	//http://127.0.0.1:8084/video-order/api/pointshop
	//参数：{"userid":"13011112222","cpid":"tencent","spid":"101","channel":"Tx_2019"}
	//heards要设置Content-type:application/json
    @RequestMapping(value = "/pointshop", method = RequestMethod.POST, produces = "application/json")
    public String addUser(@RequestBody OrderInfo orderInfo) {
        System.out.println("开始新增..."+JSON.toJSONString(orderInfo));
        return JSON.toJSONString(orderInfo);
    }
    
    //get方式传参，使用参数名传递JSON参数值
    //http://127.0.0.1:8084/video-order/api/pointshop2?orderInfo=%7B%22userid%22%3A%2213011112222%22%2C%22cpid%22%3A%22tencent%22%2C%22spid%22%3A%22101%22%2C%22channel%22%3A%22Tx_2019%22%7D 
    //"{\"userid\":\"13011112222\",\"cpid\":\"tencent\",\"spid\":\"101\",\"channel\":\"Tx_2019\"}"
    @RequestMapping(value = "/pointshop2", method = RequestMethod.GET, produces = "application/json")
    public String addUser2(String orderInfo) {
        System.out.println("开始新增..."+JSON.toJSONString(orderInfo));
        return JSON.toJSONString(orderInfo);
    }
    
    //get方式传参
    //http://127.0.0.1:8084/video-order/api/pointshop3/%7B%22userid%22:%2213011112222%22,%22cpid%22:%22tencent%22,%22spid%22:%22101%22,%22channel%22:%22Tx_2019%22%7D
    //参数：//"{\"userid\":\"13011112222\",\"cpid\":\"tencent\",\"spid\":\"101\",\"channel\":\"Tx_2019\"}"
    @RequestMapping(value = "/pointshop3/{param}", method = RequestMethod.GET, produces = "application/json")
    public String addUser3(@PathVariable String param, HttpServletRequest request) {
        System.out.println("开始新增..."+param);
        String paramLocal = param;
        try {
        	paramLocal = URLDecoder.decode(paramLocal, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            logger.error("",e);
        }
        
        OrderInfo orderInfo = JsonUtil.convertJson2Object(paramLocal, OrderInfo.class);
        System.out.println(orderInfo.getUserid());
        System.out.println("paramLocal=>"+paramLocal);
        Enumeration enumeration = request.getParameterNames();
        while(enumeration.hasMoreElements()){
        	Object obj = enumeration.nextElement();
        	System.out.println(obj);
        }
        return JSON.toJSONString(orderInfo);
    }
    
    //post方式接收参数http://127.0.0.1:8084/video-order/api/pointshop4
    //{"userid":"123456789","cpid":"2001"}
    //返回{"userid":"123456789","cpid":"2001"}
    @RequestMapping(value = "/pointshop4", method = RequestMethod.POST, produces = "application/json")
    public String addUser4(@RequestBody String orderInfo) {
        System.out.println("开始新增..."+JSON.toJSONString(orderInfo));
        return JSON.toJSONString(orderInfo);
    }
}
