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

function authorization()
{
	len=`echo $phone_no|awk '{print length($0)}'`
	if [ $len -ne 11 ];then
		echo "请输入11位手机号!"     
	else
		#查询userID
		select_sql="select id from ${TABLENAME} where phone_no=${phone_no}"
		user_id=`mysql -N $mysqld xyqb_user -e "${select_sql}"`
		echo "user_id=$user_id"

		order_id=`mysql -N $mysqld xyqb -e "select id from baitiao_order where user_id='$user_id' order by id desc limit 1"`
		plan_id=`mysql -N $mysqld xyqb -e "select id from baitiao_bill_repayment_plan where order_id='$order_id' limit 1"`
		fund_order_id=`mysql -N $mysqld xyqb -e "select id from baitiao_fund_order where order_id='$order_id'"`
		term_no=`mysql -N $mysqld xyqb -e "select count(*) from baitiao_bill_repayment_plan where order_id='$order_id'"`

		baitiao_fund_repayment_plan=`mysql -N $mysqld xyqb -e "select id from baitiao_fund_repayment_plan where plan_id='$plan_id'"`
		if [[ -n "$baitiao_fund_repayment_plan" ]]; then
			echo "finish~"
			exit
		else
			insert_sql0="insert into baitiao_fund_repayment_plan("plan_id","fund_order_id","term_no","principal","interest","discount","finance_record_id","status") values ($plan_id,'$fund_order_id',1,'0','0','0',NULL,'0')"
			mysql $mysqld ${DBNAME} -e "${insert_sql0}"
			#echo $insert_sql0
			insert_sql0="insert into baitiao_fund_repayment_plan("plan_id","fund_order_id","term_no","principal","interest","discount","finance_record_id","status") values ($plan_id+1,'$fund_order_id',2,'0','0','0',NULL,'0')"
			mysql $mysqld ${DBNAME} -e "${insert_sql0}"
			#echo $insert_sql0
			insert_sql0="insert into baitiao_fund_repayment_plan("plan_id","fund_order_id","term_no","principal","interest","discount","finance_record_id","status") values ($plan_id+2,'$fund_order_id',3,'0','0','0',NULL,'0')"
			mysql $mysqld ${DBNAME} -e "${insert_sql0}"
			#echo $insert_sql0

			update_sql="UPDATE baitiao_fund_repayment_plan as t1, baitiao_bill_repayment_plan as t2 SET t1.principal = t2.principal,t1.interest=t2.interest,t1.deadline=t2.deadline WHERE t1.plan_id = t2.id and t1.plan_id=$plan_id"
			mysql $mysqld ${DBNAME} -e "${update_sql}"
			#echo $update_sql
			update_sql="UPDATE baitiao_fund_repayment_plan as t1, baitiao_bill_repayment_plan as t2 SET t1.principal = t2.principal,t1.interest=t2.interest,t1.deadline=t2.deadline WHERE t1.plan_id = t2.id and t1.plan_id=$plan_id+1"
			mysql $mysqld ${DBNAME} -e "${update_sql}"
			#echo $update_sql
			update_sql="UPDATE baitiao_fund_repayment_plan as t1, baitiao_bill_repayment_plan as t2 SET t1.principal = t2.principal,t1.interest=t2.interest,t1.deadline=t2.deadline WHERE t1.plan_id = t2.id and t1.plan_id=$plan_id+2"
			mysql $mysqld ${DBNAME} -e "${update_sql}"
			#echo $update_sql

			mon=`date "+%Y%m"`
			insert_sql0="insert into baitiao_bill("user_id","user_phone","month","already_repay_amount","bill_repay_status","enable") values ('$user_id','$phone_no','$mon','0','1','1')"
			mysql $mysqld ${DBNAME} -e "${insert_sql0}"
			#echo $insert_sql0
			mon=`date -d '1 month' '+%Y%m'`
			insert_sql0="insert into baitiao_bill("user_id","user_phone","month","already_repay_amount","bill_repay_status","enable") values ('$user_id','$phone_no','$mon','0','0','1')"
			mysql $mysqld ${DBNAME} -e "${insert_sql0}"
			#echo $insert_sql0
			mon=`date -d "2 month" "+%Y%m"`
			insert_sql0="insert into baitiao_bill("user_id","user_phone","month","already_repay_amount","bill_repay_status","enable") values ('$user_id','$phone_no','$mon','0','0','1')"
			mysql $mysqld ${DBNAME} -e "${insert_sql0}"
			#echo $insert_sql0

			bill_id=`mysql -N $mysqld xyqb -e "select id from baitiao_bill where user_id='$user_id' order by id desc limit 1"`
			echo "bill_id=$bill_id"
			#insert_sql0="insert into baitiao_finance_record("third_no","total_amount","local_no","type","remark") values ('$date',10000,NULL,'0',N'笑脸资金方批量放款到量化派')"
			#mysql $mysqld ${DBNAME} -e "${insert_sql0}"

			update_sql="update baitiao_bill_repayment_plan set bill_id=$bill_id-2 where order_id=$order_id and term_no=1"
			mysql $mysqld ${DBNAME} -e "${update_sql}"
			#echo $update_sql
			update_sql="update baitiao_bill_repayment_plan set bill_id=$bill_id-1 where order_id=$order_id and term_no=2"
			mysql $mysqld ${DBNAME} -e "${update_sql}"
			#echo $update_sql
			update_sql="update baitiao_bill_repayment_plan set bill_id=$bill_id where order_id=$order_id and term_no=3"
			mysql $mysqld ${DBNAME} -e "${update_sql}"
			#echo $update_sql

			update_sql="update baitiao_fund_order set status=3,start_interest_date='$date_now' where order_id='$order_id'"
			mysql $mysqld ${DBNAME} -e "${update_sql}"
			#echo $update_sql

			echo "done!!! $date_now"
		fi
	fi
}

authorization $ip $phone_no
