#!/bin/bash
ip=$1
phone_no=$2
PORT=$3
namespace=$4
USERNAME="qa"
PASSWORD="qatest"
DBNAME="xyqb"
TABLENAME="loan_application_history"
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}"`
			#cho "user_id=$user_id"

			#查找loanid
			loanid="select id from ${TABLENAME} where user_id=${user_id} and is_active = 1"
			loan_id=`mysql -N $mysqld ${DBNAME} -e "${loanid}"`
			#cho "loan_id=$loan_id"

			#查询订单状态
			progressid="select progress from ${TABLENAME} where user_id=${user_id} and is_active = 1"
			progress_id=`mysql -N $mysqld ${DBNAME} -e "${progressid}"`
			echo "progress=$progress_id"

			

			#push订单状态
			#终审中
			if [ $progress_id -eq 7 ]; then
				curl -d "loanId=${loan_id}&loanProgress=FINAL_SCREENING" "http://${cash_domain}/test/loanMq/one"
				echo "推送成功 $date"

			#终审拒绝
			elif [ $progress_id -eq 8 ]; then
				curl -d "loanId=${loan_id}&loanProgress=FINAL_SCREENING_REJECT" "http://${cash_domain}/test/loanMq/one"
				echo "推送成功 $date"

			#未通过初筛
			elif [ $progress_id -eq 19 ]; then
				curl -d "loanId=${loan_id}&loanProgress=FILTERING_DECLINED" "http://${cash_domain}/test/loanMq/one"
				echo "推送成功 $date"

			#借款清单生成完成
			elif [ $progress_id -eq 9 ]; then
				curl -d "loanId=${loan_id}&loanProgress=LOAN_APPLICATION_MANIFEST_COMPLETE" "http://${cash_domain}/test/loanMq/one"
				echo "推送成功 $date"

			#等待资金方放款
			elif [ $progress_id -eq 20 ]; then
				curl -d "loanId=${loan_id}&loanProgress=WAITING_FUND" "http://${cash_domain}/test/loanMq/one"
				echo "推送成功 $date"

			#查看还款计划
			elif [ $progress_id -eq 15 ]; then
				curl -d "loanId=${loan_id}&loanProgress=CHECK_REPAYMENT_PLAN" "http://${cash_domain}/test/loanMq/one"
				echo "推送成功 $date"

			#马上金融放款失败
			elif [ $progress_id -eq 23 ]; then
				curl -d "loanId=${loan_id}&loanProgress=FINAL_FUNDED_APPROVAL_FAIL" "http://${cash_domain}/test/loanMq/one"
				echo "推送成功 $date"

			#借款完成
			elif [ $progress_id -eq 16 ]; then
				curl -d "loanId=${loan_id}&loanProgress=LOAN_COMPLETE" "http://${cash_domain}/test/loanMq/one"
			else
				echo "progress=$progress_id,订单状态不对"
			fi
			
			break
        fi

}

pushloanprogress $ip $phone_no
