HandleImportDeviceSnInitRecord.py
2.57 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
import openpyxl
from SupplyChain import DbConnection
from SupplyChain.CommonDef import CComonUnit, log
class DVSnInitRecord:
sn = ''
iccid = ''
imei = ''
remark = ''
class HandleDeviceInitRecordImport:
fileName = ''
dbConnection = None
communit = None
def __init__(self):
self.fileName = ''
self.dbConnection = None
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 readData(self):
listResult = []
workbook = openpyxl.load_workbook(self.fileName)
sheet = workbook.active
rows = sheet.max_row
for i in range(2, rows + 1):
sn = sheet['A%d' % (i)].value
iccid = sheet['B%d' % (i)].value
imei = sheet['C%d' % (i)].value
remark = sheet['D%d' % (i)].value
snRecord = DVSnInitRecord()
if sn is None:
continue
if iccid is None:
iccid = ''
if imei is None:
imei = ''
if remark is None:
remark = ''
snRecord.sn = sn
snRecord.iccid = iccid
snRecord.imei = imei
snRecord.remark = remark
listResult.append(snRecord)
return listResult
def insertDb(self,listData):
if listData is None:
return
if len(listData)== 0:
return
seekTag = self.communit.getNowDateYMDHMSD()
sql = "insert into device_sn_init_record(`DV_SN`,`DV_ICCID`,`DV_IMEI`,`DV_REMARK`,`SEED_TAG`,`created_by`,`updated_by`) values"
sqlDb = []
for item in listData:
strValue = "('%s','%s','%s','%s','%s','admin','admin')"%\
(item.sn,item.iccid,item.imei,item.remark,seekTag)
sqlDb.append(strValue)
sql += ','.join(sqlDb)
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 handleWork(self):
log.info("start")
listData = self.readData()
self.insertDb(listData)
log.info("ok")
if __name__ == "__main__":
task = HandleDeviceInitRecordImport()
task.setFileName("f:\\a\\device_sn_init_record.xlsx")
task.handleWork()