# !/usr/bin/env python3
# -*- coding:utf-8 -*-
import pymysql
import os
import time
from datetime import date, datetime
remoteDBUser='db_order'
remoteDBPasswd='db_order'
remoteDBHost='192.168.16.125'
try:
conn = pymysql.connect(host=remoteDBHost,port= 3306,user = remoteDBUser,passwd=remoteDBPasswd,db='db_order_sharding')
cur = conn.cursor()
cur.execute("select shard_name from ord_table_shards")
shardTuples = cur.fetchall()
print(shardTuples)
storeIdSet = set()
for shardTuple in shardTuples:
shardName = shardTuple[0]
#print("shardName:%s" %shardName)
storeIdStr = shardName.split('_')[-1]
#print("storeId:%s" % storeIdStr)
storeIdSet.add(storeIdStr)
print("storeIdSet:%s" %storeIdSet)
cntMap = dict()
backupDir = '/home/developer/mysqlbackup/' + time.strftime("%Y%m%d")
if not os.path.exists(backupDir):
os.makedirs(backupDir)
writeFileName = backupDir + "/updateOrderCode.sql"
if os.path.exists(writeFileName):
os.remove(writeFileName)
with open(writeFileName, 'a', encoding='utf-8') as f:
for storeId in storeIdSet:
storeIdHex = hex(int(storeId))
stripHex = storeIdHex[2:]
print("storeId:%s ,storeIdHex:%s,%s" %(storeId,storeIdHex,stripHex))
generateStr = stripHex
if len(stripHex) < 6:
prependZero = '0' * (6 -len(stripHex))
generateStr = prependZero + stripHex
print("generateStr:%s" %generateStr)
cur.execute("select id, create_time, store_id from ord_order_info_%s" %storeId)
createTimeTuples = cur.fetchall()
print(createTimeTuples)
for createTimeTuple in createTimeTuples:
orderId = createTimeTuple[0]
print("orderId:%s" %orderId)
createTime = createTimeTuple[1]
#print("createTime:%s, type:%s" %(createTime,type(createTime)))
timeStr = createTime.strftime('%Y%m%d')
print("timestr:%s" %timeStr)
key = generateStr + timeStr
print("key:%s" %key)
cnt = 0
if cntMap.get(key) is None:
print("not has key")
cnt = 1
cntMap[key] = 1
else:
print("has key")
cntMap[key] = cntMap[key] + 1
cnt = cntMap[key]
strCnt = str(cnt)
gCnt = strCnt
lenCnt = len(strCnt)
if lenCnt < 4:
pZero = '0' * (4 - lenCnt)
gCnt = pZero + strCnt
print("gCnt:%s" %gCnt)
print("gOrderCode:%s%s" %(key, gCnt))
gOrderCode = key + gCnt
sid = gOrderCode[:6]
storeIdH = int(sid,16)
print("storeIdH:%s, storeId:%s" %(storeIdH, createTimeTuple[2]))
#cur.execute("update pay_flow_%s p, ord_order_info_%s i set p.order_id = %s where p.order_id = i.id and i.id = %s" %(storeIdH, storeIdH, gOrderCode, orderId))
f.writelines("update pay_flow_%s p, ord_order_info_%s i set p.order_id = '%s' where p.order_id = i.id and i.id = '%s';\n" %(storeIdH, storeIdH, gOrderCode, orderId))
#print("update pay_flow_%s orderId:%s success!" %(storeIdH, orderId))
f.writelines("update ord_order_detail_%s d, ord_order_info_%s i set d.order_id = '%s' where d.order_id = i.id and i.id = '%s';\n" %(storeIdH, storeIdH, gOrderCode, orderId))
#cur.execute("update ord_order_detail_%s d, ord_order_info_%s i set d.order_id = %s where d.order_id = i.id and i.id = %s" %(storeIdH, storeIdH, gOrderCode, orderId))
#cur.execute("update ord_order_info_%s set id = %s where id = %s" %(storeIdH,gOrderCode,orderId))
f.writelines("update ord_order_info_%s set id = '%s' where id = '%s';\n" %(storeIdH,gOrderCode,orderId))
#f.flush()
#f.close()
cur.close()
conn.close()
except Exception as e:
print(e)
轉(zhuǎn)換數(shù)據(jù)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來车胡,“玉大人檬输,你說我怎么就攤上這事⌒偌” “怎么了丧慈?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長主卫。 經(jīng)常有香客問我逃默,道長,這世上最難降的妖魔是什么簇搅? 我笑而不...
- 正文 為了忘掉前任完域,我火速辦了婚禮,結(jié)果婚禮上瘩将,老公的妹妹穿的比我還像新娘吟税。我一直安慰自己凹耙,他們只是感情好,可當我...
- 文/花漫 我一把揭開白布肠仪。 她就那樣靜靜地躺著肖抱,像睡著了一般。 火紅的嫁衣襯著肌膚如雪异旧。 梳的紋絲不亂的頭發(fā)上意述,一...
- 文/蒼蘭香墨 我猛地睜開眼然低,長吁一口氣:“原來是場噩夢啊……” “哼喜每!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起雳攘,我...
- 正文 年R本政府宣布,位于F島的核電站涯保,受9級特大地震影響诉濒,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜夕春,卻給世界環(huán)境...
- 文/蒙蒙 一未荒、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧撇他,春花似錦茄猫、人聲如沸狈蚤。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至锌畸,卻和暖如春勇劣,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背潭枣。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn)伊佃,斷路器窜司,智...
- ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
- 一捅位、近六個月的目標:完美的伴侶 二、需要種下的種子:愛與陪伴搂抒,智慧艇搀,溫暖,財富求晶,樣貌 三焰雕、具體的播種行為 1.又到...