#!/bin/bash
ip=$1
phone_no=$2
PORT=$3
namespace=$4
USERNAME="qa"
PASSWORD="qatest"
DBNAME="spider_center"
TABLENAME="user_authorization"
TABLENAME1="user"
mysqld="-h${ip} -P${PORT} -u${USERNAME} -p${PASSWORD}"
date=`date "+%Y-%m-%d %H:%M:%S"`

spider_center_domain="sc-$namespace.q-gp.com"

if [ -z "$namespace" ];then
	spider_center_domain=$ip:7021
fi

function authorization()
{
	len=`echo $phone_no|awk '{print length($0)}'`
	if [ $len -ne 11 ]
		then
		echo "请输入11位手机号!"     
	else
		#查询userID
		select_sql="select id from ${TABLENAME1} where phone_no=${phone_no}"
		user_id=`mysql -N $mysqld ${DBNAME} -e "${select_sql}"`
		if [ -z $user_id ];then
			uuid=$(uuidgen)
			insert_sql="insert into ${TABLENAME1} ("phone_no","registered_from","uuid","enable") values ('${phone_no}','111','$uuid','1')"
			mysql $mysqld ${DBNAME} -e "${insert_sql}"
			user_id=`mysql -N $mysqld ${DBNAME} -e "${select_sql}"`
		fi
		uuid=`mysql -N $mysqld ${DBNAME} -e "select uuid from ${TABLENAME1} where phone_no=${phone_no}"`
		echo "user_id=$user_id"
		echo "uuid=$uuid"

		#插入授信数据0和3
		insert_sql0="insert into ${TABLENAME} ("user_id","auth_type","auth_status") values ('$user_id','0',1)"
		insert_sql3="insert into ${TABLENAME} ("user_id","auth_type","auth_status") values ('$user_id','3',1)"
		insert_sql7="insert into ${TABLENAME} ("user_id","auth_type","auth_status") values ('$user_id','7',1)"
		insert_sql00="insert into user_auth_info ("auth_type","last_auth_time","auth_account_name","user_id","auth_status") values ('0','$date','$phone_no','$uuid','1')"
		insert_sql03="insert into user_auth_info ("auth_type","last_auth_time","auth_account_name","user_id","auth_status") values ('3','$date','$phone_no','$uuid','1')"
		
		#echo "$insert_sql0"
		#echo "$insert_sql3"
		mysql $mysqld ${DBNAME} -e "${insert_sql0}"
		mysql $mysqld ${DBNAME} -e "${insert_sql3}"
		mysql $mysqld ${DBNAME} -e "${insert_sql7}"
		mysql $mysqld ${DBNAME} -e "${insert_sql00}"
		mysql $mysqld ${DBNAME} -e "${insert_sql03}"
		
		curl -s "http://${spider_center_domain}/ex/quartz/notity_generate_report_new"
		
		echo "done!!! $date"
	fi
}

authorization $ip $phone_no
