#!/bin/bash
#白条激活运营商、芝麻信用认证
ip=$1
phone_no=$2
merchant_id=$3  #渠道号,默认为7

HOSTNAME="$ip"   #数据库信息
PORT=$4
USERNAME="qa"
PASSWORD="qatest"

DBNAME1="xyqb"    #数据库名称
DBNAME2="xyqb_user"
DBNAME3="spider_center"
date="`date +%Y%m%d%H%M%S`"
date_now=`date +"%Y-%m-%d %T"`
echo $date_now

mysql1="-h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD}  ${DBNAME1}"
mysql2="-h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD}  ${DBNAME2}"
mysql3="-h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD}  ${DBNAME3}"

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

#xyqb_user.user插入一条用户数据
user_id_select=`mysql -N $mysql2 -e"SELECT id from xyqb_user.user where phone_no="$phone_no""`
if [[ $user_id_select == "" ]];then
xyqb_user_insert="INSERT INTO xyqb_user.user (phone_no, password, registered_from, uuid, enable, created_at, updated_at)
VALUES
	("$phone_no", '318e235d3e52648b236faa3f748000d5', 217, concat('f6529c70-57f6-45ed-8a01-fafda8c7b4bc',$date), 1, now(), now());"
mysql $mysql2 -e"set names utf8; ${xyqb_user_insert}"
fi

#xyqb_user.user_detail插入用户实名信息
user_id=`mysql -N $mysql2 -e"SELECT id from xyqb_user.user where phone_no="$phone_no""`
user_detail=`mysql -N  $mysql2 -e"SELECT id from xyqb_user.user_detail where phone_no=${phone_no} and user_id=${user_id}"`
if [[ $user_detail == "" ]];then
user_detail_insert="INSERT INTO xyqb_user.user_detail (user_id, phone_no, name, id_no, id_type, is_authenticated, gender, email, qq, created_at, updated_at)
VALUES
	(${user_id}, "${phone_no}", '滕尘', '532523198811050620', 0, 0, 1, '', 'undefined', now(), now());"
mysql $mysql2 -e"set names utf8; ${user_detail_insert}"
fi

#spider_center.user插入一条数据
spider_center_user=`mysql -N  $mysql3 -e"SELECT id from spider_center.user  where phone_no=${phone_no};"`
if [[ $spider_center_user == "" ]];then
spider_center_user_insert="INSERT INTO spider_center.user (phone_no, password, registered_from, uuid, enable, created_at, updated_at)
VALUES
	(${phone_no}, '4c00961baa8857e558c10db0e58c003e', 217, concat('f6529c70-57f6-45ed-8a01-fafda8c7b4bc',$date), 1, now(), now());"
mysql $mysql3 -e"set names utf8; ${spider_center_user_insert}"
fi

#spider_center.user_authorization插入一条授信信息
spider_center_user_id=`mysql -N  $mysql3 -e"SELECT id from spider_center.user  where phone_no=${phone_no};"`
user_authorization_type=`mysql -N  $mysql3 -e"select auth_type from spider_center.user_authorization  where user_id=${spider_center_user_id} and auth_type in (0,3);"`
user_authorization_status=`mysql -N $mysql3 -e"select auth_status from spider_center.user_authorization  where user_id=${spider_center_user_id};"`
if [[ $user_authorization_type == "" ]] ;then
user_authorization_insert="INSERT INTO spider_center.user_authorization ( auth_type, last_auth_time, auth_account_name, user_id, auth_status)
VALUES
        (3, now(), NULL, ${spider_center_user_id}, 1),
        (0, now(), NULL, ${spider_center_user_id}, 1),
	(7, now(), NULL, ${spider_center_user_id}, 1);"
mysql $mysql3 -e"set names utf8; ${user_authorization_insert}"
fi
if [[ ${user_authorization_type[*]} =~ "3" || ${user_authorization_type[*]} =~ "0"  && $user_authorization_status != "1" ]];then
mysql $mysql3 -e"update spider_center.user_authorization set auth_status=1 where user_id=${spider_center_user_id} and auth_type in (0,3,7);"
fi

#xyqb.loan_application_history插入一条贷款记录
if [[ ${merchant_id} == "" ]];then
merchant_id=7
fi

loan_application_history_insert="INSERT INTO xyqb.loan_application_history (user_id, channel_id, bank_card_id, is_active, progress, created_from, business_type, pay_approach, baitiao_merchant_id, created_at, updated_at)
VALUES
	(${user_id}, 222, 1234567890123456789, 1, 36, 222, 1, NULL, ${merchant_id}, now(), now());"
mysql $mysql2 -e"${loan_application_history_insert}"

#xyqb.baitiao_account插入一条白条信用账户
baitiao_account_insert="INSERT INTO xyqb.baitiao_account (user_id, registered_from, pay_pwd, pay_pwd_salt, credit_amount, status, created_at, updated_at)
VALUES
	(${user_id}, ${merchant_id}, '', '', 160000.00, 0,  now(), now());"
mysql $mysql2 -e"${baitiao_account_insert}"

#xyqb.baitiao_consume_history插入一条白条消费记录
baitiao_consume_history_insert="INSERT INTO xyqb.baitiao_consume_history (user_id, order_id, order_amount, remaining_amount, consume_type, consume_seq, created_at, updated_at)
VALUES
	(${user_id}, -1, 160000.00, 160000.00, 0, concat('XYBT20101511498169088',$date), now(), now());"
mysql $mysql2 -e"${baitiao_consume_history_insert}"

echo "白条审核通过"
