--合服

--_G.lua_mongo 提供的接口
--[=[
    find()
    md5()
    id()
    next()
    client()
    remove()
    index()
    auth()
    count()
    update()
]=]--


--备份和恢复
--[=[
    备份
        mongodump --db 要备份的数据库名字 --out 备份文件存放目录
        如:  mongodump --db ckwy_fy_S350052 --out /cipan1/dumpDB/
        脚本: /server/dumpDB.sh    需要修改脚本中的数据库范围


    将备份文件恢复
        mongorestore --db 数据库存放目录  备份文件目录

        mongorestore --db=ckwy_fy_S350077 --drop /server/bin3/dumpDB/ckwy_fy_S350077  --增加 --drop参数，会先删除数据库，再恢复

        mongorestore --db=ckwy_fy_S3500002  /server/bin3/dumpDB/ckwy_fy_S3500002

        脚本: /server/restoreDB.sh  需要根据dumpDB.sh 修改脚本中的备份数据所在目录, 以及数据库范围

]=]--



--[=[
    --1.使用 newUniqueTag 代替 account
    --2. 合并数据逻辑
]=]--


KEY_CIYUAN_MOZHU = "ciyuanMoZhu"                -- 次元魔蛛的key

KEY_MERGE_TIME = "mergeServerTime"              -- 合服时间

---------------------同名-------------
NEW_NAME = "银月的青锋使"                        -- 玩家同名时的新名字 .. num
MAIL_ID = 7013                                  -- 同名玩家改名后补偿邮件的ID
MAIL_NUM = 500                                  -- 邮件补偿古玉数量


-------------------烟花----------------
KEY_FIREWORKBONUS_TIME = "fireWorksBonusTime"     -- 烟花加成时间key
FIRE_MAX_TIME = 43200                             -- 烟花最大加成时间(12h)


------------日志----------------------
SUCC_TAG = "succ"
ERR_TAG = "err"
CHANGENAME_TAG = "changeName"

--------------------------------------

-- 渠道ID
CHANNEL_ID_TAP = 1
CHANNEL_ID_SANLI_ANDOIRD = 2
CHANNEL_ID_SANLI_IOS = 3
CHANNEL_ID_SANLI_QQ = 4
CHANNEL_ID_SANLI_H5 = 5
CHANNEL_ID_SANLI_4399 = 6
CHANNEL_ID_SANLI_360 = 7

CHANNEL_ID_SANLI_SHIYOU = 10
CHANNEL_ID_TAP_WX = 11

-- 数据库名中字符串部分的格式
DB_NAME_STR = "ckwy_fy_S"

-- 渠道对应的数据库名中数字部分的初始值
--!!! 当前项目 ckwy_fy_S350002 是1服的数据库, ckwy_fy_S350003 是2服的数据库, ckwy_fy_S350004 是3服的数据库, 以此类推...
CHANNEL_2_DBNUMBER = {
    -- TAP
    [CHANNEL_ID_TAP] = 350001,

    --三狸 Andorid/IOS
    [CHANNEL_ID_SANLI_ANDOIRD] = 350001,
    [CHANNEL_ID_SANLI_IOS] = 350001,

    -- 三狸QQ,360
    [CHANNEL_ID_SANLI_QQ] = 650001,
    [CHANNEL_ID_SANLI_360] = 650001,

    -- 三狸H5，4399
    [CHANNEL_ID_SANLI_H5] = 550001,
    [CHANNEL_ID_SANLI_4399] = 550001,

    -- 三狸视游
    [CHANNEL_ID_SANLI_SHIYOU] = 750001,

    -- 微信/抖音小游戏
    [CHANNEL_ID_TAP_WX] = 1150001,
}

