package com.chinacreator.video.restful.service;

import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BusinessException;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.ChannelDao;
import com.chinacreator.video.mapper.DictionaryDao;
import com.chinacreator.video.mapper.TenDrawDao;
import com.chinacreator.video.service.QryBusiService;
import com.chinacreator.video.util.DesUtil;
import com.chinacreator.video.util.MD5;
import com.chinacreator.video.validate.BusiValidate;
import org.apache.commons.lang.StringUtils;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 *奇艺20、腾讯20
 * 限时5元领15元话费活动时间服务类
 *
 */
@Component
public class SendSmsCodeService {
	
	@Autowired
	private BusiValidate busiVali;
	
    @Autowired
    private ChannelDao channelDao;



	
	private static Logger log = Logger.getLogger("sendSms");
	
	public void busiCheck(JSONObject inputObj, String invokename,OrderInfo orderInfo) throws Exception{

		paramValidate(inputObj,invokename);
		busiVali.valiIp(orderInfo);
		busiVali.valiTime(orderInfo);
		//签名验证
		signValidate(inputObj,invokename);
	}

	
	private void paramValidate(JSONObject inputObj, String invokename) throws Exception{
       if("prizeauth".equals(invokename)){
    	   if(StringUtils.isEmpty(inputObj.getString("userid")) ||
				   StringUtils.isEmpty(inputObj.getString("cpid"))||
				   StringUtils.isEmpty(inputObj.getString("spid"))||
				   StringUtils.isEmpty(inputObj.getString("signature"))||
				   StringUtils.isEmpty(inputObj.getString("channel"))){
    		   throw new BussinessException("80015", "参数异常，");
    	   }
       }
       if( StringUtils.isEmpty(inputObj.getString("timestamp"))){
    	   throw new BussinessException("80015", "参数异常，");
       }
	}
	
    /**
     * 签名验证
     * @param
     * @throws Exception
     */
    public void signValidate(JSONObject inputObj, String invokename) throws Exception{
        String lcalsign = getSign(inputObj,invokename);
		String pwd = channelDao.queryChannel(inputObj.getString("channel")).getPassword();
		lcalsign +=pwd;
        lcalsign = MD5.MD5Encode(lcalsign);
        if(!lcalsign.equals(inputObj.getString("signature"))) {
            throw new BussinessException("10006", "签名校验失败");
        }
    }
    
    /**
     * 拼接签名信息 orderid+channel+timestamp
     * @param
     * @return
     */
    public String getSign(JSONObject inputObj, String invokename){
    	StringBuilder strb = new StringBuilder();
        if("prizeauth".equals(invokename)){
        	 strb.append(inputObj.getString("userid"));
			 strb.append(inputObj.getString("channel"));
			 strb.append(inputObj.getString("cpid"));
			 strb.append(inputObj.getString("spid"));
        }else if("prizesend".equals(invokename)){
       	 	strb.append(inputObj.getString("userid"));
	       	strb.append(inputObj.getString("mappid"));
	       	strb.append(inputObj.getString("prizetime"));
	    	strb.append(inputObj.getString("prizename"));
        }else if("prizeqry".equals(invokename)){

        }
        strb.append(inputObj.getString("timestamp"));
        return strb.toString();
    }

	
    /**
     * 手机号码验证
     * @param usermob
     * @throws Exception
     */
	private void mobValidate(String usermob)throws Exception{
		boolean res = false;
        if (usermob != null && usermob.length() == 11) {
        	usermob = usermob.replaceAll("[\\s-]", "");
        	res =  usermob.matches(".*(1\\d{10}).*");
        }
        if(res == false){
        	throw new BussinessException("9022","手机号码不合法");
        }     	       	    
	}
}
