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.util.RequestUtil;
import com.chinacreator.video.util.DesUtil;
import com.chinacreator.video.util.MD5;
import com.chinacreator.video.util.URLUtil;


/**
 * 腾讯20元抽奖活动
 * @author zhou.xu
 * @date 20200424
 */
@RestController
@RequestMapping(value = "/tendraw")
public class TenDrawController {
	
	private static Logger logger = Logger.getLogger(TenDrawController.class);
	
	private static Logger log = Logger.getLogger("tendraw");
	
	@Autowired
	private TenDrawService tenDrawService;
	
	/**
	 * 查询是否有抽奖资格
	 * @param params
	 * @param request
	 * @return
	 * @throws Exception
	 */
    @RequestMapping(value = "/prizeauth", 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", "prizeauth");
    	logmap.put("reqData", params);
    	String resultcode = "";
    	String errorinfo = "";
    	try {
    		JSONObject inputObj = new JSONObject().parseObject(params);

    		tenDrawService.busiCheck(inputObj, logmap.get("forward").toString());
    		String res = tenDrawService.prizeauth(inputObj);
    		Map<String, String> data = new HashMap<String, String>();
    		String userid = "";
    		String mappid = "";
    		//0：通过手机号码查询，1：通过映射ID查询
    		if("0".equals(inputObj.getString("type"))){
    			userid = inputObj.getString("account");
    			mappid = res;
    		}else{
    			userid = res;
    			mappid = inputObj.getString("account");
    		}
    		data.put("userid", userid);
    		data.put("mappid", mappid);
    		outpipe.setData(data);
    		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 = "/prizesend", 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);
    		tenDrawService.busiCheck(inputObj, logmap.get("forward").toString());
    		tenDrawService.prizesend(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 = "/prizeqry", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> prizeqry(@RequestBody (required=false) String params, HttpServletRequest request) throws Exception {
    	DataOutPipe outpipe = new DataOutPipe();
    	Map<String,Object> logmap = new HashMap<String,Object>();
    	logmap.put("forward", "prizeqry");
    	logmap.put("reqData", params);
    	String resultcode = "";
    	String errorinfo = "";
    	try {
    		JSONObject inputObj = new JSONObject().parseObject(params);
    		tenDrawService.busiCheck(inputObj, logmap.get("forward").toString());
    		List<HashMap> prizeList = tenDrawService.prizeQry(inputObj);
    		outpipe.setData(prizeList);
    		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;
		}
    }
}
