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

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}"`
		echo "user_id=$user_id"

		insert_sql0="insert into ${TABLENAME} ("user_id","name","phone_no","relation") values ('$user_id',N'哈哈',13312341234,'0')"
		insert_sql1="insert into ${TABLENAME} ("user_id","name","phone_no","relation") values ('$user_id',N'嘿嘿',13356785678,'1')"

		mysql $mysqld ${DBNAME} -e "${insert_sql0}"
		mysql $mysqld ${DBNAME} -e "${insert_sql1}"
		echo "done!!! $date"
	fi
}

authorization $ip $phone_no
