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.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.MappUserService;

/**
 * 用户手机号码映射
 * @author zhou.xu
 * @date 20200723
 */
@RestController
@RequestMapping(value = "/usermapp")
public class MappUserController {
	
	private static Logger log = Logger.getLogger("tendraw");
	
	@Autowired
	private MappUserService service;
	
	/**
	 * 根据映射ID获取手机号码
	 * @param params
	 * @param request
	 * @return
	 * @throws Exception
	 */
    @RequestMapping(value = "/qryuserid", 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", "qryuserid");
    	logmap.put("reqData", params);
    	String resultcode = "";
        String errorinfo = "";
    	try {
    		JSONObject inputObj = new JSONObject().parseObject(params);

    		service.busiCheck(inputObj, logmap.get("forward").toString());
    		service.qryuserid(inputObj);
    		
    		Map<String, String> data = new HashMap<String, String>();
    		data.put("userid", inputObj.getString("userid"));
    		data.put("mappid", inputObj.getString("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;
		}
    }
    
    /**
     * 根据手机号码获取映射ID
     * @param params
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/qrymappid", 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", "qrymappid");
    	logmap.put("reqData", params);
    	String resultcode = "";
    	String errorinfo = "";
    	try {
    		JSONObject inputObj = new JSONObject().parseObject(params);
    		service.busiCheck(inputObj, logmap.get("forward").toString());
    		service.qrymappid(inputObj);

    		Map<String, String> data = new HashMap<String, String>();
    		data.put("userid", inputObj.getString("userid"));
    		data.put("mappid", inputObj.getString("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;
		}
    }
}
