#!/bin/bash
#白条激活运营商、芝麻信用认证
ip=$1
phone_no=$2

HOSTNAME="$ip"   #数据库信息
PORT=$3
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=`mysql -N $mysql2 -e"SELECT id from xyqb_user.user where phone_no="$phone_no""`
if [[ $user_id == "" ]];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_select=`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_select}"`
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_select, ${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

echo "运营商、芝麻信用验证通过"
