#!/bin/bash
ip=$1
phone_no=$2
PORT="3306"
USERNAME="qa"
PASSWORD="qatest"
mysqld="-h${ip} -P${PORT} -u${USERNAME} -p${PASSWORD}"

function baitiaoDeleteOrder(){

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


    select_xyqb_user="select count(*) from user where phone_no='${phone_no}'"
    count_xyqb_user=`mysql -N $mysqld xyqb_user -e "${select_xyqb_user}"`
   # echo "count_xyqb_user = $count_xyqb_user"

#xyqb_user、xyqb
    while [ $count_xyqb_user -gt 0 ];do
      # echo "count_xyqb_user = $count_xyqb_user"
        user_id_sql="select id from user where phone_no='${phone_no}' limit 1"
       # echo $user_id_sql
        user_id=`mysql -N $mysqld xyqb_user -e "${user_id_sql}"`         
        if [ ! $user_id ];then
           echo "user_id is null"
            continue
        else 
         echo "user_id=$user_id" 
            order_id_sql="select id  from baitiao_order where user_id=${user_id} LIMIT 1";
          #  echo $order_id_sql
            order_id=`mysql -N $mysqld xyqb -e "${order_id_sql}"`
           
            if [ ! $order_id ];then
               echo "order_id is null"
           else
            #  echo "order_id=$order_id"
              fund_id_sql="SELECT id from xyqb.baitiao_fund_order WHERE order_id=${order_id} limit 1";
             # echo $fund_id_sql
              fund_id=`mysql -N $mysqld xyqb -e "${fund_id_sql}"`
              #echo $fund_id_sql
              delete2="delete from baitiao_fund_order where xyqb.baitiao_fund_order.order_id=${order_id}";
             # echo $delete2;
              mysql -N $mysqld xyqb -e "${delete2}"   
              delete5="delete from baitiao_fund_repayment_plan where baitiao_fund_repayment_plan.fund_order_id=${fund_id}";
              #echo $delete5
              mysql -N $mysqld xyqb -e "${delete5}"
            fi
#xyqb.user/user_detail/baitiao_account/baitiao_bill/baitiao_bill_day/baitiao_bill_repayment_plan/
#xyqb.baitiao_consume_history/baitiao_fund_order/baitiao_fund_repayment_plan/baitiao_order/loan_application_history/loan_step_info/repayment_plan
#xyqb_user.user/user_detail
           del_user="delete from user where user.id=${user_id}"
           mysql -N $mysqld xyqb -e "${del_user}"
           mysql -N $mysqld xyqb_user -e "${del_user}"  
           #echo $del_user      
           del_user_detail="delete from user_detail where user_detail.user_id=${user_id}"
           mysql -N $mysqld xyqb -e "${del_user_detail}"
           mysql -N $mysqld xyqb_user -e "${del_user_detail}"
          # echo $del_user_detail
           del_account="delete from baitiao_account where baitiao_account.user_id=${user_id}"
           mysql -N $mysqld xyqb -e "${del_account}"
           delete1="delete from baitiao_order where xyqb.baitiao_order.user_id=${user_id}";
           #echo $delete1;
           mysql -N $mysqld xyqb -e "${delete1}"
           delete3="delete from loan_application_history where loan_application_history.user_id=${user_id}"
          # echo $delete3
           mysql -N $mysqld xyqb -e "${delete3}"
           delete4="delete from baitiao_bill_repayment_plan where xyqb.baitiao_bill_repayment_plan.user_id=${user_id}";
           #echo $delete4;
           mysql -N $mysqld xyqb -e "${delete4}"
           delete6="delete from baitiao_consume_history where baitiao_consume_history.user_id=${user_id} and baitiao_consume_history.order_id !=-1";
           #echo $delete6;
           mysql -N $mysqld xyqb -e "${delete6}"
           delete7="delete from repayment_plan where repayment_plan.user_id=${user_id}"
          # echo $delete7
           mysql -N $mysqld xyqb -e "${delete7}"
           #delete8="delete from loan_step_info where loan_step_info.user_id=${user_id}"
           echo $delete8
           mysql -N $mysqld xyqb -e "${delete8}"
          # delete9="delete from baitiao_bill_day where baitiao_bill_day.user_id=${user_id}" 
           echo $delete9
       fi
       count_xyqb_user=$[count_xyqb_user -1]
      
    done
 
    del_bill="delete from baitiao_bill where baitiao_bill.user_phone=${phone_no}"
    mysql -N $mysqld xyqb -e "${del_bill}"
    #echo $del_bill 
    del_repay="delete from baitiao_bill_repayment_plan where baitiao_bill_repayment_plan.user_phone=${phone_no}"
    mysql -N $mysqld xyqb -e "${del_repay}"
    #echo $del_repay
    del_consume="delete from baitiao_consume_history where baitiao_consume_history.consume_seq like '%${phone_no}%'"
    mysql -N $mysqld xyqb -e "${del_consume}"
   # echo $del_consume
 #spider_center.user/user_detail/authorization
    select_spi_user="select count(*) from user where phone_no='${phone_no}'"
    count_spi_user=`mysql -N $mysqld spider_center -e "${select_spi_user}"`
    #echo "count_spi_user =$count_spi_user"
    while [ $count_spi_user -gt 0 ];do   

       spi_id_sql="select id from user where phone_no='${phone_no}' limit 1"
       spi_user_id=`mysql -N $mysqld spider_center -e "${spi_id_sql}"`
      
       if [ ! $spi_user_id ];then
           echo "spi_user_id is null"
       else
       #   echo "spi_user_id=${spi_user_id}" 
          del_spi1=" DELETE from user where user.phone_no=${phone_no}"
          mysql -N $mysqld spider_center -e "${del_spi1}"
      #    echo ${del_spi1}
          del_spi2="delete  from user WHERE user.id=${spi_user_id}"
          mysql -N $mysqld spider_center -e "${del_spi2}"
        #  echo ${del_spi2}
          del_spi3="DELETE  from user_detail WHERE user_detail.user_id=${spi_user_id}"
          mysql -N $mysqld spider_center -e "${del_spi3}"
          del_spi4="DELETE  from authorization WHERE authorization.user_id=${spi_user_id}"
          mysql -N $mysqld spider_center -e "${del_spi4}"
       fi
       count_spi_user=$[count_spi_user-1] 
    done

    del_spi_user="delete from user where user.phone_no=${phone_no}"
    mysql -N $mysqld spider_center -e "${del_spi_user}"
    del_spi_detail="delete from user_detail where user_detail.phone_no=${phone_no}"
    mysql -N $mysqld spider_center -e "${del_spi_detail}"

#mall.user/user_detail/baitiao_card/mall_order
    select_mall_user="select count(*)from user where phone_no='${phone_no}'"
    count_mall_user=`mysql -N $mysqld mall -e "${select_mall_user}"`
   
   # echo "count_mall_user = $count_mall_user"

    while [ $count_mall_user -gt 0 ];do   
        select_mall_id="select id from user where user.phone_no=${phone_no}  limit 1"
        mall_user_id=`mysql -N $mysqld spider_center -e "${spi_id_sql}"`
    #    echo $select_mall_id
        
        if [ ! $mall_user_id ];then
           echo "mall_user_id is null"
          
        else 
      #     echo "mall_user_id= ${mall_user_id}"
           del_order="delete from mall_order where mall_order.user_id=${mall_user_id}"
       #    echo $del_order
           mysql -N $mysqld mall -e "${del_order}"
           del_contact="delete from contact_address where contact_address.user_id=${mall_user_id}"
           mysql -N $mysqld mall -e "${del_contact}"
        #   echo $del_contact
        fi
        count_mall_user=$[count_mall_user-1]
     done
     del_baitiao_card="delete from baitiao_card where baitiao_card.phone_no=${phone_no}"
     mysql -N $mysqld mall -e "${del_baitiao_card}"
     del_mall_user="delete from user where user.phone_no=${phone_no}"
     mysql -N $mysqld mall -e "${del_mall_user}"
     del_mall_detail="delete from user_detail where user_detail.phone_no=${phone_no}"
     mysql -N $mysqld mall -e "${del_mall_detail}"

#silk_road

#paycenter
  fi 
 echo "done!!! `date`"
}
baitiaoDeleteOrder $ip $phone_no
