mobile_dialysis_service.go 63KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. "time"
  7. )
  8. // func GetSchedualPatients(orgID int64) ([]*MDialysisScheduleVM, error) {
  9. // var vms []*MDialysisScheduleVM
  10. // err := readDb.
  11. // Table("xt_schedule as sch").
  12. // Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  13. // Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  14. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  15. // Preload("TreatmentMode", "status = 1").
  16. // Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  17. // Preload("DialysisOrder.MonitoringRecords", "status = 1 AND user_org_id = ?", orgID).
  18. // Where("sch.status = 1 AND sch.user_org_id = ? AND schedule_date = 1536768000", orgID).
  19. // Find(&vms).
  20. // Error
  21. // return vms, err
  22. // }
  23. func MobileGetDialysisScheduals(orgID int64, scheduleDate int64, scheduleType int64) ([]*MDialysisScheduleVM, error) {
  24. var vms []*MDialysisScheduleVM
  25. db := readDb.
  26. Table("xt_schedule as sch").
  27. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  28. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  29. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  30. Preload("Advices", "status = 1 AND user_org_id = ? AND advice_type = 2", orgID).
  31. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  32. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  33. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  34. Preload("DialysisOrder.DeviceNumber", "status = 1 AND org_id = ?", orgID).
  35. Preload("TreatmentSummary", "status = 1 AND user_org_id = ?", orgID).
  36. // Preload("DialysisOrder.MonitoringRecords", func(rdb *gorm.DB) *gorm.DB {
  37. // return rdb.Where("status = 1 AND user_org_id = ?", orgID).Order("operate_time DESC")
  38. // }).
  39. Where("sch.status = 1 AND sch.user_org_id = ?", orgID)
  40. if scheduleDate != 0 {
  41. db = db.Where("schedule_date = ?", scheduleDate)
  42. }
  43. if scheduleType != 0 {
  44. db = db.Where("schedule_type = ?", scheduleType)
  45. }
  46. err := db.Find(&vms).Error
  47. return vms, err
  48. }
  49. func MobileGetWaitingScheduals(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  50. var vms []*MDialysisScheduleVM
  51. db := readDb.
  52. Table("xt_schedule as sch").
  53. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  54. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  55. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  56. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  57. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  58. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  59. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  60. Preload("Advices", "status = 1 AND user_org_id = ? AND advice_type = 2 ", orgID).
  61. Preload("TreatmentMode", "status = 1").
  62. Preload("TreatmentSummary", "status = 1 AND user_org_id = ?", orgID).
  63. Where("sch.status = 1 AND sch.user_org_id = ?", orgID)
  64. if scheduleDate != 0 {
  65. db = db.Where("schedule_date = ?", scheduleDate)
  66. }
  67. err := db.Find(&vms).Error
  68. return vms, err
  69. }
  70. type VMTreatmentSummary struct {
  71. ID int64 `gorm:"column:id" json:"id"`
  72. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  73. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  74. AssessmentDate int64 `gorm:"column:assessment_date" json:"assessment_date"`
  75. DialysisSummary string `gorm:"column:dialysis_summary" json:"dialysis_summary" form:"dialysis_summary"`
  76. }
  77. func (VMTreatmentSummary) TableName() string {
  78. return "xt_treatment_summary"
  79. }
  80. type MDialysisScheduleVM struct {
  81. ID int64 `gorm:"column:id" json:"id"`
  82. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  83. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  84. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  85. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  86. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  87. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  88. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  89. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  90. Status int64 `gorm:"column:status" json:"status"`
  91. SchedualPatient *MSchedualPatientVMList `gorm:"ForeignKey:PatientId" json:"patient"`
  92. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  93. DialysisOrder *MDialysisOrderVMList `gorm:"ForeignKey:DialysisDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"dialysis_order"`
  94. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:RecordDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"prescription"`
  95. AssessmentBeforeDislysis *models.PredialysisEvaluationList `gorm:"ForeignKey:AssessmentDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"assessment_before_dislysis"`
  96. Advices []*VMDoctorAdvice `gorm:"ForeignKey:AdviceDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"doctor_advice"`
  97. TreatmentSummary *VMTreatmentSummary `gorm:"ForeignKey:AssessmentDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"treatment_summary"`
  98. }
  99. func (MDialysisScheduleVM) TableName() string {
  100. return "xt_schedule"
  101. }
  102. type MDeviceNumberVM struct {
  103. models.DeviceNumber
  104. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  105. }
  106. func (MDeviceNumberVM) TableName() string {
  107. return "xt_device_number"
  108. }
  109. type MSchedualPatientVMList struct {
  110. ID int64 `gorm:"column:id" json:"id" form:"id"`
  111. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  112. UserId int64 `gorm:"column:user_id" json:"user_id" form:"user_id"`
  113. PatientType int64 `gorm:"column:patient_type" json:"patient_type" form:"patient_type"`
  114. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  115. Avatar string `gorm:"column:avatar" json:"avatar" form:"avatar"`
  116. Gender int64 `gorm:"column:gender" json:"gender" form:"gender"`
  117. Birthday int64 `gorm:"column:birthday" json:"birthday" form:"birthday"`
  118. Age int64 `gorm:"column:age" json:"age"`
  119. Name string `gorm:"column:name" json:"name" form:"name"`
  120. IdCardNo string `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
  121. UserSysBeforeCount int64 `gorm:"column:user_sys_before_count" json:"user_sys_before_count" form:"user_sys_before_count"`
  122. }
  123. func (MSchedualPatientVMList) TableName() string {
  124. return "xt_patients"
  125. }
  126. type MSchedualPatientVM struct {
  127. ID int64 `gorm:"column:id" json:"id" form:"id"`
  128. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  129. UserId int64 `gorm:"column:user_id" json:"user_id" form:"user_id"`
  130. PatientType int64 `gorm:"column:patient_type" json:"patient_type" form:"patient_type"`
  131. Avatar string `gorm:"column:avatar" json:"avatar" form:"avatar"`
  132. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  133. AdmissionNumber string `gorm:"column:admission_number" json:"admission_number" form:"admission_number"`
  134. Source int64 `gorm:"column:source" json:"source" form:"source"`
  135. Lapseto int64 `gorm:"column:lapseto" json:"lapseto" form:"lapseto"`
  136. PartitionId int64 `gorm:"column:partition_id" json:"partition_id" form:"partition_id"`
  137. BedId int64 `gorm:"column:bed_id" json:"bed_id" form:"bed_id"`
  138. Name string `gorm:"column:name" json:"name" form:"name"`
  139. Alias string `gorm:"column:alias" json:"alias" form:"alias"`
  140. Gender int64 `gorm:"column:gender" json:"gender" form:"gender"`
  141. Nation string `gorm:"column:nation" json:"nation" form:"nation"`
  142. NativePlace string `gorm:"column:native_place" json:"native_place" form:"native_place"`
  143. MaritalStatus int64 `gorm:"column:marital_status" json:"marital_status" form:"marital_status"`
  144. IdCardNo string `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
  145. Birthday int64 `gorm:"column:birthday" json:"birthday" form:"birthday"`
  146. ReimbursementWayId int64 `gorm:"column:reimbursement_way_id" json:"reimbursement_way_id" form:"reimbursement_way_id"`
  147. HealthCareType int64 `gorm:"column:health_care_type" json:"health_care_type" form:"health_care_type"`
  148. HealthCareNo string `gorm:"column:health_care_no" json:"health_care_no" form:"health_care_no"`
  149. HealthCareDueDate int64 `gorm:"column:health_care_due_date" json:"health_care_due_date" form:"health_care_due_date"`
  150. Height int64 `gorm:"column:height" json:"height" form:"height"`
  151. BloodType int64 `gorm:"column:blood_type" json:"blood_type" form:"blood_type"`
  152. Rh int64 `gorm:"column:rh" json:"rh" form:"rh"`
  153. HealthCareDueAlertDate int64 `gorm:"column:health_care_due_alert_date" json:"health_care_due_alert_date" form:"health_care_due_alert_date"`
  154. EducationLevel int64 `gorm:"column:education_level" json:"education_level" form:"education_level"`
  155. Profession int64 `gorm:"column:profession" json:"profession" form:"profession"`
  156. Phone string `gorm:"column:phone" json:"phone" form:"phone"`
  157. HomeTelephone string `gorm:"column:home_telephone" json:"home_telephone" form:"home_telephone"`
  158. RelativePhone string `gorm:"column:relative_phone" json:"relative_phone" form:"relative_phone"`
  159. RelativeRelations string `gorm:"column:relative_relations" json:"relative_relations" form:"relative_relations"`
  160. HomeAddress string `gorm:"column:home_address" json:"home_address" form:"home_address"`
  161. WorkUnit string `gorm:"column:work_unit" json:"work_unit" form:"work_unit"`
  162. UnitAddress string `gorm:"column:unit_address" json:"unit_address" form:"unit_address"`
  163. Children int64 `gorm:"column:children" json:"children" form:"children"`
  164. ReceivingDate int64 `gorm:"column:receiving_date" json:"receiving_date" form:"receiving_date"`
  165. IsHospitalFirstDialysis int64 `gorm:"column:is_hospital_first_dialysis" json:"is_hospital_first_dialysis" form:"is_hospital_first_dialysis"`
  166. FirstDialysisDate int64 `gorm:"column:first_dialysis_date" json:"first_dialysis_date" form:"first_dialysis_date"`
  167. FirstDialysisHospital string `gorm:"column:first_dialysis_hospital" json:"first_dialysis_hospital" form:"first_dialysis_hospital"`
  168. InductionPeriod int64 `gorm:"column:induction_period" json:"induction_period" form:"induction_period"`
  169. InitialDialysis int64 `gorm:"column:initial_dialysis" json:"initial_dialysis" form:"initial_dialysis"`
  170. TotalDialysis int64 `gorm:"column:total_dialysis" json:"total_dialysis" form:"total_dialysis"`
  171. AttendingDoctorId int64 `gorm:"column:attending_doctor_id" json:"attending_doctor_id" form:"attending_doctor_id"`
  172. HeadNurseId int64 `gorm:"column:head_nurse_id" json:"head_nurse_id" form:"head_nurse_id"`
  173. Evaluate string `gorm:"column:evaluate" json:"evaluate" form:"evaluate"`
  174. Diagnose string `gorm:"column:diagnose" json:"diagnose" form:"diagnose"`
  175. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  176. RegistrarsId int64 `gorm:"column:registrars_id" json:"registrars_id" form:"registrars_id"`
  177. Registrars string `gorm:"column:registrars" json:"registrars" form:"registrars"`
  178. QrCode string `gorm:"column:qr_code" json:"qr_code" form:"qr_code"`
  179. BindingState int64 `gorm:"column:binding_state" json:"binding_state" form:"binding_state"`
  180. PatientComplains string `gorm:"column:patient_complains" json:"patient_complains"` // 主诉
  181. PresentHistory string `gorm:"column:present_history" json:"present_history"` // 现病史
  182. PastHistory string `gorm:"column:past_history" json:"past_history"` // 既往史
  183. Temperature float64 `gorm:"column:temperature" json:"temperature"` // 体格检查-体温
  184. Pulse int64 `gorm:"column:pulse" json:"pulse"` // 体格检查-脉搏
  185. Respiratory int64 `gorm:"column:respiratory" json:"respiratory"` // 体格检查-呼吸频率
  186. SBP int64 `gorm:"column:sbp" json:"sbp"` // 体格检查-收缩压
  187. DBP int64 `gorm:"column:dbp" json:"dbp"` // 体格检查-舒张压
  188. Status int64 `gorm:"column:status" json:"status" form:"status"`
  189. Age int64 `gorm:"column:age" json:"age"`
  190. IsOpenRemind int64 `gorm:"column:is_open_remind" json:"is_open_remind"`
  191. DialysisAge int64 `gorm:"column:dialysis_age" json:"dialysis_age" form:"dialysis_age"`
  192. ExpenseKind int64 `gorm:"column:expense_kind" json:"expense_kind" form:"expense_kind"`
  193. TellPhone string `gorm:"column:tell_phone" json:"tell_phone" form:"tell_phone"`
  194. FirstTreatmentDate int64 `gorm:"column:first_treatment_date" json:"first_treatment_date" form:"first_treatment_date"`
  195. ContactName string `gorm:"column:contact_name" json:"contact_name" form:"contact_name"`
  196. UserSysBeforeCount int64 `gorm:"column:user_sys_before_count" json:"user_sys_before_count" form:"user_sys_before_count"`
  197. }
  198. func (MSchedualPatientVM) TableName() string {
  199. return "xt_patients"
  200. }
  201. type MDialysisOrderVM struct {
  202. ID int64 `gorm:"column:id" json:"id"`
  203. DialysisDate int64 `gorm:"column:dialysis_date" json:"dialysis_date"`
  204. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  205. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  206. // PrescriptionId int64 `gorm:"column:prescription_id" json:"prescription_id"`
  207. Stage int64 `gorm:"column:stage" json:"stage"`
  208. // Remark string `gorm:"column:remark" json:"remark"`
  209. BedID int64 `gorm:"column:bed_id" json:"bed_id"`
  210. StartNurse int64 `gorm:"column:start_nurse" json:"start_nurse"`
  211. FinishNurse int64 `gorm:"column:finish_nurse" json:"finish_nurse"`
  212. Status int64 `gorm:"column:status" json:"status"`
  213. PunctureNurse int64 `gorm:"column:puncture_nurse" json:"puncture_nurse"`
  214. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedID" json:"device_number"`
  215. MonitoringRecords []*models.MonitoringRecord `gorm:"ForeignKey:DialysisOrderId" json:"monitoring_records"`
  216. Creator int64 `gorm:"column:creator" json:"creator"`
  217. Modifier int64 `gorm:"column:modifier" json:"modifier"`
  218. FinishCreator int64 `gorm:"column:finish_creator" json:"finish_creator"`
  219. FinishModifier int64 `gorm:"column:finish_modifier" json:"finish_modifier"`
  220. SchedualType int64 `gorm:"column:schedual_type" json:"schedual_type"`
  221. }
  222. func (MDialysisOrderVM) TableName() string {
  223. return "xt_dialysis_order"
  224. }
  225. type MDialysisOrderVMList struct {
  226. ID int64 `gorm:"column:id" json:"id"`
  227. DialysisDate int64 `gorm:"column:dialysis_date" json:"dialysis_date"`
  228. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  229. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  230. // PrescriptionId int64 `gorm:"column:prescription_id" json:"prescription_id"`
  231. Stage int64 `gorm:"column:stage" json:"stage"`
  232. // Remark string `gorm:"column:remark" json:"remark"`
  233. BedID int64 `gorm:"column:bed_id" json:"bed_id"`
  234. StartNurse int64 `gorm:"column:start_nurse" json:"start_nurse"`
  235. Status int64 `gorm:"column:status" json:"status"`
  236. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedID" json:"device_number"`
  237. Creator int64 `gorm:"column:creator" json:"creator"`
  238. }
  239. func (MDialysisOrderVMList) TableName() string {
  240. return "xt_dialysis_order"
  241. }
  242. type VMDoctorAdvice struct {
  243. ID int64 `gorm:"column:id" json:"id" form:"id"`
  244. GroupNo int64 `gorm:"column:groupno" json:"groupno" form:"groupno"`
  245. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  246. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  247. AdviceDate int64 `gorm:"column:advice_date" json:"advice_date" form:"advice_date"`
  248. Status int64 `gorm:"column:status" json:"status" form:"status"`
  249. ExecutionState int64 `gorm:"column:execution_state" json:"execution_state" form:"execution_state"`
  250. RecordDate int64 `gorm:"column:record_date" json:"record_date"`
  251. }
  252. func (VMDoctorAdvice) TableName() string {
  253. return "xt_doctor_advice"
  254. }
  255. // 获取透析记录
  256. func MobileGetDialysisRecord(orgID int64, patientID int64, recordDate int64) (*models.DialysisOrder, error) {
  257. var record models.DialysisOrder
  258. err := readDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgID, patientID, recordDate).First(&record).Error
  259. if err != nil {
  260. if err == gorm.ErrRecordNotFound {
  261. return nil, nil
  262. } else {
  263. return nil, err
  264. }
  265. }
  266. return &record, nil
  267. }
  268. // 用户基本信息
  269. func MobileGetPatientDetail(orgID int64, patientID int64) (*MPatient, error) {
  270. var patient MPatient
  271. err := readDb.Model(&MPatient{}).Where("status = 1 AND user_org_id = ? AND id = ?", orgID, patientID).First(&patient).Error
  272. if err != nil {
  273. if err == gorm.ErrRecordNotFound {
  274. return nil, nil
  275. } else {
  276. return nil, err
  277. }
  278. }
  279. return &patient, nil
  280. }
  281. // 用户排班信息
  282. func MobileGetSchedualDetail(orgID int64, patientID int64, schedualDate int64) (*MDialysisScheduleVM, error) {
  283. var vm MDialysisScheduleVM
  284. err := readDb.
  285. Table("xt_schedule").
  286. // Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  287. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  288. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND patient_id = ?", orgID, schedualDate, patientID).
  289. First(&vm).Error
  290. if err != nil {
  291. if err == gorm.ErrRecordNotFound {
  292. return nil, nil
  293. } else {
  294. return nil, err
  295. }
  296. }
  297. return &vm, err
  298. }
  299. // 用户排班信息
  300. func MobileGetPatientSchedual(orgID int64, patientID int64, schedualDate int64) (*models.Schedule, error) {
  301. var schedule models.Schedule
  302. err := readDb.
  303. Table("xt_schedule").
  304. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND patient_id = ?", orgID, schedualDate, patientID).
  305. First(&schedule).Error
  306. if err != nil {
  307. if err == gorm.ErrRecordNotFound {
  308. return nil, nil
  309. } else {
  310. return nil, err
  311. }
  312. }
  313. return &schedule, nil
  314. }
  315. type MPatient struct {
  316. ID int64 `gorm:"column:id" json:"id" form:"id"`
  317. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  318. UserId int64 `gorm:"column:user_id" json:"user_id" form:"user_id"`
  319. PatientType int64 `gorm:"column:patient_type" json:"patient_type" form:"patient_type"`
  320. Avatar string `gorm:"column:avatar" json:"avatar" form:"avatar"`
  321. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  322. AdmissionNumber string `gorm:"column:admission_number" json:"admission_number" form:"admission_number"`
  323. Source int64 `gorm:"column:source" json:"source" form:"source"`
  324. Lapseto int64 `gorm:"column:lapseto" json:"lapseto" form:"lapseto"`
  325. PartitionId int64 `gorm:"column:partition_id" json:"partition_id" form:"partition_id"`
  326. BedId int64 `gorm:"column:bed_id" json:"bed_id" form:"bed_id"`
  327. Name string `gorm:"column:name" json:"name" form:"name"`
  328. Alias string `gorm:"column:alias" json:"alias" form:"alias"`
  329. Gender int64 `gorm:"column:gender" json:"gender" form:"gender"`
  330. Nation string `gorm:"column:nation" json:"nation" form:"nation"`
  331. NativePlace string `gorm:"column:native_place" json:"native_place" form:"native_place"`
  332. MaritalStatus int64 `gorm:"column:marital_status" json:"marital_status" form:"marital_status"`
  333. IdCardNo string `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
  334. Birthday int64 `gorm:"column:birthday" json:"birthday" form:"birthday"`
  335. ReimbursementWayId int64 `gorm:"column:reimbursement_way_id" json:"reimbursement_way_id" form:"reimbursement_way_id"`
  336. HealthCareType int64 `gorm:"column:health_care_type" json:"health_care_type" form:"health_care_type"`
  337. HealthCareNo string `gorm:"column:health_care_no" json:"health_care_no" form:"health_care_no"`
  338. HealthCareDueDate int64 `gorm:"column:health_care_due_date" json:"health_care_due_date" form:"health_care_due_date"`
  339. Height int64 `gorm:"column:height" json:"height" form:"height"`
  340. BloodType int64 `gorm:"column:blood_type" json:"blood_type" form:"blood_type"`
  341. Rh int64 `gorm:"column:rh" json:"rh" form:"rh"`
  342. HealthCareDueAlertDate int64 `gorm:"column:health_care_due_alert_date" json:"health_care_due_alert_date" form:"health_care_due_alert_date"`
  343. EducationLevel int64 `gorm:"column:education_level" json:"education_level" form:"education_level"`
  344. Profession int64 `gorm:"column:profession" json:"profession" form:"profession"`
  345. Phone string `gorm:"column:phone" json:"phone" form:"phone"`
  346. HomeTelephone string `gorm:"column:home_telephone" json:"home_telephone" form:"home_telephone"`
  347. RelativePhone string `gorm:"column:relative_phone" json:"relative_phone" form:"relative_phone"`
  348. RelativeRelations string `gorm:"column:relative_relations" json:"relative_relations" form:"relative_relations"`
  349. HomeAddress string `gorm:"column:home_address" json:"home_address" form:"home_address"`
  350. WorkUnit string `gorm:"column:work_unit" json:"work_unit" form:"work_unit"`
  351. UnitAddress string `gorm:"column:unit_address" json:"unit_address" form:"unit_address"`
  352. Children int64 `gorm:"column:children" json:"children" form:"children"`
  353. ReceivingDate int64 `gorm:"column:receiving_date" json:"receiving_date" form:"receiving_date"`
  354. IsHospitalFirstDialysis int64 `gorm:"column:is_hospital_first_dialysis" json:"is_hospital_first_dialysis" form:"is_hospital_first_dialysis"`
  355. FirstDialysisDate int64 `gorm:"column:first_dialysis_date" json:"first_dialysis_date" form:"first_dialysis_date"`
  356. FirstDialysisHospital string `gorm:"column:first_dialysis_hospital" json:"first_dialysis_hospital" form:"first_dialysis_hospital"`
  357. InductionPeriod int64 `gorm:"column:induction_period" json:"induction_period" form:"induction_period"`
  358. InitialDialysis int64 `gorm:"column:initial_dialysis" json:"initial_dialysis" form:"initial_dialysis"`
  359. TotalDialysis int64 `gorm:"column:total_dialysis" json:"total_dialysis" form:"total_dialysis"`
  360. AttendingDoctorId int64 `gorm:"column:attending_doctor_id" json:"attending_doctor_id" form:"attending_doctor_id"`
  361. HeadNurseId int64 `gorm:"column:head_nurse_id" json:"head_nurse_id" form:"head_nurse_id"`
  362. Evaluate string `gorm:"column:evaluate" json:"evaluate" form:"evaluate"`
  363. Diagnose string `gorm:"column:diagnose" json:"diagnose" form:"diagnose"`
  364. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  365. RegistrarsId int64 `gorm:"column:registrars_id" json:"registrars_id" form:"registrars_id"`
  366. Registrars string `gorm:"column:registrars" json:"registrars" form:"registrars"`
  367. QrCode string `gorm:"column:qr_code" json:"qr_code" form:"qr_code"`
  368. BindingState int64 `gorm:"column:binding_state" json:"binding_state" form:"binding_state"`
  369. PatientComplains string `gorm:"column:patient_complains" json:"patient_complains"` // 主诉
  370. PresentHistory string `gorm:"column:present_history" json:"present_history"` // 现病史
  371. PastHistory string `gorm:"column:past_history" json:"past_history"` // 既往史
  372. Temperature float64 `gorm:"column:temperature" json:"temperature"` // 体格检查-体温
  373. Pulse int64 `gorm:"column:pulse" json:"pulse"` // 体格检查-脉搏
  374. Respiratory int64 `gorm:"column:respiratory" json:"respiratory"` // 体格检查-呼吸频率
  375. SBP int64 `gorm:"column:sbp" json:"sbp"` // 体格检查-收缩压
  376. DBP int64 `gorm:"column:dbp" json:"dbp"` // 体格检查-舒张压
  377. Status int64 `gorm:"column:status" json:"status" form:"status"`
  378. Age int64 `gorm:"column:age" json:"age"`
  379. IsOpenRemind int64 `gorm:"column:is_open_remind" json:"is_open_remind"`
  380. CreatedTime int64 `gorm:"column:created_time" json:"created_time" form:"created_time"`
  381. DialysisAge int64 `gorm:"column:dialysis_age" json:"dialysis_age" form:"dialysis_age"`
  382. ExpenseKind int64 `gorm:"column:expense_kind" json:"expense_kind" form:"expense_kind"`
  383. TellPhone string `gorm:"column:tell_phone" json:"tell_phone" form:"tell_phone"`
  384. FirstTreatmentDate int64 `gorm:"column:first_treatment_date" json:"first_treatment_date" form:"first_treatment_date"`
  385. ContactName string `gorm:"column:contact_name" json:"contact_name" form:"contact_name"`
  386. UserSysBeforeCount int64 `gorm:"column:user_sys_before_count" json:"user_sys_before_count" form:"user_sys_before_count"`
  387. }
  388. func (MPatient) TableName() string {
  389. return "xt_patients"
  390. }
  391. // 接诊评估
  392. func MobileGetReceiverTreatmentAccessRecord(orgID int64, patientID int64, recordDate int64) (*models.ReceiveTreatmentAsses, error) {
  393. var record models.ReceiveTreatmentAsses
  394. err = readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  395. if err != nil {
  396. if err == gorm.ErrRecordNotFound {
  397. return nil, nil
  398. } else {
  399. return nil, err
  400. }
  401. }
  402. return &record, nil
  403. }
  404. // 透前评估
  405. func MobileGetPredialysisEvaluation(orgID int64, patientID int64, recordDate int64) (*models.PredialysisEvaluation, error) {
  406. fmt.Println("recordDate", recordDate)
  407. var record models.PredialysisEvaluation
  408. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  409. if err != nil {
  410. if err == gorm.ErrRecordNotFound {
  411. return nil, nil
  412. } else {
  413. return nil, err
  414. }
  415. }
  416. return &record, nil
  417. }
  418. // 获取 maxDate 之前一次的透前评估记录
  419. func MobileGetLastTimePredialysisEvaluation(orgID int64, patientID int64, maxDate int64) (*models.PredialysisEvaluation, error) {
  420. var record models.PredialysisEvaluation
  421. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date < ?", patientID, orgID, maxDate).Order("assessment_date desc").First(&record).Error
  422. if err != nil {
  423. if err == gorm.ErrRecordNotFound {
  424. return nil, nil
  425. } else {
  426. return nil, err
  427. }
  428. }
  429. return &record, nil
  430. }
  431. // 临时医嘱
  432. func MobileGetDoctorAdvices(orgID int64, patientID int64, recordDate int64) ([]*models.DoctorAdvice, error) {
  433. var records []*models.DoctorAdvice
  434. // err := readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).Find(&records).Error
  435. err := readDb.
  436. Model(&models.DoctorAdvice{}).
  437. Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ? and (advice_type = 2 or advice_type = 3)", patientID, orgID, recordDate).
  438. Select("id,user_org_id,patient_id,advice_type,advice_date,record_date,start_time,advice_name,advice_desc,reminder_date, drug_spec, drug_spec_unit,single_dose,single_dose_unit,prescribing_number,prescribing_number_unit,delivery_way,execution_frequency,advice_doctor,status,created_time,updated_time,advice_affirm,remark,stop_time,stop_reason,stop_doctor,stop_state,parent_id,execution_time,execution_staff,execution_state,checker, check_state, check_time,IF(parent_id>0, parent_id, id) as advice_order,groupno").
  439. Order("start_time asc, groupno desc, advice_order desc, id").
  440. Scan(&records).Error
  441. if err != nil {
  442. return nil, err
  443. }
  444. return records, nil
  445. }
  446. func MobileGetDoctorAdvicesByGroups(orgID int64, patientID int64, recordDate int64) ([]*models.DoctorAdvice, error) {
  447. var records []*models.DoctorAdvice
  448. // err := readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).Find(&records).Error
  449. err := readDb.
  450. Model(&models.DoctorAdvice{}).
  451. Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ? and advice_type = 2", patientID, orgID, recordDate).
  452. Select("id, user_org_id, patient_id, advice_type, advice_date, record_date, start_time, advice_name,advice_desc, reminder_date, drug_spec, drug_spec_unit, single_dose, single_dose_unit, prescribing_number, prescribing_number_unit, delivery_way, execution_frequency, advice_doctor, status, created_time,updated_time, advice_affirm, remark, stop_time, stop_reason, stop_doctor, stop_state, parent_id, execution_time, execution_staff, execution_state, checker, check_state, check_time, groupno, IF(parent_id > 0, parent_id, id) as advice_order").
  453. Order("start_time asc, groupno desc, advice_order desc, id asc").
  454. Scan(&records).Error
  455. if err != nil {
  456. return nil, err
  457. }
  458. return records, nil
  459. }
  460. // 透析记录
  461. func MobileGetSchedualDialysisRecord(orgID int64, patientID int64, recordDate int64) (*models.DialysisOrder, error) {
  462. var record models.DialysisOrder
  463. err := readDb.Model(&models.DialysisOrder{}).Preload("DeviceNumber", "org_id = ? AND status = 1", orgID).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgID, patientID, recordDate).First(&record).Error
  464. if err != nil {
  465. if err == gorm.ErrRecordNotFound {
  466. return nil, nil
  467. } else {
  468. return nil, err
  469. }
  470. }
  471. return &record, nil
  472. }
  473. // 双人核对
  474. func MobileGetDoubleCheck(orgID int64, patientID int64, recordDate int64) (*models.DoubleCheck, error) {
  475. var record models.DoubleCheck
  476. err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? and user_org_id = ? and status = 1 and check_date = ?", patientID, orgID, recordDate).First(&record).Error
  477. if err != nil {
  478. if err == gorm.ErrRecordNotFound {
  479. return nil, nil
  480. } else {
  481. return nil, err
  482. }
  483. }
  484. return &record, nil
  485. }
  486. // 透析监测记录
  487. func MobileGetMonitorRecords(orgID int64, patientID int64, recordDate int64) ([]*models.MonitoringRecord, error) {
  488. var records []*models.MonitoringRecord
  489. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1 and monitoring_date = ?", patientID, orgID, recordDate).Order("operate_time asc").Find(&records).Error
  490. if err != nil {
  491. return nil, err
  492. }
  493. return records, nil
  494. }
  495. func MobileGetMonitorRecordFirst(orgID int64, patientID int64, recordDate int64) (*models.MonitoringRecord, error) {
  496. var records models.MonitoringRecord
  497. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1 and monitoring_date = ?", patientID, orgID, recordDate).Order("operate_time asc").First(&records).Error
  498. if err != nil {
  499. return nil, err
  500. }
  501. return &records, nil
  502. }
  503. func MobileGetLastMonitorRecord(orgID int64, patientID int64, beforeDate int64) (*models.MonitoringRecord, error) {
  504. var record models.MonitoringRecord
  505. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1 and monitoring_date = ?", patientID, orgID, beforeDate).Order("operate_time desc").First(&record).Error
  506. if err != nil {
  507. if err == gorm.ErrRecordNotFound {
  508. return nil, nil
  509. } else {
  510. return nil, err
  511. }
  512. }
  513. return &record, nil
  514. }
  515. // 透后评估
  516. func MobileGetAssessmentAfterDislysis(orgID int64, patientID int64, recordDate int64) (*models.AssessmentAfterDislysis, error) {
  517. var record models.AssessmentAfterDislysis
  518. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  519. if err != nil {
  520. if err == gorm.ErrRecordNotFound {
  521. return nil, nil
  522. } else {
  523. return nil, err
  524. }
  525. }
  526. return &record, nil
  527. }
  528. // 获取 maxDate 之前一次的透后评估记录
  529. func MobileGetLastTimeAssessmentAfterDislysis(orgID int64, patientID int64, maxDate int64) (*models.AssessmentAfterDislysis, error) {
  530. var record models.AssessmentAfterDislysis
  531. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date < ?", patientID, orgID, maxDate).Order("assessment_date desc").First(&record).Error
  532. if err != nil {
  533. if err == gorm.ErrRecordNotFound {
  534. return nil, nil
  535. } else {
  536. return nil, err
  537. }
  538. }
  539. return &record, nil
  540. }
  541. func MobileGetLast(orgID int64, patientID int64, maxDate int64) (models.AssessmentAfterDislysis, error) {
  542. dislysis := models.AssessmentAfterDislysis{}
  543. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date < ?", patientID, orgID, maxDate).Order("assessment_date desc").First(&dislysis).Error
  544. return dislysis, err
  545. }
  546. // 治疗小结
  547. func MobileGetTreatmentSummary(orgID int64, patientID int64, recordDate int64) (*models.TreatmentSummary, error) {
  548. var record models.TreatmentSummary
  549. err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  550. if err != nil {
  551. if err == gorm.ErrRecordNotFound {
  552. return nil, nil
  553. } else {
  554. return nil, err
  555. }
  556. }
  557. return &record, nil
  558. }
  559. // 透析处方
  560. func MobileGetDialysisPrescribe(orgID int64, patientID int64, recordDate int64) (*models.DialysisPrescription, error) {
  561. var record models.DialysisPrescription
  562. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  563. if err != nil {
  564. if err == gorm.ErrRecordNotFound {
  565. return nil, nil
  566. } else {
  567. return nil, err
  568. }
  569. }
  570. return &record, nil
  571. }
  572. // 透析方案
  573. func MobileGetDialysisSolution(orgID int64, patientID int64) (*models.DialysisSolution, error) {
  574. var record models.DialysisSolution
  575. err := readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  576. if err != nil {
  577. if err == gorm.ErrRecordNotFound {
  578. return nil, nil
  579. } else {
  580. return nil, err
  581. }
  582. }
  583. return &record, nil
  584. }
  585. func MobileGetPatientById(orgID int64, patientID int64) (*models.Patients, error) {
  586. var patient models.Patients
  587. err := readDb.Model(&models.Patients{}).Where("id = ? and user_org_id = ? and status = 1", patientID, orgID).First(&patient).Error
  588. if err != nil {
  589. if err == gorm.ErrRecordNotFound {
  590. return nil, nil
  591. } else {
  592. return nil, err
  593. }
  594. }
  595. return &patient, nil
  596. }
  597. func DisableMonitor(orgID int64, patientID int64, recordID int64, admin_user_id int64) error {
  598. fmt.Println()
  599. tx := writeDb.Begin()
  600. updateTime := time.Now().Unix()
  601. err := tx.Model(&models.MonitoringRecord{}).Where("user_org_id = ? AND patient_id = ? AND id = ? AND status = 1 ", orgID, patientID, recordID).Updates(map[string]interface{}{"status": 0, "updated_time": updateTime, "modify": admin_user_id}).Error
  602. if err != nil {
  603. tx.Rollback()
  604. return err
  605. }
  606. tx.Commit()
  607. return nil
  608. }
  609. func GetMonitor(orgID int64, patientID int64, id int64) (*models.MonitoringRecord, error) {
  610. var monitor models.MonitoringRecord
  611. var err error
  612. err = readDb.Model(&models.MonitoringRecord{}).Where("id = ? AND user_org_id = ? AND patient_id = ? AND status = 1 ", id, orgID, patientID).Find(&monitor).Error
  613. if err == gorm.ErrRecordNotFound {
  614. return nil, nil
  615. }
  616. if err != nil {
  617. return nil, err
  618. }
  619. return &monitor, nil
  620. }
  621. type MScheduleDoctorAdviceVM struct {
  622. ID int64 `gorm:"column:id" json:"id"`
  623. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  624. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  625. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  626. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  627. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  628. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  629. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  630. Status int64 `gorm:"column:status" json:"status"`
  631. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:DialysisDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"dialysis_order"`
  632. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  633. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  634. DoctorAdvices []*MDoctorAdviceVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  635. }
  636. func (MScheduleDoctorAdviceVM) TableName() string {
  637. return "xt_schedule"
  638. }
  639. type MDoctorAdviceVM struct {
  640. ID int64 `gorm:"column:id" json:"id"`
  641. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  642. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  643. AdviceType int64 `gorm:"column:advice_type" json:"advice_type"`
  644. AdviceDate int64 `gorm:"column:advice_date" json:"advice_date"`
  645. StartTime int64 `gorm:"column:start_time" json:"start_time"`
  646. AdviceName string `gorm:"column:advice_name" json:"advice_name"`
  647. AdviceDesc string `gorm:"column:advice_desc" json:"advice_desc"`
  648. ReminderDate int64 `gorm:"column:reminder_date" json:"reminder_date"`
  649. SingleDose float64 `gorm:"column:single_dose" json:"single_dose"`
  650. SingleDoseUnit string `gorm:"column:single_dose_unit" json:"single_dose_unit"`
  651. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number"`
  652. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit"`
  653. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way"`
  654. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency"`
  655. AdviceDoctor int64 `gorm:"column:advice_doctor" json:"advice_doctor"`
  656. Status int64 `gorm:"column:status" json:"status"`
  657. CreatedTime int64 `gorm:"column:created_time" json:"created_time"`
  658. UpdatedTime int64 `gorm:"column:updated_time" json:"updated_time"`
  659. AdviceAffirm string `gorm:"column:advice_affirm" json:"advice_affirm"`
  660. Remark string `gorm:"column:remark" json:"remark"`
  661. StopTime int64 `gorm:"column:stop_time" json:"stop_time"`
  662. StopReason string `gorm:"column:stop_reason" json:"stop_reason"`
  663. StopDoctor int64 `gorm:"column:stop_doctor" json:"stop_doctor"`
  664. StopState int64 `gorm:"column:stop_state" json:"stop_state"`
  665. ParentId int64 `gorm:"column:parent_id" json:"parent_id"`
  666. ExecutionTime int64 `gorm:"column:execution_time" json:"execution_time"`
  667. ExecutionStaff int64 `gorm:"column:execution_staff" json:"execution_staff"`
  668. ExecutionState int64 `gorm:"column:execution_state" json:"execution_state"`
  669. Checker int64 `gorm:"column:checker" json:"checker"`
  670. RecordDate int64 `gorm:"column:record_date" json:"record_date"`
  671. DialysisOrderId int64 `gorm:"column:dialysis_order_id" json:"dialysis_order_id"`
  672. CheckTime int64 `gorm:"column:check_time" json:"check_time"`
  673. CheckState int64 `gorm:"column:check_state" json:"check_state"`
  674. DrugSpec float64 `gorm:"column:drug_spec" json:"drug_spec"`
  675. DrugSpecUnit string `gorm:"column:drug_spec_unit" json:"drug_spec_unit"`
  676. Groupno int64 `gorm:"column:groupno" json:"groupno"`
  677. RemindType int64 `gorm:"column:remind_type" json:"remind_type"`
  678. FrequencyType int64 `gorm:"column:frequency_type" json:"frequency_type"`
  679. DayCount int64 `gorm:"column:day_count" json:"day_count"`
  680. WeekDay string `gorm:"column:week_day" json:"week_day"`
  681. TemplateId string `gorm:"column:template_id" json:"template_id"`
  682. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  683. }
  684. func (MDoctorAdviceVM) TableName() string {
  685. return "xt_doctor_advice"
  686. }
  687. func MobileGetScheduleDoctorAdvices(orgID int64, scheduleDate int64, adviceType int, patientType int, adminUserId int64) ([]*MScheduleDoctorAdviceVM, error) {
  688. var vms []*MScheduleDoctorAdviceVM
  689. adviceWhere := ""
  690. fmt.Println("advicetype", adviceType)
  691. fmt.Println("patientType", patientType)
  692. fmt.Println("adminUserId", adminUserId)
  693. adviceCondition := []interface{}{}
  694. if adviceType == 0 {
  695. if patientType == 0 {
  696. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1)"
  697. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  698. } else if patientType == 1 {
  699. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_doctor = ? AND(advice_type = 3 OR advice_type = 1)"
  700. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  701. } else if patientType == 2 {
  702. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND execution_staff = 0 AND(advice_type = 3 OR advice_type = 1)"
  703. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  704. }
  705. } else if adviceType == 1 {
  706. if patientType == 0 {
  707. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? "
  708. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  709. } else if patientType == 1 {
  710. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? "
  711. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  712. } else if patientType == 2 {
  713. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0"
  714. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  715. }
  716. } else if adviceType == 3 {
  717. if patientType == 0 {
  718. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? "
  719. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  720. } else if patientType == 1 {
  721. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? "
  722. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  723. } else if patientType == 2 {
  724. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0"
  725. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  726. }
  727. } else if adviceType == 2 {
  728. if patientType == 0 {
  729. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? "
  730. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  731. } else if patientType == 1 {
  732. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? "
  733. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  734. } else if patientType == 2 {
  735. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0"
  736. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  737. }
  738. }
  739. db := readDb.
  740. Table("xt_schedule").
  741. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  742. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  743. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  744. }).
  745. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  746. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  747. Preload("DoctorAdvices", adviceCondition...).
  748. Where("status = 1 AND user_org_id = ?", orgID)
  749. if scheduleDate != 0 {
  750. db = db.Where("schedule_date = ?", scheduleDate)
  751. }
  752. err := db.Find(&vms).Error
  753. return vms, err
  754. }
  755. func MobileCreateDialysisOrder(orgID int64, patientID int64, order *models.DialysisOrder) error {
  756. now := time.Now()
  757. tx := writeDb.Begin()
  758. if createOrderErr := tx.Model(&models.DialysisOrder{}).Create(order).Error; createOrderErr != nil {
  759. tx.Rollback()
  760. return createOrderErr
  761. }
  762. // 更新透析记录 ID
  763. // 透析处方
  764. if err := tx.Model(&models.DialysisPrescription{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"record_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  765. tx.Rollback()
  766. return err
  767. }
  768. // 接诊评估
  769. if err := tx.Model(&models.ReceiveTreatmentAsses{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"record_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  770. tx.Rollback()
  771. return err
  772. }
  773. // 透前评估
  774. if err := tx.Model(&models.PredialysisEvaluation{}).Where("user_org_id = ? AND patient_id = ? AND assessment_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  775. tx.Rollback()
  776. return err
  777. }
  778. // 临时医嘱
  779. if err := tx.Model(&models.DoctorAdvice{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? AND status = 1 AND advice_type = 2", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  780. tx.Rollback()
  781. return err
  782. }
  783. // 双人核对
  784. if err := tx.Model(&models.DoubleCheck{}).Where("user_org_id = ? AND patient_id = ? AND check_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  785. tx.Rollback()
  786. return err
  787. }
  788. // 透后评估
  789. if err := tx.Model(&models.AssessmentAfterDislysis{}).Where("user_org_id = ? AND patient_id = ? AND assessment_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  790. tx.Rollback()
  791. return err
  792. }
  793. // 治疗小结
  794. if err := tx.Model(&models.TreatmentSummary{}).Where("user_org_id = ? AND patient_id = ? AND assessment_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  795. tx.Rollback()
  796. return err
  797. }
  798. // 透析监测
  799. if err := tx.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id=? and status=1 and monitoring_date = ?", patientID, orgID, order.DialysisDate).Update(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  800. tx.Rollback()
  801. return err
  802. }
  803. tx.Commit()
  804. return nil
  805. }
  806. type MobileUrgentSchedulePatientVM struct {
  807. ID int64 `gorm:"column:id" json:"id"`
  808. Name string `gorm:"column:name" json:"name"`
  809. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no"`
  810. }
  811. func (MobileUrgentSchedulePatientVM) TableName() string {
  812. return "xt_patients"
  813. }
  814. type MobileUrgentScheduleTreatmentModeVM struct {
  815. ID int64 `gorm:"column:id" json:"id"`
  816. Name string `gorm:"column:name" json:"name"`
  817. }
  818. func (MobileUrgentScheduleTreatmentModeVM) TableName() string {
  819. return "xt_treatment_mode"
  820. }
  821. func MobileGetAllPatientsForUrgentSchedule(orgID int64, record_date int64) ([]*MobileUrgentSchedulePatientVM, error) {
  822. var vms []*MobileUrgentSchedulePatientVM = make([]*MobileUrgentSchedulePatientVM, 0)
  823. rows, err := readDb.Raw("SELECT p.* FROM xt_patients as p WHERE (p.user_org_id = ? AND p.status = 1 AND p.lapseto = 1) AND NOT EXISTS (Select * FROM `xt_dialysis_order` as d Where d.`dialysis_date` = ? AND d.`status` = 1 AND d.`patient_id` = p.id AND d.user_org_id = p.user_org_id )", orgID, record_date).Rows()
  824. defer rows.Close()
  825. if err != nil {
  826. return nil, err
  827. }
  828. for rows.Next() {
  829. var vm MobileUrgentSchedulePatientVM
  830. readDb.ScanRows(rows, &vm)
  831. vms = append(vms, &vm)
  832. }
  833. return vms, nil
  834. }
  835. func MobileGetAllTrearmentModesForUrgentSchedule() ([]*MobileUrgentScheduleTreatmentModeVM, error) {
  836. var modes []*MobileUrgentScheduleTreatmentModeVM
  837. err := readDb.Model(&MobileUrgentScheduleTreatmentModeVM{}).Where("status = 1").Find(&modes).Error
  838. if err != nil {
  839. return nil, err
  840. }
  841. return modes, nil
  842. }
  843. type MobileUrgentScheduleScheduleListVM struct {
  844. ID int64 `gorm:"column:id" json:"id"`
  845. // ZoneID int64 `gorm:"column:partition_id" json:"zone_id"`
  846. DeviceNumberID int64 `gorm:"column:bed_id" json:"bed_id"`
  847. PatientID int64 `gorm:"column:patient_id" json:"patient_id"`
  848. ScheduleType int `gorm:"column:schedule_type" json:"schedule_type"`
  849. DeviceNumber *models.DeviceNumber `gorm:"ForeignKey:DeviceNumberID" json:"device_number"`
  850. // DeviceZone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"device_zone"`
  851. }
  852. func (MobileUrgentScheduleScheduleListVM) TableName() string {
  853. return "xt_schedule"
  854. }
  855. func MobileGetSchedulesForUrgentSchedule(orgID int64, scheduleDate int64, schedule_type int) (schedules []*MobileUrgentScheduleScheduleListVM, err error) {
  856. db := readDb.
  857. Model(&MobileUrgentScheduleScheduleListVM{}).
  858. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  859. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND schedule_type = ? ", orgID, scheduleDate, schedule_type)
  860. err = db.Find(&schedules).Error
  861. if err != nil {
  862. return nil, err
  863. }
  864. return schedules, nil
  865. }
  866. func MobileGetOtherSchedulesForUrgentSchedule(orgID int64, scheduleDate int64, schedule_type int) ([]*MobileUrgentScheduleScheduleListVM, error) {
  867. var vms []*MobileUrgentScheduleScheduleListVM = make([]*MobileUrgentScheduleScheduleListVM, 0)
  868. rows, err := readDb.Raw("SELECT * FROM xt_schedule as s WHERE s.user_org_id = ? AND s.status = 1 AND s.schedule_date = ? ", orgID, scheduleDate).Rows()
  869. defer rows.Close()
  870. if err != nil {
  871. return nil, err
  872. }
  873. for rows.Next() {
  874. var vm MobileUrgentScheduleScheduleListVM
  875. readDb.ScanRows(rows, &vm)
  876. vms = append(vms, &vm)
  877. }
  878. return vms, nil
  879. }
  880. type MobileUrgentScheduleDeviceNumberVM struct {
  881. ID int64 `gorm:"column:id" json:"id"`
  882. Number string `gorm:"column:number" json:"number"`
  883. ZoneID int64 `gorm:"column:zone_id" json:"zone_id"`
  884. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  885. }
  886. func (MobileUrgentScheduleDeviceNumberVM) TableName() string {
  887. return "xt_device_number"
  888. }
  889. func MobileGetAllDeviceNumbersForUrgentSchedule(orgID int64, record_date int64, schedule_type int) ([]*DeviceNumberViewModel, error) {
  890. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  891. rows, err := readDb.Raw("SELECT n.*, z.name as zone_name, g.name as group_name FROM xt_device_number as n join xt_device_zone as z on z.id = n.zone_id join xt_device_group as g on g.id = n.group_id WHERE (n.org_id = ? AND n.status = 1) AND NOT EXISTS (Select * FROM xt_schedule as s Where s.`schedule_date` = ? AND s.user_org_id = n.org_id AND s.`bed_id` = n.id AND s.`schedule_type` = ? AND s.status = 1 )", orgID, record_date, schedule_type).Rows()
  892. defer rows.Close()
  893. if err != nil {
  894. return nil, err
  895. }
  896. for rows.Next() {
  897. var vm DeviceNumberViewModel
  898. readDb.ScanRows(rows, &vm)
  899. vms = append(vms, &vm)
  900. }
  901. return vms, nil
  902. //var deviceNumbers []*MobileUrgentScheduleDeviceNumberVM
  903. //db := readDb.
  904. // Model(&MobileUrgentScheduleDeviceNumberVM{}).
  905. // Preload("Zone", "status = 1 AND org_id = ?", orgID).
  906. // Where("status = 1 AND org_id = ?", orgID)
  907. //err := db.Order("zone_id asc").Find(&deviceNumbers).Error
  908. //if err != nil {
  909. // return nil, err
  910. //}
  911. //return deviceNumbers, nil
  912. }
  913. func GetValidScheduleMonitorRecordCount() (int64, error) {
  914. var total int64
  915. err := readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND operate_time = 0 AND monitoring_date <> 0").Count(&total).Error
  916. return total, err
  917. }
  918. func GetTop1000ValidScheduleMonitorRecord() ([]*models.MonitoringRecord, error) {
  919. var monitors []*models.MonitoringRecord
  920. err := readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND operate_time = 0 AND monitoring_date <> 0").Order("id asc").Limit(100).Find(&monitors).Error
  921. if err != nil {
  922. return nil, err
  923. }
  924. return monitors, nil
  925. }
  926. func BatchUpdateMonitors(monitors []*models.MonitoringRecord) error {
  927. tx := writeDb.Begin()
  928. for index := 0; index < len(monitors); index++ {
  929. tx.Save(monitors[index])
  930. }
  931. return tx.Commit().Error
  932. }
  933. func ModifyStartDialysisOrder(order *models.DialysisOrder) error {
  934. tx := writeDb.Begin()
  935. updateTime := time.Now().Unix()
  936. err := tx.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND id = ? AND status = 1 ", order.UserOrgId, order.ID).Updates(map[string]interface{}{"start_nurse": order.StartNurse, "updated_time": updateTime, "bed_id": order.BedID, "puncture_nurse": order.PunctureNurse, "start_time": order.StartTime, "modifier": order.Modifier, "schedual_type": order.SchedualType}).Error
  937. if err != nil {
  938. tx.Rollback()
  939. return err
  940. }
  941. tx.Commit()
  942. return err
  943. }
  944. func ModifyFinishDialysisOrder(order *models.DialysisOrder) error {
  945. tx := writeDb.Begin()
  946. updateTime := time.Now().Unix()
  947. err := tx.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND id = ? AND status = 1 ", order.UserOrgId, order.ID).Updates(map[string]interface{}{"finish_nurse": order.FinishNurse, "updated_time": updateTime, "end_time": order.EndTime, "finish_modifier": order.FinishModifier}).Error
  948. if err != nil {
  949. tx.Rollback()
  950. return err
  951. }
  952. tx.Commit()
  953. return err
  954. }
  955. func MobileGetLastDryWeight(orgID int64, patientID int64) (*models.SgjPatientDryweight, error) {
  956. var record models.SgjPatientDryweight
  957. err := readDb.Model(&models.SgjPatientDryweight{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  958. if err != nil {
  959. if err == gorm.ErrRecordNotFound {
  960. return nil, nil
  961. } else {
  962. return nil, err
  963. }
  964. }
  965. return &record, nil
  966. }
  967. // 透析方案
  968. func MobileGetDialysisSolutionByModeId(orgID int64, patientID int64, mode_id int64) (*models.DialysisSolution, error) {
  969. var record models.DialysisSolution
  970. err := readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  971. if err != nil {
  972. if err == gorm.ErrRecordNotFound {
  973. return nil, nil
  974. } else {
  975. return nil, err
  976. }
  977. }
  978. return &record, nil
  979. }
  980. // 透析处方
  981. func MobileGetDialysisPrescribeByModeId(orgID int64, patientID int64, recordDate int64, mode_id int64) (*models.DialysisPrescription, error) {
  982. var record models.DialysisPrescription
  983. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ? AND mode_id = ?", patientID, orgID, recordDate, mode_id).First(&record).Error
  984. if err != nil {
  985. if err == gorm.ErrRecordNotFound {
  986. return nil, nil
  987. } else {
  988. return nil, err
  989. }
  990. }
  991. return &record, nil
  992. }
  993. func MobileGetLastDialysisPrescribe(orgID int64, patientID int64) (*models.DialysisPrescription, error) {
  994. var record models.DialysisPrescription
  995. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 ", patientID, orgID).Last(&record).Error
  996. if err != nil {
  997. if err == gorm.ErrRecordNotFound {
  998. return nil, nil
  999. } else {
  1000. return nil, err
  1001. }
  1002. }
  1003. return &record, nil
  1004. }
  1005. func MobileGetLastDialysisPrescribeByModeId(orgID int64, patientID int64, mode_id int64) (*models.DialysisPrescription, error) {
  1006. var record models.DialysisPrescription
  1007. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  1008. if err != nil {
  1009. if err == gorm.ErrRecordNotFound {
  1010. return nil, nil
  1011. } else {
  1012. return nil, err
  1013. }
  1014. }
  1015. return &record, nil
  1016. }
  1017. func GetAllAvaildDeviceNumbers(orgID int64, record_date int64, schedule_type int) ([]*DeviceNumberViewModel, error) {
  1018. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  1019. rows, err := readDb.Raw("SELECT n.*, z.name as zone_name, g.name as group_name FROM xt_device_number as n join xt_device_zone as z on z.id = n.zone_id join xt_device_group as g on g.id = n.group_id WHERE (n.org_id = ? AND n.status = 1) AND NOT EXISTS (Select * FROM xt_schedule as s Where s.`schedule_date` = ? AND s.user_org_id = n.org_id AND s.`bed_id` = n.id AND s.`schedule_type` = ? AND s.status = 1 )", orgID, record_date, schedule_type).Rows()
  1020. defer rows.Close()
  1021. if err != nil {
  1022. return nil, err
  1023. }
  1024. for rows.Next() {
  1025. var vm DeviceNumberViewModel
  1026. readDb.ScanRows(rows, &vm)
  1027. vms = append(vms, &vm)
  1028. }
  1029. return vms, nil
  1030. }
  1031. // 获取 maxDate 之前一次的透前评估记录
  1032. func GetLastTimePredialysisEvaluation(orgID int64, patientID int64, maxDate int64) (*models.PredialysisEvaluation, error) {
  1033. var record models.PredialysisEvaluation
  1034. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date < ?", patientID, orgID, maxDate).Order("assessment_date desc").First(&record).Error
  1035. if err != nil {
  1036. if err == gorm.ErrRecordNotFound {
  1037. return nil, nil
  1038. } else {
  1039. return nil, err
  1040. }
  1041. }
  1042. return &record, nil
  1043. }
  1044. func GetLastMonitorRecord(orgID int64, patientID int64, beforeDate int64) (*models.MonitoringRecord, error) {
  1045. var record models.MonitoringRecord
  1046. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1 and monitoring_date = ?", patientID, orgID, beforeDate).Order("operate_time desc").First(&record).Error
  1047. if err != nil {
  1048. if err == gorm.ErrRecordNotFound {
  1049. return nil, nil
  1050. } else {
  1051. return nil, err
  1052. }
  1053. }
  1054. return &record, nil
  1055. }
  1056. // 获取 maxDate 之前一次的透后评估记录
  1057. func GetLastTimeAssessmentAfterDislysis(orgID int64, patientID int64, maxDate int64) (*models.AssessmentAfterDislysis, error) {
  1058. var record models.AssessmentAfterDislysis
  1059. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date < ?", patientID, orgID, maxDate).Order("assessment_date desc").First(&record).Error
  1060. if err != nil {
  1061. if err == gorm.ErrRecordNotFound {
  1062. return nil, nil
  1063. } else {
  1064. return nil, err
  1065. }
  1066. }
  1067. return &record, nil
  1068. }
  1069. // 透析处方
  1070. func GetDialysisPrescribe(orgID int64, patientID int64, recordDate int64) (*models.DialysisPrescription, error) {
  1071. var record models.DialysisPrescription
  1072. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  1073. if err != nil {
  1074. if err == gorm.ErrRecordNotFound {
  1075. return nil, nil
  1076. } else {
  1077. return nil, err
  1078. }
  1079. }
  1080. return &record, nil
  1081. }
  1082. // 透析方案
  1083. func GetDialysisSolution(orgID int64, patientID int64, mode_id int64) (*models.DialysisSolution, error) {
  1084. var record models.DialysisSolution
  1085. err := readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  1086. if err != nil {
  1087. if err == gorm.ErrRecordNotFound {
  1088. return nil, nil
  1089. } else {
  1090. return nil, err
  1091. }
  1092. }
  1093. return &record, nil
  1094. }
  1095. func GetLastDialysisPrescribeByModeId(orgID int64, patientID int64, mode_id int64) (*models.DialysisPrescription, error) {
  1096. var record models.DialysisPrescription
  1097. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  1098. if err != nil {
  1099. if err == gorm.ErrRecordNotFound {
  1100. return nil, nil
  1101. } else {
  1102. return nil, err
  1103. }
  1104. }
  1105. return &record, nil
  1106. }
  1107. func GetLastDryWeight(orgID int64, patientID int64) (*models.SgjPatientDryweight, error) {
  1108. var record models.SgjPatientDryweight
  1109. err := readDb.Model(&models.SgjPatientDryweight{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  1110. if err != nil {
  1111. if err == gorm.ErrRecordNotFound {
  1112. return nil, nil
  1113. } else {
  1114. return nil, err
  1115. }
  1116. }
  1117. return &record, nil
  1118. }
  1119. func MobileGetSystemDialysisPrescribeByModeId(orgID int64, mode_id int64) (*models.SystemPrescription, error) {
  1120. var record models.SystemPrescription
  1121. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).First(&record).Error
  1122. if err != nil {
  1123. if err == gorm.ErrRecordNotFound {
  1124. return nil, nil
  1125. } else {
  1126. return nil, err
  1127. }
  1128. }
  1129. return &record, nil
  1130. }
  1131. func GetSystemDialysisPrescribeByModeId(orgID int64, mode_id int64) (*models.SystemPrescription, error) {
  1132. var record models.SystemPrescription
  1133. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).Last(&record).Error
  1134. if err != nil {
  1135. if err == gorm.ErrRecordNotFound {
  1136. return nil, nil
  1137. } else {
  1138. return nil, err
  1139. }
  1140. }
  1141. return &record, nil
  1142. }
  1143. func GetDialysisOrderCount(orgID int64, patient_id int64, recordDate int64) (count int64, err error) {
  1144. err = readDb.Model(&models.DialysisOrder{}).Where("dialysis_date <= ? AND status = 1 AND stage = 2 AND user_org_id = ? AND patient_id = ?", recordDate, orgID, patient_id).Count(&count).Error
  1145. return
  1146. }
  1147. func GetFirstDateOfMonth(d time.Time) time.Time {
  1148. d = d.AddDate(0, 0, -d.Day()+1)
  1149. return GetZeroTime(d)
  1150. }
  1151. func GetZeroTime(d time.Time) time.Time {
  1152. return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
  1153. }
  1154. func GetDialysisCountByPatientId(startime int64, endtime int64, patientid int64, orgid int64) (order []*models.BloodDialysisOrderCount, err error) {
  1155. db := XTReadDB().Table("xt_dialysis_order as o")
  1156. err = db.Raw("select count(o.id) as count,o.patient_id from xt_dialysis_order as o left join xt_schedule AS x ON x.patient_id = o.patient_id where o.status = 1 and o.dialysis_date>=? and o.dialysis_date<=? and o.user_org_id = ? and x.schedule_date = o.dialysis_date and x.status = 1 and o.patient_id = ?", startime, endtime, orgid, patientid).Scan(&order).Group("o.patient_id").Error
  1157. return order, err
  1158. }