#!/bin/bash
ip=$1
phone_no=$2
set_channel=$3
DBNAME="xyqb_user"
DBNAME1="xyqb"
DBNAME2="risk_control"
TABLENAME="user"
TABLENAME1="loan_application_history"
TABLENAME2="review_history"
TABLENAME3="loan_submit_info"

mysqld="-h$ip -p3306 -uqa -pqatest"

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

			#修改用户表渠道
			update_user_channel="update ${TABLENAME} set registered_from=$set_channel where phone_no=${phone_no}"
			#修改订单表渠道
			update_loan_channel="update ${TABLENAME1} set created_from=$set_channel where user_id=${user_id}"
			#修改谛听渠道
			update_diting_channel="update ${TABLENAME2} set created_from=$set_channel where loan_application_history_id=${select_loan_id}"
			#修改订单提交渠道
			update_submit_channel="update ${TABLENAME3} set channel_id=$set_channel where loan_id=${select_loan_id}"

			
			mysql $mysqld ${DBNAME} -e "${update_user_channel}"
			mysql $mysqld ${DBNAME1} -e "${update_loan_channel}"
			mysql $mysqld ${DBNAME2} -e "${update_diting_channel}"
			mysql $mysqld ${DBNAME1} -e "${update_submit_channel}"

			echo "修改成功"
            break
        fi

}

updatecarddetail $ip $phone_no $set_channel