修改问题

duizhaopin
apan_youxiang@163.com 2 years ago
parent 2585b01dde
commit f69472bc52

@ -10,10 +10,11 @@ import datetime
from string import ascii_uppercase from string import ascii_uppercase
from openpyxl import load_workbook from openpyxl import load_workbook
class ReadExcel: class ReadExcel:
def __init__(self, excelName): def __init__(self, excelName):
self.wb = load_workbook(excelName) self.wb = load_workbook(filename=excelName)
self.ws = self.wb.active self.ws = self.wb.active
self.template_list = [] self.template_list = []
@ -29,49 +30,69 @@ class ReadExcel:
# lineIndex = 0 # lineIndex = 0
try: try:
for line in range(2, max_lines + 1): for line in range(2, max_lines + 1):
# 局部方法,传递字符和默认值返回方法调用,简化代码量 # 局部方法,传递字符和默认值返回方法调用,简化代码量
value_func = lambda x,y='': str(self.ws[f"{x}{line}"].value or y) value_func = lambda x, y='': str(
self.ws[f"{x}{line}"].value or y)
# 声明一个字典存放每一行的数据 每遍历一次字典重置一次 # 声明一个字典存放每一行的数据 每遍历一次字典重置一次
drug_template_dict = { drug_template_dict = {
"variety_id": str(uuid.uuid1()), "variety_id": str(uuid.uuid1()),
} }
key_list = [ key_list = [
"remark1", "remark2", "remark3", "remark4", "name", "name", "english_name", "purity", "cas_number", "speci", "net_weight_unit", "export_count", "export_unit",
"english_name", "purity", "cas_number", "remark5", "net_weight", "production_date", "shelf_life", "expiration_date", "price", "is_supervise", "total", "manufacturer", "remain"
"net_weight_unit", "export_count", "remark6"
] ]
# 采用列表和字母对应的两个列表,进行循环的方式写入字典,简化代码量 # 采用列表和字母对应的两个列表,进行循环的方式写入字典,简化代码量
v_list = list("ABCDEFGHIJKLM") v_list = list("EFGHJKLMNOPQRSTU")
str_tim = "%Y-%m-%d %H:%M:%S"
def date_func(x): return datetime.datetime.strptime(
x, str_tim)
for i in range(len(key_list)): for i in range(len(key_list)):
drug_template_dict[key_list[i]] = value_func(v_list[i]) v_key = v_list[i]
key_key = key_list[i]
# 时间格式转换
if key_key == "production_date" or key_key == "expiration_date":
value = date_func(value_func(
v_key)).strftime(str_tim)
elif key_key == "is_supervise":
va = value_func(v_key)
if va == "":
value = 0
else:
value = 1
elif key_key == "remain":
value = drug_template_dict.get("speci")
else:
value = value_func(v_key)
drug_template_dict[key_list[i]] = value
if key_list[i] == "name" and drug_template_dict.get("name") == "": if key_list[i] == "name" and drug_template_dict.get("name") == "":
self.template_list.append(json.dumps(drug_list)) self.template_list.append(json.dumps(drug_list))
return self.template_list return self.template_list
# 时间判断逻辑较为凌乱,采用原思路进行重写 # 时间判断逻辑较为凌乱,采用原思路进行重写
# 定义当前时间对象 # 定义当前时间对象
pDate = datetime.datetime.now() # pDate = datetime.datetime.now()
# 局部性时间字符串,共用 # 局部性时间字符串,共用
str_tim = "%Y-%m-%d %H:%M:%S"
# 定义公共方法,处理时间转换 # 定义公共方法,处理时间转换
date_func = lambda x: datetime.datetime.strptime(x, str_tim)
# 和原逻辑一样,简化写法 # 和原逻辑一样,简化写法
if not value_func("P"): # if not value_func("P"):
if value_func("O"): # if value_func("O"):
pDate = date_func(value_func("N")) # pDate = date_func(value_func("N"))
shelf_life = value_func("O", "3650") # shelf_life = value_func("O", "3650")
else: # else:
if value_func("N"): # if value_func("N"):
pDate = date_func(value_func('N'), str_tim) # pDate = date_func(value_func('N'), str_tim)
shelf_life = self.time_long( # shelf_life = self.time_long(
date_func(pDate.strftime(str_tim)), date_func(value_func("P"))) # date_func(pDate.strftime(str_tim)), date_func(value_func("P")))
drug_template_dict["production_date"] = pDate.strftime(str_tim) # drug_template_dict["production_date"] = pDate.strftime(
drug_template_dict["shelf_life"] = shelf_life # str_tim)
# drug_template_dict["shelf_life"] = shelf_life
# if(not self.ws['P{}'.format(str(line))].value): # if(not self.ws['P{}'.format(str(line))].value):
# pDate = datetime.datetime.now() # pDate = datetime.datetime.now()
# if(self.ws['O{}'.format(str(line))].value): # if(self.ws['O{}'.format(str(line))].value):
@ -85,23 +106,23 @@ class ReadExcel:
# drug_template_dict['ProductionDate'] = pDate.strftime("%Y-%m-%d") # drug_template_dict['ProductionDate'] = pDate.strftime("%Y-%m-%d")
# drug_template_dict['ShelfLife'] = self.time_long( # drug_template_dict['ShelfLife'] = self.time_long(
# datetime.datetime.strptime(pDate.strftime("%Y-%m-%d %H:%M:%S"), "%Y-%m-%d %H:%M:%S"), datetime.datetime.strptime(str(self.ws['P{}'.format(str(line))].value), "%Y-%m-%d %H:%M:%S")) # datetime.datetime.strptime(pDate.strftime("%Y-%m-%d %H:%M:%S"), "%Y-%m-%d %H:%M:%S"), datetime.datetime.strptime(str(self.ws['P{}'.format(str(line))].value), "%Y-%m-%d %H:%M:%S"))
v_list1 = list("QRSTUVW") # v_list1 = list("QRSTUVW")
key_list1 = [ # key_list1 = [
"price", "is_supervise", "remain", "manufacturer", "remark8", "remark9", "remark10" # "price", "is_supervise", "remain", "manufacturer", "remark8", "remark9", "remark10"
] # ]
for x in range(len(key_list1)): # for x in range(len(key_list1)):
ex = '' # ex = ''
if key_list1[x] in ["price", "remain"]: # if key_list1[x] in ["price", "remain"]:
ex = "0" # ex = "0"
val = value_func(v_list1[x], ex) # val = value_func(v_list1[x], ex)
if v_list1[x] == 'R': # if v_list1[x] == 'R':
if val != "": # if val != "是":
value = 0 # value = 0
else: # else:
value = 1 # value = 1
else: # else:
value = val # value = val
drug_template_dict[key_list1[x]] = value # drug_template_dict[key_list1[x]] = value
drug_list.append(drug_template_dict) drug_list.append(drug_template_dict)
self.template_list.append(json.dumps(drug_list)) self.template_list.append(json.dumps(drug_list))
@ -122,8 +143,8 @@ class ReadExcel:
:param type: 返回结果的时间类型暂时就是返回相差天数 :param type: 返回结果的时间类型暂时就是返回相差天数
:return: 相差的天数 :return: 相差的天数
""" """
da = lambda x: time.strptime(str(x), '%Y-%m-%d %H:%M:%S') def da(x): return time.strptime(str(x), '%Y-%m-%d %H:%M:%S')
if type == 'day': if type == 'day':
day_num = (int(time.mktime(da(time2))) - int(time.mktime(da(time1)))) / ( day_num = (int(time.mktime(da(time2))) - int(time.mktime(da(time1)))) / (
24 * 60 * 60) 24 * 60 * 60)

@ -214,7 +214,7 @@ def import_tmplate():
else: else:
client_id = request.values.get("cliet_id", '') client_id = request.values.get("cliet_id", '')
template_name = request.values.get("template_name", '') template_name = request.values.get("template_name", '')
user_id = request.values.get("user_id") # user_id = request.values.get("user_id")
read_excel = ReadExcel(file_path + "/" + template_name) read_excel = ReadExcel(file_path + "/" + template_name)
sql_all = """ sql_all = """
select bar_code_count, start_bar_code from rms_medicament_template where start_bar_code=(select max(start_bar_code) max_start from rms_medicament_template) select bar_code_count, start_bar_code from rms_medicament_template where start_bar_code=(select max(start_bar_code) max_start from rms_medicament_template)
@ -230,7 +230,8 @@ def import_tmplate():
result_list = read_excel.read() result_list = read_excel.read()
client_obj = BllClient().findEntity(client_id) client_obj = BllClient().findEntity(client_id)
user_obj = BllUser().findEntity(user_id) # user_obj = BllUser().findEntity(user_id)
user_obj = g.current_user
if result_list: if result_list:
inster_obj_list = [] inster_obj_list = []
for value in result_list: for value in result_list:

Loading…
Cancel
Save