package com.chinacreator.video.restful.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONObject;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
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.exception.BussinessException;
import com.chinacreator.video.restful.bean.ShopInObject;
import com.chinacreator.video.restful.bean.ShopOutObject;
import com.chinacreator.video.restful.service.PointShopService;
import com.chinacreator.video.util.IpAddrUtil;

@RestController
@RequestMapping(value = "/point")
public class AsyncOrderController {
	
	private static Logger logger = Logger.getLogger(AsyncOrderController.class);
	
	private static Logger log = Logger.getLogger("pointshop");
	
	@Autowired
	private PointShopService service;
	
	/**
	 * POST方式办理业务
	 * @param inputObj
	 * @param request
	 * @return
	 * @throws Exception 
	 */
    @RequestMapping(value = "/pOrder", method = RequestMethod.POST, produces = "application/json")
    public String order(@RequestBody ShopInObject inputObj, HttpServletRequest request) throws Exception {
    	ShopOutObject outputObj = new ShopOutObject();
    	String inputParams = "";
    	String outputParams = "";
    	try {
        	inputParams = JSON.toJSONString(inputObj); //输入参数
        	//outputObj = service.busiOper(inputObj);
        	outputParams = JSON.toJSONString(outputObj);
            return JSON.toJSONString(outputObj);
		} catch (Exception e) {
			e.printStackTrace();
			if(e instanceof BussinessException) {
				throw e;
			}else {
				throw new BussinessException("8003", "出现异常，"+e.getMessage());
			}
		} finally {
			//发送短信
			log.info("inputObj=>"+inputParams);
			log.info("outputObj=>"+outputParams);
		}

    }
    
    /**
     * GET方式办理业务
     * @param param
     * @param request
     * @return
     */
    @RequestMapping(value = "/gOrder/{param}", method = RequestMethod.GET, produces = "application/json")
    public String qryOrder(@PathVariable String param, HttpServletRequest request)  throws Exception {
        System.out.println("开始新增..."+param);
        String paramLocal = param;
        try {
        	paramLocal = URLDecoder.decode(paramLocal, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            logger.error("",e);
        }
        String ip = IpAddrUtil.getIpAddr2(request);
		if(!ips.contains(ip)){
			throw new BussinessException("9008", "IP地址不合法"+ip);
		}
		
		JSONObject json = new JSONObject().fromObject(paramLocal);
        String orderId = json.getString("orderId");
        return JSON.toJSONString(paramLocal);
    }
    
    private static List<String> ips = new ArrayList<String>();
    static{
   	 ips.add("127.0.0.1");
   	 ips.add("10.199.99.147"); //生产环境
   	 ips.add("10.199.99.177"); //测试环境
    }
}
