#!/bin/sh
# -------------------------------------------------------------------------------
# Filename:    db_gen_config.sh
# Revision:    1.0
# Date:        2016/12
# Author:      dekun.dai
# Description: 生成一个同步表结构用的配置文件
# -------------------------------------------------------------------------------
source ~/.bash_profile

source_db_host=$1
source_db_user=$2
source_db_pwd='Uiwb@o^fR&nHOa5t'

target_db_host=$4
target_db_user=$5
target_db_pwd=$6

sync_db_name=$7
database_name=$sync_db_name

config_file_name=$8
db_backup_sub_folders=$9

port=""
sys_table_list=""

sync_config_folder=~/db_sync_configs
base_config_file=$sync_config_folder/base_config.json

if [ $# -ne 9 ]; then
	echo "db_gen_config.sh"
	echo "Total Number of Parameters : $#"
	echo "source_db_infos：source_db_host: $1, source_db_user: $2"
	echo "target_db_infos：target_db_host: $4, target_db_user: $5"
	echo "sync_db_name: $7"
	echo "config_file_name: $8"
	echo "db_backup_sub_folders: $9"
	exit 1
fi

echo "prepare to get port"
if [[ $database_name = 'xyqb' ]]; then
	# 信用钱包database
	# repay v_manifest v_repay 是view类型的
	# secure_proxy 表里面的数据需要被同步
	sys_table_list='baitiao_merchant baitiao_secure_key channel funding_contract funding_corp loan_product merchant_channel payeco_return_url_info red_packet t_phonenum zmxy_return_url configuration repay v_manifest v_repay secure_proxy funding_corp_config funding_info_map baitiao_preferential_rule'
	port='3306'
elif [[ $database_name = 'xyqb_app' ]]; then
	# app-backend使用的数据库
	sys_table_list='activities app_banners app_config app_versions banners blanknote_entries credit_questions hot_games loan_banners loan_questions migrations sharings widgets js_patch upgrade_version'
	port='3307'
elif [[ $database_name = 'payment_center' ]]; then
	# 支付中心
	sys_table_list='card_switcher merchant merchant_config pay_approach_config merchant_pay_approach_relation'
	port='3308'
elif [[  $database_name = 'risk_control'  ]]; then
	# 谛听database
	sys_table_list='company product_strategy_dispatcher quantify_product red_packet_info user'
	port='3309'
elif [[  $database_name = 'dujiangyan'  ]]; then
	# dujiangyan
	sys_table_list='external_coopr'
	port='3310'
elif [[  $database_name = 'mall'  ]]; then
	# 白条商城
	sys_table_list='activity_3c_product ad_detail admin_banner advertisement_type album app_channel app_channel_ad app_name app_name_ad category_banner category_nature commodity_slogan commodity_slogan_product configuration edx_city faster_entrance faster_entrance_image goods_after_sale_config goods_after_sale_step home_config home_page hot_category jd_address jd_after_sale_config jd_brand jd_category jd_product_sku_image jd_property jd_sku jd_sku_detail_image jd_sku_specs life_faster_entrance life_service mall_category mall_config merchant nature_value new_3c_product product product_category product_nature product_nature_value_bak product_sku product_sku_image product_specification product_spu recommend_brand recommend_product sale_product_detail sale_product_theme spu_image supplier tab_navigate title_banner'
	port='3310'
elif [[  $database_name = 'qinqiong'  ]]; then
	# qinqiong
	sys_table_list='secure_key secure_proxy'
	port='3310'
elif [[  $database_name = 'cash_loan_flow'  ]]; then
	# qinqiong
	sys_table_list='order_callback callback_configuration channel_configuration'
	port='3310'
elif [[  $database_name = 'quartz'  ]]; then
	# 定时任务
	sys_table_list='certain_time_job'
	port='3310'
elif [[ $database_name = 'contract' ]]; then
	# 合同
	sys_table_list='contract_template_new'
	port='3311'
elif [[ $database_name = 'fund_manage' ]]; then
	# fund_manager
	sys_table_list='business_info email_info fund_info merchant'
	port='3310'
elif [[  $database_name = 'financial_system'  ]]; then
	# 财务系统
	sys_table_list='collection_user sys_dictionary sys_status_edge sys_status_vertice'
	port='6603'
elif [[  $database_name = 'financial_system_api'  ]]; then
	# 财务系统api
	sys_table_list='mailinfo'
	port='6603'
elif [[  $database_name = 'gyxd'  ]]; then
	# 广源小贷系统
	sys_table_list='administrative_districts templet'
	source_db_host='192.168.4.80'
	port='6602'
elif [[  $database_name = 'offline'  ]]; then
	# 线下白条
	sys_table_list='goods_detail merchant operator_user'
	port='3311'
elif [[  $database_name = 'redpacket'  ]]; then
	# 红包系统
	sys_table_list='tb_activity_channel tb_dict_channel tb_dict_out_channel'
	port='3311'
elif [[  $database_name = 'spider_center'  ]]; then
	# 授信中心
	sys_table_list='merchant merchant_config'
	port='3312'
elif [[  $database_name = 'silk_road'  ]]; then
	# 
	sys_table_list='task'
	source_db_host='192.168.4.80'
	port='9999'
elif [[  $database_name = 'xyqb_user'  ]]; then
	# 用户中心
	sys_table_list='merchant_config merchant'
	port='3313'
elif [[ $database_name = 'merchant' ]]; then
	# 用户中心
	sys_table_list='authority product_nature role role_authority role_supplier user'
	port='3314'
elif [[ $database_name = 'xyqb_btzt' ]]; then
	# 白条直投
	sys_table_list='zt_channel zt_fund zt_fund_channel zt_fund_credit zt_fund_term'
	source_db_host='192.168.4.116'
	port='7001'
 elif [[ $database_name = 'notify' ]]; then
 	# 白条直投
 	sys_table_list='merchant merchant_config'
	source_db_host='192.168.4.6'
 	port='7306'
	echo "---------------------------"
fi

echo "port is $port"
echo "copy ${base_config_file} to ${config_file_name}"
cp -f $base_config_file $config_file_name

echo "sed -i s/source_db_host/${source_db_host}/g $config_file_name" 
sed -i "s/source_db_host/${source_db_host}/g" $config_file_name
sed -i "s/source_db_user/${source_db_user}/g" $config_file_name
sed -i "s/source_db_pwd/${source_db_pwd}/g" $config_file_name
sed -i "s/target_db_host/${target_db_host}/g" $config_file_name
sed -i "s/target_db_user/${target_db_user}/g" $config_file_name
sed -i "s/target_db_pwd/${target_db_pwd}/g" $config_file_name
sed -i "s/sync_db_name/${sync_db_name}/g" $config_file_name
sed -i "s/source_port/${port}/g" $config_file_name

#同步基础数据
for table_name in $sys_table_list
do
	file_name=$db_backup_sub_folders/$table_name.sql
	
	if [[ $table_name = 'repay' || $table_name = 'v_manifest' || $table_name = 'v_repay' ]]; then
		echo "skip view table $table_name"
	else
		echo "mysqldump -u${source_db_user} -p${source_db_pwd} -h${source_db_host} -P${port} $database_name --tables $table_name >> $file_name"
		mysqldump -u${source_db_user} -p${source_db_pwd} -h${source_db_host} -P${port} $database_name --skip-lock-tables --tables $table_name --skip-add-locks >> $file_name
	fi
done
