#!/bin/bash
ip=$1
phone_no=$2
num=$3  #激活白条的数量
merchant_id=$4 #渠道编号
bill_num_a=0
PORT="3306"
USERNAME="qa"
PASSWORD="qatest"
mysqld="-h${ip} -P${PORT} -u${USERNAME} -p${PASSWORD}"
date_now=`date "+%Y-%m-%d %H:%M:%S"`

function activate()
{
    len=`echo $phone_no|awk '{print length($0)}'`
    if [ $len -ne 11 ] ; then
       echo "请输入11位手机号 ！"
    else
       if [ -z $num ];then
          num=1
       fi
       if [ -z ${merchant_id} ];then
          merchant_id=7
       fi

       i=0
       while [ $i -lt $num ]; do
           uuid='928ba526-29f1-4ef8-8bb8-'${phone_no}
           insert_xyqb_user="INSERT INTO user( "phone_no", "password", "registered_from", "uuid", "enable", "created_at", "updated_at")VALUES ( ${phone_no}, '318e235d3e52648b236faa3f748000d5', '222', '${uuid}', '1', '$date_now', '$date_now')"
           mysql  $mysqld xyqb_user -e "set names utf8;${insert_xyqb_user}"

           select_user_id="SELECT id  from user where user.phone_no=${phone_no}"
           xyqb_user_id=`mysql -N $mysqld xyqb_user -e "set names utf8;${select_user_id}"`

           #：用户信息表增加数据
          insert_user_detail="INSERT INTO user_detail("user_id", "phone_no", "name", "id_no", "id_type", "is_authenticated", "gender", "email", "qq", "created_at", "updated_at")VALUES (${xyqb_user_id}, ${phone_no}, '滕尘', '532523198811050620', 0, 0, 1, '', 'undefined', '$date_now', '$date_now')"
          mysql  $mysqld xyqb_user -e "set names utf8;${insert_user_detail}"
          # 贷款记录表增加数据
          insert_history="INSERT INTO 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 (${xyqb_user_id}, 222, 100, 1, 39, 222, 1, NULL, ${merchant_id}, '${date_now}', '${date_now}')"
          mysql  $mysqld xyqb -e "set names utf8;${insert_history}"

          #激活记录表增加数据
          insert_step_info="INSERT INTO loan_step_info ("user_id", "merchant_id", "status_code", "created_at")VALUES (${xyqb_user_id}, ${merchant_id}, 0, '$date_now'), (${xyqb_user_id}, ${merchant_id}, 5, '$date_now')"
          mysql  $mysqld xyqb -e "set names utf8;${insert_step_info}"

          #白条额度表修改数据
          insert_account="INSERT INTO baitiao_account( "user_id", "registered_from", "pay_pwd", "pay_pwd_salt", "credit_amount", "status", "created_at", "updated_at")VALUES ( ${xyqb_user_id}, ${merchant_id}, '55cfe3b492c6528ef2ba27a52b439a93', 'Q0I0QTQzRjcxNTExMDUyQjAwNTY2MTI2', '16000.00', '1', '$date_now', '$date_now')"
          mysql  $mysqld xyqb -e "set names utf8;${insert_account}"

          #白条额度变化表增加数据
          consume_seq='XYBT201708051511458'${phone_no}
          insert_consume="INSERT INTO baitiao_consume_history ("user_id", "order_id", "order_amount", "remaining_amount", "consume_type", "consume_seq", "created_at", "updated_at")VALUES (${xyqb_user_id}, -1, 16000.00, 16000.00, 0, '${consume_seq}','$date_now', '$date_now' )"
          mysql  $mysqld xyqb -e "set names utf8;${insert_consume}"

          #授信用户表
          uuid1='69891c47-e8c7-4b96-8a8d-'${phone_no}
          insert_spider="INSERT INTO user ("phone_no", "password", "registered_from", "uuid", "enable", "created_at", "updated_at")VALUES (${phone_no}, '4c00961baa8857e558c10db0e58c003e', 222, '${uuid1}', 1, '$date_now', '$date_now')"
          mysql  $mysqld spider_center -e "set names utf8;${insert_spider}"
         #授权表增加数据
         select_spider_user_id="select id  FROM user WHERE user.phone_no=${phone_no}"
         spider_user_id=`mysql -N $mysqld spider_center -e "${select_spider_user_id}"`
         insert_authorization="INSERT INTO user_authorization ("auth_type", "last_auth_time", "auth_account_name", "user_id", "auth_status")VALUES(0, '$date_now', NULL, ${spider_user_id}, 1),(3, '$date_now', NULL, ${spider_user_id}, 1)"
         mysql  $mysqld spider_center -e "set names utf8;${insert_authorization}"

         #mall用户表
         uuid2='3a324e8b-78dc-45a7-bf16-'${phone_no}
         insert_mall="INSERT INTO user("phone_no", "password", "registered_from", "uuid", "enable", "created_at", "updated_at")VALUES ( ${phone_no}, '318e235d3e52648b236faa3f748000d5', '214', '${uuid2}', '1', '$date_now', '$date_now')"
         mysql  $mysqld mall -e "set names utf8;${insert_mall}"
         select_mall_user_id="select  id  FROM user WHERE  phone_no=${phone_no}"
         mall_user_id=`mysql -N  $mysqld mall -e "${select_mall_user_id}"`
         insert_mall_user_detail="INSERT INTO user_detail ( "user_id", "phone_no", "name", "id_no", "id_type", "is_authenticated", "gender", "created_at", "updated_at")VALUES (${mall_user_id}, ${phone_no}, '滕尘', '532523198811050620', '0', '0', '1', '$date_now', '$date_now')"
         mysql  $mysqld mall -e "set names utf8;${insert_mall_user_detail}"
         #mall.baitiao_card
         insert_mall_card="INSERT INTO baitiao_card ( "user_id", "phone_no", "baitiao_activate_status", "created_at", "updated_at", "available_credit", "overdue_qgbc", "overdue_xyqb", "credit_enough", "next_redirect", "risk_control_message", "original_credit", "pass_risk_control")VALUES ( ${mall_user_id}, ${phone_no}, '2', '$date_now', '$date_now', '16000.00', '0', '0', '1', NULL,NULL, '16000.00', '1')"
         mysql  $mysqld mall -e "set names utf8;${insert_mall_card}"
         i=$(( $i + 1 ))
         phone_no=$(( $phone_no + 1 ))
      done
   
   if [ $i -eq $num ];then
     echo "successful done! `date`"
   else 
     echo "failed! `date` 建议输入合适的手机号基数"
   fi
 fi
}
activate $ip $phone_no $num $merchant_id
