#!/bin/bash
ip=$1
phone_no=$2
fund_id=$3
PORT=$4
USERNAME="qa"
PASSWORD="qatest"
len=`echo $phone_no|awk '{print length($0)}'`
if [ $len -ne 11 ];then
	echo "请输入11位手机号!"
	exit -1
fi

if [ -z $fund_id ];then
	fund_id=140
fi

date=`date "+%Y-%m-%d %H:%M:%S"`

mysql -h"$ip" -P"${PORT}" -u${USERNAME} -p${PASSWORD} <<EOF
use xyqb;
update xyqb.loan_application_history
set xyqb.loan_application_history.progress=9
where business_type=0 and user_id=(select id from xyqb_user.user where phone_no =$phone_no) and is_active=1;

delete from xyqb.loan_application_manifest_history 
where loan_application_history_id =111112222233333;

INSERT INTO xyqb.loan_application_manifest_history (user_id, loan_application_history_id, channel_id, product_id, transaction_flow_id, funding_corp_id, funding_corp_name, contract_loan_amount, contract_term, service_fee_per_term, one_time_service_fee, monthly_interest_rate, total_repayment_amount, monthly_repayment, real_loan_amount, annual_interest_rate, principal_and_interest_per_term, total_principal_and_interest, company_income, loan_received_at, loan_paid_at, transaction_status, remark, red_packet_strategy) 
VALUES (1, 111112222233333, 1, 32, 0, $fund_id, N'测试资金方', 2000.00, 3, 50.00, 0.00, 0.011675, 2490.94, 830.31, 2000.00, 0.140100, 680.31, 2040.94, 40.94, '2017-06-08 18:39:33', '1989-09-29 00:00:00', 0, '', NULL);

delete from xyqb.loan_application_manifest_history
where loan_application_history_id=(select id from xyqb.loan_application_history where business_type=0 and user_id=(select id from xyqb_user.user where phone_no =$phone_no) and is_active=1);

update xyqb.loan_application_manifest_history 
set user_id=(select id from xyqb_user.user where phone_no =$phone_no),
loan_application_history_id=(select id from xyqb.loan_application_history where business_type=0 and user_id=(select id from xyqb_user.user where phone_no =$phone_no) and is_active=1),
loan_received_at='$date',
product_id=(select distinct(product_id) from risk_control.product_strategy_dispatcher where funding_corp_id=$fund_id),
monthly_interest_rate=(select monthly_interest_rate from xyqb.funding_corp where id=$fund_id),
annual_interest_rate=(select monthly_interest_rate from xyqb.funding_corp where id=$fund_id)*12,
loan_paid_at='$date' 
where loan_application_history_id =111112222233333;

update xyqb.page_state_info
set state='AuditedSuccState'
where loan_id=(select id from xyqb.loan_application_history where business_type=0 and user_id=(select id from xyqb_user.user where phone_no =$phone_no) and is_active=1);

#update risk_control.review_history
#set progress=0
#where phone_no=$phone_no

EOF
echo "done!!! $date"