-- 要进行合并的数据库所属渠道ID, 用于检测合错数据库的情况, 与下面的 MERGE_DB_TB 的值一一对应
-- 格式: 渠道Id, 区服Id, 标识(0-宿主服, 1-被合服, 2-被合服, 且被合并到另外一个跨服/渠道)
MERGE_CHECK_TB = {
    -- 木子/Tap
    [CHANNEL_ID_TAP] = {
        { {CHANNEL_ID_TAP, 161, 0},  {CHANNEL_ID_TAP, 171, 1} },
        { {CHANNEL_ID_TAP, 163, 0},  {CHANNEL_ID_TAP, 173, 1} },
        { {CHANNEL_ID_TAP, 165, 0},  {CHANNEL_ID_TAP, 175, 1} },
        { {CHANNEL_ID_TAP, 167, 0},  {CHANNEL_ID_TAP, 177, 1} },
        { {CHANNEL_ID_TAP, 169, 0},  {CHANNEL_ID_TAP, 179, 1} },

        { {CHANNEL_ID_TAP, 211, 0},  {CHANNEL_ID_TAP, 212, 1} },
        { {CHANNEL_ID_TAP, 213, 0},  {CHANNEL_ID_TAP, 214, 1} },
        { {CHANNEL_ID_TAP, 215, 0},  {CHANNEL_ID_TAP, 216, 1} },
        { {CHANNEL_ID_TAP, 217, 0},  {CHANNEL_ID_TAP, 218, 1} },
        { {CHANNEL_ID_TAP, 219, 0},  {CHANNEL_ID_TAP, 220, 1} },

    },

    -- 三狸功夫
    -- [CHANNEL_ID_SANLI_ANDOIRD] = {
    --     { {CHANNEL_ID_SANLI_ANDOIRD, 121, 0}, {CHANNEL_ID_SANLI_ANDOIRD, 123, 1} },
    --     { {CHANNEL_ID_SANLI_ANDOIRD, 125, 0}, {CHANNEL_ID_SANLI_ANDOIRD, 127, 1}, {CHANNEL_ID_SANLI_ANDOIRD, 129, 1} },

    --     { {CHANNEL_ID_SANLI_ANDOIRD, 131, 0}, {CHANNEL_ID_SANLI_ANDOIRD, 133, 1} },
    --     { {CHANNEL_ID_SANLI_ANDOIRD, 135, 0}, {CHANNEL_ID_SANLI_ANDOIRD, 137, 1}, {CHANNEL_ID_SANLI_ANDOIRD, 139, 1} },

    --     { {CHANNEL_ID_SANLI_ANDOIRD, 141, 0}, {CHANNEL_ID_SANLI_ANDOIRD, 143, 1} },
    --     { {CHANNEL_ID_SANLI_ANDOIRD, 145, 0}, {CHANNEL_ID_SANLI_ANDOIRD, 147, 1}, {CHANNEL_ID_SANLI_ANDOIRD, 149, 1} },

    --     { {CHANNEL_ID_SANLI_ANDOIRD, 191, 0}, {CHANNEL_ID_SANLI_ANDOIRD, 192, 1} },
    --     { {CHANNEL_ID_SANLI_ANDOIRD, 193, 0}, {CHANNEL_ID_SANLI_ANDOIRD, 194, 1} },
    --     { {CHANNEL_ID_SANLI_ANDOIRD, 195, 0}, {CHANNEL_ID_SANLI_ANDOIRD, 196, 1} },
    --     { {CHANNEL_ID_SANLI_ANDOIRD, 197, 0}, {CHANNEL_ID_SANLI_ANDOIRD, 198, 1} },
    --     { {CHANNEL_ID_SANLI_ANDOIRD, 199, 0}, {CHANNEL_ID_SANLI_ANDOIRD, 200, 1} },
        
    -- },


    -- 三狸H5
    -- [CHANNEL_ID_SANLI_H5] = {
    --     { {CHANNEL_ID_SANLI_ANDOIRD, 41, 0},  {CHANNEL_ID_SANLI_H5, 31, 2}, {CHANNEL_ID_SANLI_H5, 32, 2} },
    --     { {CHANNEL_ID_SANLI_ANDOIRD, 43, 0},  {CHANNEL_ID_SANLI_H5, 33, 2}, {CHANNEL_ID_SANLI_H5, 34, 2} },
    --     { {CHANNEL_ID_SANLI_ANDOIRD, 45, 0},  {CHANNEL_ID_SANLI_H5, 35, 2}, {CHANNEL_ID_SANLI_H5, 36, 2} },
    --     { {CHANNEL_ID_SANLI_ANDOIRD, 47, 0},  {CHANNEL_ID_SANLI_H5, 37, 2}, {CHANNEL_ID_SANLI_H5, 38, 2} },
    --     { {CHANNEL_ID_SANLI_ANDOIRD, 49, 0},  {CHANNEL_ID_SANLI_H5, 39, 2}, {CHANNEL_ID_SANLI_H5, 40, 2} },
    -- },

    
    -- [CHANNEL_ID_SANLI_QQ] = {
    --     { {CHANNEL_ID_SANLI_QQ, 151, 0},  {CHANNEL_ID_SANLI_QQ, 152, 1}, {CHANNEL_ID_SANLI_QQ, 153, 1}, {CHANNEL_ID_SANLI_QQ, 154, 1}, {CHANNEL_ID_SANLI_QQ, 155, 1}},
    --     { {CHANNEL_ID_SANLI_QQ, 156, 0},  {CHANNEL_ID_SANLI_QQ, 157, 1}, {CHANNEL_ID_SANLI_QQ, 158, 1}, {CHANNEL_ID_SANLI_QQ, 159, 1}, {CHANNEL_ID_SANLI_QQ, 160, 1}},

    --     { {CHANNEL_ID_SANLI_QQ, 161, 0},  {CHANNEL_ID_SANLI_QQ, 162, 1}, {CHANNEL_ID_SANLI_QQ, 163, 1}, {CHANNEL_ID_SANLI_QQ, 164, 1}, {CHANNEL_ID_SANLI_QQ, 165, 1}},
    --     { {CHANNEL_ID_SANLI_QQ, 166, 0},  {CHANNEL_ID_SANLI_QQ, 167, 1}, {CHANNEL_ID_SANLI_QQ, 168, 1}, {CHANNEL_ID_SANLI_QQ, 169, 1}, {CHANNEL_ID_SANLI_QQ, 170, 1}},
    -- },


    -- 三狸视游
    -- [CHANNEL_ID_SANLI_SHIYOU] = {
    --     { {CHANNEL_ID_SANLI_SHIYOU, 1, 0},  {CHANNEL_ID_SANLI_SHIYOU, 3, 1} },
    --     { {CHANNEL_ID_SANLI_SHIYOU, 5, 0},  {CHANNEL_ID_SANLI_SHIYOU, 7, 1}, {CHANNEL_ID_SANLI_SHIYOU, 9, 1} },

    --     { {CHANNEL_ID_SANLI_SHIYOU, 11, 0},  {CHANNEL_ID_SANLI_SHIYOU, 12, 1} },
    --     { {CHANNEL_ID_SANLI_SHIYOU, 13, 0},  {CHANNEL_ID_SANLI_SHIYOU, 14, 1} },
    --     { {CHANNEL_ID_SANLI_SHIYOU, 15, 0},  {CHANNEL_ID_SANLI_SHIYOU, 16, 1} },
    --     { {CHANNEL_ID_SANLI_SHIYOU, 17, 0},  {CHANNEL_ID_SANLI_SHIYOU, 18, 1} },
    --     { {CHANNEL_ID_SANLI_SHIYOU, 19, 0},  {CHANNEL_ID_SANLI_SHIYOU, 20, 1} },

    --     { {CHANNEL_ID_SANLI_SHIYOU, 81, 0},  {CHANNEL_ID_SANLI_SHIYOU, 82, 1} },
    --     { {CHANNEL_ID_SANLI_SHIYOU, 83, 0},  {CHANNEL_ID_SANLI_SHIYOU, 84, 1} },
    --     { {CHANNEL_ID_SANLI_SHIYOU, 85, 0},  {CHANNEL_ID_SANLI_SHIYOU, 86, 1} },
    --     { {CHANNEL_ID_SANLI_SHIYOU, 87, 0},  {CHANNEL_ID_SANLI_SHIYOU, 88, 1} },
    --     { {CHANNEL_ID_SANLI_SHIYOU, 89, 0},  {CHANNEL_ID_SANLI_SHIYOU, 90, 1} },
    --     { {CHANNEL_ID_SANLI_SHIYOU, 91, 0},  {CHANNEL_ID_SANLI_SHIYOU, 92, 1} },
    --     { {CHANNEL_ID_SANLI_SHIYOU, 93, 0},  {CHANNEL_ID_SANLI_SHIYOU, 94, 1} },
    --     { {CHANNEL_ID_SANLI_SHIYOU, 95, 0},  {CHANNEL_ID_SANLI_SHIYOU, 96, 1} },
    --     { {CHANNEL_ID_SANLI_SHIYOU, 97, 0},  {CHANNEL_ID_SANLI_SHIYOU, 98, 1} },
    --     { {CHANNEL_ID_SANLI_SHIYOU, 99, 0},  {CHANNEL_ID_SANLI_SHIYOU, 100, 1} },
    -- },

    -- 微信/抖音小游戏
    [CHANNEL_ID_TAP_WX] = {
        { {CHANNEL_ID_TAP_WX, 1, 0},  {CHANNEL_ID_TAP_WX, 2, 1}, {CHANNEL_ID_TAP_WX, 3, 1}, {CHANNEL_ID_TAP_WX, 4, 1}, {CHANNEL_ID_TAP_WX, 5, 1}},
        { {CHANNEL_ID_TAP_WX, 6, 0},  {CHANNEL_ID_TAP_WX, 7, 1}, {CHANNEL_ID_TAP_WX, 8, 1}, {CHANNEL_ID_TAP_WX, 9, 1}, {CHANNEL_ID_TAP_WX, 10, 1}},
    },
}



--要进行数据合并的数据库列表, 每个value中第一个为目标数据库，其他为源数据库
--!!! 当前项目 ckwy_fy_S350002 是1服的数据库, ckwy_fy_S350003 是2服的数据库, ckwy_fy_S350004 是3服的数据库, 以此类推...
MERGE_DB_TB = {
    -- 木子
    {"ckwy_fy_S350022", "ckwy_fy_S350023"},
    {"ckwy_fy_S350024", "ckwy_fy_S350025"},
    {"ckwy_fy_S350026", "ckwy_fy_S350027"},
    {"ckwy_fy_S350028", "ckwy_fy_S350029"},
    {"ckwy_fy_S350030", "ckwy_fy_S350031"},

    -- 三狸安卓/H5
    {"ckwy_fy_S350042", "ckwy_fy_S350043", "ckwy_fy_S550032", "ckwy_fy_S550033"},
    {"ckwy_fy_S350044", "ckwy_fy_S350045", "ckwy_fy_S550034", "ckwy_fy_S550035"},
    {"ckwy_fy_S350046", "ckwy_fy_S350047", "ckwy_fy_S550036", "ckwy_fy_S550037"},
    {"ckwy_fy_S350048", "ckwy_fy_S350049", "ckwy_fy_S550038", "ckwy_fy_S550039"},
    {"ckwy_fy_S350050", "ckwy_fy_S350051", "ckwy_fy_S550040", "ckwy_fy_S550041"},

    -- 三狸QQ
    {"ckwy_fy_S650022", "ckwy_fy_S650023"},
    {"ckwy_fy_S650024", "ckwy_fy_S650025"},
    {"ckwy_fy_S650026", "ckwy_fy_S650027"},
    {"ckwy_fy_S650028", "ckwy_fy_S650029"},
    {"ckwy_fy_S650030", "ckwy_fy_S650031"},
}


