#!/usr/bin/env python # -*- encoding: utf-8 -*- ''' @Date:2022/07/18 16:54:45 ''' from re import template import sys sys.path.append('.') import datetime from sqlalchemy import distinct, or_, and_, desc, asc from sqlalchemy.sql import func from db_logic.db_base import Repository from models.medicament_models import EntityMedicament, EntityMedicamentRecord, EntityMedicamentTemplate from models.client_models import EntityClient from models.user_models import EntityUser from db_logic.meidcament_variety import BllMedicamentVariety from common.utils import Utils, DrugStatus, DrugRecordType, PageParam #药剂流程业务逻辑类 class BllMedicament(Repository): #_instance_lock = threading.Lock() ##实现单例模式 #def __new__(cls, *args, **kwargs): # if not hasattr(BllMedicament, "_instance"): # with BllMedicament._instance_lock: # if not hasattr(BllMedicament, "_instance"): # BllMedicament._instance = object.__new__(cls) # return BllMedicament._instance def __init__(self, entityType=EntityMedicament): return super().__init__(entityType) #获取药剂列表 def getDrugList(self, customerId, keyWord, pageParam): keyWord = '%' + keyWord + '%' orm_query = self.findList().filter( EntityMedicament.customer_id == customerId ).filter( # or_(EntityMedicament.RFID.like(keyWord), EntityMedicament.name.like(keyWord)) or_(EntityMedicament.RFID.like(keyWord), EntityMedicament.name.like(keyWord)) ).order_by( # desc(EntityMedicament.PutInStorageDate) desc(EntityMedicament.put_in_date) ) return self.queryPage(orm_query, pageParam) # 药剂入库 def drugPutIn(self, entityDrug=EntityMedicament(), entityClient=EntityClient(), entityUser=EntityUser()): entityDrugRecord = EntityMedicamentRecord( customer_id=entityClient.customer_id, client_id=entityClient.client_id, client_code=entityClient.client_code, variety_id=entityDrug.variety_id, medicament_id=entityDrug.medicament_id, price=entityDrug.price, record_type=DrugRecordType.PutIn, record_remain=float(entityDrug.remain), is_empty=0, create_date=Utils.get_str_datetime(), create_user_id=entityUser.user_id, create_user_name=entityUser.real_name, ) # 创建事务 self.beginTrans() self.session.add(entityDrug) self.session.add(entityDrugRecord) self.commitTrans() # 药剂领用 def drugUse(self, entityDrug=EntityMedicament(), entityClient=EntityClient(), entityUser=EntityUser()): #创建事务 self.beginTrans() self.session.merge(entityDrug) entityDrugRecord = EntityMedicamentRecord( customer_id=entityClient.customer_id, client_id=entityClient.client_id, client_code=entityClient.client_code, variety_id=entityDrug.variety_id, medicament_id=entityDrug.medicament_id, price=entityDrug.price, record_type=DrugRecordType.Use, is_empty=0, record_remain=float(entityDrug.remain), create_date=Utils.get_str_datetime(), create_user_id=entityUser.user_id, create_user_name=entityUser.real_name, ) self.session.add(entityDrugRecord) entityVariety = BllMedicamentVariety().findEntity(entityDrug.variety_id) entityVariety.normal_count -= 1 entityVariety.use_count += 1 self.session.merge(entityVariety) self.commitTrans() # 药剂归还 def drugReturn(self, entityDrug=EntityMedicament(), entityClient=EntityClient(), entityUser=EntityUser()): #创建事务 self.beginTrans() self.session.merge(entityDrug) drug = BllMedicament().findEntity(entityDrug.medicament_id) lastRemain = float(drug.remain) entityDrugRecord = EntityMedicamentRecord( customer_id=entityClient.customer_id, client_id=entityClient.client_id, client_code=entityClient.client_code, variety_id=entityDrug.variety_id, medicament_id=entityDrug.medicament_id, price=entityDrug.price, use_quantity=float(lastRemain) - float(entityDrug.remain if entityDrug.remain else 0), record_type=DrugRecordType.Return, record_remain=float(entityDrug.remain), is_empty=1 if(entityDrug.status == DrugStatus.Empty) else 0, create_date=Utils.get_str_datetime(), create_user_id=entityUser.user_id, create_user_name=entityUser.real_name, ) self.session.add(entityDrugRecord) entityVariety = BllMedicamentVariety().findEntity(entityDrug.variety_id) if(entityDrug.remain != 0): entityVariety.normal_count += 1 else: entityVariety.empty_count += 1 entityVariety.use_count -= 1 self.session.merge(entityVariety) self.commitTrans() # 获取信息详情 def get_medicament_info(self): # 名称、规格、纯度进行去重展示 并分页 self.session = self.DBSession() self.session.query(distinct()).group_by( self.entityType.name, self.entityType.net_weight_unit, self.entityType.purity) # 获取库存信息总览 def get_stock_all_info(self, page, page_size, name=None): page_param = PageParam(page, page_size) base_case = [ self.entityType.name, self.entityType.cas_number, self.entityType.net_weight_unit ] 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 ) return self.queryPage(data, page_param) #获取所有药剂列表 def getAllDrugList(self, search_word, page, page_size, customer_id=None, client_id=None): # (Name like :searchWord or BarCode like :searchWord or EnglishName like :searchWord) """ [ 'medicament_id', 'variety_id', 'bar_code', 'client_id', 'client_code', 'customer_id', 'cas_number', 'name', 'english_name', 'manufacturer', 'distributor', 'production_date', 'expiration_date', 'shelf_life', 'inventory_warning_value', 'shelf_life_warning_value', 'use_days_warning_value', 'is_weigh', 'weigh_flag', 'remain', 'total', 'net_weight_unit', 'net_weight', 'tp', 'purity', 'price', 'flow_position_code', 'cell_position_code', 'place', 'status', 'is_supervise', 'by_user_date', 'by_user_id', 'by_user_name', 'put_in_date', 'put_in_user_id', 'put_in_user_name', 'is_add', 'user_auditing'] """ page_param = PageParam(page, page_size) filter_base = [] if customer_id: filter_base.append(self.entityType.customer_id == customer_id) if client_id: filter_base.append(self.entityType.client_id == client_id) if search_word: seach_w = f"%{search_word}%" filter_base.append( or_( self.entityType.name.like(seach_w), self.entityType.bar_code.like(seach_w), self.entityType.english_name.like(seach_w), ) ) data = self.session.query( self.entityType.name, self.entityType.distributor, self.entityType.net_weight_unit, self.entityType.remain, self.entityType.production_date, self.entityType.expiration_date, self.entityType.shelf_life, self.entityType.put_in_user_name, self.entityType.put_in_date ).filter(*filter_base) fields = [ "name", "distributor", "net_weight_unit", "remain", "production_date", "expiration_date", "shelf_life", "put_in_user_name", "put_in_date" ] data_list = [] page_data = self.queryPage(data, page_param) for i in page_data: data_list.append(dict(zip(fields, i))) return data_list # # 获取库存消耗 # def get_comsume_stock(self): # self.session.query( # self.entityType.status, # func.count(self.entityType.medicament_id) # ) # #获取指定用户当前领用药剂 # def getUserUseDrugList(self, userId): # drugList = self.findList( # EntityMedicament.ByUserId == userId, EntityMedicament.Status == DrugStatus.Out).all() # return drugList