diff --git a/Common/read_excel.py b/Common/read_excel.py index 16f3224..66dd769 100644 --- a/Common/read_excel.py +++ b/Common/read_excel.py @@ -10,10 +10,11 @@ import datetime from string import ascii_uppercase from openpyxl import load_workbook + class ReadExcel: def __init__(self, excelName): - self.wb = load_workbook(excelName) + self.wb = load_workbook(filename=excelName) self.ws = self.wb.active self.template_list = [] @@ -29,49 +30,69 @@ class ReadExcel: # lineIndex = 0 try: 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 = { "variety_id": str(uuid.uuid1()), } key_list = [ - "remark1", "remark2", "remark3", "remark4", "name", - "english_name", "purity", "cas_number", "remark5", "net_weight", - "net_weight_unit", "export_count", "remark6" + "name", "english_name", "purity", "cas_number", "speci", "net_weight_unit", "export_count", "export_unit", + "production_date", "shelf_life", "expiration_date", "price", "is_supervise", "total", "manufacturer", "remain" ] # 采用列表和字母对应的两个列表,进行循环的方式写入字典,简化代码量 - 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)): - 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") == "": self.template_list.append(json.dumps(drug_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 value_func("O"): - pDate = date_func(value_func("N")) - shelf_life = value_func("O", "3650") - else: - if value_func("N"): - pDate = date_func(value_func('N'), str_tim) - shelf_life = self.time_long( - date_func(pDate.strftime(str_tim)), date_func(value_func("P"))) - drug_template_dict["production_date"] = pDate.strftime(str_tim) - drug_template_dict["shelf_life"] = shelf_life - + # if not value_func("P"): + # if value_func("O"): + # pDate = date_func(value_func("N")) + # shelf_life = value_func("O", "3650") + # else: + # if value_func("N"): + # pDate = date_func(value_func('N'), str_tim) + # shelf_life = self.time_long( + # date_func(pDate.strftime(str_tim)), date_func(value_func("P"))) + # drug_template_dict["production_date"] = pDate.strftime( + # str_tim) + # drug_template_dict["shelf_life"] = shelf_life + # if(not self.ws['P{}'.format(str(line))].value): # pDate = datetime.datetime.now() # 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['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")) - v_list1 = list("QRSTUVW") - key_list1 = [ - "price", "is_supervise", "remain", "manufacturer", "remark8", "remark9", "remark10" - ] - for x in range(len(key_list1)): - ex = '' - if key_list1[x] in ["price", "remain"]: - ex = "0" - val = value_func(v_list1[x], ex) - if v_list1[x] == 'R': - if val != "是": - value = 0 - else: - value = 1 - else: - value = val - drug_template_dict[key_list1[x]] = value + # v_list1 = list("QRSTUVW") + # key_list1 = [ + # "price", "is_supervise", "remain", "manufacturer", "remark8", "remark9", "remark10" + # ] + # for x in range(len(key_list1)): + # ex = '' + # if key_list1[x] in ["price", "remain"]: + # ex = "0" + # val = value_func(v_list1[x], ex) + # if v_list1[x] == 'R': + # if val != "是": + # value = 0 + # else: + # value = 1 + # else: + # value = val + # drug_template_dict[key_list1[x]] = value drug_list.append(drug_template_dict) self.template_list.append(json.dumps(drug_list)) @@ -122,8 +143,8 @@ class ReadExcel: :param type: 返回结果的时间类型(暂时就是返回相差天数) :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': day_num = (int(time.mktime(da(time2))) - int(time.mktime(da(time1)))) / ( 24 * 60 * 60) diff --git a/apps/drug_template/views.py b/apps/drug_template/views.py index f9c3bac..a496476 100644 --- a/apps/drug_template/views.py +++ b/apps/drug_template/views.py @@ -214,7 +214,7 @@ def import_tmplate(): else: client_id = request.values.get("cliet_id", '') 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) 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) @@ -230,7 +230,8 @@ def import_tmplate(): result_list = read_excel.read() 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: inster_obj_list = [] for value in result_list: