From b6ed0329e18fd33d9ed588224141344f23004050 Mon Sep 17 00:00:00 2001 From: apan Date: Thu, 28 Jul 2022 11:28:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E6=8E=A5=E5=8F=A3=E8=B0=83?= =?UTF-8?q?=E6=95=B4;=20=E6=8A=A5=E8=A1=A8=E6=8E=A5=E5=8F=A3=E9=83=A8?= =?UTF-8?q?=E5=88=86=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/Utils.py | 2 +- apps/drug/views.py | 2 +- apps/drug_template/views.py | 37 ++++++++++------ apps/report/views.py | 18 +++----- config/server.py | 18 ++++---- db_logic/medicament.py | 68 ++++++++++++++++------------- db_logic/medicament_template.py | 12 ++++-- models/power_models.py | 73 ++++++++++++++++++++++++++++++++ requirements.txt | Bin 962 -> 1166 bytes 9 files changed, 163 insertions(+), 67 deletions(-) create mode 100644 models/power_models.py diff --git a/Common/Utils.py b/Common/Utils.py index 9baf45b..fa2b634 100644 --- a/Common/Utils.py +++ b/Common/Utils.py @@ -64,7 +64,7 @@ class Utils(object): # sqlalchemy 原生sql转换实体列表 def msyql_table_model(data_list): - return[i._mapping for i in data_list] + return[dict(i._mapping) for i in data_list] #生成统一格式接口数据 def resultData(status, message, data=""): diff --git a/apps/drug/views.py b/apps/drug/views.py index 033ab4b..795f2ff 100644 --- a/apps/drug/views.py +++ b/apps/drug/views.py @@ -114,7 +114,7 @@ def get_drug_info(): # 试剂管理 @drug_router.route("/drug_manage", methods=["GET", "POST"]) -def get_drug_info(): +def get_drug_manage(): seach_word = request.values.get("seach_word") client_id = request.values.get("client_id") data_list = BllMedicament().get_drug_info_distinct_list(seach_word=seach_word, client_id=client_id) diff --git a/apps/drug_template/views.py b/apps/drug_template/views.py index 268f9bc..4655db6 100644 --- a/apps/drug_template/views.py +++ b/apps/drug_template/views.py @@ -3,15 +3,16 @@ ''' @Date:2022/07/25 09:32:18 ''' - +import sys +sys.path.append(".") import datetime import json -from flask import jsonify, request +from flask import jsonify, request, g from flask import Blueprint from common.read_excel import ReadExcel -from common.utils import Utils +from common.utils import Utils, PageParam from db_logic.client import BllClient from db_logic.medicament import BllMedicament from db_logic.user import BllUser @@ -30,9 +31,12 @@ tmp_router = Blueprint("drug_tmplate", __name__) @tmp_router.route("/show_tmp", methods=["GET", "POST"]) def show_template(): client_id = request.values.get("client_id") - data = BllMedicamentTemplate().getAllTemplateList(client_id=client_id) + page = int(request.values.get("page", 1)) + page_size = int(request.values.get("page_size", 10)) + page_param = PageParam(page, page_size) + data = BllMedicamentTemplate().getAllTemplateList(client_id=client_id, page_param=page_param) data = Utils.msyql_table_model(data) - return jsonify(Utils.true_return(data=data)) + return jsonify(Utils.true_return(data={"total_count":page_param.totalRecords, "data_list":data})) @@ -42,17 +46,23 @@ def show_template(): @tmp_router.route("/select_drug", methods=["GET", "POST"]) def select_drug_info(): seach_word =request.values.get("seach_word") - data_list = BllMedicament().get_drug_info_distinct_list(seach_word) - return jsonify(Utils.true_return(data=Utils.msyql_table_model(data_list))) + page = int(request.values.get("page", 1)) + page_size = int(request.values.get("page_size", 10)) + page_param = PageParam(page, page_size) + data_list = BllMedicament().get_drug_info_distinct_list( + seach_word=seach_word, page_param=page_param) + data_list = Utils.msyql_table_model(data_list) + return jsonify(Utils.true_return(data={"total_count": page_param.totalRecords, "data_list": data_list})) # 绑定入库 @tmp_router.route("/bind_tmp_enter_warehouse", methods=["GET", "POST"]) def bind_tmp_input_db(): tmp_content = request.values.get("tmplate_content") customer_id = request.values.get("customer_id", '') - client_id = request.values.get("client_id") + # client_id = request.values.get("client_id") + client_id = '1c39cb24-07f8-11ed-abd4-f47b094925e1' bar_code = request.values.get("bar_code") - user_id = request.values.get("user_id") + # user_id = request.values.get("user_id") if not bar_code: return jsonify(Utils.false_return(msg="条码不能为空")) drug_obj = BllMedicament().findEntity(EntityMedicament.bar_code==bar_code) @@ -60,7 +70,9 @@ def bind_tmp_input_db(): return jsonify(Utils.false_return(msg="该药剂已被锁定")) else: drug_info = eval(tmp_content) - user_info = BllUser().findEntity(user_id) + user_info = BllUser().findEntity('4cea74ee-0d8b-11ed-943e-f47b094925e1') + # user_info = g.current_user + drug_variety = BllMedicamentVariety().createDrugVariety( customer_id=customer_id, drug_info=drug_info, user_info=user_info @@ -109,9 +121,10 @@ def bind_tmp_input_db(): @tmp_router.route("/add_tmp", methods=["GET", "POST"]) def add_tmp_info(): tmp_name = request.values.get("template_name") - client_id = request.values.get("client_id") + # client_id = request.values.get("client_id") + client_id = '1c39cb24-07f8-11ed-abd4-f47b094925e1' tmp_content = request.values.get("tmplate_content") - user_id = request.values.get("user_id") + user_id = '4cea74ee-0d8b-11ed-943e-f47b094925e1' user = BllUser().findEntity(user_id) client = BllClient().findEntity(client_id) sql_all = """ diff --git a/apps/report/views.py b/apps/report/views.py index 869ceea..a4ef39a 100644 --- a/apps/report/views.py +++ b/apps/report/views.py @@ -43,18 +43,14 @@ def get_drug_user_use_number(): @report_router.route("/stock_data_info", methods=["GET", "POST"]) def get_stock_data_info(): name = request.values.get("name", None) - page = request.values.get('page', 1) - page_size = request.values.get('page_size', 15) - finds = ["name", "cas_number", "net_weight_unit", - "client_id", "count_surplus", "sum_remain"] + page = int(request.values.get('page', 1)) + page_size = int(request.values.get('page_size', 15)) page_param = PageParam(page, page_size) - data_list = BllMedicament.get_stock_all_info( + data_list = BllMedicament().get_stock_all_info( name=name, page_param=page_param) + data_list = Utils.msyql_table_model(data_list) if data_list: - data = [] - for i in data_list: - data.append(dict(zip(finds, list(i)))) - return jsonify(Utils.true_return(data={"total_count": page_param.totalRecords, "data": data})) + return jsonify(Utils.true_return(data={"total_count": page_param.totalRecords, "data": data_list})) else: return jsonify(Utils.false_return(msg="无效数据")) @@ -98,7 +94,7 @@ def stock_loss_info(): # 试剂用量消耗 @report_router.route("/drug_use_expend", methods=["GET", "POST"]) -def drug_use_expend(): +def get_drug_use_expend(): seach_word = request.values.get('seach_word') page = request.values.get("page", 1) page_size = request.values.get("page_size", 10) @@ -109,7 +105,7 @@ def drug_use_expend(): # 人员用量消耗 @report_router.route("/drug_user_use_expend", methods=["GET", "POST"]) -def drug_use_expend(): +def get_drug_user_use_expend(): seach_word = request.values.get('seach_word') page = request.values.get("page", 1) page_size = request.values.get("page_size", 10) diff --git a/config/server.py b/config/server.py index 7b30a25..9cf083d 100644 --- a/config/server.py +++ b/config/server.py @@ -62,20 +62,20 @@ class JSONEncoder(_JSONEncoder): class Flask(New_flask): json_encoder = JSONEncoder -def register_blueprints(app: Flask): +def register_blueprints(app: New_flask): """ """ - # from apps import all_router - # from apps.home.views import home_router - + from apps.home.views import home_router + from apps.drug.views import drug_router + from apps.drug_template.views import tmp_router from apps.user.views import user_router - # from apps.report.views import report_router + from apps.report.views import report_router - # all_router = Blueprint('all_router', __name__) - # app.register_blueprint(home_router, url_prefix="/api/home") - # app.register_blueprint(report_router, url_prefix="/api/report") + app.register_blueprint(home_router, url_prefix="/api/home") + app.register_blueprint(drug_router, url_prefix="/api/drug") + app.register_blueprint(tmp_router, url_prefix="/api/drug_tmplate") + app.register_blueprint(report_router, url_prefix="/api/report") app.register_blueprint(user_router, url_prefix="/api/user") - # app.register_blueprint(all_router, url_prefix="/api") def create_app(): diff --git a/db_logic/medicament.py b/db_logic/medicament.py index 2534a74..a394616 100644 --- a/db_logic/medicament.py +++ b/db_logic/medicament.py @@ -4,6 +4,8 @@ @Date:2022/07/18 16:54:45 ''' import sys + +import sqlalchemy sys.path.append('.') import datetime from sqlalchemy import distinct, or_, and_, desc, asc @@ -30,6 +32,10 @@ class BllMedicament(Repository): def __init__(self, entityType=EntityMedicament): return super().__init__(entityType) + + def sql_paging_assemble(self, sql_all, page_param): + sql_all += ' limit ' + str((page_param.curPage - 1) * page_param.pageRows) + ',' + str(page_param.pageRows) + return sql_all # 获取离保质期最近的同类药剂 def getDrugNearExpired(self, varietyId, customerId): @@ -52,13 +58,18 @@ class BllMedicament(Repository): sum(CASE WHEN `status`=1 THEN 1 ELSE 0 END) surplus_number from rms_medicament %s GROUP BY `name`, cas_number, purity """ + # 动态添加查询条件 where_base = "" if client_id: where_base = f" client_id='{client_id}'" if where_base: where_base = " where " + where_base + # 组装sql sql_all = sql_all % where_base + # 调用分页组装sql + sql_all = self.sql_paging_assemble(sql_all, page_param) data_list = self.execute(sql_all).fetchall() + # 查询页总数 count_sql = f"SELECT count(*) FROM rms_medicament %s GROUP BY `name`, cas_number, purity" % where_base page_param.totalRecords = self.execute(count_sql).fetchone()[0] return data_list @@ -97,11 +108,13 @@ class BllMedicament(Repository): return self.queryPage(orm_query, pageParam) # 选择药剂入库--试剂详情 - def get_drug_info_distinct_list(self, seach_word, client_id=None): + def get_drug_info_distinct_list(self, seach_word, page_param, client_id=None): sql_all = """ - select * from rms_medicament %s + select %s from rms_medicament %s """ + # 分组查询 group_base = " group by `name`, cas_number, net_weight_unit" + # 动态添加查询条件 where_base = "" if client_id: where_base += f"client_id='{client_id}'" @@ -111,8 +124,14 @@ class BllMedicament(Repository): where_base += f" `name` like '{seach_word}' or english_name like '{seach_word}'" if where_base: where_base = "where" + where_base - sql_all = sql_all % (where_base+group_base, ) - return self.execute(sql_all).fetchall() + # 组装最终sql + run_sql = sql_all % ("*", where_base+group_base) + run_sql = self.sql_paging_assemble(run_sql, page_param) + run_sql_data = self.execute(run_sql).fetchall() + page_param.totalRecords = self.execute( + sql_all % ("count(*)", where_base + group_base)).fetchone()[0] + + return run_sql_data # 药剂入库 def drugPutIn(self, entityDrug=EntityMedicament(), entityClient=EntityClient(), entityUser=EntityUser()): @@ -224,34 +243,25 @@ class BllMedicament(Repository): # 获取库存信息总览 def get_stock_all_info(self, page_param, name=None): - base_case = [ - self.entityType.name, - self.entityType.cas_number, - self.entityType.net_weight_unit - ] + sql_all = """ + select DISTINCT `name`,cas_number,net_weight_unit, client_id, + sum(CASE WHEN `status`=1 THEN 1 ELSE 0 END) count_number, + sum(CASE WHEN `status`=1 THEN remain ELSE 0 END) sum_remain + FROM rms_medicament %s + """ + filter_base = "" if name: - filt = [self.entityType.name.like(f"%{name}%")] - else: - filt = [] - data = self.session.query( - *base_case, - self.entityType.client_id, - func.count(self.entityType.medicament_id), - func.sum(self.entityType.remain) - ).select_from( - self.entityType - ).distinct( - *base_case - ).filter( - *filt - ).group_by( - *base_case - ) - count_sql = f"SELECT count(medicament_id) FROM rms_medicament " - page_param.totalRecords = self.execute(count_sql).fetchone()[0] + filter_base = f"where `name` LIKE %'{name}'%" - return self.queryPage(data, page_param) + filter_base += " GROUP BY `name`, cas_number, net_weight_unit" + sql_all = sql_all % filter_base + sql_all = self.sql_paging_assemble(sql_all, page_param) + sql_data = self.execute(sql_all).fetchall() + count_sql = f"SELECT count(medicament_id) FROM rms_medicament GROUP BY `name`, cas_number, net_weight_unit " + page_param.totalRecords = len(self.execute(count_sql).fetchone()) + + return sql_data #获取所有药剂列表 def getAllDrugList(self, search_word, page_param, customer_id=None, client_id=None): # (Name like :searchWord or BarCode like :searchWord or EnglishName like :searchWord) diff --git a/db_logic/medicament_template.py b/db_logic/medicament_template.py index 8519543..6a34903 100644 --- a/db_logic/medicament_template.py +++ b/db_logic/medicament_template.py @@ -21,12 +21,16 @@ class BllMedicamentTemplate(Repository): return super().__init__(entityType) # 获取所有模板列表 - def getAllTemplateList(self, client_id): - sql_all = "select * from rms_medicament_template where is_wait_export=1 " + def getAllTemplateList(self, client_id, page_param): + sql_all = "select %s from rms_medicament_template where is_wait_export=1 " end_sql = "order by create_date desc" if client_id: - sql_all = sql_all + f"and client_id='{client_id}'" - data = self.execute(sql_all+end_sql).fetchall() + sql_all = sql_all + f"and client_id='{client_id}'" + + page_param.totalRecords = self.execute(sql_all% ("count(*)")).fetchone()[0] + + sql_all = sql_all + end_sql + ' limit ' + str((page_param.curPage - 1) * page_param.pageRows) + ',' + str(page_param.pageRows) + data = self.execute(sql_all % "*").fetchall() return data def inster_base_log(self, asss): diff --git a/models/power_models.py b/models/power_models.py new file mode 100644 index 0000000..a0f9471 --- /dev/null +++ b/models/power_models.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +''' +@Date:2022/07/28 08:37:59 +''' +import sys +sys.path.append(".") +from sqlalchemy import Column, String, Integer +from models.models_base import Base + + +#终端模块实体类 +class EntityModule(Base): + __tablename__ = "rms_module" + __table_args__ = ( + { + "comment": "终端模块表" + } + ) + + + module_id = Column(String(50), primary_key=True, comment="模块ID") + module_type = Column(String(50), comment="模块类型") + module_code = Column(String(50), comment="") + module_name = Column(String(50), comment="模块名称") + icon = Column(String(50), comment="图片") + url_address = Column(String(50), comment="图片地址") + sort_index = Column(Integer, comment="排序下标") + module_weight = Column(Integer, comment="模块权重") + parent_id = Column(String(50), comment="父id") + is_enabled = Column(Integer, comment="是否启用") + description = Column(String(50), comment="备注") + create_date = Column(String(50), comment="创建时间") + create_user_id = Column(String(50), comment="创建用户id") + create_user_name = Column(String(50), comment="创建用户名") + is_add = Column(Integer, comment="") + + +# #终端模块权限实体类 +class EntityModuleRelation(Base): + __tablename__ = "rms_module_relation" + __table_args__ = ( + { + "comment": "模块权限表" + } + ) + + module_relation_id = Column(String(50), primary_key=True, comment="模块权限id") + customer_id = Column(String(50), comment="客户id") + object_type = Column(String(50), comment="类型") + object_id = Column(String(50), comment="类型id") + module_type = Column(String(50), comment="模块类型") + module_id = Column(String(50), comment="模块id") + create_date = Column(String(50), comment="创建时间") + create_user_id = Column(String(50), comment="创建用户id") + create_user_name = Column(String(50), comment="创建用户名称") + is_add = Column(Integer, comment="") + +# if __name__ == '__main__': +# from sqlalchemy import create_engine +# from config.SystemConfig import SystemConfig +# from sqlalchemy.pool import NullPool + +# from sqlalchemy.orm import sessionmaker +# engine = create_engine(SystemConfig.getConfig( +# 'dbconntion'), poolclass=NullPool) +# # if not database_exists(engine.url): +# # create_database(engine.url) +# # 创建session工厂 +# # DBSession = sessionmaker(bind=engine, expire_on_commit=False) +# # # 创建session对象 +# # session = DBSession() +# Base.metadata.create_all(engine) diff --git a/requirements.txt b/requirements.txt index 1634b8bd22039709995bbbb438e5af101b69930f..1d3bfee69ab36f0d83c202d802f23af900b91277 100644 GIT binary patch delta 206 zcmX@a-p4uNL#P`=4nrbCF+(P=zDXGjLCH2|tJ1Y)C&uO~8^7BEx-M@uc$(#uQ D%grW( delta 19 bcmeC&Qc%#S8(v7`V1O%ewk