#!/bin/bash
ip=$1
phone_no=$2
bill_num=3
bill_num_a=0
PORT=$3
USERNAME="qa"
PASSWORD="qatest"
mysqld="-h${ip} -P${PORT} -u${USERNAME} -p${PASSWORD}"
date_now=`date "+%Y-%m-%d %H:%M:%S"`

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



                       #生成订单
                       
                      #白条订单表增加数据

                       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}"`
                     #  echo $select_user_id

                       order_no='xyqb'${phone_no}
                       order="INSERT INTO baitiao_order( "user_id", "merchant_id", "order_no", "order_name", "order_amount", "repay_term", "status", "created_at", "updated_at", "baitiao_order_type") VALUES (${xyqb_user_id}, 7, '${order_no}', '信用钱包订单', 2499.000000, 1,1, '$date_now', '$date_now', 0)"
                       mysql  $mysqld xyqb -e "set names utf8;${order}"
                      # echo " order=$order"


                       select_order_id="SELECT id  from baitiao_order where user_id=${xyqb_user_id}"
                       baitiao_order_id=`mysql -N $mysqld xyqb -e "set names utf8;${select_order_id}"`
                       #echo "select_order_id=$select_order_id"
                      # echo "baitiao_order_id=$baitiao_order_id"
                       insert_fund="INSERT INTO baitiao_fund_order ( "order_id", "status", "rate", "refund_status", "start_interest_date", "fund_id", "finance_record_id", "created_at", "updated_at") VALUES ( $baitiao_order_id, 0, 0.060200, 0, NULL, '3', NULL, '$date_now', '$date_now')"
                      # echo "insert_fund=$insert_fund"
                        mysql  $mysqld xyqb -e "set names utf8;${insert_fund}"
                       #白条还款计划表增加数据
                       repayment_plan1="INSERT INTO baitiao_bill_repayment_plan ("bill_id", "user_id", "user_phone", "order_id", "order_no", "order_name", "order_time", "merchant_id", "order_amount", "deadline", "term_no", "contract_term", "rate", "principal", "interest", "penalty_interest", "red_packet_amount", "collection_reduction", "repay_amount", "repay_amount_compute_time", "lock_time", "repaid_at", "repayment_received_at", "repayment_status", "pay_center_order_no", "enable", "created_at", "updated_at") VALUES (NULL, ${xyqb_user_id}, '${phone_no}', ${baitiao_order_id},'信用钱包订单', '手机', '$date_now', 7, 2499.000000, '2018-01-20 23:59:59', 1, 3, 0.030000, 833.000000, 74.970000, 0.000000, 0.000000, 0.000000, 907.970000, '$date_now', NULL, '$date_now', NULL, '0', NULL, 1, '$date_now', '$date_now')"
                       mysql  $mysqld xyqb -e "set names utf8;${repayment_plan1}"
                     # echo "plan1=$repayment_plan1"
                                                           

                       repayment_plan2="INSERT INTO baitiao_bill_repayment_plan ("bill_id", "user_id", "user_phone", "order_id", "order_no", "order_name", "order_time", "merchant_id", "order_amount", "deadline", "term_no", "contract_term", "rate", "principal", "interest", "penalty_interest", "red_packet_amount", "collection_reduction", "repay_amount", "repay_amount_compute_time", "lock_time", "repaid_at", "repayment_received_at", "repayment_status", "pay_center_order_no", "enable", "created_at", "updated_at") VALUES (NULL, ${xyqb_user_id}, '${phone_no}', ${baitiao_order_id}, '信用钱包订单', '手机','$date_now', 7, 2499.000000, '2018-02-20 23:59:59', 2, 3, 0.030000, 833.000000, 74.970000, 0.000000, 0.000000, 0.000000, 907.970000, '$date_now', NULL, '$date_now',NULL, '0', NULL, 1,'$date_now', '$date_now')"

                       mysql  $mysqld xyqb -e "set names utf8;${repayment_plan2}"
                      #echo "plan2=$repayment_plan2"
                       repayment_plan3="INSERT INTO baitiao_bill_repayment_plan ("bill_id", "user_id", "user_phone", "order_id", "order_no", "order_name", "order_time", "merchant_id", "order_amount", "deadline", "term_no", "contract_term", "rate", "principal", "interest", "penalty_interest", "red_packet_amount", "collection_reduction", "repay_amount", "repay_amount_compute_time", "lock_time", "repaid_at", "repayment_received_at", "repayment_status", "pay_center_order_no", "enable", "created_at", "updated_at") VALUES (NULL, ${xyqb_user_id}, ${phone_no}, ${baitiao_order_id}, '信用钱包订单', '手机', '$date_now', 7, '2499.000000', '2018-03-20 23:59:59', '3', '3', '0.030000', '833.000000', '74.970000', '0.000000', '0.000000', '0.000000', '907.970000', '$date_now', NULL,'$date_now', NULL, '0', NULL, '1', '$date_now','$date_now')"
                       mysql  $mysqld xyqb -e "set names utf8;${repayment_plan3}"

                       #echo "plan3=$repayment_plan3"
                       #白条额度表变化表增加数据

                       consume_seq1='ABCD201708051511458'${phone_no}

                       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}, ${baitiao_order_id}, '2499.00', '13501.00', '1', '${consume_seq1}', '$date_now','$date_now')"
           #            echo "consume=$consume"
                       mysql  $mysqld xyqb -e "set names utf8;${consume}"
          fi
}
 baitiao_order $ip $phone_no 
