package com.chinacreator.video.web;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.exception.BusinessException;
import com.chinacreator.video.util.AESUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.dto.IncrDataOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.ChannelDao;
import com.chinacreator.video.mapper.CpSpDao;
import com.chinacreator.video.mapper.DictionaryDao;
import com.chinacreator.video.service.BackBusiOrderService;
import com.chinacreator.video.service.QryBusiService;
import com.chinacreator.video.util.DesUtil;
import com.chinacreator.video.util.MD5;
import com.chinacreator.video.util.URLUtil;
import com.chinacreator.video.validate.ChannelValidate;
import com.chinacreator.video.validate.IpValidate;
import com.chinacreator.video.validate.MobValidate;
import com.chinacreator.video.validate.TimetampValidate;

/**
 * 统一查询接口
 * @author xu.zhou
 * @date 20190628
 */
@RestController
public class BackBusiOrderControl {
    public static void main(String[] args) throws Exception {
        Map<String,Object> map=new LinkedHashMap<>();
        map.put("usermob","1234567890");
        map.put("productid","10010");
        map.put("busiid","order");
        map.put("useraccount","001");
        String str = JSONObject.toJSONString(map);
        System.out.println(str);
        System.out.println(AESUtil.encrypt(str, "1111222233334444").toLowerCase());
        System.out.println(DesUtil.encode("15510291698","eb9b1e8b"));
    }
	private static final  Logger logger = LoggerFactory.getLogger(BackBusiOrderControl.class);
	
	private static final  Logger log = LoggerFactory.getLogger("commonOrder");
	
	@Autowired
	private BackBusiOrderService service;

	@Autowired
	private DictionaryDao dictionaryDao;

	@RequestMapping("/backorder")
	public DataOutPipe<?> hand(OrderInfo orderInfo) throws Exception {
		Map<String,Object> logmap = new HashMap<String,Object>();
		DataOutPipe outpipe = new DataOutPipe();
		String resultcode = "";
		String errorinfo = "";
		long startime = System.currentTimeMillis();
		logmap.put("forward", "backorder");
		logmap.put("userInfo", orderInfo);
		try {
		    orderInfo.setInvokeface("backorder");
			service.busiCheck(orderInfo);
			String numberlimit = dictionaryDao.getValue("Numberlimit");
			if (numberlimit != null && numberlimit.indexOf(orderInfo.getUserid().substring(0, 7)) != -1) {
				throw new BussinessException("9010", "用户为黑名单用户");
			}
			logmap.put("userInfo", orderInfo);
			String url = service.getOrderUrl(orderInfo);
			logmap.put("url", url);
			String result = doHandle(url);
			if(!JSON.parseObject(result).get("resultcode").equals("0")) {
				throw new BussinessException(JSON.parseObject(result).get("resultcode").toString(),JSON.parseObject(result).get("errorinfo").toString());
			}
			resultcode = "0";
			errorinfo = "ok";
		}catch(Exception e) {

			if(e instanceof BussinessException) {
				resultcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) e).getMessage();
				throw e;
			}else {
				e.printStackTrace();
				resultcode = "80003";
				errorinfo = "出现异常，"+e.getMessage();
			
                //不返回videoif相关的错误信息
            	if(errorinfo != null && errorinfo.indexOf("videoif") != -1){
            		 throw new BussinessException(resultcode, "系统忙，请稍候再试。");
            	}else{
            		throw new BussinessException(resultcode, errorinfo);
				}
			}
		}finally {
			outpipe.setResultcode(resultcode);
			outpipe.setErrorinfo(errorinfo);
			logmap.put("resultcode", resultcode);
			logmap.put("errorinfo", errorinfo);
			logmap.put("times", System.currentTimeMillis()-startime);
			log.info(JSON.toJSONString(logmap));
		}
		return outpipe;
	}
	
	public String doHandle(String url) throws Exception {
		return  URLUtil.get(url,0);
	}
}
