|
|
#!/usr/bin/env python
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
'''
|
|
|
@Date:2022/07/18 16:54:45
|
|
|
'''
|
|
|
import datetime
|
|
|
from sqlalchemy import 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 common.utils import Utils, DrugStatus, DrugRecordType
|
|
|
|
|
|
|
|
|
|
|
|
#药剂流程业务逻辑类
|
|
|
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.CustomerId == customerId
|
|
|
).filter(or_(EntityMedicament.RFID.like(keyWord), EntityMedicament.Name.like(keyWord))).order_by(desc(EntityMedicament.PutInStorageDate))
|
|
|
return self.queryPage(orm_query, pageParam)
|
|
|
|
|
|
# 药剂入库
|
|
|
def drugPutIn(self, entityDrug=EntityMedicament(), entityClient=EntityClient(), entityUser=EntityUser()):
|
|
|
|
|
|
entityDrugRecord = EntityMedicamentRecord()
|
|
|
entityDrugRecord.RecordId = Utils.UUID()
|
|
|
entityDrugRecord.CustomerId = entityClient.CustomerId
|
|
|
entityDrugRecord.ClientId = entityClient.ClientId
|
|
|
entityDrugRecord.ClientCode = entityClient.ClientCode
|
|
|
entityDrugRecord.VarietyId = entityDrug.VarietyId
|
|
|
entityDrugRecord.MedicamentId = entityDrug.MedicamentId
|
|
|
entityDrugRecord.Price = entityDrug.Price
|
|
|
entityDrugRecord.RecordType = DrugRecordType.PutIn
|
|
|
entityDrugRecord.RecordRemain = float(entityDrug.Remain)
|
|
|
|
|
|
entityDrugRecord.IsEmpty = 0
|
|
|
entityDrugRecord.CreateDate = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
entityDrugRecord.CreateUserId = entityUser.UserId
|
|
|
entityDrugRecord.CreateUserName = entityUser.RealName
|
|
|
# 创建事务
|
|
|
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)
|
|
|
|
|
|
#if(BllUserMedicament().isJInZhiUser(entityUser.UserId,entityDrug.MedicamentId)):
|
|
|
# warning_obj = EntityWarning(WarningId=str(Utils.UUID()), CustomerId=entityDrug.CustomerId,
|
|
|
# ObjectType=7, ObjectId=entityDrug.MedicamentId, ObjectName=entityDrug.Name,
|
|
|
# WarningContent= entityUser.RealName+'违规领用了药剂“'+entityDrug.Name+'”('+entityDrug.BarCode+')',WarningDate=datetime.datetime.now(),
|
|
|
# WarningUserName=entityUser.RealName, IsSolve=0, IsAdd=1)
|
|
|
# BllWarning().insert(warning_obj)
|
|
|
|
|
|
entityDrugRecord = EntityMedicamentRecord()
|
|
|
entityDrugRecord.RecordId = Utils.UUID()
|
|
|
entityDrugRecord.CustomerId = entityClient.CustomerId
|
|
|
entityDrugRecord.ClientId = entityClient.ClientId
|
|
|
entityDrugRecord.ClientCode = entityClient.ClientCode
|
|
|
entityDrugRecord.VarietyId = entityDrug.VarietyId
|
|
|
entityDrugRecord.MedicamentId = entityDrug.MedicamentId
|
|
|
entityDrugRecord.Price = entityDrug.Price
|
|
|
entityDrugRecord.RecordType = DrugRecordType.Use
|
|
|
entityDrugRecord.IsEmpty = 0
|
|
|
entityDrugRecord.RecordRemain = float(entityDrug.Remain)
|
|
|
entityDrugRecord.CreateDate = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
entityDrugRecord.CreateUserId = entityUser.UserId
|
|
|
entityDrugRecord.CreateUserName = entityUser.RealName
|
|
|
self.session.add(entityDrugRecord)
|
|
|
entityVariety = BllMedicamentVariety().findEntity(entityDrug.VarietyId)
|
|
|
entityVariety.NormalCount -= 1
|
|
|
entityVariety.UseCount += 1
|
|
|
self.session.merge(entityVariety)
|
|
|
self.commitTrans()
|
|
|
|
|
|
# 药剂归还
|
|
|
def drugReturn(self, entityDrug=EntityMedicament(), entityClient=EntityClient(), entityUser=EntityUser()):
|
|
|
#创建事务
|
|
|
self.beginTrans()
|
|
|
self.session.merge(entityDrug)
|
|
|
entityDrugRecord = EntityMedicamentRecord()
|
|
|
entityDrugRecord.RecordId = Utils.UUID()
|
|
|
entityDrugRecord.CustomerId = entityClient.CustomerId
|
|
|
entityDrugRecord.ClientId = entityClient.ClientId
|
|
|
entityDrugRecord.ClientCode = entityClient.ClientCode
|
|
|
entityDrugRecord.VarietyId = entityDrug.VarietyId
|
|
|
entityDrugRecord.MedicamentId = entityDrug.MedicamentId
|
|
|
entityDrugRecord.Price = entityDrug.Price
|
|
|
drug = BllMedicament().findEntity(entityDrug.MedicamentId)
|
|
|
lastRemain = float(drug.Remain)
|
|
|
entityDrugRecord.UseQuantity = float(
|
|
|
lastRemain) - float(entityDrug.Remain if entityDrug.Remain else 0)
|
|
|
entityDrugRecord.RecordType = DrugRecordType.Return
|
|
|
entityDrugRecord.RecordRemain = float(entityDrug.Remain)
|
|
|
entityDrugRecord.IsEmpty = 1 if(
|
|
|
entityDrug.Status == DrugStatus.Empty) else 0
|
|
|
entityDrugRecord.CreateDate = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
entityDrugRecord.CreateUserId = entityUser.UserId
|
|
|
entityDrugRecord.CreateUserName = entityUser.RealName
|
|
|
self.session.add(entityDrugRecord)
|
|
|
entityVariety = BllMedicamentVariety().findEntity(entityDrug.VarietyId)
|
|
|
if(entityDrug.Remain != 0):
|
|
|
entityVariety.NormalCount += 1
|
|
|
else:
|
|
|
entityVariety.EmptyCount += 1
|
|
|
entityVariety.UseCount -= 1
|
|
|
self.session.merge(entityVariety)
|
|
|
|
|
|
self.commitTrans()
|
|
|
|
|
|
#获取离保质期最近的同类药剂
|
|
|
def getDrugNearExpired(self, varietyId, customerId):
|
|
|
drugList = self.findList(and_(EntityMedicament.Status == DrugStatus.Normal, EntityMedicament.VarietyId == varietyId)).order_by(
|
|
|
asc(EntityMedicament.ExpirationDate)).limit(1)
|
|
|
return drugList.first()
|
|
|
|
|
|
#获过期药剂列表
|
|
|
def getExpiredDrugList(self, customerId):
|
|
|
todayDate = datetime.datetime.now().strftime("%Y-%m-%d")
|
|
|
drugList = self.findList(and_(func.date_format(
|
|
|
EntityMedicament.ExpirationDate, '%Y-%m-%d') <= todayDate, EntityMedicament.Status != 3)).all()
|
|
|
return drugList
|
|
|
|
|
|
#获取待归还药剂列表
|
|
|
def getWaitReturnDrugList(self, customerId):
|
|
|
drugList = self.findList(and_(EntityMedicament.CustomerId ==
|
|
|
customerId, EntityMedicament.Status == DrugStatus.Out)).all()
|
|
|
return drugList
|
|
|
|
|
|
#获取待入库药剂模板
|
|
|
def getWaitPutInDrugTemplateList(self, customerId, clientId):
|
|
|
templateList = BllMedicamentTemplate().findList(and_(EntityMedicamentTemplate.CustomerId == customerId, EntityMedicamentTemplate.ClientId ==
|
|
|
clientId, EntityMedicamentTemplate.IsWaitExport == 1)).order_by(EntityMedicamentTemplate.CreateDate.desc()).limit(1).all()
|
|
|
return templateList
|
|
|
|
|
|
#获取所有药剂列表
|
|
|
def getAllDrugList(self, searchWord, pageParam, customerId):
|
|
|
queryStr = '(select * from RMS_Medicament where CustomerId=:customerId and ClientId=:clientId and (Name like :searchWord or BarCode like :searchWord or EnglishName like :searchWord)) '
|
|
|
queryStr += ' union all (select * from RMS_Medicament where CustomerId=:customerId and ClientId!=:clientId and (Name like :searchWord or BarCode like :searchWord or EnglishName like :searchWord) order by ClientCode ASC ) '
|
|
|
queryParams = {"searchWord": '%' + searchWord + '%',
|
|
|
"customerId": customerId, "clientId": CurrentInfo.ClientInfo.ClientId}
|
|
|
templateList = self.execute(queryStr + ' limit ' + str((pageParam.curPage - 1)
|
|
|
* pageParam.pageRows) + ',' + str(pageParam.pageRows), queryParams).fetchall()
|
|
|
|
|
|
countQuery = "select count(*) from RMS_Medicament where CustomerId=:customerId and (Name like :searchWord or BarCode like :searchWord or EnglishName like :searchWord)"
|
|
|
pageParam.totalRecords = self.execute(
|
|
|
countQuery, queryParams).fetchone()[0]
|
|
|
jsonData = Utils.mysqlTable2Model(templateList)
|
|
|
return jsonData
|
|
|
|
|
|
#获取指定用户当前领用药剂
|
|
|
def getUserUseDrugList(self, userId):
|
|
|
|
|
|
drugList = self.findList(
|
|
|
EntityMedicament.ByUserId == userId, EntityMedicament.Status == DrugStatus.Out).all()
|
|
|
return drugList
|