

#!/bin/bash


set -e
ip=$1
phone_no=$2
port=$3
#loan_application_history_id=$3
#name=$4


HOSTNAME="$ip"
USERNAME="qa"
PASSWORD="qatest"

DBNAME0="risk_control"
TABLENAME0="review_history"

DBNAME1="xyqb"
TABLENAME1="waiting_funding_corp_operate_people"
TABLENAME5="contract"

DBNAME2="gyxd"
TABLENAME2="loan_application_manifest_history"
TABLENAME3="repayment_plan"
TABLENAME4="loan_user"

mysqld="-h$ip -P$port -uqa -pqatest"


date="`date +%Y%m%d%H%M%S`"
echo $date
date_now=`date +"%Y-%m-%d %T"`
echo $date_now

deadline1=`date +"%Y-%m-%d %T" -d "next month"`
deadline2=`date +"%Y-%m-%d %T" -d "2 month"`
deadline3=`date +"%Y-%m-%d %T" -d "3 month"`



len=`echo $phone_no|awk '{print length($0)}'`
if [ $len -ne 11 ];then

        echo "请输入11位手机号!"
        exit -1
fi

#clotho进行直投推送，数据至广源后台系统

loan_application_history_id=`mysql -N $mysqld ${DBNAME0} -e "select loan_application_history_id from ${TABLENAME0} where phone_no=${phone_no}"`

con1="update xyqb.waiting_funding_corp_operate_people
set xyqb.waiting_funding_corp_operate_people.funding_corp_progress=4
where is_enable=1 and loan_application_history_id=$loan_application_history_id";

mysql -N $mysqld  ${DBNAME1} -e"set names utf8; ${con1}"

#推送成功后广源库loan_user表增加推送数据

name=`mysql -N $mysqld ${DBNAME0} -e "select name from ${TABLENAME0} where phone_no=${phone_no}"`
id_no=`mysql -N $mysqld ${DBNAME0} -e "select id_no from ${TABLENAME0} where phone_no=${phone_no}"`


con2="INSERT INTO gyxd.loan_user(customer_id, channel_id, phone_no, name, id_no, id_type,gender, birthday, postal_address, postal_address_zip_code, residence_address, residence_address_zip_code, education, degree, professional_title, marital_status, template_id, template_location_type, template_is_farmer, template_is_self_employed, template_work_unit, template_industry, is_put, level, created_at, updated_at, template_invest_industry)

VALUES
( '', 1, ${phone_no},'${name}', ${id_no}, 0, 2, '1985-02-10', '北京市市辖区怀柔区1', '101400', '北京市市辖区怀柔区1', '101400', 0, 6, 4, 2, 130, 1, 0, 0, '未知', 'L', 0, 0, '$date_now', '$date_now', 'L7440');"


mysql  -N $mysqld  ${DBNAME2} -e"set names utf8; ${con2}"

#广源库loan_application_manifest_history表增加数据

user_id=`mysql -N $mysqld ${DBNAME2} -e "select id from ${TABLENAME4} where phone_no=${phone_no}"`
protocol_number=`mysql -N $mysqld ${DBNAME1} -e "select ssq_contract_no from ${TABLENAME5} where loan_application_history_id=${loan_application_history_id}"`
echo $protocol_number


con3="INSERT INTO gyxd.loan_application_manifest_history(loan_id,loan_user_id,channel_id,credit_assignment,bank_card_id,loan_progress,reported_progress,amount,term, purpose,protocol_number,service_fee_per_term,total_repayment_amount,monthly_repayment,annual_interest_rate,principal_and_interest_per_term,total_principal_and_interest,loan_received_at,loan_paid_at,transaction_status,remark,payment_notification_url,created_at,updated_at,notify_at,sign_status,loan_status,recall_status, repay_status,debt_status,distribute_fund_id,credit_assigned_at)
VALUES
    (${loan_application_history_id}, ${user_id}, 1, 99, 313380, 7, 1, 2000.00, 3, '消费', '${protocol_number}', 150.00, 2490.93, 830.31, 0.122400, 680.31, 2040.93, '$date_now', '$date_now', NULL, '', 'http://${ip}:7082/external/gy/callback', '$date_now', '$date_now', '$date_now', 2, 2, 0, 0, 0, 2, '$date_now');"


echo user_id
echo ${user_id}
mysql -N $mysqld  ${DBNAME2} -e"set names utf8; ${con3}"

#gyxd增加还款计划

loan_application_manifest_history_id=`mysql -N $mysqld ${DBNAME2} -e "select id from ${TABLENAME2} where loan_id =${loan_application_history_id}"`



con4="INSERT INTO gyxd.repayment_plan(loan_user_id,loan_application_manifest_history_id,loan_id,channel_id,term_no,deadline,current_repayment,required_repayment,repayment_status,repaid_at,qg_repay_at,credit_assigned_at,overdue_interest,activity_relief,repayment_received_at,remark,collection_relief,principal_amount, interest,service_fee,reported_progress,credit_assignment,debt_fund_id,debt_status,pay_approach,pay_approach_desc,compensation_status,compensation_at,overdue_service_fee,pay_order_no,actual_service_fee)
VALUES
(${user_id}, ${loan_application_manifest_history_id}, ${loan_application_history_id}, 0, 1, '${deadline1}', 0.00, 830.31, 0, '2000-01-01 00:00:00', NULL, NULL, 0.00, 0.00, '2000-01-01 00:00:00', '', 0.00, 666.64, 13.67, 150.00, 0, 99, NULL, 0, NULL, NULL, 0, NULL, 0.00, NULL, 0.00),
(${user_id}, ${loan_application_manifest_history_id}, ${loan_application_history_id}, 0, 2, '${deadline2}', 0.00, 830.31, 0, '2000-01-01 00:00:00', NULL, NULL, 0.00, 0.00, '2000-01-01 00:00:00', '', 0.00, 666.64, 13.67, 150.00, 0, 99, NULL, 0, NULL, NULL, 0, NULL, 0.00, NULL, 0.00),
(${user_id}, ${loan_application_manifest_history_id}, ${loan_application_history_id}, 0, 3, '${deadline3}', 0.00, 830.32, 0, '2000-01-01 00:00:00', NULL, NULL, 0.00, 0.00, '2000-01-01 00:00:00', '', 0.00, 673.45, 6.87, 150.00, 0, 99, NULL, 0, NULL, NULL, 0, NULL, 0.00, NULL, 0.00);"

mysql -N $mysqld  ${DBNAME2} -e"set names utf8; ${con4}"

#广源系统签约、放款、回盘

con5="update gyxd.loan_application_manifest_history
set gyxd.loan_application_manifest_history.loan_progress=7,gyxd.loan_application_manifest_history.sign_status=2
where loan_id=$loan_application_history_id";

echo con5
echo $con5

 mysql -N $mysqld  ${DBNAME2} -e"set names utf8; ${con5}"
  

#放款结果回调xyqb，loan表及people表进行数据状态变更

 con6="update xyqb.loan_application_history
 set xyqb.loan_application_history.progress=15
 where id=$loan_application_history_id";

echo con6
echo $con6
 
  mysql -N $mysqld  ${DBNAME1} -e"set names utf8; ${con6}"

 con7="update xyqb.waiting_funding_corp_operate_people
 set xyqb.waiting_funding_corp_operate_people.funding_corp_progress=5
 where loan_application_history_id=$loan_application_history_id";

echo con7
echo $con7

 
  mysql -N $mysqld ${DBNAME1} -e"set names utf8; ${con7}"

