package com.chinacreator.video.restful.controller;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
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.bean.OrderInfo;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.CommConfDao;
import com.chinacreator.video.mapper.CpSpDao;
import com.chinacreator.video.mapper.DictionaryDao;
import com.chinacreator.video.mapper.ShangHaiOrderDao;
import com.chinacreator.video.restful.bean.ShangHaiObject;
import com.chinacreator.video.restful.service.ShangHaiOrderService;
import com.chinacreator.video.restful.service.TenDrawService;
import com.chinacreator.video.restful.service.UnicomSmsOrderService;
import com.chinacreator.video.restful.util.RequestUtil;
import com.chinacreator.video.util.DesUtil;
import com.chinacreator.video.util.MD5;
import com.chinacreator.video.util.URLUtil;


/**
 * 联通短信订购处理控制
 * @author zhou.xu
 * @date 20201123
 */
@RestController
@RequestMapping(value = "/smsorder")
public class UnicomSmsOrderController {
	
	private static Logger logger = Logger.getLogger(UnicomSmsOrderController.class);
	
	private static Logger log = Logger.getLogger("smsorder");
	
	@Autowired
	private UnicomSmsOrderService service;
	
	/**
	 * 短信订购回调通知
	 * @param params
	 * @param request
	 * @return
	 * @throws Exception
	 */
    @RequestMapping(value = "/callback", 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", "callback");
    	logmap.put("reqData", params);
    	String resultcode = "";
    	String errorinfo = "";
    	try {
    		JSONObject inputObj = new JSONObject().parseObject(params);
    		service.busiCheck(inputObj, logmap.get("forward").toString());
    		service.callback(inputObj);
    		resultcode = "0";
        	errorinfo = "ok";
		} catch (Exception e) {
			e.printStackTrace();
			resultcode = "8000";
			errorinfo = "出现异常，"+e.getMessage();
			if(e instanceof BussinessException) {
				resultcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) 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;
		}
    }
    
    /**
     * 短信订购（未使用）
     * @param params
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/smssend", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> prizesend(@RequestBody (required=false) String params, HttpServletRequest request) throws Exception {
    	DataOutPipe outpipe = new DataOutPipe();
    	Map<String,Object> logmap = new HashMap<String,Object>();
    	logmap.put("forward", "prizesend");
    	logmap.put("reqData", params);
    	String resultcode = "";
    	String errorinfo = "";
    	try {
    		JSONObject inputObj = new JSONObject().parseObject(params);

    		resultcode = "0";
        	errorinfo = "ok";
		} catch (Exception e) {
			e.printStackTrace();
			resultcode = "8000";
			errorinfo = "出现异常，"+e.getMessage();
			if(e instanceof BussinessException) {
				resultcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) 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;
		}
    }
    
}
