package com.chinacreator.video.restful.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
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.alibaba.fastjson.JSONObject;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.service.FlowlimitService;

/**
 * 流量限制相关接口
 * @author xu.zhou
 * @date 20200619
 */
@RestController
@RequestMapping(value = "/flowlimit")
public class FlowlimitController {
	
	private static Logger log = Logger.getLogger("flowlimit");
	
	@Autowired
	private FlowlimitService flowlimitService;
	
    @RequestMapping(value = "/qryfbyproductid", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> qryfbyproductid(@RequestBody (required=false) String params, HttpServletRequest request) throws Exception {
    	DataOutPipe outpipe = new DataOutPipe();
    	Map<String,Object> logmap = new HashMap<String,Object>();
    	logmap.put("invoke", "qryfbyproductid");
    	logmap.put("reqData", params);
    	String resultcode = "";
    	String errorinfo = "";
    	try {
    		JSONObject inputObj = new JSONObject().parseObject(params);
    		//业务验证
    		flowlimitService.busiCheck(inputObj, "qryfbyproductid");
    		//获取流量数据
    		List data = flowlimitService.qryfbyproductid(inputObj);
    		outpipe.setData(data);
    		resultcode = "0";
        	errorinfo = "ok";
		} catch (Exception e) {
			if(e instanceof BussinessException) {
				resultcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) e).getMessage();
			}else{
				e.printStackTrace();
				resultcode = "8000";
				errorinfo = "出现异常，"+e.getMessage();
			}
		} finally {			 			
			logmap.put("resultcode",resultcode);
			logmap.put("errorinfo",errorinfo);	
			logmap.put("rspData", outpipe.getData());
			log.info(JSON.toJSONString(logmap));	
			outpipe.setResultcode(resultcode);
			outpipe.setErrorinfo(errorinfo);
			return outpipe;
		}
    }
    
    /**
     * 根据SPID查询流量，整合IP免流（能力平台接口）及非IP免流（LUA接口）
     * @param params
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/qryflowspid", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> qryflowspid(@RequestBody (required=false) String params, HttpServletRequest request) throws Exception {
    	DataOutPipe outpipe = new DataOutPipe();
    	Map<String,Object> logmap = new HashMap<String,Object>();
    	logmap.put("invoke", "qryflowspid");
    	logmap.put("reqData", params);
    	String resultcode = "";
    	String errorinfo = "";
    	try {
    		JSONObject inputObj = new JSONObject().parseObject(params);
    		//业务验证
    		flowlimitService.busiCheck(inputObj, "qryflowspid");
    		//获取流量数据
    		flowlimitService.qryflowspid(inputObj);
    		if(inputObj.get("data") != null){
        		outpipe.setData(inputObj.get("data").toString());
    		}
    		
    		resultcode = "0";
        	errorinfo = "ok";
		} catch (Exception e) {
			if(e instanceof BussinessException) {
				resultcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) e).getMessage();
			}else{
				e.printStackTrace();
				resultcode = "8000";
				errorinfo = "出现异常，"+e.getMessage();
			}
		} finally {			 			
			logmap.put("resultcode",resultcode);
			logmap.put("errorinfo",errorinfo);	
			logmap.put("rspData", outpipe.getData());
			log.info(JSON.toJSONString(logmap));	
			outpipe.setResultcode(resultcode);
			outpipe.setErrorinfo(errorinfo);
			return outpipe;
		}
    }
	
    /**
     * 根据USERID查询流量，走能力平台，查询套餐内流量
     * @param params
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/qryflowfeeset", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> qryflowFeeset(@RequestBody (required=false) String params, HttpServletRequest request) throws Exception {
    	DataOutPipe outpipe = new DataOutPipe();
    	Map<String,Object> logmap = new HashMap<String,Object>();
    	logmap.put("invoke", "qryflowfeeset");
    	logmap.put("reqData", params);
    	String resultcode = "";
    	String errorinfo = "";
    	try {
    		JSONObject inputObj = new JSONObject().parseObject(params);
    		//业务验证
    		flowlimitService.busiCheck(inputObj, "qryflowfeeset");
    		//获取流量数据
    		flowlimitService.qryflowfeeset(inputObj);
    		if(inputObj.get("data") != null){
        		outpipe.setData(inputObj.get("data"));
    		}
    		
    		resultcode = "0";
        	errorinfo = "ok";
		} catch (Exception e) {
			if(e instanceof BussinessException) {
				resultcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) e).getMessage();
			}else{
				e.printStackTrace();
				resultcode = "8000";
				errorinfo = "出现异常，"+e.getMessage();
			}
		} finally {			 			
			logmap.put("resultcode",resultcode);
			logmap.put("errorinfo",errorinfo);	
			logmap.put("rspData", outpipe.getData());
			log.info(JSON.toJSONString(logmap));	
			outpipe.setResultcode(resultcode);
			outpipe.setErrorinfo(errorinfo);
			return outpipe;
		}
    }
    
    /**
     * 根据USERID查询所有流量，CDN流量查orderFlow，IP免流走走能力平台
     * @param params
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/qryflowunify", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> qryflowunify(@RequestBody (required=false) String params, HttpServletRequest request) throws Exception {
    	DataOutPipe outpipe = new DataOutPipe();
    	Map<String,Object> logmap = new HashMap<String,Object>();
    	logmap.put("invoke", "qryflowunify");
    	logmap.put("reqData", params);
    	String resultcode = "";
    	String errorinfo = "";
    	try {
    		JSONObject inputObj = new JSONObject().parseObject(params);
    		//业务验证
    		flowlimitService.busiCheck(inputObj, "qryflowunify");
    		//获取流量数据
    		flowlimitService.qryflowunify(inputObj);
    		if(inputObj.get("data") != null){
        		outpipe.setData(inputObj.get("data"));
    		}   		
    		resultcode = "0";
        	errorinfo = "ok";
		} catch (Exception e) {
			if(e instanceof BussinessException) {
				resultcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) e).getMessage();
			}else{
				e.printStackTrace();
				resultcode = "8000";
				errorinfo = "出现异常，"+e.getMessage();
			}
		} finally {			 			
			logmap.put("resultcode",resultcode);
			logmap.put("errorinfo",errorinfo);	
			logmap.put("rspData", outpipe.getData());
			log.info(JSON.toJSONString(logmap));	
			outpipe.setResultcode(resultcode);
			outpipe.setErrorinfo(errorinfo);
			return outpipe;
		}
    }
}
