package com.chinacreator.video.restful.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.javassist.runtime.Desc;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.ChannelDao;
import com.chinacreator.video.mapper.CommConfDao;
import com.chinacreator.video.mapper.DictionaryDao;
import com.chinacreator.video.mapper.TenDrawDao;
import com.chinacreator.video.mapper.UnicomSmsOrderDao;
import com.chinacreator.video.restful.bean.OrderLog;
import com.chinacreator.video.restful.util.RequestUtil;
import com.chinacreator.video.service.CommonService;
import com.chinacreator.video.util.DesUtil;
import com.chinacreator.video.util.MD5;
import com.chinacreator.video.validate.BusiValidate;
import com.chinacreator.video.validate.ChannelValidate;
import com.chinacreator.video.validate.IpValidate;
import com.chinacreator.video.validate.MobValidate;
import com.chinacreator.video.validate.ParamValidate;
import com.chinacreator.video.validate.SignValidate;
import com.chinacreator.video.validate.TimetampValidate;

/**
 * 联通短信订购服务
 * @author xu.zhou
 * @date 20201123
 */
@Component
public class UnicomSmsOrderService {
	
	@Autowired
	private BusiValidate busiVali;
	
    @Autowired
    private ChannelDao channelDao;
    
	@Autowired
	private DictionaryDao dictionaryDao;
	
	@Autowired
	private UnicomSmsOrderDao unicomSmsOrderDao;
	
	private static Logger log = Logger.getLogger("smsorder");
	
	public void busiCheck(JSONObject inputObj, String invokename) throws Exception{
		
		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setChannel("smsorder");
		orderInfo.setCpid(invokename);
		busiVali.valiIp(orderInfo);
		paramValidate(inputObj,invokename);		
		//签名验证
		signValidate(inputObj,invokename);
		//手机号码验证
		mobValidate(inputObj);

	}
	
	
	public void callback(JSONObject inputObj){
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("USERID", inputObj.getString("userid"));
		params.put("USERMOB", inputObj.getString("USERMOB"));
		params.put("CPID", inputObj.getString("cpid"));
		params.put("SPID", inputObj.getString("spid"));
		params.put("ORDERTIME", inputObj.getString("ordertime"));
		params.put("CBCODE", inputObj.getString("code"));
		params.put("CBINFO", inputObj.getString("info"));
		unicomSmsOrderDao.addCallbackRec(params);
	}
	
	/**
	 * 参数验证
	 * @param inputObj
	 * @param invokename
	 * @throws Exception
	 */
	private void paramValidate(JSONObject inputObj, String invokename) throws Exception{
       if("sendsms".equals(invokename)){//发送订购短信
    	   if(StringUtils.isEmpty(inputObj.getString("userid"))
    			   || StringUtils.isEmpty(inputObj.getString("channel"))
    			   || StringUtils.isEmpty(inputObj.getString("timestamp"))
    			   || StringUtils.isEmpty(inputObj.getString("sign"))
    			   ){
    		   throw new BussinessException("80015", "参数异常，");
    	   }
       }else if("callback".equals(invokename)){
    	   if(StringUtils.isEmpty(inputObj.getString("userid"))
    			   || StringUtils.isEmpty(inputObj.getString("code"))
    			   || StringUtils.isEmpty(inputObj.getString("info"))
    			   || StringUtils.isEmpty(inputObj.getString("cpid"))
    			   || StringUtils.isEmpty(inputObj.getString("spid"))
    			   || StringUtils.isEmpty(inputObj.getString("ordertime"))
    			   ){
    		   throw new BussinessException("80015", "参数异常，");
    	   }
       }else {
    	   throw new BussinessException("80015", "参数异常，");
       }
	}
	
    /**
     * 签名验证
     * @param orderInfo
     * @throws Exception
     */
    public void signValidate(JSONObject inputObj, String invokename) throws Exception{
    	if("sendsms".equals(invokename)){
    		
    	}
    }
    
    /**
     * 拼接签名信息 orderid+channel+timestamp
     * @param orderInfo
     * @return
     */
    public String getSign(JSONObject inputObj, String invokename){
    	StringBuilder strb = new StringBuilder();
        /************
         * 
         * 
         * 
         */
        return strb.toString();
    }
	
    /**
     * 手机号码验证
     * @param usermob
     * @throws Exception
     */
	private void mobValidate(JSONObject inputObj)throws Exception{
		String usermob = inputObj.getString("userid");
		String channel = inputObj.getString("channel");
		boolean res = false;
        if (usermob != null && usermob.length() != 11) {
        	String pwd = "";
        	String unicomsmsorderpwd = dictionaryDao.getValue("unicomsmsorderpwd");
        	if(!StringUtils.isEmpty(unicomsmsorderpwd)){
        		String [] pwdArray = unicomsmsorderpwd.split("\\|");
        		for(String s : pwdArray){
        			if(!StringUtils.isEmpty(s) && channel.equals(s.split("###")[0])){
        				pwd = s.split("###")[1];
        				break;
        			}
        		}
        		if(!StringUtils.isEmpty(pwd)){
        			usermob = DesUtil.decode(usermob, pwd);  
        		}            	
        	}
        }
        inputObj.put("USERMOB", usermob);
    	usermob = usermob.replaceAll("[\\s-]", "");
    	res =  usermob.matches(".*(1\\d{10}).*");
        if(res == false){
        	throw new BussinessException("9022","手机号码不合法");
        }     	       	    
	}
}
