#!/bin/bash
ip=$1
phone_no=$2
port=$3
DBNAME="xyqb_user"
DBNAME1="payment_center"
DBNAME2="xyqb"
DBNAME3="spider_center"
TABLENAME="user"
mysqld="-h$ip -P$port -uqa -pqatest"
date=`date "+%Y-%m-%d %H:%M:%S"`

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

mysql $mysqld -e "select version();" &>/dev/null
if [ $? -ne 0 ];then
	echo "$ip Mysql is not running! $date"
	exit -1
fi

function deleteAlldata()
{
	len=`echo $phone_no|awk '{print length($0)}'`
	if [ $len -ne 11 ]
	then
		echo "请输入11位手机号!"
	else
		#各数据库中获取id值
		xyqb_user_id=`mysql -N $mysqld ${DBNAME} -e "select id from ${TABLENAME} where phone_no=${phone_no}"`
		if [ -z $xyqb_user_id ];then
			echo "用户中心不存在该手机号用户"
		else
			echo "删除用户中心数据，xyqb_user_id=$xyqb_user_id"
			#订单号
			loan_ids=`mysql -N $mysqld ${DBNAME2} -e "select id from loan_application_history where user_id=${xyqb_user_id}"`
			loan_id=`echo $loan_ids | sed "s# #,#g"`
			mysql  $mysqld ${DBNAME} -e "delete from user where phone_no=$phone_no"
			mysql  $mysqld ${DBNAME} -e "delete from user_detail where phone_no=$phone_no"
			mysql  $mysqld ${DBNAME} -e "delete from address where user_id=$xyqb_user_id"
			mysql  $mysqld ${DBNAME} -e "delete from contact where user_id=$xyqb_user_id"
			mysql  $mysqld ${DBNAME} -e "delete from user_ext_info where user_id=$xyqb_user_id"
			mysql  $mysqld ${DBNAME} -e "delete from wechat_userinfo where user_id=$xyqb_user_id"

			echo "删除xyqb数据，xyqb_user_id=$xyqb_user_id，loan_id=$loan_id"
			mysql  $mysqld ${DBNAME2} -e "delete from user_contact where user_id=$xyqb_user_id"
			mysql  $mysqld ${DBNAME2} -e "delete from loan_application_history where user_id=$xyqb_user_id"
			mysql  $mysqld ${DBNAME2} -e "delete from baitiao_account where user_id=$xyqb_user_id"
			mysql  $mysqld ${DBNAME2} -e "delete from baitiao_bill where user_id=$xyqb_user_id"
			mysql  $mysqld ${DBNAME2} -e "delete from baitiao_bill_repayment_plan where user_id=$xyqb_user_id"
			mysql  $mysqld ${DBNAME2} -e "delete from baitiao_bill_transfer_record where user_id=$xyqb_user_id"
			mysql  $mysqld ${DBNAME2} -e "delete from baitiao_consume_history where user_id=$xyqb_user_id"
			mysql  $mysqld ${DBNAME2} -e "delete from baitiao_order where user_id=$xyqb_user_id"
			mysql  $mysqld ${DBNAME2} -e "delete from baitiao_order_history where user_id=$xyqb_user_id"
			mysql  $mysqld ${DBNAME2} -e "delete from baitiao_repayment_plan where user_id=$xyqb_user_id"
			mysql  $mysqld ${DBNAME2} -e "delete from loan_application_manifest_history where user_id=$xyqb_user_id"
			mysql  $mysqld ${DBNAME2} -e "delete from loan_demand_history_app where phone_no=$phone_no"
			mysql  $mysqld ${DBNAME2} -e "delete from pay_center_bank_card where phone_no=$phone_no"
			mysql  $mysqld ${DBNAME2} -e "delete from pay_center_biao_tiao_bank_card where phone_no=$phone_no"
			mysql  $mysqld ${DBNAME2} -e "delete from repayment_plan where user_id=$xyqb_user_id"
			if [ -n "$loan_id" ];then
				mysql  $mysqld ${DBNAME2} -e "delete from loan_demand_history where loan_application_history_id in ($loan_id)"
				mysql  $mysqld ${DBNAME2} -e "delete from loan_submit_info where loan_id in ($loan_id)"
				mysql  $mysqld ${DBNAME2} -e "delete from user_authorization_history where loan_application_history_id in ($loan_id)"
				mysql  $mysqld ${DBNAME2} -e "delete from user_finance_history where loan_application_history_id in ($loan_id)"
				mysql  $mysqld ${DBNAME2} -e "delete from waiting_for_assignment where loan_application_history_id in ($loan_id)"
				mysql  $mysqld ${DBNAME2} -e "delete from waiting_funding_corp_operate_people where loan_application_history_id in ($loan_id)"

				echo "删除cash-loan-flow数据"
				mysql  $mysqld cash_loan_flow -e "delete from order_progress_record where loan_id in ($loan_id)"
			fi
			echo "删除redis缓存"
			redis_key=`redis-cli -h $ip -p 6379 keys *$phone_no*`
			if [ -n "$redis_key" ];then
				echo $redis_key
				redis-cli -h $ip -p 6379 del $redis_key
			fi
			redis_key=`redis-cli -h $ip -p 6379 keys *$xyqb_user_id*`
			if [ -n "$redis_key" ];then
				echo $redis_key
				token=`redis-cli -h $ip -p 6379 get userid-sessionvalue:cache::$xyqb_user_id:xyqb`
				redis-cli -h $ip -p 6379 del $redis_key
				redis_key=`redis-cli -h $ip -p 6379 keys *$token*`
				echo $redis_key
				redis-cli -h $ip -p 6379 del $redis_key
				#redis-cli -h $ip -p 6379 keys *$token* | xargs redis-cli -h $ip -p 6379 del
			fi
		fi

		#支付中心id
		payment_center_user_id=`mysql -N $mysqld ${DBNAME1} -e "select id from ${TABLENAME} where phone_no=${phone_no}"`
		if [ -z $payment_center_user_id ];then
			echo "支付中心不存在该手机号用户"
		else
			card_nos=`mysql -N $mysqld ${DBNAME1} -e "select card_no from bank_card where user_id=${payment_center_user_id}"`
			card_no=`echo $card_nos | sed "s# #,#g"`
			echo "删除支付中心数据，payment_center_user_id=$payment_center_user_id，card_no:$card_no"
			mysql  $mysqld ${DBNAME1} -e "delete from ${TABLENAME} where phone_no=$phone_no"
			mysql  $mysqld ${DBNAME1} -e "delete from user_detail where phone_no=$phone_no"
			mysql  $mysqld ${DBNAME1} -e "delete from bank_card_detail where card_no in ($card_no)"
			mysql  $mysqld ${DBNAME1} -e "delete from bank_card where user_id=$payment_center_user_id"
			mysql  $mysqld ${DBNAME1} -e "delete from batch_pay_bankcard where user_id=$payment_center_user_id"
			mysql  $mysqld ${DBNAME1} -e "delete from bind_card_order where user_id=$payment_center_user_id"
			mysql  $mysqld ${DBNAME1} -e "delete from card_bind_info where user_id=$payment_center_user_id"
			mysql  $mysqld ${DBNAME1} -e "delete from merchant_card_rel where user_id=$payment_center_user_id"
			mysql  $mysqld ${DBNAME1} -e "delete from repay_order where user_id=$payment_center_user_id"
		fi


		#授信中心用户ID
		spider_id=`mysql -N $mysqld ${DBNAME3} -e "select id from ${TABLENAME} where phone_no=${phone_no}"`
		if [ -z $spider_id ];then
			echo "爬虫中心不存在该手机号用户"
		else
			echo "删除爬虫中心数据 spider_id=$spider_id"
			mysql  $mysqld ${DBNAME3} -e "delete from user_authorization where user_id=$spider_id"
			mysql  $mysqld ${DBNAME3} -e "delete from merchant_auth_rel where user_id=$spider_id"
			mysql  $mysqld ${DBNAME3} -e "delete from ${TABLENAME} where phone_no=$phone_no"
			mysql  $mysqld ${DBNAME3} -e "delete from user_detail where phone_no=$phone_no"
			merchant_order_no=`mysql -N $mysqld ${DBNAME3} -e "select merchant_order_no from merchant_auth_rel where user_id=${spider_id}"`
			if [ -n "$merchant_order_no" ];then
				mysql  $mysqld ${DBNAME3} -e "delete from notify_message where merchant_order_no=$merchant_order_no"
			fi
		fi

		echo "删除红包系统数据"
		mysql  $mysqld redpacket -e "delete from redpacket_mall where phone_no=$phone_no"
		mysql  $mysqld redpacket -e "delete from tb_coupon where phone=$phone_no"

		echo "删除谛听中心数据"
		mysql  $mysqld risk_control -e "delete from review_history where phone_no=$phone_no"

	fi
}
 
deleteAlldata $ip $phone_no
