fix: improved product information indexing

This commit is contained in:
2026-07-17 19:16:46 +08:00
parent 8ba58f4749
commit c79d738082
2 changed files with 21 additions and 16 deletions
+1 -1
View File
@@ -233,7 +233,7 @@ while (await new Promise(o => setTimeout(o, 1000, true))) {
$('#statusLabel').innerText = status ? status.charAt(0).toUpperCase() + status.slice(1).toLowerCase() : ''; $('#statusLabel').innerText = status ? status.charAt(0).toUpperCase() + status.slice(1).toLowerCase() : '';
switch (status) { switch (status) {
case 'IDLE': case 'BUSY':
continue; continue;
case 'READY': case 'READY':
$('#begin > span.text').innerText = 'Begin'; $('#begin > span.text').innerText = 'Begin';
+20 -15
View File
@@ -42,7 +42,7 @@ def main(driver: WebDriver, logger = logging.getLogger('main')):
sp = ServiceProvider.default() sp = ServiceProvider.default()
class Status(Enum): class Status(Enum):
IDLE = 0 BUSY = 0
READY = 1 READY = 1
RUNNING = 2 RUNNING = 2
STANDBY = 3 STANDBY = 3
@@ -64,7 +64,7 @@ def main(driver: WebDriver, logger = logging.getLogger('main')):
t1 = Timer() t1 = Timer()
t2 = Timer() t2 = Timer()
options = dict() options = dict()
status = Status.IDLE status = Status.BUSY
def begin(opts: dict, args: dict): def begin(opts: dict, args: dict):
nonlocal status nonlocal status
@@ -141,23 +141,26 @@ def main(driver: WebDriver, logger = logging.getLogger('main')):
class ProductInfo: class ProductInfo:
def __init__(self, file): def __init__(self, file):
self.wb = openpyxl.load_workbook(file, read_only=True) self.wb = openpyxl.load_workbook(file, read_only=True)
self.headers = list() self.indices = dict()
self.headers = dict()
def index(self, *keys: str):
self.indices.clear()
self.headers.clear()
for col in range(1, self.wb.active.max_column + 1): for col in range(1, self.wb.active.max_column + 1):
value = str(self.wb.active.cell(1, col).value) value = str(self.wb.active.cell(1, col).value)
self.headers.append(value) if value in keys: self.headers[value] = col
def search(self, by: str, needle) -> dict:
a = self.headers.index(by)
b = None
for row in range(1, self.wb.active.max_row + 1): for row in range(1, self.wb.active.max_row + 1):
if self.wb.active.cell(row, a+1).value == needle: for key in keys:
b = row col = self.headers[key]
break value = str(self.wb.active.cell(row, col).value)
self.indices.setdefault(key, dict())[value] = row
if b is None: return dict() def at(self, by: str, index) -> dict:
return { k: self.wb.active.cell(b, c+1).value for c, k in enumerate(self.headers) } if (a := self.indices.get(by)) is None or (b := dict.get(a, index)) is None: return dict()
return { k: self.wb.active.cell(b, c).value for k, c in self.headers.items() }
try: try:
logger.info('Downloading product list...') logger.info('Downloading product list...')
@@ -183,6 +186,8 @@ def main(driver: WebDriver, logger = logging.getLogger('main')):
p = ProductInfo(file) p = ProductInfo(file)
driver.close() driver.close()
driver.switch_to.window(driver.window_handles[0]) driver.switch_to.window(driver.window_handles[0])
logger.info('Indexing product information...')
p.index('产品编号', '产品名称', '产品型号')
logger.info('Done') logger.info('Done')
status = Status.READY status = Status.READY
except Exception as e: except Exception as e:
@@ -416,7 +421,7 @@ def main(driver: WebDriver, logger = logging.getLogger('main')):
match kind: match kind:
case 'vat': case 'vat':
record[8] = product record[8] = product
record[9] = (p.search('产品型号', code) or p.search('产品名称', product)).get('产品编号') record[9] = (p.at('产品型号', code) or p.at('产品名称', product)).get('产品编号')
record[10] = code record[10] = code
record[11] = '%.2f' % price record[11] = '%.2f' % price
record[12] = '%g%%' % discount record[12] = '%g%%' % discount
@@ -424,7 +429,7 @@ def main(driver: WebDriver, logger = logging.getLogger('main')):
record[14] = '%g' % quantity record[14] = '%g' % quantity
record[15] = description record[15] = description
case 'correction': case 'correction':
record[8] = p.search('产品编号', profile.remise).get('产品名称') record[8] = p.at('产品编号', profile.remise).get('产品名称')
record[9] = profile.remise record[9] = profile.remise
record[13] = '0' record[13] = '0'
record[14] = '0' record[14] = '0'