package com.chinacreator.video.restful.controller;

import java.util.HashMap;
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.druid.util.StringUtils;
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.ShortAddrService;

/**
 * 短地址生成接口中
 * @author zhou.xu
 * @date 20211012
 */
@RestController
public class ShortAddrController {
	
	private static Logger log = Logger.getLogger("tendraw");
	
	@Autowired
	private ShortAddrService service;
	
	/**
	 * 根据长地址生成短地址
	 * @param params
	 * @param request
	 * @return
	 * @throws Exception
	 */
    @RequestMapping(value = "/shortaddr", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> prizeauth(@RequestBody (required=false) String params, HttpServletRequest request) throws Exception {
    	DataOutPipe outpipe = new DataOutPipe();
    	Map<String,Object> logmap = new HashMap<String,Object>();
    	logmap.put("forward", "shortaddr");
    	logmap.put("reqData", params);
    	String resultcode = "";
        String errorinfo = "";
    	try {
    		JSONObject inputObj = new JSONObject().parseObject(params == null ? "" : params.trim());

    		service.busiCheck(inputObj, logmap.get("forward").toString());
    		String res = service.process(inputObj);
    		if(!StringUtils.isEmpty(res)){
    			if(res.indexOf("v.wo.cn") != -1){
    				String shortcode = res.split("//")[2];
    	    		Map<String, String> data = new HashMap<String, String>();
    	    		data.put("shortcode", shortcode);
    	    		data.put("longurl", inputObj.getString("longurl"));
    	    		outpipe.setData(data);
    	    		resultcode = "0";
    	        	errorinfo = "ok";
    			}else{
    				JSONObject resobj = new JSONObject().parseObject(res);
    				resultcode = resobj.getString("resultCode");
    				errorinfo = resobj.getString("resultMsg");
    			}
    		}else{
    			resultcode = "8002";
    			errorinfo = "未生成短地址编码";
    		}
		} catch (Exception e) {
			if(e instanceof BussinessException) {
				resultcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) e).getMessage();
			}else{
				e.printStackTrace();
				resultcode = "8000";
				errorinfo = "出现异常，"+e.getMessage();
				log.error("业务处理出现异常，"+e.getMessage(),e);
			}
		} 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;
		}
    }
}