--所有要处理的集合
COLLECTIONS = {
    ".char",                    -- 角色表, 可以直接插入
    ".mail"	,					-- 邮件，可以直接插入
    ".common",					-- 通用数据（开服时间)
    ".friend",					-- 好友数据库表， 可以直接插入
    ".boss_data",		    	-- boss记录表，	可以直接插入
    ".union",             		-- 工会数据表，	可以直接插入
    ".jjc",						-- 单人竞技场， 可以直接插入
    ".combat_video",			-- 战斗录像[本地记录 个人记录 单人竞技场 通天塔 王者争霸 战役记录],可以直接插入
    ".drill_data",        		-- 圣树试炼/过关斩将，可以直接插入
    ".union_war",   			-- 公会战，可以直接插入
    ".union_war_record",   		-- 公会战记录，	可以直接插入
    ".the_stars",          		-- 星空争霸，	可以直接插入
    ".abs_billboard",			-- 绝对时间排行榜记录，	可以直接插入
    ".middle_act_group",		-- 跨服活动分组记录， 暂时没使用，不管
    ".battle_shark",	    	-- 战役通关战报
    ".tower",	   				-- 全服通天塔 信息，	可以直接插入
    ".hero_pingfen",	   		-- 英雄评分信息用于排名， 可以直接插入           
    ".jjcGodWar",				-- 众神之战				，可以直接插入
    ".jjcGodWar_record",		-- 众神之战记录			，可以直接插入
    ".chat_record",        		-- 好友聊天记录			，可以直接插入
    ".jjcLadder",    	    	-- 天梯争霸				，可以直接插入
    ".jjcLadder_record",   		-- 天梯争霸记录			，可以直接插入
    ".yjTreasure",   			-- 遗迹探宝				， 可以直接插入
    ".yjTreasure_rank",   	    -- 遗迹探宝排行
    ".yjTreasure_role",   		-- 遗迹探宝角色信息		， 可以直接插入
    ".valley",   				-- 荣耀峡谷/龙族战场	，可以直接插入
    ".valley_record",   		-- 荣耀峡谷/龙族战场记录， 可以直接插入
    ".war_report",   	    	-- 战报[跨服记录 冠军联赛 王者争霸 天梯 切磋 系列排名存储记录]， 可以直接插入
    ".openServer_billboard",	-- 开服排行类活动，可以直接插入
    ".lost_temple",             -- 神庙
    ".common_rank",				-- 通用排行榜，可以直接插入
}

--源数据库中不需要插入目标数据库的集合列表
--[=[
    middle_act_group        暂时没用到这个集合，所以也只用目标数据库的集合
    the_stars               战神殿, 只保留宿主服的记录
    battle_shark            主线闯关录像, 只用宿主服的
]=]--
NO_INSERT_COLLECTIONS = {
    --[".common"] = 1,
    [".middle_act_group"] = 1,
    [".the_stars"] = 1,
    [".battle_shark"] = 1,
    [".jjcGodWar"] = 1,
    --[".jjcGodWar_record"] = 1,
    --[".tower"] = 1,
}


--用到char 集合中的 _id 字段的集合, 如果 char 集合中的 _id 有修改, 则这些集合中的 _id 也需要修改
NEED_CHANGE_COLLECTIONS = {
    [".friend"] = '1',
    [".mail"] = '1',
    [".union"] = '1',
    [".the_stars"] = '1',
    [".jjc"] = '1',
    [".combat_video"] = '1',
}