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

cash_domain="qapi-$namespace.q-gp.com"

if [ -z "$namespace" ];then
	cash_domain=$ip:7037
fi


function pushloanprogress(){
	len=`echo $phone_no|awk '{print length($0)}'`
        if [ $len -ne 11 ]
        	then
                echo "请输入11位手机号!"     
        else
			#echo "开始修改"
			#查询userID
			select_sql="select id from ${TABLENAME1} where phone_no=${phone_no}"
			user_id=`mysql -N $mysqld ${DBNAME1} -e "${select_sql}"`
			#echo "user_id=$user_id"

			#查找loanid
			loanid="select id from ${TABLENAME} where user_id=${user_id} order by id desc limit 1"
			loan_id=`mysql -N $mysqld ${DBNAME} -e "${loanid}"`
			#echo "loan_id=$loan_id"

			#查询订单状态
			progressid="select progress from ${TABLENAME} where user_id=${user_id} order by id desc limit 1"
			progressid_id=`mysql -N $mysqld ${DBNAME} -e "${progressid}"`
			#echo "progress=$progressid_id"
			
			#查询授信订单号
			order_no="select order_no from ${TABLENAME2} where user_id=${user_id} order by id desc limit 1"
                        applyno=`mysql -N $mysqld ${DBNAME} -e "${order_no}"`
                        #echo "applyno=$applyno"
			

			#push订单状态
			if [ -z "$applyno" ];then
				echo "未找到applyno，请检查授信订单号"
				break
			else
				echo "loan_id=$loan_id"
				echo "progress=$progress_id"
				curl -d "loanId=${loan_id}&progress=${progress_id}&applyNo=${applyno}" "http://${cash_domain}/test/loanMq/one"
				echo "推送成功 $date"
			fi
			
        fi

}

pushloanprogress $ip $phone_no
