patient_service.go 74KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. package service
  2. import (
  3. "XT_New/models"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/jinzhu/gorm"
  10. )
  11. //GetPatientList 返回患者的列表
  12. func GetPatientList(orgID int64, keywords string, page, limit, schedulType, bindingState, lapseto, source, startTime, endTime, contagion, reimbursementWay, isscheduling, isprescription int64, isStartTime, isEndTime bool) (patients []*models.Patients, total int64, err error) {
  13. db := readDb.Table("xt_patients as p").Where("p.status=1")
  14. if orgID > 0 {
  15. db = db.Where("p.user_org_id=?", orgID)
  16. }
  17. if len(keywords) > 0 {
  18. likeKey := "%" + keywords + "%"
  19. db = db.Where("p.name LIKE ? OR p.dialysis_no LIKE ?", likeKey, likeKey)
  20. }
  21. if schedulType > 0 {
  22. db = db.Joins("JOIN xt_schedule as s ON s.patient_id=p.id")
  23. db = db.Where("s.status=1 and s.schedule_type=?", schedulType)
  24. }
  25. if contagion > 0 {
  26. db = db.Joins("JOIN xt_patients_infectious_diseases as xpid ON xpid.patient_id=p.id")
  27. db = db.Where("xpid.disease_id=? and xpid.status=1", contagion)
  28. }
  29. if isscheduling == 1 {
  30. db = db.Where("EXISTS (?)", readDb.Table("xt_schedule as iss").Where("iss.patient_id=p.id and iss.status=1").QueryExpr())
  31. } else if isscheduling == 2 {
  32. db = db.Where("NOT EXISTS (?)", readDb.Table("xt_schedule as iss").Where("iss.patient_id=p.id and iss.status=1").QueryExpr())
  33. }
  34. if isprescription == 1 {
  35. db = db.Where("EXISTS (?)", readDb.Table("xt_dialysis_prescription as xdp").Where("xdp.patient_id=p.id and xdp.status=1").QueryExpr())
  36. } else if isprescription == 2 {
  37. db = db.Where("NOT EXISTS (?)", readDb.Table("xt_dialysis_prescription as xdp").Where("xdp.patient_id=p.id and xdp.status=1").QueryExpr())
  38. }
  39. if bindingState > 0 {
  40. db = db.Where("p.binding_state=?", bindingState)
  41. }
  42. if lapseto > 0 {
  43. db = db.Where("p.lapseto=?", lapseto)
  44. }
  45. if source > 0 {
  46. db = db.Where("p.source=?", source)
  47. }
  48. if reimbursementWay > 0 {
  49. db = db.Where("p.reimbursement_way_id = ?", reimbursementWay)
  50. }
  51. if isStartTime {
  52. db = db.Where("p.created_time>=?", startTime)
  53. }
  54. if isEndTime {
  55. db = db.Where("p.created_time<=?", endTime)
  56. }
  57. offset := (page - 1) * limit
  58. err = db.Order("p.id desc").Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time,p.user_sys_before_count,p.out_reason,p.death_time").Group("p.id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
  59. return
  60. }
  61. //GetAllPatientList 返回全部患者的列表
  62. func GetAllPatientList(orgID int64) (patients []*models.Patients, total int64, err error) {
  63. db := readDb.Table("xt_patients as p").Where("p.user_org_id=? and p.status=1 and p.lapseto = 1", orgID)
  64. err = db.Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time").Count(&total).Find(&patients).Error
  65. return
  66. }
  67. //GetAllPatientList 返回全部患者的列表
  68. func GetAllPatientListByList(orgID int64) (patients []*models.MSchedualPatientList, err error) {
  69. redis := RedisClient()
  70. defer redis.Close()
  71. key := strconv.FormatInt(orgID, 10) + ":"+ ":patient_list_all"
  72. patient_info_str, _ := redis.Get(key).Result()
  73. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  74. err = readDb.Model(&models.MSchedualPatientList{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  75. if err != nil {
  76. if err == gorm.ErrRecordNotFound {
  77. return nil, nil
  78. } else {
  79. return nil, err
  80. }
  81. } else {
  82. if len(patients) > 0 {
  83. //缓存数据
  84. patient_info_json, err := json.Marshal(&patients)
  85. if err == nil {
  86. redis.Set(key, patient_info_json, time.Second*60*60*18)
  87. }
  88. }
  89. return patients, nil
  90. }
  91. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  92. json.Unmarshal([]byte(patient_info_str), &patients)
  93. return patients, nil
  94. }
  95. }
  96. func GetAllDevicetByList(orgID int64) (devices []*models.MDeviceNumberForList, err error) {
  97. redis := RedisClient()
  98. defer redis.Close()
  99. key := strconv.FormatInt(orgID, 10) + ":"+ ":device_list_all"
  100. device_list_str, _ := redis.Get(key).Result()
  101. if len(device_list_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  102. err = readDb.Model(&models.MDeviceNumberForList{}).Preload("Zone", "status = 1 AND org_id = ?", orgID).Where("org_id=? and status=1 ", orgID).Find(&devices).Error
  103. if err != nil {
  104. if err == gorm.ErrRecordNotFound {
  105. return nil, nil
  106. } else {
  107. return nil, err
  108. }
  109. } else {
  110. if len(devices) > 0 {
  111. //缓存数据
  112. device_list_str, err := json.Marshal(&devices)
  113. if err == nil {
  114. redis.Set(key, device_list_str, time.Second*60*60*18)
  115. }
  116. }
  117. return devices, nil
  118. }
  119. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  120. json.Unmarshal([]byte(device_list_str), &devices)
  121. return devices, nil
  122. }
  123. }
  124. func GetAllAdvicestByList(orgID int64,scheduleDate int64) (advices []models.VMDoctorAdviceForList, err error) {
  125. redis := RedisClient()
  126. defer redis.Close()
  127. key := strconv.FormatInt(orgID, 10) + ":"+ ":advice_list_all"
  128. advice_list_str, _ := redis.Get(key).Result()
  129. if len(advice_list_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  130. err = readDb.Model(&models.VMDoctorAdviceForList{}).Where("status = 1 AND user_org_id = ? AND advice_type = 2 AND advice_date = ? ", orgID, scheduleDate).Find(&advices).Error
  131. if err != nil {
  132. if err == gorm.ErrRecordNotFound {
  133. return nil, nil
  134. } else {
  135. return nil, err
  136. }
  137. } else {
  138. if len(advices) > 0 {
  139. //缓存数据
  140. advice_list_str, err := json.Marshal(&advices)
  141. if err == nil {
  142. redis.Set(key, advice_list_str, time.Second*60*60*18)
  143. }
  144. }
  145. return advices, nil
  146. }
  147. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  148. json.Unmarshal([]byte(advice_list_str), &advices)
  149. return advices, nil
  150. }
  151. }
  152. func GetAllPrescriptionsByList(orgID int64,scheduleDate int64) (prescriptions []*models.DialysisPrescriptionList, err error) {
  153. redis := RedisClient()
  154. defer redis.Close()
  155. key := strconv.FormatInt(orgID, 10) + ":"+ ":prescriptions_list_all"
  156. prescriptions_list_all, _ := redis.Get(key).Result()
  157. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  158. err = readDb.Model(&models.DialysisPrescriptionList{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  159. if err != nil {
  160. if err == gorm.ErrRecordNotFound {
  161. return nil, nil
  162. } else {
  163. return nil, err
  164. }
  165. } else {
  166. if len(prescriptions) > 0 {
  167. //缓存数据
  168. prescriptions_list_all, err := json.Marshal(&prescriptions)
  169. if err == nil {
  170. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  171. }
  172. }
  173. return prescriptions, nil
  174. }
  175. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  176. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  177. return prescriptions, nil
  178. }
  179. }
  180. func GetAllAssessmentBeforesByList(orgID int64,scheduleDate int64) (assessmentBefores []*models.PredialysisEvaluationList, err error) {
  181. redis := RedisClient()
  182. defer redis.Close()
  183. key := strconv.FormatInt(orgID, 10) + ":"+ ":assessment_befores_list_all"
  184. assessment_befores_list_all, _ := redis.Get(key).Result()
  185. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  186. err = readDb.Model(&models.PredialysisEvaluationList{}).Where( "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  187. if err != nil {
  188. if err == gorm.ErrRecordNotFound {
  189. return nil, nil
  190. } else {
  191. return nil, err
  192. }
  193. } else {
  194. if len(assessmentBefores) > 0 {
  195. //缓存数据
  196. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  197. if err == nil {
  198. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  199. }
  200. }
  201. return assessmentBefores, nil
  202. }
  203. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  204. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  205. return assessmentBefores, nil
  206. }
  207. }
  208. func GetAllDialysisOrdersByList(orgID int64,scheduleDate int64) (dialysisOrders []*models.MDialysisOrderForList, err error) {
  209. redis := RedisClient()
  210. defer redis.Close()
  211. key := strconv.FormatInt(orgID, 10) + ":"+ ":dialysis_orders_list_all"
  212. dialysis_orders_list_all, _ := redis.Get(key).Result()
  213. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  214. err = readDb.Model(&models.MDialysisOrderForList{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  215. if err != nil {
  216. if err == gorm.ErrRecordNotFound {
  217. return nil, nil
  218. } else {
  219. return nil, err
  220. }
  221. } else {
  222. if len(dialysisOrders) > 0 {
  223. //缓存数据
  224. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  225. if err == nil {
  226. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  227. }
  228. }
  229. return dialysisOrders, nil
  230. }
  231. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  232. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  233. return dialysisOrders, nil
  234. }
  235. }
  236. func GetAllTreatmentSummarysByList(orgID int64,scheduleDate int64) (treatmentSummarys []*models.VMTreatmentSummaryForList, err error) {
  237. redis := RedisClient()
  238. defer redis.Close()
  239. key := strconv.FormatInt(orgID, 10) + ":"+ ":treatment_summarys_list_all"
  240. treatment_summarys_all, _ := redis.Get(key).Result()
  241. if len(treatment_summarys_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  242. err = readDb.Model(&models.VMTreatmentSummaryForList{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&treatmentSummarys).Error
  243. if err != nil {
  244. if err == gorm.ErrRecordNotFound {
  245. return nil, nil
  246. } else {
  247. return nil, err
  248. }
  249. } else {
  250. if len(treatmentSummarys) > 0 {
  251. //缓存数据
  252. treatment_summarys_all, err := json.Marshal(&treatmentSummarys)
  253. if err == nil {
  254. redis.Set(key, treatment_summarys_all, time.Second*60*60*18)
  255. }
  256. }
  257. return treatmentSummarys, nil
  258. }
  259. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  260. json.Unmarshal([]byte(treatment_summarys_all), &treatmentSummarys)
  261. return treatmentSummarys, nil
  262. }
  263. }
  264. func GetAllAssessmentAfterDislysisByList(orgID int64,scheduleDate int64) (assessmentAfterDislysis []*models.VMAssessmentAfterDislysis, err error) {
  265. redis := RedisClient()
  266. defer redis.Close()
  267. key := strconv.FormatInt(orgID, 10) + ":"+ ":assessment_after_dislysis_list_all"
  268. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  269. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  270. err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  271. if err != nil {
  272. if err == gorm.ErrRecordNotFound {
  273. return nil, nil
  274. } else {
  275. return nil, err
  276. }
  277. } else {
  278. if len(assessmentAfterDislysis) > 0 {
  279. //缓存数据
  280. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  281. if err == nil {
  282. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  283. }
  284. }
  285. return assessmentAfterDislysis, nil
  286. }
  287. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  288. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  289. return assessmentAfterDislysis, nil
  290. }
  291. }
  292. func GetAllHisAdvicesByList(orgID int64,scheduleDate int64) (hisAdvices []VMHisDoctorAdviceInfo, err error) {
  293. redis := RedisClient()
  294. defer redis.Close()
  295. key := strconv.FormatInt(orgID, 10) + ":"+ ":his_advices_list_all"
  296. his_advices_all, _ := redis.Get(key).Result()
  297. if len(his_advices_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  298. err = readDb.Model(&VMHisDoctorAdviceInfo{}).Where("status = 1 AND user_org_id = ? AND advice_date = ?", orgID, scheduleDate).Find(&hisAdvices).Error
  299. if err != nil {
  300. if err == gorm.ErrRecordNotFound {
  301. return nil, nil
  302. } else {
  303. return nil, err
  304. }
  305. } else {
  306. if len(hisAdvices) > 0 {
  307. //缓存数据
  308. his_advices_all, err := json.Marshal(&hisAdvices)
  309. if err == nil {
  310. redis.Set(key, his_advices_all, time.Second*60*60*18)
  311. }
  312. }
  313. return hisAdvices, nil
  314. }
  315. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  316. json.Unmarshal([]byte(his_advices_all), &hisAdvices)
  317. return hisAdvices, nil
  318. }
  319. }
  320. func GetPatientListByUpdateTime(orgID int64, syncTime int64) (patients []*models.PatientListForFaceList, total int64, err error) {
  321. db := readDb.Model(&models.PatientListForFaceList{}).Where("user_org_id=? AND status=1 AND avatar <> 'https://images.shengws.com/201809182128222.png' AND avatar <> 'https://images.shengws.com/201809182128111.png' AND avatar <> ''", orgID)
  322. db = db.Where("updated_time >= ?", syncTime)
  323. err = db.Count(&total).Find(&patients).Error
  324. return
  325. }
  326. func GetPatientListBySchedules(orgID int64, syncTime int64) (patients []*models.SchedulePatients, total int64, err error) {
  327. db := readDb.Model(&models.SchedulePatients{}).Where("user_org_id=? AND status=1 AND schedule_date = ?", orgID, syncTime).Preload("Patient", "status = 1 AND user_org_id = ? ", orgID)
  328. err = db.Count(&total).Find(&patients).Error
  329. return
  330. }
  331. func GetPatientListBySchedulesFind(orgID int64, syncTime int64, patient_name string) (patients []*models.SchedulePatients, total int64, err error) {
  332. db := readDb.Model(&models.SchedulePatients{}).Where("user_org_id=? AND status=1 AND schedule_date = ?", orgID, syncTime).Preload("Patient", "status = 1 AND user_org_id = ? and name like ? ", orgID, "%"+patient_name+"%")
  333. err = db.Count(&total).Find(&patients).Error
  334. return
  335. }
  336. func GetPatientListById(orgID int64, patientId int64) (patients models.PatientListForFace, err error) {
  337. err = readDb.Model(&models.PatientListForFaceList{}).Where("user_org_id=? and id = ? and status=1", orgID, patientId).First(&patients).Error
  338. return
  339. }
  340. func GetPatientCount(orgID int64) (total int64) {
  341. readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1", orgID).Count(&total)
  342. return
  343. }
  344. func GetLapsetoPatientCount(orgID int64, lapseto int64) (total int64) {
  345. readDb.Model(&models.Patients{}).Where("user_org_id=? and lapseto=? and status=1", orgID, lapseto).Count(&total)
  346. return
  347. }
  348. func ChechLastDialysisNo(orgID int64) (dialysisNo int64) {
  349. var patient models.Patients
  350. err := readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=?", orgID).Order("dialysis_no desc").First(&patient).Error
  351. if err != nil {
  352. return
  353. }
  354. if patient.ID == 0 {
  355. return
  356. }
  357. dialysisNo, _ = strconv.ParseInt(patient.DialysisNo, 10, 64)
  358. return
  359. }
  360. func FindPatientByDialysisNo(orgID int64, dialysisNo string) (patient models.Patients, err error) {
  361. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and dialysis_no=?", orgID, dialysisNo).First(&patient).Error
  362. return
  363. }
  364. func FindPatientByIdCardNo(orgID int64, idCardNo string) (patient models.Patients, err error) {
  365. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and id_card_no=?", orgID, idCardNo).First(&patient).Error
  366. return
  367. }
  368. func FindPatientByMobile(orgID int64, mobile string) (patient models.Patients, err error) {
  369. err = readDb.Model(&models.Patients{}).Where("phone=? and user_org_id=? and status=1", mobile, orgID).First(&patient).Error
  370. return
  371. }
  372. func FindPatientById(orgID int64, id int64) (patient models.Patients, err error) {
  373. err = readDb.Model(&models.Patients{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  374. return
  375. }
  376. func FindPatientByIdTwo(orgID int64, id int64) (patient models.XtPatientsNew, err error) {
  377. err = readDb.Model(&models.XtPatientsNew{}).Where("blood_id = ? and user_org_id = ? and status =1", id, orgID).First(&patient).Error
  378. return
  379. }
  380. func FindPatientByIdWithDiseases(orgID int64, id int64) (patient models.Patients, err error) {
  381. redis := RedisClient()
  382. defer redis.Close()
  383. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(id, 10) + ":patient_info"
  384. patient_info_str, _ := redis.Get(key).Result()
  385. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  386. err = readDb.Model(&models.Patients{}).Preload("Contagions", "status = 1").Preload("Diseases", "status = 1").Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  387. if err != nil {
  388. if err == gorm.ErrRecordNotFound {
  389. return patient, nil
  390. } else {
  391. return patient, err
  392. }
  393. } else {
  394. if patient.ID > 0 {
  395. //缓存数据
  396. patient_info_json, err := json.Marshal(&patient)
  397. if err == nil {
  398. redis.Set(key, patient_info_json, time.Second*60*60*18)
  399. }
  400. }
  401. return patient, nil
  402. }
  403. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  404. json.Unmarshal([]byte(patient_info_str), &patient)
  405. return patient, nil
  406. }
  407. }
  408. func FindPatientWithDeviceByNo(orgID int64, no string, time int64) (patient models.SchedualPatient2, err error) {
  409. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  410. return db.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  411. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  412. Where("user_org_id = ? AND schedule_date = ? ", orgID, time)
  413. }).Preload("InfectiousDiseases", "status = 1").Where("user_org_id=? and dialysis_no = ? and status=1", orgID, no).First(&patient).Error
  414. return
  415. }
  416. func CreatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  417. user, _ := GetSgjUserByMobild(patient.Phone)
  418. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  419. utx := writeDb.Begin()
  420. btx := writeUserDb.Begin()
  421. if user.ID == 0 {
  422. user.Mobile = patient.Phone
  423. user.Avatar = patient.Avatar
  424. user.AvatarThumb = patient.Avatar
  425. user.Birthday = patient.Birthday
  426. user.Username = patient.Name
  427. user.Gender = patient.Gender
  428. user.Sources = 11
  429. user.Introduce = patient.Remark
  430. user.Status = 1
  431. user.UpdatedTime = patient.UpdatedTime
  432. user.CreatedTime = patient.CreatedTime
  433. err = btx.Create(&user).Error
  434. if err != nil {
  435. utx.Rollback()
  436. btx.Rollback()
  437. return
  438. }
  439. }
  440. patient.UserId = user.ID
  441. if customer == nil {
  442. err = btx.Create(&models.SgjCustomer{
  443. UserOrgId: patient.UserOrgId,
  444. UserId: user.ID,
  445. Mobile: patient.Phone,
  446. Name: patient.Name,
  447. Gender: patient.Gender,
  448. Birthday: patient.Birthday,
  449. Sources: 11,
  450. Status: 1,
  451. CreatedTime: patient.CreatedTime,
  452. UpdatedTime: patient.UpdatedTime,
  453. Avatar: patient.Avatar,
  454. Remark: patient.Remark,
  455. }).Error
  456. if err != nil {
  457. utx.Rollback()
  458. btx.Rollback()
  459. return
  460. }
  461. }
  462. err = utx.Create(patient).Error
  463. if err != nil {
  464. utx.Rollback()
  465. btx.Rollback()
  466. return
  467. }
  468. var lapseto models.PatientLapseto
  469. lapseto.PatientId = patient.ID
  470. lapseto.LapsetoType = patient.Lapseto
  471. lapseto.CreatedTime = patient.CreatedTime
  472. lapseto.UpdatedTime = patient.CreatedTime
  473. lapseto.Status = 1
  474. lapseto.LapsetoTime = patient.CreatedTime
  475. err = utx.Create(&lapseto).Error
  476. if err != nil {
  477. utx.Rollback()
  478. btx.Rollback()
  479. return
  480. }
  481. if len(contagions) > 0 {
  482. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  483. insertParams := make([]string, 0)
  484. insertData := make([]interface{}, 0)
  485. for _, contagion := range contagions {
  486. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  487. insertData = append(insertData, patient.ID)
  488. insertData = append(insertData, contagion)
  489. insertData = append(insertData, 1)
  490. insertData = append(insertData, patient.CreatedTime)
  491. insertData = append(insertData, patient.UpdatedTime)
  492. }
  493. thisSQL += strings.Join(insertParams, ", ")
  494. err = utx.Exec(thisSQL, insertData...).Error
  495. if err != nil {
  496. utx.Rollback()
  497. btx.Rollback()
  498. return
  499. }
  500. }
  501. if len(diseases) > 0 {
  502. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  503. insertParams := make([]string, 0)
  504. insertData := make([]interface{}, 0)
  505. for _, disease := range diseases {
  506. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  507. insertData = append(insertData, patient.ID)
  508. insertData = append(insertData, disease)
  509. insertData = append(insertData, 1)
  510. insertData = append(insertData, patient.CreatedTime)
  511. insertData = append(insertData, patient.UpdatedTime)
  512. }
  513. thisSQL += strings.Join(insertParams, ", ")
  514. err = utx.Exec(thisSQL, insertData...).Error
  515. if err != nil {
  516. utx.Rollback()
  517. btx.Rollback()
  518. return
  519. }
  520. }
  521. utx.Commit()
  522. btx.Commit()
  523. return
  524. }
  525. func GetLastPatientData(orgid int64) (models.Patients, error) {
  526. patients := models.Patients{}
  527. err := XTReadDB().Model(&patients).Where("user_org_id = ? and status = 1", orgid).Last(&patients).Error
  528. return patients, err
  529. }
  530. func CreatePatientsNew(patientsNew *models.XtPatientsNew) error {
  531. err := XTWriteDB().Model(&patientsNew).Create(&patientsNew).Error
  532. return err
  533. }
  534. func EditPatientLapseto(patient *models.Patients, lapseto *models.PatientLapseto) (err error) {
  535. utx := writeDb.Begin()
  536. err = utx.Model(&models.Patients{}).Where("id=?", patient.ID).Update(map[string]interface{}{"lapseto": patient.Lapseto, "out_reason": patient.OutReason, "death_time": patient.DeathTime}).Error
  537. //err = utx.Model(&models.PatientLapseto{}).Where("id=?", patient.ID).Update(map[string]interface{}{"lapseto_type": patient.Lapseto, "lapseto_time": time.Now().Unix(), "updated_time": time.Now().Unix()}).Error
  538. if err != nil {
  539. utx.Rollback()
  540. return
  541. }
  542. err = utx.Create(lapseto).Error
  543. if err != nil {
  544. utx.Rollback()
  545. return
  546. }
  547. // 删除排班和排班模板信息
  548. if lapseto.LapsetoType == 2 {
  549. now := time.Now()
  550. deleteScheduleErr := utx.Model(&models.PatientSchedule{}).Where("patient_id = ? AND schedule_date >= ? AND status = 1", patient.ID, lapseto.LapsetoTime).Updates(map[string]interface{}{
  551. "status": 0,
  552. "updated_time": now.Unix(),
  553. }).Error
  554. if deleteScheduleErr != nil {
  555. utx.Rollback()
  556. err = deleteScheduleErr
  557. return
  558. }
  559. deleteSchTempItemErr := utx.Model(&models.PatientScheduleTemplateItem{}).Where("patient_id = ? AND status = 1", patient.ID).Updates(map[string]interface{}{
  560. "status": 0,
  561. "mtime": now.Unix(),
  562. }).Error
  563. if deleteSchTempItemErr != nil {
  564. utx.Rollback()
  565. err = deleteSchTempItemErr
  566. return
  567. }
  568. }
  569. utx.Commit()
  570. return
  571. }
  572. func UpdatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  573. // if len(contagions) > 0 || len(diseases) > 0 {
  574. utx := writeDb.Begin()
  575. err = utx.Save(patient).Error
  576. if err != nil {
  577. utx.Rollback()
  578. return
  579. }
  580. err = utx.Model(&models.InfectiousDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  581. fmt.Println("err", err)
  582. if err != nil {
  583. utx.Rollback()
  584. return
  585. }
  586. if len(contagions) > 0 && patient.IsInfectious == 2 {
  587. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  588. insertParams := make([]string, 0)
  589. insertData := make([]interface{}, 0)
  590. for _, contagion := range contagions {
  591. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  592. insertData = append(insertData, patient.ID)
  593. insertData = append(insertData, contagion)
  594. insertData = append(insertData, 1)
  595. insertData = append(insertData, time.Now().Unix())
  596. insertData = append(insertData, time.Now().Unix())
  597. }
  598. thisSQL += strings.Join(insertParams, ", ")
  599. err = utx.Exec(thisSQL, insertData...).Error
  600. if err != nil {
  601. utx.Rollback()
  602. return
  603. }
  604. }
  605. err = utx.Model(&models.ChronicDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  606. if err != nil {
  607. utx.Rollback()
  608. return
  609. }
  610. if len(diseases) > 0 {
  611. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  612. insertParams := make([]string, 0)
  613. insertData := make([]interface{}, 0)
  614. for _, disease := range diseases {
  615. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  616. insertData = append(insertData, patient.ID)
  617. insertData = append(insertData, disease)
  618. insertData = append(insertData, 1)
  619. insertData = append(insertData, time.Now().Unix())
  620. insertData = append(insertData, time.Now().Unix())
  621. }
  622. thisSQL += strings.Join(insertParams, ", ")
  623. err = utx.Exec(thisSQL, insertData...).Error
  624. if err != nil {
  625. utx.Rollback()
  626. return
  627. }
  628. }
  629. utx.Commit()
  630. // } else {
  631. // err = writeDb.Save(patient).Error
  632. // }
  633. return
  634. }
  635. func UpdatePatientLapseto(patientid int64, lapseto models.PatientLapseto) error {
  636. err := XTWriteDB().Model(&lapseto).Where("patient_id = ?", patientid).Updates(map[string]interface{}{"lapseto_type": lapseto.LapsetoType, "lapseto_time": time.Now().Unix(), "updated_time": time.Now().Unix()}).Error
  637. return err
  638. }
  639. func UpdatepatientTwo(patientsNew *models.XtPatientsNew, id int64) error {
  640. err := XTWriteDB().Model(&patientsNew).Where("blood_id = ?", id).Update(map[string]interface{}{"user_org_id": patientsNew.UserOrgId, "user_id": patientsNew.UserId, "avatar": patientsNew.Avatar, "patient_type": patientsNew.Avatar, "dialysis_no": patientsNew.DialysisNo, "admission_number": patientsNew.AdmissionNumber, "source": patientsNew.Source, "lapseto": patientsNew.Lapseto, "partition_id": patientsNew.PartitionId, "bed_id": patientsNew.BedId, "name": patientsNew.Name, "alias": patientsNew.Alias, "gender": patientsNew.Gender, "marital_status": patientsNew.MaritalStatus, "id_card_no": patientsNew.IdCardNo, "birthday": patientsNew.Birthday, "reimbursement_way_id": patientsNew.ReimbursementWayId, "health_care_type": patientsNew.HealthCareType, "health_care_no": patientsNew.HealthCareType, "health_care_due_date": patientsNew.HealthCareType, "height": patientsNew.Height, "blood_type": patientsNew.BloodType, "rh": patientsNew.Rh, "health_care_due_alert_date": patientsNew.HealthCareDueAlertDate, "education_level": patientsNew.EducationLevel, "profession": patientsNew.Profession, "phone": patientsNew.Phone, "home_telephone": patientsNew.HomeTelephone, "relative_phone": patientsNew.RelativePhone, "relative_relations": patientsNew.RelativeRelations, "home_address": patientsNew.HomeAddress, "work_unit": patientsNew.WorkUnit, "unit_address": patientsNew.UnitAddress, "children": patientsNew.Children, "receiving_date": patientsNew.ReceivingDate, "is_hospital_first_dialysis": patientsNew.IsHospitalFirstDialysis, "first_dialysis_date": patientsNew.FirstDialysisDate, "first_dialysis_hospital": patientsNew.FirstDialysisHospital, "predialysis_condition": patientsNew.PredialysisCondition, "pre_hospital_dialysis_frequency": patientsNew.PreHospitalDialysisFrequency, "pre_hospital_dialysis_times": patientsNew.PreHospitalDialysisFrequency, "hospital_first_dialysis_date": patientsNew.HospitalFirstDialysisDate, "induction_period": patientsNew.InductionPeriod, "initial_dialysis": patientsNew.InitialDialysis, "total_dialysis": patientsNew.TotalDialysis, "attending_doctor_id": patientsNew.AttendingDoctorId, "head_nurse_id": patientsNew.HeadNurseId, "evaluate": patientsNew.Evaluate, "diagnose": patientsNew.Diagnose, "remark": patientsNew.Remark, "registrars_id": patientsNew.RegistrarsId, "registrars": patientsNew.Registrars, "qr_code": patientsNew.QrCode, "binding_state": patientsNew.BindingState, "patient_complains": patientsNew.PatientComplains, "present_history": patientsNew.PresentHistory, "past_history": patientsNew.PastHistory, "temperature": patientsNew.Temperature,
  641. "pulse": patientsNew.Pulse, "respiratory": patientsNew.Respiratory, "sbp": patientsNew.Sbp, "dbp": patientsNew.Dbp, "nation": patientsNew.Nation, "native_place": patientsNew.NativePlace, "age": patientsNew.Age, "infectious_next_record_time": patientsNew.InfectiousNextRecordTime, "is_infectious": patientsNew.IsInfectious, "remind_cycle": patientsNew.RemindCycle, "response_result": patientsNew.ResponseResult, "is_open_remind": patientsNew.IsOpenRemind, "first_treatment_date": patientsNew.FirstTreatmentDate, "dialysis_age": patientsNew.DialysisAge, "expense_kind": patientsNew.ExpenseKind, "tell_phone": patientsNew.ExpenseKind, "contact_name": patientsNew.ContactName, "blood_patients": patientsNew.BloodPatients, "slow_patients": patientsNew.SlowPatients, "member_patients": patientsNew.MemberPatients, "ecommer_patients": patientsNew.EcommerPatients}).Error
  642. return err
  643. }
  644. func GetLastInfectionRecord(id int64, org_id int64, project_id int64, date int64) (inspection models.Inspection, err error) {
  645. err = readDb.Model(&models.Inspection{}).Where("patient_id=? and status=1 and org_id = ? and project_id = ? AND inspect_date = ? ", id, org_id, project_id, date).Last(&inspection).Error
  646. return
  647. }
  648. func GetAllInfectionRecord(date int64, org_id int64, patient_id int64, project_id int64) (inspection []*models.Inspection, err error) {
  649. err = readDb.Model(&models.Inspection{}).Where("patient_id=? and status=1 and org_id = ? and project_id = ? and inspect_date = ?", patient_id, org_id, project_id, date).Find(&inspection).Error
  650. return
  651. }
  652. func GetPatientDiseases(id int64) []int64 {
  653. var dis []models.ChronicDiseases
  654. ids := make([]int64, 0)
  655. err := readDb.Model(&models.ChronicDiseases{}).Where("patient_id=? and status=1", id).Find(&dis).Error
  656. if err != nil || len(dis) == 0 {
  657. return ids
  658. }
  659. for _, item := range dis {
  660. ids = append(ids, item.DiseaseId)
  661. }
  662. return ids
  663. }
  664. func GetPatientContagions(id int64) []int64 {
  665. var cis []models.InfectiousDiseases
  666. ids := make([]int64, 0)
  667. err := readDb.Model(&models.InfectiousDiseases{}).Where("patient_id=? and status=1", id).Find(&cis).Error
  668. if err != nil || len(cis) == 0 {
  669. return ids
  670. }
  671. for _, item := range cis {
  672. ids = append(ids, item.DiseaseId)
  673. }
  674. return ids
  675. }
  676. func FindPatientDialysisSolutionByMode(orgID int64, patientID, modeId int64) (solution models.DialysisSolution, err error) {
  677. err = readDb.Model(&models.DialysisSolution{}).Where("user_org_id=? and patient_id=? and mode_id=? and parent_id=0 and status=1", orgID, patientID, modeId).First(&solution).Error
  678. return
  679. }
  680. func FindPatientDialysisSolution(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  681. err = readDb.Model(&models.DialysisSolution{}).Where("id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  682. return
  683. }
  684. func FindPatientDialysisSolutionChild(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  685. err = readDb.Model(&models.DialysisSolution{}).Where("parent_id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  686. return
  687. }
  688. func CreatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  689. err = writeDb.Create(solution).Error
  690. return
  691. }
  692. func UpdatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  693. err = writeDb.Save(solution).Error
  694. return
  695. }
  696. //GetPatientDialysisSolutionList 返回患者透析方案的列表
  697. func GetPatientDialysisSolutionList(orgID int64, patientID int64, page, limit int64) (solutions []*models.DialysisSolution, total int64, err error) {
  698. offset := (page - 1) * limit
  699. db := readDb.Table("xt_dialysis_solution as ds").Where("ds.status=1")
  700. if orgID > 0 {
  701. db = db.Where("ds.user_org_id=?", orgID)
  702. }
  703. if patientID > 0 {
  704. db = db.Where("ds.patient_id=?", patientID)
  705. }
  706. db = db.Count(&total).Offset(offset).Limit(limit)
  707. err = db.Order("id desc").Find(&solutions).Error
  708. if err != nil {
  709. return
  710. }
  711. if len(solutions) > 0 {
  712. nilNameIds := make([]int64, 0)
  713. for _, solution := range solutions {
  714. if len(solution.ModeName) == 0 {
  715. nilNameIds = append(nilNameIds, solution.ModeId)
  716. }
  717. }
  718. if len(nilNameIds) > 0 {
  719. var modes []*models.TreatmentMode
  720. err = readDb.Model(&models.TreatmentMode{}).Where("id IN (?)", nilNameIds).Find(&modes).Error
  721. if err != nil {
  722. return
  723. }
  724. modesMap := make(map[int64]models.TreatmentMode, 0)
  725. for _, mode := range modes {
  726. modesMap[mode.ID] = *mode
  727. }
  728. for index, solution := range solutions {
  729. if _, exixt := modesMap[solution.ModeId]; exixt && len(solution.ModeName) == 0 {
  730. solutions[index].ModeName = modesMap[solution.ModeId].Name
  731. }
  732. }
  733. }
  734. }
  735. return
  736. }
  737. //GetPatientDryWeightAdjustList 返回患者调整干体重的列表
  738. func GetPatientDryWeightAdjustList(orgID int64, patientID int64, page int64, limit int64) (weights []*models.DryWeightAdjust, total int64, err error) {
  739. db := readDb.Table("xt_dry_weight_adjust as dwa").Where("dwa.status=1")
  740. if orgID > 0 {
  741. db = db.Where("dwa.user_org_id=?", orgID)
  742. }
  743. if patientID > 0 {
  744. db = db.Where("dwa.patient_id=?", patientID)
  745. }
  746. db = db.Select("dwa.id, dwa.user_org_id, dwa.patient_id, dwa.weight, dwa.adjusted_value, dwa.doctor, dwa.registrars_id, dwa.remark, dwa.status, dwa.created_time, dwa.updated_time").Count(&total).Order("id desc")
  747. if page > 0 && limit > 0 {
  748. offset := (page - 1) * limit
  749. db = db.Offset(offset).Limit(limit)
  750. }
  751. err = db.Find(&weights).Error
  752. return
  753. }
  754. func CreateDryWeightAdjust(m *models.DryWeightAdjust) (err error) {
  755. err = writeDb.Create(m).Error
  756. return
  757. }
  758. func FindPatientLastDryWeightAdjust(orgID int64, id int64) (weight models.DryWeightAdjust, err error) {
  759. err = readDb.Model(&models.DryWeightAdjust{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  760. return
  761. }
  762. func CreateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  763. return writeDb.Create(m).Error
  764. }
  765. func GetMaxAdviceGroupID(orgId int64) (group int64) {
  766. var advice models.DoctorAdvice
  767. err := readDb.Table("xt_doctor_advice").Where("user_org_id=?", orgId).Select("max(groupno) as groupno").First(&advice).Error
  768. if err != nil {
  769. fmt.Println(err)
  770. group = 0
  771. }
  772. group = advice.GroupNo
  773. return
  774. }
  775. func CreateGroupAdvice(orgId int64, group int64, advices []*models.GroupAdvice) (err error) {
  776. if group == 0 {
  777. group = GetMaxAdviceGroupID(orgId) + 1
  778. }
  779. tx := writeDb.Begin()
  780. defer func() {
  781. if r := recover(); r != nil {
  782. tx.Rollback()
  783. }
  784. }()
  785. for _, advice := range advices {
  786. advice.GroupNo = group
  787. if err = tx.Create(advice).Error; err != nil {
  788. tx.Rollback()
  789. return
  790. }
  791. }
  792. tx.Commit()
  793. return
  794. }
  795. func CreateMGroupAdvice(orgId int64, advices []*models.GroupAdvice, groupNo int64) (list []*models.GroupAdvice, err error) {
  796. if groupNo <= 0 {
  797. group := GetMaxAdviceGroupID(orgId)
  798. groupNo = group + 1
  799. }
  800. tx := writeDb.Begin()
  801. defer func() {
  802. if r := recover(); r != nil {
  803. tx.Rollback()
  804. }
  805. }()
  806. for _, advice := range advices {
  807. advice.GroupNo = groupNo
  808. if err = tx.Create(advice).Error; err != nil {
  809. tx.Rollback()
  810. return
  811. }
  812. list = append(list, advice)
  813. if len(advice.Children) > 0 {
  814. for _, child := range advice.Children {
  815. child.GroupNo = groupNo
  816. child.ParentId = advice.ID
  817. fmt.Println(child)
  818. if err = tx.Create(&child).Error; err != nil {
  819. tx.Rollback()
  820. return
  821. }
  822. list = append(list, child)
  823. }
  824. }
  825. }
  826. tx.Commit()
  827. return
  828. }
  829. func UpdateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  830. return writeDb.Save(m).Error
  831. }
  832. func StopGroupAdvice(orgId int64, groupNo int64, m *models.DoctorAdvice) (err error) {
  833. err = writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? and groupno=?", orgId, groupNo).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "StopState": 1, "StopReason": m.StopReason, "StopDoctor": m.StopDoctor, "StopTime": m.StopTime}).Error
  834. if err != nil {
  835. return
  836. }
  837. return
  838. }
  839. func StopDoctorAdvice(m *models.DoctorAdvice) (err error) {
  840. ut := writeDb.Begin()
  841. err = ut.Save(m).Error
  842. if err != nil {
  843. ut.Rollback()
  844. return
  845. }
  846. err = ut.Model(&models.DoctorAdvice{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "StopState": 1, "StopReason": m.StopReason, "StopDoctor": m.StopDoctor, "StopTime": m.StopTime, "Modifier": m.Modifier}).Error
  847. if err != nil {
  848. ut.Rollback()
  849. return
  850. }
  851. ut.Commit()
  852. return
  853. }
  854. func DeleteSolution(m *models.DialysisSolution) (err error) {
  855. if m.ParentId > 0 {
  856. return writeDb.Save(m).Error
  857. } else {
  858. ut := writeDb.Begin()
  859. err = ut.Save(m).Error
  860. if err != nil {
  861. ut.Rollback()
  862. return
  863. }
  864. err = ut.Model(&models.DialysisSolution{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0}).Error
  865. if err != nil {
  866. ut.Rollback()
  867. return
  868. }
  869. ut.Commit()
  870. }
  871. return
  872. }
  873. func DeleteDoctorAdvice(m *models.DoctorAdvice) (err error) {
  874. if m.ParentId > 0 {
  875. return writeDb.Save(m).Error
  876. } else {
  877. ut := writeDb.Begin()
  878. err = ut.Save(m).Error
  879. if err != nil {
  880. ut.Rollback()
  881. return
  882. }
  883. err = ut.Model(&models.DoctorAdvice{}).Where("parent_id=? AND user_org_id = ?", m.ID, m.UserOrgId).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0, "Modifier": m.Modifier}).Error
  884. if err != nil {
  885. ut.Rollback()
  886. return
  887. }
  888. ut.Commit()
  889. }
  890. return
  891. }
  892. func DeleteGroupAdvice(orgId int64, groupNo int64, admin_user_id int64) (err error) {
  893. err = writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id = ? and groupno = ?", orgId, groupNo).Update(map[string]interface{}{"UpdatedTime": time.Now().Unix(), "Status": 0, "Modifier": admin_user_id}).Error
  894. if err != nil {
  895. return
  896. }
  897. return
  898. }
  899. func FindDoctorAdvice(orgID, id int64) (advice models.DoctorAdvice, err error) {
  900. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  901. return
  902. }
  903. func FindHisDoctorAdvice(orgID, id int64) (advice models.HisDoctorAdviceInfo, err error) {
  904. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  905. return
  906. }
  907. func FindDoctorAdviceByGroupNo(orgID, groupNo int64) (advice models.DoctorAdvice, err error) {
  908. err = readDb.Model(&models.DoctorAdvice{}).Where("groupno = ? and user_org_id=? and status = 1", groupNo, orgID).First(&advice).Error
  909. return
  910. }
  911. func GetDoctorAdviceList(orgID, patientID, advice_type, stop, start, end int64, keywords string) (advices []*models.DoctorAdvices, total int64, err error) {
  912. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  913. table := UserReadDB().Table("sgj_user_admin_role as r")
  914. fmt.Print("table", table)
  915. if orgID > 0 {
  916. db = db.Where("x.user_org_id=?", orgID)
  917. }
  918. if patientID > 0 {
  919. db = db.Where("x.patient_id = ?", patientID)
  920. }
  921. if advice_type > 0 {
  922. db = db.Where("x.advice_type = ?", advice_type)
  923. } else if advice_type == 0 {
  924. db = db.Where("x.advice_type in (?)", []int{1, 3})
  925. }
  926. if stop == 1 {
  927. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  928. } else if stop == 2 {
  929. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  930. }
  931. if start != 0 {
  932. db = db.Where("x.start_time>=?", start)
  933. }
  934. if end != 0 {
  935. db = db.Where("start_time<=?", end)
  936. }
  937. if len(keywords) > 0 {
  938. likeKey := "%" + keywords + "%"
  939. db = db.Where("x.advice_name LIKE ?", likeKey)
  940. }
  941. err = db.Group("x.id").Count(&total).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, x.advice_affirm, x.remark, x.stop_time, x.stop_reason, x.stop_doctor, x.stop_state, x.parent_id, x.execution_time, x.execution_staff, x.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&advices).Error
  942. fmt.Print("err", err)
  943. return
  944. }
  945. func GetDoctorAdviceListOne(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64) (advices []*models.DoctorAdvices, total int64, err error) {
  946. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  947. table := UserReadDB().Table("sgj_user_admin_role as r")
  948. fmt.Print("table", table)
  949. if orgID > 0 {
  950. db = db.Where("x.user_org_id=?", orgID)
  951. }
  952. if patientID > 0 {
  953. db = db.Where("x.patient_id = ?", patientID)
  954. }
  955. if advice_type == 1 && advice_type > 0 {
  956. db = db.Where("x.advice_type = ?", advice_type)
  957. }
  958. if advice_type == 3 && advice_type > 0 {
  959. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  960. }
  961. if advice_type == 4 && advice_type > 0 {
  962. db = db.Where("x.advice_type = 4")
  963. }
  964. if stop == 1 {
  965. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  966. } else if stop == 2 {
  967. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  968. }
  969. if start != 0 {
  970. db = db.Where("x.start_time>=?", start)
  971. }
  972. if end != 0 {
  973. db = db.Where("start_time<=?", end)
  974. }
  975. if len(keywords) > 0 {
  976. likeKey := "%" + keywords + "%"
  977. db = db.Where("x.advice_name LIKE ?", likeKey)
  978. }
  979. offset := (page - 1) * limit
  980. err = db.Group("x.id").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, x.advice_affirm, x.remark, x.stop_time, x.stop_reason, x.stop_doctor, x.stop_state, x.parent_id, x.execution_time, x.execution_staff, x.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&advices).Error
  981. fmt.Print("err", err)
  982. return
  983. }
  984. func GetDoctorAdviceListTwo(orgID int64, patientID int64, advice_type int64, stop int64, start int64, end int64, keywords string, limit int64, page int64) (advices []*models.DoctorAdvices, total int64, err error) {
  985. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  986. table := UserReadDB().Table("sgj_user_admin_role as r")
  987. fmt.Println(table)
  988. if orgID > 0 {
  989. db = db.Where("x.user_org_id=?", orgID)
  990. }
  991. if patientID > 0 {
  992. db = db.Where("x.patient_id = ?", patientID)
  993. }
  994. if advice_type == 1 {
  995. db = db.Where("x.advice_type = ?", advice_type)
  996. }
  997. if advice_type == 3 && advice_type > 0 {
  998. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  999. }
  1000. if advice_type == 4 && advice_type > 0 {
  1001. db = db.Where("x.advice_type = 4")
  1002. }
  1003. if stop == 1 {
  1004. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1005. } else if stop == 2 {
  1006. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1007. }
  1008. if start != 0 {
  1009. db = db.Where("x.start_time>=?", start)
  1010. }
  1011. if end != 0 {
  1012. db = db.Where("start_time<=?", end)
  1013. }
  1014. if len(keywords) > 0 {
  1015. likeKey := "%" + keywords + "%"
  1016. db = db.Where("x.advice_name LIKE ?", likeKey)
  1017. }
  1018. offset := (page - 1) * limit
  1019. err = db.Order("x.start_time desc").Group("x.start_time").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, x.advice_affirm, x.remark, x.stop_time, x.stop_reason, x.stop_doctor, x.stop_state, x.parent_id, x.execution_time, x.execution_staff, x.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Scan(&advices).Error
  1020. fmt.Print("错误是什么", err)
  1021. return
  1022. }
  1023. func GetDoctorAdvicePageList(orgID, patientID, advice_type, stop, start, end int64, keywords string, page, limit int64) (advices []*models.DoctorAdvice, total int64, err error) {
  1024. offset := (page - 1) * limit
  1025. db := readDb.Model(&models.DoctorAdvice{}).Where("status=1")
  1026. if orgID > 0 {
  1027. db = db.Where("user_org_id=?", orgID)
  1028. }
  1029. if patientID > 0 {
  1030. db = db.Where("patient_id = ?", patientID)
  1031. }
  1032. if advice_type > 0 {
  1033. db = db.Where("advice_type = ?", advice_type)
  1034. }
  1035. if stop == 1 {
  1036. db = db.Where("(stop_state=? or execution_state=?) and parent_id=0", stop, stop)
  1037. } else if stop == 2 {
  1038. db = db.Where("stop_state=? and execution_state=?", stop, stop)
  1039. }
  1040. if start != 0 {
  1041. db = db.Where("start_time>=?", start)
  1042. }
  1043. if end != 0 {
  1044. db = db.Where("start_time<=?", end)
  1045. }
  1046. if len(keywords) > 0 {
  1047. likeKey := "%" + keywords + "%"
  1048. db = db.Where("advice_name LIKE ?", likeKey)
  1049. }
  1050. err = db.Count(&total).Select("id,user_org_id,patient_id,advice_type,advice_date,start_time,advice_name,advice_desc,reminder_date,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 IF(parent_id>0, parent_id, id) as advice_order").Order("advice_order desc, id").Offset(offset).Limit(limit).Scan(&advices).Error
  1051. return
  1052. }
  1053. func CreateSubDoctorAdvice(advices []*models.DoctorAdvice) (err error) {
  1054. if len(advices) > 0 {
  1055. utx := writeDb.Begin()
  1056. if len(advices) > 0 {
  1057. thisSQL := "INSERT INTO xt_doctor_advice (single_dose_unit, prescribing_number, prescribing_number_unit, advice_name, advice_desc,single_dose,created_time,updated_time,patient_id,parent_id,user_org_id,record_date,advice_type,drug_spec_unit) VALUES "
  1058. insertParams := make([]string, 0)
  1059. insertData := make([]interface{}, 0)
  1060. for _, advice := range advices {
  1061. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1062. insertData = append(insertData, advice.SingleDoseUnit)
  1063. insertData = append(insertData, advice.PrescribingNumber)
  1064. insertData = append(insertData, advice.PrescribingNumberUnit)
  1065. insertData = append(insertData, advice.AdviceName)
  1066. insertData = append(insertData, advice.AdviceDesc)
  1067. insertData = append(insertData, advice.SingleDose)
  1068. insertData = append(insertData, advice.CreatedTime)
  1069. insertData = append(insertData, advice.UpdatedTime)
  1070. insertData = append(insertData, advice.PatientId)
  1071. insertData = append(insertData, advice.ParentId)
  1072. insertData = append(insertData, advice.UserOrgId)
  1073. insertData = append(insertData, advice.RecordDate)
  1074. insertData = append(insertData, 2)
  1075. insertData = append(insertData, advice.DrugSpecUnit)
  1076. }
  1077. thisSQL += strings.Join(insertParams, ", ")
  1078. err = utx.Exec(thisSQL, insertData...).Error
  1079. if err != nil {
  1080. utx.Rollback()
  1081. return
  1082. }
  1083. }
  1084. utx.Commit()
  1085. }
  1086. return
  1087. }
  1088. func GetPatientDialysisRecord(orgID, patientID int64, page, limit, start, end, mode_id int64) ([]*models.PatientDialysisRecord, int64, error) {
  1089. offset := (page - 1) * limit
  1090. var total int64
  1091. var err error
  1092. var orders []*models.PatientDialysisRecord
  1093. // err = readDb.Table("xt_dialysis_order as do").
  1094. // Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1095. // Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1096. // Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1097. // Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1098. // Joins("JOIN xt_schedule as s ON s.patient_id=? and FROM_UNIXTIME(s.schedule_date, '%Y-%m-%d')=FROM_UNIXTIME(do.dialysis_date, '%Y-%m-%d')", patientID).
  1099. // Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  1100. // Where("do.patient_id=? and do.user_org_id=? and do.stage = 2 and do.status=1", patientID, orgID).Count(&total).Offset(offset).Limit(limit).Order("do.dialysis_date desc").Select(" do.id, do.dialysis_date, do.user_org_id, do.patient_id, do.prescription_id, do.stage, do.remark, do.status, do.created_time, do.updated_time, s.schedule_type, s.partition_id, dz.name as partition_name").Find(&orders).Error
  1101. db := readDb.Table("xt_dialysis_order as do").
  1102. Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1103. Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1104. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  1105. return readDb.Where("patient_id=? and user_org_id=? and status=1", patientID, orgID).Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1106. return readUserDb.Where("status = 1")
  1107. })
  1108. }).
  1109. Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1110. Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1111. Preload("Device", "org_id=? and status=1", orgID).
  1112. Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1113. return readUserDb.Where("org_id=? and status = 1", orgID)
  1114. }).
  1115. Joins("JOIN xt_schedule as s ON s.patient_id=? and FROM_UNIXTIME(s.schedule_date, '%Y-%m-%d')=FROM_UNIXTIME(do.dialysis_date, '%Y-%m-%d')", patientID).
  1116. Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  1117. Where("do.patient_id=? and do.user_org_id=? and do.stage = 2 and do.status=1", patientID, orgID).Group("s.schedule_date")
  1118. if start != 0 {
  1119. db = db.Where("do.dialysis_date>=?", start)
  1120. }
  1121. if end != 0 {
  1122. db = db.Where("do.dialysis_date<=?", end)
  1123. }
  1124. if mode_id > 0 {
  1125. db = db.Joins("JOIN xt_dialysis_prescription as dp ON dp.patient_id=do.patient_id and dp.user_org_id = do.user_org_id and dp.record_date = do.dialysis_date")
  1126. db = db.Where("dp.mode_id=?", mode_id)
  1127. }
  1128. err = db.Count(&total).Offset(offset).Limit(limit).Order("do.dialysis_date desc").Select("do.bed_id, do.id, do.dialysis_date, do.user_org_id, do.patient_id, do.prescription_id, do.stage, do.remark, do.status, do.created_time, do.updated_time,do.start_nurse,do.finish_nurse ,s.schedule_type, s.partition_id, dz.name as partition_name").Find(&orders).Error
  1129. if len(orders) > 0 {
  1130. ids := make([]int64, 0)
  1131. for _, order := range orders {
  1132. dialyzer := order.DialysisPrescription.Dialyzer
  1133. ids = append(ids, dialyzer)
  1134. }
  1135. if len(ids) > 0 {
  1136. var dialyzers []*models.DeviceNumber
  1137. err = readDb.Model(&models.DeviceNumber{}).Where("id IN (?) and org_id=? and status=1", ids, orgID).Find(&dialyzers).Error
  1138. if err != nil {
  1139. return nil, 0, err
  1140. }
  1141. dialyzerMap := make(map[int64]models.DeviceNumber, 0)
  1142. for _, item := range dialyzers {
  1143. dialyzerMap[item.ID] = *item
  1144. }
  1145. for orderIndex, order := range orders {
  1146. if _, exist := dialyzerMap[order.DialysisPrescription.Dialyzer]; exist {
  1147. orders[orderIndex].DeviceNumber = dialyzerMap[order.DialysisPrescription.Dialyzer].Number
  1148. }
  1149. }
  1150. }
  1151. }
  1152. return orders, total, err
  1153. }
  1154. func GetPatientTreatmentSummaryList(orgID, patientID, page, limit, start, end int64) (list []*models.TreatmentSummary, total int, err error) {
  1155. offset := (page - 1) * limit
  1156. fmt.Println(offset)
  1157. fmt.Println(limit)
  1158. db := readDb.Model(&models.TreatmentSummary{}).Where("user_org_id = ? and patient_id=?", orgID, patientID)
  1159. if start != 0 {
  1160. db = db.Where("assessment_date >= ?", start)
  1161. }
  1162. if end != 0 {
  1163. db = db.Where("assessment_date <= ?", end)
  1164. }
  1165. db = db.Where("status=1")
  1166. err = db.Count(&total).Offset(offset).Limit(limit).Order("assessment_date desc, id desc").Find(&list).Error
  1167. return
  1168. }
  1169. func GetPatientScheduleList(orgID int64, patientID int64, page int64, limit int64, start int64) (schedules []*models.PatientSchedule, err error) {
  1170. offset := (page - 1) * limit
  1171. err = readDb.Table("xt_schedule as s").
  1172. Preload("DeviceZone", "org_id=? and status=1", orgID).
  1173. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  1174. Preload("TreatmentMode", "status=1").
  1175. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, start).
  1176. Limit(limit).Offset(offset).Find(&schedules).Error
  1177. return
  1178. }
  1179. func GetMonitorRecord(orgID int64, date int64, partition int64, patientId int64) ([]*models.NewVMMonitorDialysisSchedule, error) {
  1180. var mds []*models.NewVMMonitorDialysisSchedule
  1181. if patientId == 0 {
  1182. db := readDb.
  1183. Model(&models.NewVMMonitorDialysisSchedule{}).
  1184. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1185. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  1186. Preload("TreatmentMode", "status = 1").
  1187. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, date).
  1188. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, date).
  1189. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, date).
  1190. Preload("MonitoringRecord", "status = 1 AND user_org_id = ? AND monitoring_date = ?", orgID, date).
  1191. Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, date).
  1192. Preload("DialysisOrder.DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1193. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  1194. Where("status = 1 AND user_org_id = ?", orgID)
  1195. if date != 0 {
  1196. db = db.Where("schedule_date = ? ", date)
  1197. }
  1198. if partition != 0 {
  1199. db = db.Where("partition_id = ? ", partition)
  1200. }
  1201. err := db.Find(&mds).Error
  1202. fmt.Println("err", err)
  1203. }
  1204. if patientId > 0 {
  1205. db := readDb.Table("xt_schedule as x").Where("x.status = 1")
  1206. db = db.
  1207. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1208. Preload("TreatmentMode", "status = 1").
  1209. Preload("Prescription", "status = 1 AND user_org_id = ? AND patient_id = ?", orgID, patientId).
  1210. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND patient_id = ?", orgID, patientId).
  1211. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? AND patient_id = ? ", orgID, patientId).
  1212. Preload("MonitoringRecord", "status = 1 AND user_org_id = ? AND patient_id = ?", orgID, patientId).
  1213. Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND patient_id = ?", orgID, patientId).
  1214. Preload("DialysisOrder.DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1215. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  1216. Where("status = 1 AND user_org_id = ?", orgID)
  1217. if partition != 0 {
  1218. db = db.Where("partition_id = ? ", partition)
  1219. }
  1220. db = db.Where("schedule_date <= ? ", time.Now().Unix())
  1221. db = db.Where("patient_id = ?", patientId)
  1222. db = db.Order("schedule_date desc")
  1223. err := db.Find(&mds).Error
  1224. fmt.Println("err", err)
  1225. }
  1226. return mds, err
  1227. }
  1228. func MobileGetMonitorsWithPatient(orgID int64, keyword string, page int) ([]*models.MonitorDialysisSchedule, error) {
  1229. var patients []*models.Patients
  1230. getPatientErr := readDb.Model(&models.Patients{}).Where("status = 1 AND user_org_id = ? AND (name like ? OR dialysis_no like ?)", orgID, "%"+keyword+"%", "%"+keyword+"%").Find(&patients).Error
  1231. if getPatientErr != nil {
  1232. return nil, getPatientErr
  1233. }
  1234. patientIDs := make([]int64, len(patients))
  1235. for index, patient := range patients {
  1236. patientIDs[index] = patient.ID
  1237. }
  1238. db := readDb.
  1239. Model(&models.MonitorDialysisSchedule{}).
  1240. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1241. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  1242. Preload("TreatmentMode", "status = 1").
  1243. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  1244. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  1245. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  1246. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  1247. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  1248. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  1249. Where("status = 1 AND user_org_id = ? AND patient_id in (?)", orgID, patientIDs)
  1250. var schedules []*models.MonitorDialysisSchedule
  1251. err := db.Offset(20 * (page - 1)).Limit(20).Order("schedule_date desc").Find(&schedules).Error
  1252. return schedules, err
  1253. }
  1254. func GetPatientByKeyWord(orgID int64, keywords string) (patient []*models.Patients, err error) {
  1255. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1", orgID)
  1256. if len(keywords) > 0 {
  1257. likekey := "%" + keywords + "%"
  1258. err = db.Where("name LIKE ? OR dialysis_no LIKE ? ", likekey, likekey).Find(&patient).Error
  1259. } else {
  1260. err = db.Find(&patient).Error
  1261. }
  1262. return
  1263. }
  1264. func FindDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice models.DoctorAdvice, err error) {
  1265. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).First(&advice).Error
  1266. return
  1267. }
  1268. func FindOldDoctorAdvice(orgID int64, advice_id int64) (advice models.DoctorAdvice, err error) {
  1269. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND user_org_id=? AND status = 1", advice_id, orgID).First(&advice).Error
  1270. return
  1271. }
  1272. func UpdateAdviceGroupStartTime(orgID int64, groupNO int64, startTime int64, admin_user_id int64) error {
  1273. now := time.Now().Unix()
  1274. err := writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id = ? AND status = 1 AND execution_state <> 1 AND check_state <> 1 AND groupno = ? AND admin_user_id = ?", orgID, groupNO, admin_user_id).Updates(map[string]interface{}{
  1275. "start_time": startTime,
  1276. "updated_time": now,
  1277. "modifier": admin_user_id,
  1278. }).Error
  1279. return err
  1280. }
  1281. func QueryPatientById(id int64) *models.Patients {
  1282. var pat models.Patients
  1283. err := readDb.Model(&models.Patients{}).Where("id=?", id).First(&pat).Error
  1284. if err != nil {
  1285. }
  1286. return &pat
  1287. }
  1288. func FindAllDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1289. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).Find(&advice).Error
  1290. return
  1291. }
  1292. func FindDoctorAdviceByIds(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1293. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND user_org_id = ? AND status = 1", ids, orgID).Find(&advice).Error
  1294. return
  1295. }
  1296. func BatchDeleteDoctorAdvice(ids []string, user_id int64) (err error) {
  1297. ut := writeDb.Begin()
  1298. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix(), "modifier": user_id}).Error
  1299. if err != nil {
  1300. ut.Rollback()
  1301. return
  1302. }
  1303. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix(), "modifier": user_id}).Error
  1304. if err != nil {
  1305. ut.Rollback()
  1306. return
  1307. }
  1308. ut.Commit()
  1309. return err
  1310. }
  1311. func FindAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1312. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND parent_id = 0", orgID, groupno).Find(&advice).Error
  1313. return
  1314. }
  1315. func UpdateDoctorAdviceAndSubAdvice(m *models.DoctorAdvice) (err error) {
  1316. ut := writeDb.Begin()
  1317. err = ut.Save(m).Error
  1318. if err != nil {
  1319. ut.Rollback()
  1320. return
  1321. }
  1322. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?)", m.ID).Updates(map[string]interface{}{"start_time": m.StartTime, "groupno": m.GroupNo, "mtime": time.Now().Unix()}).Error
  1323. if err != nil {
  1324. ut.Rollback()
  1325. return
  1326. }
  1327. ut.Commit()
  1328. return err
  1329. }
  1330. func GetAllWaitRemindPatient(org_id int64, page int64, limit int64) (total int64, patient []*models.Patients, err error) {
  1331. type Total struct {
  1332. Count int64
  1333. }
  1334. var totals Total
  1335. offset := (page - 1) * limit
  1336. err = readDb.Raw("select * from xt_patients where user_org_id = ? AND lapseto = 1 AND infectious_next_record_time > 0 AND status = 1 AND date_sub(DATE_FORMAT(date(from_unixtime(`xt_patients`.`infectious_next_record_time`)),'%Y-%m-%d'), interval 7 day) <= now() Order by infectious_next_record_time", org_id).Offset(offset).Limit(limit).Scan(&patient).Error
  1337. readDb.Raw("select Count(id) as count from xt_patients where user_org_id = ? AND lapseto = 1 AND infectious_next_record_time > 0 AND status = 1 AND date_sub(DATE_FORMAT(date(from_unixtime(`xt_patients`.`infectious_next_record_time`)),'%Y-%m-%d'), interval 7 day) <= now() Order by infectious_next_record_time", org_id).Scan(&totals)
  1338. return totals.Count, patient, err
  1339. }
  1340. func UpdatePatientRemindStatus(patient_id int64, remind int64, org_id int64) (err error) {
  1341. err = writeDb.Model(&models.Patients{}).Where("status = 1 AND id = ? AND user_org_id = ?", patient_id, org_id).Updates(map[string]interface{}{"is_open_remind": remind}).Error
  1342. return
  1343. }
  1344. func CreatePatientWeightAdjust(m *models.SgjPatientDryweight) (err error) {
  1345. err = writeDb.Create(m).Error
  1346. return
  1347. }
  1348. func FindLastDryWeightAdjust(orgID int64, id int64) (weight models.SgjPatientDryweight, err error) {
  1349. err = readDb.Model(&models.SgjPatientDryweight{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  1350. return
  1351. }
  1352. func GetSchedualPatientByKeyWord(orgID int64, keywords string, date int64) (patient []*models.Patients, err error) {
  1353. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1354. if len(keywords) > 0 {
  1355. likekey := "%" + keywords + "%"
  1356. err = db.Where("(name LIKE ? OR dialysis_no LIKE ?) AND NOT EXISTS (Select * FROM `xt_dialysis_order` as d Where d.`dialysis_date` = ? AND d.`status` = 1 AND d.`patient_id` = xt_patients.id AND d.user_org_id = xt_patients.user_org_id)", likekey, likekey, date).Find(&patient).Error
  1357. } else {
  1358. err = db.Find(&patient).Error
  1359. }
  1360. return
  1361. }
  1362. func GetPatientScheduleOne(patientid int64, nowdate int64, orgid int64) (models.XtSchedule, error) {
  1363. schedule := models.XtSchedule{}
  1364. err := XTReadDB().Model(&schedule).Where("patient_id = ? and schedule_date = ? and user_org_id = ? and status =1", patientid, nowdate, orgid).Find(&schedule).Error
  1365. return schedule, err
  1366. }
  1367. func CreateExportPatient(patient *models.Patients, contagions []int64, org_creator int64) (err error) {
  1368. user, _ := GetSgjUserByMobild(patient.Phone)
  1369. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  1370. utx := writeDb.Begin()
  1371. btx := writeUserDb.Begin()
  1372. if user.ID == 0 {
  1373. user.Mobile = patient.Phone
  1374. user.Avatar = patient.Avatar
  1375. user.AvatarThumb = patient.Avatar
  1376. user.Birthday = patient.Birthday
  1377. user.Username = patient.Name
  1378. user.Gender = patient.Gender
  1379. user.Sources = 11
  1380. user.Introduce = patient.Remark
  1381. user.Status = 1
  1382. user.UpdatedTime = patient.UpdatedTime
  1383. user.CreatedTime = patient.CreatedTime
  1384. err = btx.Create(&user).Error
  1385. if err != nil {
  1386. utx.Rollback()
  1387. btx.Rollback()
  1388. return
  1389. }
  1390. }
  1391. patient.UserId = user.ID
  1392. if customer == nil {
  1393. err = btx.Create(&models.SgjCustomer{
  1394. UserOrgId: patient.UserOrgId,
  1395. UserId: user.ID,
  1396. Mobile: patient.Phone,
  1397. Name: patient.Name,
  1398. Gender: patient.Gender,
  1399. Birthday: patient.Birthday,
  1400. Sources: 11,
  1401. Status: 1,
  1402. CreatedTime: patient.CreatedTime,
  1403. UpdatedTime: patient.UpdatedTime,
  1404. Avatar: patient.Avatar,
  1405. Remark: patient.Remark,
  1406. }).Error
  1407. if err != nil {
  1408. utx.Rollback()
  1409. btx.Rollback()
  1410. return
  1411. }
  1412. }
  1413. err = utx.Create(patient).Error
  1414. if err != nil {
  1415. utx.Rollback()
  1416. btx.Rollback()
  1417. return
  1418. }
  1419. if patient.DryWeight > 0 {
  1420. var dryWeight models.SgjPatientDryweight
  1421. dryWeight.PatientId = patient.ID
  1422. dryWeight.UserOrgId = patient.UserOrgId
  1423. dryWeight.Status = 1
  1424. dryWeight.AdjustedValue = "/"
  1425. dryWeight.Creator = org_creator
  1426. dryWeight.UserId = org_creator
  1427. dryWeight.Ctime = time.Now().Unix()
  1428. dryWeight.Mtime = time.Now().Unix()
  1429. dryWeight.DryWeight = patient.DryWeight
  1430. dryWeight.Remakes = ""
  1431. err = utx.Create(&dryWeight).Error
  1432. }
  1433. var lapseto models.PatientLapseto
  1434. lapseto.PatientId = patient.ID
  1435. lapseto.LapsetoType = patient.Lapseto
  1436. lapseto.CreatedTime = patient.CreatedTime
  1437. lapseto.UpdatedTime = patient.CreatedTime
  1438. lapseto.Status = 1
  1439. lapseto.LapsetoTime = patient.CreatedTime
  1440. err = utx.Create(&lapseto).Error
  1441. if err != nil {
  1442. utx.Rollback()
  1443. btx.Rollback()
  1444. return
  1445. }
  1446. if len(contagions) > 0 {
  1447. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  1448. insertParams := make([]string, 0)
  1449. insertData := make([]interface{}, 0)
  1450. for _, contagion := range contagions {
  1451. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  1452. insertData = append(insertData, patient.ID)
  1453. insertData = append(insertData, contagion)
  1454. insertData = append(insertData, 1)
  1455. insertData = append(insertData, patient.CreatedTime)
  1456. insertData = append(insertData, patient.UpdatedTime)
  1457. }
  1458. thisSQL += strings.Join(insertParams, ", ")
  1459. err = utx.Exec(thisSQL, insertData...).Error
  1460. if err != nil {
  1461. utx.Rollback()
  1462. btx.Rollback()
  1463. return
  1464. }
  1465. }
  1466. patientsNew := models.XtPatientsNew{
  1467. Name: patient.Name,
  1468. Gender: patient.Gender,
  1469. Phone: patient.Phone,
  1470. IdCardNo: patient.IdCardNo,
  1471. FirstDialysisDate: patient.FirstDialysisDate,
  1472. Source: patient.Source,
  1473. Lapseto: patient.Lapseto,
  1474. IsInfectious: patient.IsInfectious,
  1475. DialysisNo: patient.DialysisNo,
  1476. Height: patient.Height,
  1477. HomeAddress: patient.HomeAddress,
  1478. IsExcelExport: 1,
  1479. BloodPatients: 1,
  1480. Status: 1,
  1481. CreatedTime: time.Now().Unix(),
  1482. UserOrgId: patient.UserOrgId,
  1483. BloodId: patient.ID,
  1484. Avatar: "https://images.shengws.com/201809182128111.png",
  1485. }
  1486. err = utx.Create(&patientsNew).Error
  1487. if err != nil {
  1488. utx.Rollback()
  1489. btx.Rollback()
  1490. return
  1491. }
  1492. utx.Commit()
  1493. btx.Commit()
  1494. return
  1495. }
  1496. func FindPatientPhoneIsExist(phone string, org_id int64) (count int64) {
  1497. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND phone = ? AND status = 1", org_id, phone).Count(&count)
  1498. return
  1499. }
  1500. func FindPatientIdCardNoIsExist(id_card_no string, org_id int64) (count int64) {
  1501. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND id_card_no = ? AND status = 1", org_id, id_card_no).Count(&count)
  1502. return
  1503. }
  1504. func CreateExportErrLog(log *models.ExportErrLog) {
  1505. writeDb.Create(&log)
  1506. return
  1507. }
  1508. func FindPatientExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1509. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 1", org_id, export_time).Find(&errLogs).Error
  1510. return
  1511. }
  1512. func FindPatientExportLogOne(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1513. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 4", org_id, export_time).Find(&errLogs).Error
  1514. return
  1515. }
  1516. func FindPatientExportLogTwo(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1517. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 5", org_id, export_time).Find(&errLogs).Error
  1518. return
  1519. }
  1520. func FindPatientExportLogThree(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1521. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 6", org_id, export_time).Find(&errLogs).Error
  1522. return
  1523. }
  1524. func CreateExportLog(log *models.ExportLog) {
  1525. writeDb.Create(&log)
  1526. }
  1527. func UpdateDoctorEditAdvice(advice models.XtDoctorAdvice, orgid int64, groupno int64, date int64, patientid int64) error {
  1528. err := XTWriteDB().Model(&advice).Where("user_org_id = ? and groupno = ? and advice_date = ? and patient_id = ?", orgid, groupno, date, patientid).Update(map[string]interface{}{"start_time": advice.StartTime, "updated_time": advice.UpdatedTime}).Error
  1529. return err
  1530. }
  1531. func GetPatientsByKey(orgID int64, keywords string) (patient []*models.Patients, err error) {
  1532. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1533. if len(keywords) > 0 {
  1534. likekey := "%" + keywords + "%"
  1535. err = db.Where("(name LIKE ? OR dialysis_no LIKE ?)", likekey, likekey).Find(&patient).Error
  1536. } else {
  1537. err = db.Find(&patient).Error
  1538. }
  1539. return
  1540. }
  1541. func FindAllDoctorAdviceByGoroupNoTwo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1542. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND execution_state = 2", orgID, groupno).Find(&advice).Error
  1543. return
  1544. }
  1545. func FindDoctorAdviceByIdsTwo(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1546. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id = ? AND status = 1 AND execution_state = 2 AND (id IN (?) OR parent_id IN (?))", orgID, ids, ids).Find(&advice).Error
  1547. return
  1548. }
  1549. func CreateHisGroupAdvice(orgId int64, advices []*models.HisGroupAdvice, groupNo int64) (list []*models.HisGroupAdvice, err error) {
  1550. if groupNo <= 0 {
  1551. group := GetMaxAdviceGroupID(orgId)
  1552. groupNo = group + 1
  1553. }
  1554. tx := writeDb.Begin()
  1555. defer func() {
  1556. if r := recover(); r != nil {
  1557. tx.Rollback()
  1558. }
  1559. }()
  1560. for _, advice := range advices {
  1561. advice.Groupno = groupNo
  1562. if err = tx.Create(advice).Error; err != nil {
  1563. tx.Rollback()
  1564. return
  1565. }
  1566. list = append(list, advice)
  1567. if len(advice.Children) > 0 {
  1568. for _, child := range advice.Children {
  1569. child.Groupno = groupNo
  1570. child.ParentId = advice.ID
  1571. fmt.Println(child)
  1572. if err = tx.Create(&child).Error; err != nil {
  1573. tx.Rollback()
  1574. return
  1575. }
  1576. list = append(list, child)
  1577. }
  1578. }
  1579. }
  1580. tx.Commit()
  1581. return
  1582. }
  1583. func GetDialysisCount(orgid int64, partitionid int64) (order []*models.BloodDialysisOrderCount, err error) {
  1584. db := XTReadDB().Table("xt_dialysis_order as o")
  1585. if partitionid == 0 {
  1586. 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.user_org_id = ? and x.schedule_date = o.dialysis_date and x.status = 1", orgid).Group("o.patient_id").Scan(&order)
  1587. }
  1588. if partitionid > 0 {
  1589. 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.user_org_id = ? and x.partition_id = ? and x.schedule_date = o.dialysis_date and x.status = 1", orgid, partitionid).Group("o.patient_id").Scan(&order)
  1590. }
  1591. return order, err
  1592. }
  1593. func UpdatePatientDialysisSolutionOne(patientid int64, orgid int64, prescription *models.DialysisPrescription, timenow int64) error {
  1594. err = XTWriteDB().Model(&prescription).Where("patient_id = ? and user_org_id = ? and record_date = ? ", patientid, orgid, timenow).Updates(map[string]interface{}{"dialyzer": prescription.Dialyzer, "anticoagulant": prescription.Anticoagulant, "mode_id": prescription.ModeId, "dialysis_duration_hour": prescription.DialysisDurationHour, "anticoagulant_shouji": prescription.AnticoagulantShouji, "anticoagulant_weichi": prescription.AnticoagulantWeichi, "anticoagulant_zongliang": prescription.AnticoagulantZongliang, "kalium": prescription.Kalium, "sodium": prescription.Sodium, "calcium": prescription.Calcium, "dialyzer_perfusion_apparatus": prescription.DialyzerPerfusionApparatus, "blood_access": prescription.BloodAccess, "dialysate_flow": prescription.DialysateFlow, "dialysate_temperature": prescription.DialysateTemperature, "dialysis_dialyszers": prescription.DialysisDialyszers, "dialysis_irrigation": prescription.DialysisIrrigation, "plasma_separator": prescription.PlasmaSeparator, "bilirubin_adsorption_column": prescription.BilirubinAdsorptionColumn, "oxygen_uptake": prescription.OxygenUptake, "oxygen_flow": prescription.OxygenFlow, "oxygen_time": prescription.OxygenTime}).Error
  1595. return err
  1596. }
  1597. //func FindRemindAdvice(user_org_id int64, advice_name string, advice_desc string, template_id string, fre_type int64, patient_id int64, record_date int64) (advice models.DoctorAdvice, err error) {
  1598. // err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and record_date = ? and advice_name = ? and advice_desc = ? and template_id = ? and frequency_type = ? ", patient_id, user_org_id, record_date, advice_name, advice_desc, template_id, fre_type).First(&advice).Error
  1599. // return
  1600. //}
  1601. func FindRemindAdvice(user_org_id int64, advice_name string, advice_desc string, template_id string, fre_type int64, patient_id int64, record_date int64) (advice models.DoctorAdvice, err error) {
  1602. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and record_date = ? and advice_name = ? and advice_desc = ? and template_id = ? and frequency_type = ? AND advice_type = 2 ", patient_id, user_org_id, record_date, advice_name, advice_desc, template_id, fre_type).First(&advice).Error
  1603. return
  1604. }
  1605. func UpdateScheduleByDeathTime(patientid int64, shcheduledate int64) error {
  1606. err := XTWriteDB().Model(models.XtSchedule{}).Where("patient_id = ? and schedule_date>?", patientid, shcheduledate).Updates(map[string]interface{}{"status": 0}).Error
  1607. return err
  1608. }
  1609. func UpdateScheduleItemByPatientId(id int64) error {
  1610. err := XTWriteDB().Model(models.PatientScheduleTemplateItem{}).Where("patient_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  1611. return err
  1612. }