HandleBstMerchantStockInit.py
7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import openpyxl
from SupplyChain import DbConnection
from SupplyChain.CommonDef import log, CComonUnit
from SupplyChain.DataBase import DateBase
class DTMerchantStockInit:
bmsMerchantCode = ''
bmsMerchantName = ''
bmsWarehouseCode = ''
bmsWarehouseName = ''
bmsMaterialCode = ''
bmsMaterialName = ''
bmsYearMonth = ''
bmsCount = 0
createdBy = ''
updatedBy = ''
createdDate = ''
updatedDate = ''
deletedFlag = ''
class HandleMerchantStockInit:
fileName = ''
dbConnection = None
dataBase = None
mapDealerUserInfo = None
mapMaterialInfo = None
mapWarehouseRelation = None
communit = None
def __init__(self):
self.fileName = ''
self.dbConnection = None
self.dataBase = DateBase()
self.mapDealerUserInfo = self.dataBase.mapDealerUserInfo()
self.mapMaterialInfo = self.dataBase.mapMaterialInfo()
self.mapWarehouseRelation = self.dataBase.mapWarehouseCode2Info()
self.communit = CComonUnit()
def getDbConnection(self):
if self.dbConnection is None:
self.dbConnection = DbConnection.MySQL(DbConnection.g_dbconnect_supplychain)
return self.dbConnection
def setFileName(self, fileName):
self.fileName = fileName
def readExcel(self):
listResult = []
workbook = openpyxl.load_workbook(self.fileName)
sheet = workbook.active
# rows = sheet.max_row
rows = 16
for i in range(2, rows + 1):
merchantCode = sheet['A%d' % (i)].value
merchantName = sheet['B%d' % (i)].value
warehouseCode = sheet['C%d' % (i)].value
warehouseName = sheet['D%d' % (i)].value
materialCode = sheet['E%d' % (i)].value
materialName = sheet['F%d' % (i)].value
yearMonth = sheet['G%d' % (i)].value
count = sheet['H%d' % (i)].value
if merchantCode is None:
log.error("HandleMerchantStockInit::readExcel merchantcode is None i=%d", i)
return None
if merchantName is None:
log.error("HandleMerchantStockInit::readExcel merchantName is None i=%d", i)
return None
if warehouseCode is None:
log.error("HandleMerchantStockInit::readExcel warehouseCode is None i=%d", i)
return None
if warehouseName is None:
log.error("HandleMerchantStockInit::readExcel warehouseName is None i=%d", i)
return None
if materialCode is None:
log.error("HandleMerchantStockInit::readExcel materialCode is None i=%d", i)
return None
if materialName is None:
log.error("HandleMerchantStockInit::readExcel materialName is None i=%d", i)
return None
if yearMonth is None:
log.error("HandleMerchantStockInit::readExcel yearMonth is None i=%d", i)
return None
if count is None:
log.error("HandleMerchantStockInit::readExcel count is None i=%d", i)
return None
dealerUserInfo = self.mapDealerUserInfo.get(merchantCode)
if dealerUserInfo is None:
log.error("HandleMerchantStockInit::readExcel merchantcode is not exists merchantCode='%s'",
merchantCode)
return None
warehouseInfo = self.mapWarehouseRelation.get(warehouseCode)
if warehouseInfo is None:
log.error("HandleMerchantStockInit::readExcel warehosuecode is not exists warehouseCode='%s'",
warehouseCode)
return None
materialInfo = self.mapMaterialInfo.get(materialCode)
if materialInfo is None:
log.error("HandleMerchantStockInit::readExcel materialCode is not exists materialCode='%s'",
materialCode)
return None
if warehouseInfo.merchantCode != merchantCode:
log.error("HandleMerchantStockInit::readExcel merchantcode is not equal whsinfo merchantcode warehouseInfo.merchantCode='%s',merchantCode='%s'",
warehouseInfo.merchantCode,merchantCode)
return None
initObject = DTMerchantStockInit()
initObject.bmsCount = count
initObject.bmsMaterialCode = materialCode
initObject.bmsMaterialName = materialName
initObject.bmsMerchantCode = merchantCode
initObject.bmsMerchantName = merchantName
initObject.bmsWarehouseCode = warehouseCode
initObject.bmsWarehouseName = warehouseName
strYearMonth = self.communit.getDateStrY_M_DFromDateYmd(yearMonth)
initObject.bmsYearMonth = strYearMonth[0:7]
initObject.createdBy = "admin"
initObject.updatedBy = "admin"
initObject.createdDate = self.communit.getNowDate()
initObject.updatedDate = self.communit.getNowDate()
initObject.deletedFlag = "N"
listResult.append(initObject)
return listResult
def insertMerchantStockInit(self,listObject):
if listObject is None:
return
if len(listObject) == 0:
return
sql = "insert into bst_merchant_stock_init(`bms_merchant_code`,`bms_merchant_name`,`bms_warehouse_code`,`bms_warehouse_name`,`bms_material_code`,`bms_material_name`,`bms_year_month`,`bms_count`,`created_by`,`created_date`,`updated_by`,`updated_date`,`deleted_flag`) values"
sqlDb = []
for item in listObject:
strValue = "('%s','%s','%s','%s','%s','%s','%s','%d','%s',now(),'%s',now(),'%s')"%\
(item.bmsMerchantCode,item.bmsMerchantName,item.bmsWarehouseCode,item.bmsWarehouseName,item.bmsMaterialCode,item.bmsMaterialName,item.bmsYearMonth,item.bmsCount,item.createdBy,item.updatedBy,item.deletedFlag)
sqlDb.append(strValue)
sql += ','.join(sqlDb)
sql += " on duplicate key update `bms_merchant_code`=values(`bms_merchant_code`),`bms_merchant_name`=values(`bms_merchant_name`),`bms_warehouse_code`=values(`bms_warehouse_code`),`bms_warehouse_name`=values(`bms_warehouse_name`),`bms_material_code`=values(`bms_material_code`),`bms_material_name`=values(`bms_material_name`),`bms_year_month`=values(`bms_year_month`),`bms_count`=values(`bms_count`),`created_by`=values(`created_by`),`created_date`=values(`created_date`),`updated_by`=values(`updated_by`),`updated_date`=values(`updated_date`),`deleted_flag`=values(`deleted_flag`)"
connect = self.getDbConnection()
result = connect.query(sql)
if result == False:
log.error("DateBaseSupply::insertDeviceCardManager sql query error, sql=%s", sql)
return False
connect.commit()
def doWork(self):
listObject = self.readExcel()
self.insertMerchantStockInit(listObject)
if __name__ == "__main__":
handleTask = HandleMerchantStockInit()
handleTask.setFileName("F:\\a\\商户库存V3.5 商户库存盘点初始表 (3).xlsx")
handleTask.doWork()