patient_service.go 74KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  1. package service
  2. import (
  3. "XT_New/models"
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  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. fmt.Println("keyw233223322332332233232323232323223233", key)
  385. patient_info_str, _ := redis.Get(key).Result()
  386. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  387. 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
  388. if err != nil {
  389. if err == gorm.ErrRecordNotFound {
  390. return patient, nil
  391. } else {
  392. return patient, err
  393. }
  394. } else {
  395. if patient.ID > 0 {
  396. //缓存数据
  397. patient_info_json, err := json.Marshal(&patient)
  398. if err == nil {
  399. redis.Set(key, patient_info_json, time.Second*60*60*18)
  400. }
  401. }
  402. return patient, nil
  403. }
  404. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  405. json.Unmarshal([]byte(patient_info_str), &patient)
  406. return patient, nil
  407. }
  408. }
  409. func FindPatientWithDeviceByNo(orgID int64, no string, time int64) (patient models.SchedualPatient2, err error) {
  410. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  411. return db.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  412. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  413. Where("user_org_id = ? AND schedule_date = ? ", orgID, time)
  414. }).Preload("InfectiousDiseases", "status = 1").Where("user_org_id=? and dialysis_no = ? and status=1", orgID, no).First(&patient).Error
  415. return
  416. }
  417. func CreatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  418. user, _ := GetSgjUserByMobild(patient.Phone)
  419. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  420. utx := writeDb.Begin()
  421. btx := writeUserDb.Begin()
  422. if user.ID == 0 {
  423. user.Mobile = patient.Phone
  424. user.Avatar = patient.Avatar
  425. user.AvatarThumb = patient.Avatar
  426. user.Birthday = patient.Birthday
  427. user.Username = patient.Name
  428. user.Gender = patient.Gender
  429. user.Sources = 11
  430. user.Introduce = patient.Remark
  431. user.Status = 1
  432. user.UpdatedTime = patient.UpdatedTime
  433. user.CreatedTime = patient.CreatedTime
  434. err = btx.Create(&user).Error
  435. if err != nil {
  436. utx.Rollback()
  437. btx.Rollback()
  438. return
  439. }
  440. }
  441. patient.UserId = user.ID
  442. if customer == nil {
  443. err = btx.Create(&models.SgjCustomer{
  444. UserOrgId: patient.UserOrgId,
  445. UserId: user.ID,
  446. Mobile: patient.Phone,
  447. Name: patient.Name,
  448. Gender: patient.Gender,
  449. Birthday: patient.Birthday,
  450. Sources: 11,
  451. Status: 1,
  452. CreatedTime: patient.CreatedTime,
  453. UpdatedTime: patient.UpdatedTime,
  454. Avatar: patient.Avatar,
  455. Remark: patient.Remark,
  456. }).Error
  457. if err != nil {
  458. utx.Rollback()
  459. btx.Rollback()
  460. return
  461. }
  462. }
  463. err = utx.Create(patient).Error
  464. if err != nil {
  465. utx.Rollback()
  466. btx.Rollback()
  467. return
  468. }
  469. var lapseto models.PatientLapseto
  470. lapseto.PatientId = patient.ID
  471. lapseto.LapsetoType = patient.Lapseto
  472. lapseto.CreatedTime = patient.CreatedTime
  473. lapseto.UpdatedTime = patient.CreatedTime
  474. lapseto.Status = 1
  475. lapseto.LapsetoTime = patient.CreatedTime
  476. err = utx.Create(&lapseto).Error
  477. if err != nil {
  478. utx.Rollback()
  479. btx.Rollback()
  480. return
  481. }
  482. if len(contagions) > 0 {
  483. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  484. insertParams := make([]string, 0)
  485. insertData := make([]interface{}, 0)
  486. for _, contagion := range contagions {
  487. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  488. insertData = append(insertData, patient.ID)
  489. insertData = append(insertData, contagion)
  490. insertData = append(insertData, 1)
  491. insertData = append(insertData, patient.CreatedTime)
  492. insertData = append(insertData, patient.UpdatedTime)
  493. }
  494. thisSQL += strings.Join(insertParams, ", ")
  495. err = utx.Exec(thisSQL, insertData...).Error
  496. if err != nil {
  497. utx.Rollback()
  498. btx.Rollback()
  499. return
  500. }
  501. }
  502. if len(diseases) > 0 {
  503. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  504. insertParams := make([]string, 0)
  505. insertData := make([]interface{}, 0)
  506. for _, disease := range diseases {
  507. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  508. insertData = append(insertData, patient.ID)
  509. insertData = append(insertData, disease)
  510. insertData = append(insertData, 1)
  511. insertData = append(insertData, patient.CreatedTime)
  512. insertData = append(insertData, patient.UpdatedTime)
  513. }
  514. thisSQL += strings.Join(insertParams, ", ")
  515. err = utx.Exec(thisSQL, insertData...).Error
  516. if err != nil {
  517. utx.Rollback()
  518. btx.Rollback()
  519. return
  520. }
  521. }
  522. utx.Commit()
  523. btx.Commit()
  524. return
  525. }
  526. func GetLastPatientData(orgid int64) (models.Patients, error) {
  527. patients := models.Patients{}
  528. err := XTReadDB().Model(&patients).Where("user_org_id = ? and status = 1", orgid).Last(&patients).Error
  529. return patients, err
  530. }
  531. func CreatePatientsNew(patientsNew *models.XtPatientsNew) error {
  532. err := XTWriteDB().Model(&patientsNew).Create(&patientsNew).Error
  533. return err
  534. }
  535. func EditPatientLapseto(patient *models.Patients, lapseto *models.PatientLapseto) (err error) {
  536. utx := writeDb.Begin()
  537. 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
  538. //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
  539. if err != nil {
  540. utx.Rollback()
  541. return
  542. }
  543. err = utx.Create(lapseto).Error
  544. if err != nil {
  545. utx.Rollback()
  546. return
  547. }
  548. // 删除排班和排班模板信息
  549. if lapseto.LapsetoType == 2 {
  550. now := time.Now()
  551. deleteScheduleErr := utx.Model(&models.PatientSchedule{}).Where("patient_id = ? AND schedule_date >= ? AND status = 1", patient.ID, lapseto.LapsetoTime).Updates(map[string]interface{}{
  552. "status": 0,
  553. "updated_time": now.Unix(),
  554. }).Error
  555. if deleteScheduleErr != nil {
  556. utx.Rollback()
  557. err = deleteScheduleErr
  558. return
  559. }
  560. deleteSchTempItemErr := utx.Model(&models.PatientScheduleTemplateItem{}).Where("patient_id = ? AND status = 1", patient.ID).Updates(map[string]interface{}{
  561. "status": 0,
  562. "mtime": now.Unix(),
  563. }).Error
  564. if deleteSchTempItemErr != nil {
  565. utx.Rollback()
  566. err = deleteSchTempItemErr
  567. return
  568. }
  569. }
  570. utx.Commit()
  571. return
  572. }
  573. func UpdatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  574. // if len(contagions) > 0 || len(diseases) > 0 {
  575. utx := writeDb.Begin()
  576. err = utx.Save(patient).Error
  577. if err != nil {
  578. utx.Rollback()
  579. return
  580. }
  581. err = utx.Model(&models.InfectiousDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  582. fmt.Println("err", err)
  583. if err != nil {
  584. utx.Rollback()
  585. return
  586. }
  587. if len(contagions) > 0 && patient.IsInfectious == 2 {
  588. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  589. insertParams := make([]string, 0)
  590. insertData := make([]interface{}, 0)
  591. for _, contagion := range contagions {
  592. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  593. insertData = append(insertData, patient.ID)
  594. insertData = append(insertData, contagion)
  595. insertData = append(insertData, 1)
  596. insertData = append(insertData, time.Now().Unix())
  597. insertData = append(insertData, time.Now().Unix())
  598. }
  599. thisSQL += strings.Join(insertParams, ", ")
  600. err = utx.Exec(thisSQL, insertData...).Error
  601. if err != nil {
  602. utx.Rollback()
  603. return
  604. }
  605. }
  606. err = utx.Model(&models.ChronicDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  607. if err != nil {
  608. utx.Rollback()
  609. return
  610. }
  611. if len(diseases) > 0 {
  612. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  613. insertParams := make([]string, 0)
  614. insertData := make([]interface{}, 0)
  615. for _, disease := range diseases {
  616. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  617. insertData = append(insertData, patient.ID)
  618. insertData = append(insertData, disease)
  619. insertData = append(insertData, 1)
  620. insertData = append(insertData, time.Now().Unix())
  621. insertData = append(insertData, time.Now().Unix())
  622. }
  623. thisSQL += strings.Join(insertParams, ", ")
  624. err = utx.Exec(thisSQL, insertData...).Error
  625. if err != nil {
  626. utx.Rollback()
  627. return
  628. }
  629. }
  630. utx.Commit()
  631. // } else {
  632. // err = writeDb.Save(patient).Error
  633. // }
  634. return
  635. }
  636. func UpdatePatientLapseto(patientid int64, lapseto models.PatientLapseto) error {
  637. 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
  638. return err
  639. }
  640. func UpdatepatientTwo(patientsNew *models.XtPatientsNew, id int64) error {
  641. 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,
  642. "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
  643. return err
  644. }
  645. func GetLastInfectionRecord(id int64, org_id int64, project_id int64, date int64) (inspection models.Inspection, err error) {
  646. 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
  647. return
  648. }
  649. func GetAllInfectionRecord(date int64, org_id int64, patient_id int64, project_id int64) (inspection []*models.Inspection, err error) {
  650. 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
  651. return
  652. }
  653. func GetPatientDiseases(id int64) []int64 {
  654. var dis []models.ChronicDiseases
  655. ids := make([]int64, 0)
  656. err := readDb.Model(&models.ChronicDiseases{}).Where("patient_id=? and status=1", id).Find(&dis).Error
  657. if err != nil || len(dis) == 0 {
  658. return ids
  659. }
  660. for _, item := range dis {
  661. ids = append(ids, item.DiseaseId)
  662. }
  663. return ids
  664. }
  665. func GetPatientContagions(id int64) []int64 {
  666. var cis []models.InfectiousDiseases
  667. ids := make([]int64, 0)
  668. err := readDb.Model(&models.InfectiousDiseases{}).Where("patient_id=? and status=1", id).Find(&cis).Error
  669. if err != nil || len(cis) == 0 {
  670. return ids
  671. }
  672. for _, item := range cis {
  673. ids = append(ids, item.DiseaseId)
  674. }
  675. return ids
  676. }
  677. func FindPatientDialysisSolutionByMode(orgID int64, patientID, modeId int64) (solution models.DialysisSolution, err error) {
  678. 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
  679. return
  680. }
  681. func FindPatientDialysisSolution(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  682. err = readDb.Model(&models.DialysisSolution{}).Where("id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  683. return
  684. }
  685. func FindPatientDialysisSolutionChild(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  686. err = readDb.Model(&models.DialysisSolution{}).Where("parent_id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  687. return
  688. }
  689. func CreatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  690. err = writeDb.Create(solution).Error
  691. return
  692. }
  693. func UpdatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  694. err = writeDb.Save(solution).Error
  695. return
  696. }
  697. //GetPatientDialysisSolutionList 返回患者透析方案的列表
  698. func GetPatientDialysisSolutionList(orgID int64, patientID int64, page, limit int64) (solutions []*models.DialysisSolution, total int64, err error) {
  699. offset := (page - 1) * limit
  700. db := readDb.Table("xt_dialysis_solution as ds").Where("ds.status=1")
  701. if orgID > 0 {
  702. db = db.Where("ds.user_org_id=?", orgID)
  703. }
  704. if patientID > 0 {
  705. db = db.Where("ds.patient_id=?", patientID)
  706. }
  707. db = db.Count(&total).Offset(offset).Limit(limit)
  708. err = db.Order("id desc").Find(&solutions).Error
  709. if err != nil {
  710. return
  711. }
  712. if len(solutions) > 0 {
  713. nilNameIds := make([]int64, 0)
  714. for _, solution := range solutions {
  715. if len(solution.ModeName) == 0 {
  716. nilNameIds = append(nilNameIds, solution.ModeId)
  717. }
  718. }
  719. if len(nilNameIds) > 0 {
  720. var modes []*models.TreatmentMode
  721. err = readDb.Model(&models.TreatmentMode{}).Where("id IN (?)", nilNameIds).Find(&modes).Error
  722. if err != nil {
  723. return
  724. }
  725. modesMap := make(map[int64]models.TreatmentMode, 0)
  726. for _, mode := range modes {
  727. modesMap[mode.ID] = *mode
  728. }
  729. for index, solution := range solutions {
  730. if _, exixt := modesMap[solution.ModeId]; exixt && len(solution.ModeName) == 0 {
  731. solutions[index].ModeName = modesMap[solution.ModeId].Name
  732. }
  733. }
  734. }
  735. }
  736. return
  737. }
  738. //GetPatientDryWeightAdjustList 返回患者调整干体重的列表
  739. func GetPatientDryWeightAdjustList(orgID int64, patientID int64, page int64, limit int64) (weights []*models.DryWeightAdjust, total int64, err error) {
  740. db := readDb.Table("xt_dry_weight_adjust as dwa").Where("dwa.status=1")
  741. if orgID > 0 {
  742. db = db.Where("dwa.user_org_id=?", orgID)
  743. }
  744. if patientID > 0 {
  745. db = db.Where("dwa.patient_id=?", patientID)
  746. }
  747. 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")
  748. if page > 0 && limit > 0 {
  749. offset := (page - 1) * limit
  750. db = db.Offset(offset).Limit(limit)
  751. }
  752. err = db.Find(&weights).Error
  753. return
  754. }
  755. func CreateDryWeightAdjust(m *models.DryWeightAdjust) (err error) {
  756. err = writeDb.Create(m).Error
  757. return
  758. }
  759. func FindPatientLastDryWeightAdjust(orgID int64, id int64) (weight models.DryWeightAdjust, err error) {
  760. err = readDb.Model(&models.DryWeightAdjust{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  761. return
  762. }
  763. func CreateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  764. return writeDb.Create(m).Error
  765. }
  766. func GetMaxAdviceGroupID(orgId int64) (group int64) {
  767. var advice models.DoctorAdvice
  768. err := readDb.Table("xt_doctor_advice").Where("user_org_id=?", orgId).Select("max(groupno) as groupno").First(&advice).Error
  769. if err != nil {
  770. fmt.Println(err)
  771. group = 0
  772. }
  773. group = advice.GroupNo
  774. return
  775. }
  776. func CreateGroupAdvice(orgId int64, group int64, advices []*models.GroupAdvice) (err error) {
  777. if group == 0 {
  778. group = GetMaxAdviceGroupID(orgId) + 1
  779. }
  780. tx := writeDb.Begin()
  781. defer func() {
  782. if r := recover(); r != nil {
  783. tx.Rollback()
  784. }
  785. }()
  786. for _, advice := range advices {
  787. advice.GroupNo = group
  788. if err = tx.Create(advice).Error; err != nil {
  789. tx.Rollback()
  790. return
  791. }
  792. }
  793. tx.Commit()
  794. return
  795. }
  796. func CreateMGroupAdvice(orgId int64, advices []*models.GroupAdvice, groupNo int64) (list []*models.GroupAdvice, err error) {
  797. if groupNo <= 0 {
  798. group := GetMaxAdviceGroupID(orgId)
  799. groupNo = group + 1
  800. }
  801. tx := writeDb.Begin()
  802. defer func() {
  803. if r := recover(); r != nil {
  804. tx.Rollback()
  805. }
  806. }()
  807. for _, advice := range advices {
  808. advice.GroupNo = groupNo
  809. if err = tx.Create(advice).Error; err != nil {
  810. tx.Rollback()
  811. return
  812. }
  813. list = append(list, advice)
  814. if len(advice.Children) > 0 {
  815. for _, child := range advice.Children {
  816. child.GroupNo = groupNo
  817. child.ParentId = advice.ID
  818. fmt.Println(child)
  819. if err = tx.Create(&child).Error; err != nil {
  820. tx.Rollback()
  821. return
  822. }
  823. list = append(list, child)
  824. }
  825. }
  826. }
  827. tx.Commit()
  828. return
  829. }
  830. func UpdateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  831. return writeDb.Save(m).Error
  832. }
  833. func StopGroupAdvice(orgId int64, groupNo int64, m *models.DoctorAdvice) (err error) {
  834. 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
  835. if err != nil {
  836. return
  837. }
  838. return
  839. }
  840. func StopDoctorAdvice(m *models.DoctorAdvice) (err error) {
  841. ut := writeDb.Begin()
  842. err = ut.Save(m).Error
  843. if err != nil {
  844. ut.Rollback()
  845. return
  846. }
  847. 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
  848. if err != nil {
  849. ut.Rollback()
  850. return
  851. }
  852. ut.Commit()
  853. return
  854. }
  855. func DeleteSolution(m *models.DialysisSolution) (err error) {
  856. if m.ParentId > 0 {
  857. return writeDb.Save(m).Error
  858. } else {
  859. ut := writeDb.Begin()
  860. err = ut.Save(m).Error
  861. if err != nil {
  862. ut.Rollback()
  863. return
  864. }
  865. err = ut.Model(&models.DialysisSolution{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0}).Error
  866. if err != nil {
  867. ut.Rollback()
  868. return
  869. }
  870. ut.Commit()
  871. }
  872. return
  873. }
  874. func DeleteDoctorAdvice(m *models.DoctorAdvice) (err error) {
  875. if m.ParentId > 0 {
  876. return writeDb.Save(m).Error
  877. } else {
  878. ut := writeDb.Begin()
  879. err = ut.Save(m).Error
  880. if err != nil {
  881. ut.Rollback()
  882. return
  883. }
  884. 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
  885. if err != nil {
  886. ut.Rollback()
  887. return
  888. }
  889. ut.Commit()
  890. }
  891. return
  892. }
  893. func DeleteGroupAdvice(orgId int64, groupNo int64, admin_user_id int64) (err error) {
  894. 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
  895. if err != nil {
  896. return
  897. }
  898. return
  899. }
  900. func FindDoctorAdvice(orgID, id int64) (advice models.DoctorAdvice, err error) {
  901. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  902. return
  903. }
  904. func FindHisDoctorAdvice(orgID, id int64) (advice models.HisDoctorAdviceInfo, err error) {
  905. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  906. return
  907. }
  908. func FindDoctorAdviceByGroupNo(orgID, groupNo int64) (advice models.DoctorAdvice, err error) {
  909. err = readDb.Model(&models.DoctorAdvice{}).Where("groupno = ? and user_org_id=? and status = 1", groupNo, orgID).First(&advice).Error
  910. return
  911. }
  912. func GetDoctorAdviceList(orgID, patientID, advice_type, stop, start, end int64, keywords string) (advices []*models.DoctorAdvices, total int64, err error) {
  913. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  914. table := UserReadDB().Table("sgj_user_admin_role as r")
  915. fmt.Print("table", table)
  916. if orgID > 0 {
  917. db = db.Where("x.user_org_id=?", orgID)
  918. }
  919. if patientID > 0 {
  920. db = db.Where("x.patient_id = ?", patientID)
  921. }
  922. if advice_type > 0 {
  923. db = db.Where("x.advice_type = ?", advice_type)
  924. } else if advice_type == 0 {
  925. db = db.Where("x.advice_type in (?)", []int{1, 3})
  926. }
  927. if stop == 1 {
  928. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  929. } else if stop == 2 {
  930. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  931. }
  932. if start != 0 {
  933. db = db.Where("x.start_time>=?", start)
  934. }
  935. if end != 0 {
  936. db = db.Where("start_time<=?", end)
  937. }
  938. if len(keywords) > 0 {
  939. likeKey := "%" + keywords + "%"
  940. db = db.Where("x.advice_name LIKE ?", likeKey)
  941. }
  942. 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
  943. fmt.Print("err", err)
  944. return
  945. }
  946. func GetDoctorAdviceListOne(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64) (advices []*models.DoctorAdvices, total int64, err error) {
  947. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  948. table := UserReadDB().Table("sgj_user_admin_role as r")
  949. fmt.Print("table", table)
  950. if orgID > 0 {
  951. db = db.Where("x.user_org_id=?", orgID)
  952. }
  953. if patientID > 0 {
  954. db = db.Where("x.patient_id = ?", patientID)
  955. }
  956. if advice_type == 1 && advice_type > 0 {
  957. db = db.Where("x.advice_type = ?", advice_type)
  958. }
  959. if advice_type == 3 && advice_type > 0 {
  960. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  961. }
  962. if advice_type == 4 && advice_type > 0 {
  963. db = db.Where("x.advice_type = 4")
  964. }
  965. if stop == 1 {
  966. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  967. } else if stop == 2 {
  968. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  969. }
  970. if start != 0 {
  971. db = db.Where("x.start_time>=?", start)
  972. }
  973. if end != 0 {
  974. db = db.Where("start_time<=?", end)
  975. }
  976. if len(keywords) > 0 {
  977. likeKey := "%" + keywords + "%"
  978. db = db.Where("x.advice_name LIKE ?", likeKey)
  979. }
  980. offset := (page - 1) * limit
  981. 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
  982. fmt.Print("err", err)
  983. return
  984. }
  985. 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) {
  986. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  987. table := UserReadDB().Table("sgj_user_admin_role as r")
  988. fmt.Println(table)
  989. if orgID > 0 {
  990. db = db.Where("x.user_org_id=?", orgID)
  991. }
  992. if patientID > 0 {
  993. db = db.Where("x.patient_id = ?", patientID)
  994. }
  995. if advice_type == 1 {
  996. db = db.Where("x.advice_type = ?", advice_type)
  997. }
  998. if advice_type == 3 && advice_type > 0 {
  999. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1000. }
  1001. if advice_type == 4 && advice_type > 0 {
  1002. db = db.Where("x.advice_type = 4")
  1003. }
  1004. if stop == 1 {
  1005. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1006. } else if stop == 2 {
  1007. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1008. }
  1009. if start != 0 {
  1010. db = db.Where("x.start_time>=?", start)
  1011. }
  1012. if end != 0 {
  1013. db = db.Where("start_time<=?", end)
  1014. }
  1015. if len(keywords) > 0 {
  1016. likeKey := "%" + keywords + "%"
  1017. db = db.Where("x.advice_name LIKE ?", likeKey)
  1018. }
  1019. offset := (page - 1) * limit
  1020. 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
  1021. fmt.Print("错误是什么", err)
  1022. return
  1023. }
  1024. func GetDoctorAdvicePageList(orgID, patientID, advice_type, stop, start, end int64, keywords string, page, limit int64) (advices []*models.DoctorAdvice, total int64, err error) {
  1025. offset := (page - 1) * limit
  1026. db := readDb.Model(&models.DoctorAdvice{}).Where("status=1")
  1027. if orgID > 0 {
  1028. db = db.Where("user_org_id=?", orgID)
  1029. }
  1030. if patientID > 0 {
  1031. db = db.Where("patient_id = ?", patientID)
  1032. }
  1033. if advice_type > 0 {
  1034. db = db.Where("advice_type = ?", advice_type)
  1035. }
  1036. if stop == 1 {
  1037. db = db.Where("(stop_state=? or execution_state=?) and parent_id=0", stop, stop)
  1038. } else if stop == 2 {
  1039. db = db.Where("stop_state=? and execution_state=?", stop, stop)
  1040. }
  1041. if start != 0 {
  1042. db = db.Where("start_time>=?", start)
  1043. }
  1044. if end != 0 {
  1045. db = db.Where("start_time<=?", end)
  1046. }
  1047. if len(keywords) > 0 {
  1048. likeKey := "%" + keywords + "%"
  1049. db = db.Where("advice_name LIKE ?", likeKey)
  1050. }
  1051. 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
  1052. return
  1053. }
  1054. func CreateSubDoctorAdvice(advices []*models.DoctorAdvice) (err error) {
  1055. if len(advices) > 0 {
  1056. utx := writeDb.Begin()
  1057. if len(advices) > 0 {
  1058. 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 "
  1059. insertParams := make([]string, 0)
  1060. insertData := make([]interface{}, 0)
  1061. for _, advice := range advices {
  1062. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1063. insertData = append(insertData, advice.SingleDoseUnit)
  1064. insertData = append(insertData, advice.PrescribingNumber)
  1065. insertData = append(insertData, advice.PrescribingNumberUnit)
  1066. insertData = append(insertData, advice.AdviceName)
  1067. insertData = append(insertData, advice.AdviceDesc)
  1068. insertData = append(insertData, advice.SingleDose)
  1069. insertData = append(insertData, advice.CreatedTime)
  1070. insertData = append(insertData, advice.UpdatedTime)
  1071. insertData = append(insertData, advice.PatientId)
  1072. insertData = append(insertData, advice.ParentId)
  1073. insertData = append(insertData, advice.UserOrgId)
  1074. insertData = append(insertData, advice.RecordDate)
  1075. insertData = append(insertData, 2)
  1076. insertData = append(insertData, advice.DrugSpecUnit)
  1077. }
  1078. thisSQL += strings.Join(insertParams, ", ")
  1079. err = utx.Exec(thisSQL, insertData...).Error
  1080. if err != nil {
  1081. utx.Rollback()
  1082. return
  1083. }
  1084. }
  1085. utx.Commit()
  1086. }
  1087. return
  1088. }
  1089. func GetPatientDialysisRecord(orgID, patientID int64, page, limit, start, end, mode_id int64) ([]*models.PatientDialysisRecord, int64, error) {
  1090. offset := (page - 1) * limit
  1091. var total int64
  1092. var err error
  1093. var orders []*models.PatientDialysisRecord
  1094. // err = readDb.Table("xt_dialysis_order as do").
  1095. // Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1096. // Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1097. // Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1098. // Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1099. // 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).
  1100. // Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  1101. // 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
  1102. db := readDb.Table("xt_dialysis_order as do").
  1103. Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1104. Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1105. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  1106. return readDb.Where("patient_id=? and user_org_id=? and status=1", patientID, orgID).Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1107. return readUserDb.Where("status = 1")
  1108. })
  1109. }).
  1110. Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1111. Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1112. Preload("Device", "org_id=? and status=1", orgID).
  1113. Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1114. return readUserDb.Where("org_id=? and status = 1", orgID)
  1115. }).
  1116. 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).
  1117. Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  1118. Where("do.patient_id=? and do.user_org_id=? and do.stage = 2 and do.status=1", patientID, orgID).Group("s.schedule_date")
  1119. if start != 0 {
  1120. db = db.Where("do.dialysis_date>=?", start)
  1121. }
  1122. if end != 0 {
  1123. db = db.Where("do.dialysis_date<=?", end)
  1124. }
  1125. if mode_id > 0 {
  1126. 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")
  1127. db = db.Where("dp.mode_id=?", mode_id)
  1128. }
  1129. 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
  1130. if len(orders) > 0 {
  1131. ids := make([]int64, 0)
  1132. for _, order := range orders {
  1133. dialyzer := order.DialysisPrescription.Dialyzer
  1134. ids = append(ids, dialyzer)
  1135. }
  1136. if len(ids) > 0 {
  1137. var dialyzers []*models.DeviceNumber
  1138. err = readDb.Model(&models.DeviceNumber{}).Where("id IN (?) and org_id=? and status=1", ids, orgID).Find(&dialyzers).Error
  1139. if err != nil {
  1140. return nil, 0, err
  1141. }
  1142. dialyzerMap := make(map[int64]models.DeviceNumber, 0)
  1143. for _, item := range dialyzers {
  1144. dialyzerMap[item.ID] = *item
  1145. }
  1146. for orderIndex, order := range orders {
  1147. if _, exist := dialyzerMap[order.DialysisPrescription.Dialyzer]; exist {
  1148. orders[orderIndex].DeviceNumber = dialyzerMap[order.DialysisPrescription.Dialyzer].Number
  1149. }
  1150. }
  1151. }
  1152. }
  1153. return orders, total, err
  1154. }
  1155. func GetPatientTreatmentSummaryList(orgID, patientID, page, limit, start, end int64) (list []*models.TreatmentSummary, total int, err error) {
  1156. offset := (page - 1) * limit
  1157. fmt.Println(offset)
  1158. fmt.Println(limit)
  1159. db := readDb.Model(&models.TreatmentSummary{}).Where("user_org_id = ? and patient_id=?", orgID, patientID)
  1160. if start != 0 {
  1161. db = db.Where("assessment_date >= ?", start)
  1162. }
  1163. if end != 0 {
  1164. db = db.Where("assessment_date <= ?", end)
  1165. }
  1166. db = db.Where("status=1")
  1167. err = db.Count(&total).Offset(offset).Limit(limit).Order("assessment_date desc, id desc").Find(&list).Error
  1168. return
  1169. }
  1170. func GetPatientScheduleList(orgID int64, patientID int64, page int64, limit int64, start int64) (schedules []*models.PatientSchedule, err error) {
  1171. offset := (page - 1) * limit
  1172. err = readDb.Table("xt_schedule as s").
  1173. Preload("DeviceZone", "org_id=? and status=1", orgID).
  1174. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  1175. Preload("TreatmentMode", "status=1").
  1176. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, start).
  1177. Limit(limit).Offset(offset).Find(&schedules).Error
  1178. return
  1179. }
  1180. func GetMonitorRecord(orgID int64, date int64, partition int64, patientId int64) ([]*models.NewVMMonitorDialysisSchedule, error) {
  1181. var mds []*models.NewVMMonitorDialysisSchedule
  1182. if patientId == 0 {
  1183. db := readDb.
  1184. Model(&models.NewVMMonitorDialysisSchedule{}).
  1185. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1186. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  1187. Preload("TreatmentMode", "status = 1").
  1188. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, date).
  1189. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, date).
  1190. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, date).
  1191. Preload("MonitoringRecord", "status = 1 AND user_org_id = ? AND monitoring_date = ?", orgID, date).
  1192. Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, date).
  1193. Preload("DialysisOrder.DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1194. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  1195. Where("status = 1 AND user_org_id = ?", orgID)
  1196. if date != 0 {
  1197. db = db.Where("schedule_date = ? ", date)
  1198. }
  1199. if partition != 0 {
  1200. db = db.Where("partition_id = ? ", partition)
  1201. }
  1202. err := db.Find(&mds).Error
  1203. fmt.Println("err", err)
  1204. }
  1205. if patientId > 0 {
  1206. db := readDb.Table("xt_schedule as x").Where("x.status = 1")
  1207. db = db.
  1208. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1209. Preload("TreatmentMode", "status = 1").
  1210. Preload("Prescription", "status = 1 AND user_org_id = ? AND patient_id = ?", orgID, patientId).
  1211. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND patient_id = ?", orgID, patientId).
  1212. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? AND patient_id = ? ", orgID, patientId).
  1213. Preload("MonitoringRecord", "status = 1 AND user_org_id = ? AND patient_id = ?", orgID, patientId).
  1214. Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND patient_id = ?", orgID, patientId).
  1215. Preload("DialysisOrder.DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1216. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  1217. Where("status = 1 AND user_org_id = ?", orgID)
  1218. if partition != 0 {
  1219. db = db.Where("partition_id = ? ", partition)
  1220. }
  1221. db = db.Where("schedule_date <= ? ", time.Now().Unix())
  1222. db = db.Where("patient_id = ?", patientId)
  1223. db = db.Order("schedule_date desc")
  1224. err := db.Find(&mds).Error
  1225. fmt.Println("err", err)
  1226. }
  1227. return mds, err
  1228. }
  1229. func MobileGetMonitorsWithPatient(orgID int64, keyword string, page int) ([]*models.MonitorDialysisSchedule, error) {
  1230. var patients []*models.Patients
  1231. 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
  1232. if getPatientErr != nil {
  1233. return nil, getPatientErr
  1234. }
  1235. patientIDs := make([]int64, len(patients))
  1236. for index, patient := range patients {
  1237. patientIDs[index] = patient.ID
  1238. }
  1239. db := readDb.
  1240. Model(&models.MonitorDialysisSchedule{}).
  1241. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1242. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  1243. Preload("TreatmentMode", "status = 1").
  1244. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  1245. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  1246. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  1247. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  1248. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  1249. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  1250. Where("status = 1 AND user_org_id = ? AND patient_id in (?)", orgID, patientIDs)
  1251. var schedules []*models.MonitorDialysisSchedule
  1252. err := db.Offset(20 * (page - 1)).Limit(20).Order("schedule_date desc").Find(&schedules).Error
  1253. return schedules, err
  1254. }
  1255. func GetPatientByKeyWord(orgID int64, keywords string) (patient []*models.Patients, err error) {
  1256. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1", orgID)
  1257. if len(keywords) > 0 {
  1258. likekey := "%" + keywords + "%"
  1259. err = db.Where("name LIKE ? OR dialysis_no LIKE ? ", likekey, likekey).Find(&patient).Error
  1260. } else {
  1261. err = db.Find(&patient).Error
  1262. }
  1263. return
  1264. }
  1265. func FindDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice models.DoctorAdvice, err error) {
  1266. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).First(&advice).Error
  1267. return
  1268. }
  1269. func FindOldDoctorAdvice(orgID int64, advice_id int64) (advice models.DoctorAdvice, err error) {
  1270. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND user_org_id=? AND status = 1", advice_id, orgID).First(&advice).Error
  1271. return
  1272. }
  1273. func UpdateAdviceGroupStartTime(orgID int64, groupNO int64, startTime int64, admin_user_id int64) error {
  1274. now := time.Now().Unix()
  1275. 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{}{
  1276. "start_time": startTime,
  1277. "updated_time": now,
  1278. "modifier": admin_user_id,
  1279. }).Error
  1280. return err
  1281. }
  1282. func QueryPatientById(id int64) *models.Patients {
  1283. var pat models.Patients
  1284. err := readDb.Model(&models.Patients{}).Where("id=?", id).First(&pat).Error
  1285. if err != nil {
  1286. }
  1287. return &pat
  1288. }
  1289. func FindAllDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1290. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).Find(&advice).Error
  1291. return
  1292. }
  1293. func FindDoctorAdviceByIds(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1294. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND user_org_id = ? AND status = 1", ids, orgID).Find(&advice).Error
  1295. return
  1296. }
  1297. func BatchDeleteDoctorAdvice(ids []string, user_id int64) (err error) {
  1298. ut := writeDb.Begin()
  1299. 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
  1300. if err != nil {
  1301. ut.Rollback()
  1302. return
  1303. }
  1304. 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
  1305. if err != nil {
  1306. ut.Rollback()
  1307. return
  1308. }
  1309. ut.Commit()
  1310. return err
  1311. }
  1312. func FindAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1313. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND parent_id = 0", orgID, groupno).Find(&advice).Error
  1314. return
  1315. }
  1316. func UpdateDoctorAdviceAndSubAdvice(m *models.DoctorAdvice) (err error) {
  1317. ut := writeDb.Begin()
  1318. err = ut.Save(m).Error
  1319. if err != nil {
  1320. ut.Rollback()
  1321. return
  1322. }
  1323. 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
  1324. if err != nil {
  1325. ut.Rollback()
  1326. return
  1327. }
  1328. ut.Commit()
  1329. return err
  1330. }
  1331. func GetAllWaitRemindPatient(org_id int64, page int64, limit int64) (total int64, patient []*models.Patients, err error) {
  1332. type Total struct {
  1333. Count int64
  1334. }
  1335. var totals Total
  1336. offset := (page - 1) * limit
  1337. 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
  1338. 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)
  1339. return totals.Count, patient, err
  1340. }
  1341. func UpdatePatientRemindStatus(patient_id int64, remind int64, org_id int64) (err error) {
  1342. 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
  1343. return
  1344. }
  1345. func CreatePatientWeightAdjust(m *models.SgjPatientDryweight) (err error) {
  1346. err = writeDb.Create(m).Error
  1347. return
  1348. }
  1349. func FindLastDryWeightAdjust(orgID int64, id int64) (weight models.SgjPatientDryweight, err error) {
  1350. err = readDb.Model(&models.SgjPatientDryweight{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  1351. return
  1352. }
  1353. func GetSchedualPatientByKeyWord(orgID int64, keywords string, date int64) (patient []*models.Patients, err error) {
  1354. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1355. if len(keywords) > 0 {
  1356. likekey := "%" + keywords + "%"
  1357. 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
  1358. } else {
  1359. err = db.Find(&patient).Error
  1360. }
  1361. return
  1362. }
  1363. func GetPatientScheduleOne(patientid int64, nowdate int64, orgid int64) (models.XtSchedule, error) {
  1364. schedule := models.XtSchedule{}
  1365. err := XTReadDB().Model(&schedule).Where("patient_id = ? and schedule_date = ? and user_org_id = ? and status =1", patientid, nowdate, orgid).Find(&schedule).Error
  1366. return schedule, err
  1367. }
  1368. func CreateExportPatient(patient *models.Patients, contagions []int64, org_creator int64) (err error) {
  1369. user, _ := GetSgjUserByMobild(patient.Phone)
  1370. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  1371. utx := writeDb.Begin()
  1372. btx := writeUserDb.Begin()
  1373. if user.ID == 0 {
  1374. user.Mobile = patient.Phone
  1375. user.Avatar = patient.Avatar
  1376. user.AvatarThumb = patient.Avatar
  1377. user.Birthday = patient.Birthday
  1378. user.Username = patient.Name
  1379. user.Gender = patient.Gender
  1380. user.Sources = 11
  1381. user.Introduce = patient.Remark
  1382. user.Status = 1
  1383. user.UpdatedTime = patient.UpdatedTime
  1384. user.CreatedTime = patient.CreatedTime
  1385. err = btx.Create(&user).Error
  1386. if err != nil {
  1387. utx.Rollback()
  1388. btx.Rollback()
  1389. return
  1390. }
  1391. }
  1392. patient.UserId = user.ID
  1393. if customer == nil {
  1394. err = btx.Create(&models.SgjCustomer{
  1395. UserOrgId: patient.UserOrgId,
  1396. UserId: user.ID,
  1397. Mobile: patient.Phone,
  1398. Name: patient.Name,
  1399. Gender: patient.Gender,
  1400. Birthday: patient.Birthday,
  1401. Sources: 11,
  1402. Status: 1,
  1403. CreatedTime: patient.CreatedTime,
  1404. UpdatedTime: patient.UpdatedTime,
  1405. Avatar: patient.Avatar,
  1406. Remark: patient.Remark,
  1407. }).Error
  1408. if err != nil {
  1409. utx.Rollback()
  1410. btx.Rollback()
  1411. return
  1412. }
  1413. }
  1414. err = utx.Create(patient).Error
  1415. if err != nil {
  1416. utx.Rollback()
  1417. btx.Rollback()
  1418. return
  1419. }
  1420. if patient.DryWeight > 0 {
  1421. var dryWeight models.SgjPatientDryweight
  1422. dryWeight.PatientId = patient.ID
  1423. dryWeight.UserOrgId = patient.UserOrgId
  1424. dryWeight.Status = 1
  1425. dryWeight.AdjustedValue = "/"
  1426. dryWeight.Creator = org_creator
  1427. dryWeight.UserId = org_creator
  1428. dryWeight.Ctime = time.Now().Unix()
  1429. dryWeight.Mtime = time.Now().Unix()
  1430. dryWeight.DryWeight = patient.DryWeight
  1431. dryWeight.Remakes = ""
  1432. err = utx.Create(&dryWeight).Error
  1433. }
  1434. var lapseto models.PatientLapseto
  1435. lapseto.PatientId = patient.ID
  1436. lapseto.LapsetoType = patient.Lapseto
  1437. lapseto.CreatedTime = patient.CreatedTime
  1438. lapseto.UpdatedTime = patient.CreatedTime
  1439. lapseto.Status = 1
  1440. lapseto.LapsetoTime = patient.CreatedTime
  1441. err = utx.Create(&lapseto).Error
  1442. if err != nil {
  1443. utx.Rollback()
  1444. btx.Rollback()
  1445. return
  1446. }
  1447. if len(contagions) > 0 {
  1448. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  1449. insertParams := make([]string, 0)
  1450. insertData := make([]interface{}, 0)
  1451. for _, contagion := range contagions {
  1452. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  1453. insertData = append(insertData, patient.ID)
  1454. insertData = append(insertData, contagion)
  1455. insertData = append(insertData, 1)
  1456. insertData = append(insertData, patient.CreatedTime)
  1457. insertData = append(insertData, patient.UpdatedTime)
  1458. }
  1459. thisSQL += strings.Join(insertParams, ", ")
  1460. err = utx.Exec(thisSQL, insertData...).Error
  1461. if err != nil {
  1462. utx.Rollback()
  1463. btx.Rollback()
  1464. return
  1465. }
  1466. }
  1467. patientsNew := models.XtPatientsNew{
  1468. Name: patient.Name,
  1469. Gender: patient.Gender,
  1470. Phone: patient.Phone,
  1471. IdCardNo: patient.IdCardNo,
  1472. FirstDialysisDate: patient.FirstDialysisDate,
  1473. Source: patient.Source,
  1474. Lapseto: patient.Lapseto,
  1475. IsInfectious: patient.IsInfectious,
  1476. DialysisNo: patient.DialysisNo,
  1477. Height: patient.Height,
  1478. HomeAddress: patient.HomeAddress,
  1479. IsExcelExport: 1,
  1480. BloodPatients: 1,
  1481. Status: 1,
  1482. CreatedTime: time.Now().Unix(),
  1483. UserOrgId: patient.UserOrgId,
  1484. BloodId: patient.ID,
  1485. Avatar: "https://images.shengws.com/201809182128111.png",
  1486. }
  1487. err = utx.Create(&patientsNew).Error
  1488. if err != nil {
  1489. utx.Rollback()
  1490. btx.Rollback()
  1491. return
  1492. }
  1493. utx.Commit()
  1494. btx.Commit()
  1495. return
  1496. }
  1497. func FindPatientPhoneIsExist(phone string, org_id int64) (count int64) {
  1498. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND phone = ? AND status = 1", org_id, phone).Count(&count)
  1499. return
  1500. }
  1501. func FindPatientIdCardNoIsExist(id_card_no string, org_id int64) (count int64) {
  1502. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND id_card_no = ? AND status = 1", org_id, id_card_no).Count(&count)
  1503. return
  1504. }
  1505. func CreateExportErrLog(log *models.ExportErrLog) {
  1506. writeDb.Create(&log)
  1507. return
  1508. }
  1509. func FindPatientExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1510. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 1", org_id, export_time).Find(&errLogs).Error
  1511. return
  1512. }
  1513. func FindPatientExportLogOne(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1514. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 4", org_id, export_time).Find(&errLogs).Error
  1515. return
  1516. }
  1517. func FindPatientExportLogTwo(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1518. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 5", org_id, export_time).Find(&errLogs).Error
  1519. return
  1520. }
  1521. func FindPatientExportLogThree(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1522. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 6", org_id, export_time).Find(&errLogs).Error
  1523. return
  1524. }
  1525. func CreateExportLog(log *models.ExportLog) {
  1526. writeDb.Create(&log)
  1527. }
  1528. func UpdateDoctorEditAdvice(advice models.XtDoctorAdvice, orgid int64, groupno int64, date int64, patientid int64) error {
  1529. 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
  1530. return err
  1531. }
  1532. func GetPatientsByKey(orgID int64, keywords string) (patient []*models.Patients, err error) {
  1533. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1534. if len(keywords) > 0 {
  1535. likekey := "%" + keywords + "%"
  1536. err = db.Where("(name LIKE ? OR dialysis_no LIKE ?)", likekey, likekey).Find(&patient).Error
  1537. } else {
  1538. err = db.Find(&patient).Error
  1539. }
  1540. return
  1541. }
  1542. func FindAllDoctorAdviceByGoroupNoTwo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1543. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND execution_state = 2", orgID, groupno).Find(&advice).Error
  1544. return
  1545. }
  1546. func FindDoctorAdviceByIdsTwo(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1547. 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
  1548. return
  1549. }
  1550. func CreateHisGroupAdvice(orgId int64, advices []*models.HisGroupAdvice, groupNo int64) (list []*models.HisGroupAdvice, err error) {
  1551. if groupNo <= 0 {
  1552. group := GetMaxAdviceGroupID(orgId)
  1553. groupNo = group + 1
  1554. }
  1555. tx := writeDb.Begin()
  1556. defer func() {
  1557. if r := recover(); r != nil {
  1558. tx.Rollback()
  1559. }
  1560. }()
  1561. for _, advice := range advices {
  1562. advice.Groupno = groupNo
  1563. if err = tx.Create(advice).Error; err != nil {
  1564. tx.Rollback()
  1565. return
  1566. }
  1567. list = append(list, advice)
  1568. if len(advice.Children) > 0 {
  1569. for _, child := range advice.Children {
  1570. child.Groupno = groupNo
  1571. child.ParentId = advice.ID
  1572. fmt.Println(child)
  1573. if err = tx.Create(&child).Error; err != nil {
  1574. tx.Rollback()
  1575. return
  1576. }
  1577. list = append(list, child)
  1578. }
  1579. }
  1580. }
  1581. tx.Commit()
  1582. return
  1583. }
  1584. func GetDialysisCount(orgid int64, partitionid int64) (order []*models.BloodDialysisOrderCount, err error) {
  1585. db := XTReadDB().Table("xt_dialysis_order as o")
  1586. if partitionid == 0 {
  1587. 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)
  1588. }
  1589. if partitionid > 0 {
  1590. 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)
  1591. }
  1592. return order, err
  1593. }
  1594. func UpdatePatientDialysisSolutionOne(patientid int64, orgid int64, prescription *models.DialysisPrescription, timenow int64) error {
  1595. 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
  1596. return err
  1597. }
  1598. //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) {
  1599. // 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
  1600. // return
  1601. //}
  1602. 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) {
  1603. 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
  1604. return
  1605. }
  1606. func UpdateScheduleByDeathTime(patientid int64, shcheduledate int64) error {
  1607. err := XTWriteDB().Model(models.XtSchedule{}).Where("patient_id = ? and schedule_date>?", patientid, shcheduledate).Updates(map[string]interface{}{"status": 0}).Error
  1608. return err
  1609. }
  1610. func UpdateScheduleItemByPatientId(id int64) error {
  1611. err := XTWriteDB().Model(models.PatientScheduleTemplateItem{}).Where("patient_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  1612. return err
  1613. }