#!/bin/bash
ip=$1
phone_no=$2
PORT=$3
# 默认金额39
amount=$4
USERNAME="qa"
PASSWORD="qatest"
DBNAME="xyqb"
DBNAME1="xyqb_user"
TABLENAME="loan_application_history"
TABLENAME1="loan_extra_fee"
TABLENAME2="user"
mysqld="-h${ip} -P${PORT} -u${USERNAME} -p${PASSWORD}"
date=`date "+%Y-%m-%d %H:%M:%S"`

function xyqb_speed()
{
	len=`echo $phone_no|awk '{print length($0)}'`
	if [ $len -ne 11 ];then
		echo "请输入11位手机号!" 
		exit -1
	fi

# 默认金额39
	if [ -z "$amount" ];then
		amount=39
	fi

		#查询userID
		select_sql="select id from ${TABLENAME2} where phone_no=${phone_no}"
		user_id=`mysql -N $mysqld ${DBNAME1} -e "${select_sql}"`
		echo "user_id=$user_id"

		# 查询loan_ID和channel_ID
		select_loanID="select id from ${TABLENAME} where user_id=${user_id} and business_type=0 order by id desc limit 1" 
		select_channelID="select created_from from ${TABLENAME} where user_id=${user_id} and business_type=0 order by id desc limit 1"
		loan_id=`mysql -N $mysqld ${DBNAME} -e "${select_loanID}"`
		channel_id=`mysql -N $mysqld ${DBNAME} -e "${select_channelID}"`
		echo "loan_id=$loan_id"
		echo "channel_id=$channel_id"

		# 插入加速包数据
		insert_sql0="insert into ${TABLENAME1} ( "loan_id", "channel_id", "user_id", "product_type", "product_desc", "amount", "pay_status", "loan_status", "pay_order_no", "pay_started_at", "pay_finished_at", "is_active") values ('$loan_id', '$channel_id', '$user_id', '1', N'加速卡', '$amount', '0', '0', 'ACCELERATE_CARD_$loan_id', null, null, '1');"
		insert_sql1="insert into ${TABLENAME1} ( "loan_id", "channel_id", "user_id", "product_type", "product_desc", "amount", "pay_status", "loan_status", "pay_order_no", "pay_started_at", "pay_finished_at", "is_active") values ('$loan_id', '$channel_id', '$user_id', '2', N'趸收服务费', '60', '0', '0', 'EXTRA_SERVICE_FEE_$loan_id', null, null, '1');"
		
		#echo "insert=$insert_sql0"

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

xyqb_speed $ip $phone_no
