patient_service.go 54KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. package service
  2. import (
  3. "XT_New/models"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "fmt"
  8. "github.com/jinzhu/gorm"
  9. )
  10. //GetPatientList 返回患者的列表
  11. 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) {
  12. db := readDb.Table("xt_patients as p").Where("p.status=1")
  13. if orgID > 0 {
  14. db = db.Where("p.user_org_id=?", orgID)
  15. }
  16. if len(keywords) > 0 {
  17. likeKey := "%" + keywords + "%"
  18. db = db.Where("p.name LIKE ? OR p.dialysis_no LIKE ?", likeKey, likeKey)
  19. }
  20. if schedulType > 0 {
  21. db = db.Joins("JOIN xt_schedule as s ON s.patient_id=p.id")
  22. db = db.Where("s.status=1 and s.schedule_type=?", schedulType)
  23. }
  24. if contagion > 0 {
  25. db = db.Joins("JOIN xt_patients_infectious_diseases as xpid ON xpid.patient_id=p.id")
  26. db = db.Where("xpid.disease_id=? and xpid.status=1", contagion)
  27. }
  28. if isscheduling == 1 {
  29. db = db.Where("EXISTS (?)", readDb.Table("xt_schedule as iss").Where("iss.patient_id=p.id and iss.status=1").QueryExpr())
  30. } else if isscheduling == 2 {
  31. db = db.Where("NOT EXISTS (?)", readDb.Table("xt_schedule as iss").Where("iss.patient_id=p.id and iss.status=1").QueryExpr())
  32. }
  33. if isprescription == 1 {
  34. db = db.Where("EXISTS (?)", readDb.Table("xt_dialysis_prescription as xdp").Where("xdp.patient_id=p.id and xdp.status=1").QueryExpr())
  35. } else if isprescription == 2 {
  36. db = db.Where("NOT EXISTS (?)", readDb.Table("xt_dialysis_prescription as xdp").Where("xdp.patient_id=p.id and xdp.status=1").QueryExpr())
  37. }
  38. if bindingState > 0 {
  39. db = db.Where("p.binding_state=?", bindingState)
  40. }
  41. if lapseto > 0 {
  42. db = db.Where("p.lapseto=?", lapseto)
  43. }
  44. if source > 0 {
  45. db = db.Where("p.source=?", source)
  46. }
  47. if reimbursementWay > 0 {
  48. db = db.Where("p.reimbursement_way_id = ?", reimbursementWay)
  49. }
  50. if isStartTime {
  51. db = db.Where("p.created_time>=?", startTime)
  52. }
  53. if isEndTime {
  54. db = db.Where("p.created_time<=?", endTime)
  55. }
  56. offset := (page - 1) * limit
  57. 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").Group("p.id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
  58. return
  59. }
  60. //GetAllPatientList 返回全部患者的列表
  61. func GetAllPatientList(orgID int64) (patients []*models.Patients, total int64, err error) {
  62. db := readDb.Table("xt_patients as p").Where("p.user_org_id=? and p.status=1", orgID)
  63. 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
  64. return
  65. }
  66. func GetPatientListByUpdateTime(orgID int64,syncTime int64,force int64) (patients []*models.PatientListForFace, total int64, err error) {
  67. db := readDb.Model(&models.PatientListForFace{}).Where("user_org_id=? and status=1", orgID)
  68. if force == 0 {
  69. db = db.Where("updated_time >= ?", syncTime)
  70. }
  71. err = db.Count(&total).Find(&patients).Error
  72. return
  73. }
  74. func GetPatientCount(orgID int64) (total int64) {
  75. readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1", orgID).Count(&total)
  76. return
  77. }
  78. func GetLapsetoPatientCount(orgID int64, lapseto int64) (total int64) {
  79. readDb.Model(&models.Patients{}).Where("user_org_id=? and lapseto=? and status=1", orgID, lapseto).Count(&total)
  80. return
  81. }
  82. func ChechLastDialysisNo(orgID int64) (dialysisNo int64) {
  83. var patient models.Patients
  84. err := readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=?", orgID).Order("dialysis_no desc").First(&patient).Error
  85. if err != nil {
  86. return
  87. }
  88. if patient.ID == 0 {
  89. return
  90. }
  91. dialysisNo, _ = strconv.ParseInt(patient.DialysisNo, 10, 64)
  92. return
  93. }
  94. func FindPatientByDialysisNo(orgID int64, dialysisNo string) (patient models.Patients, err error) {
  95. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and dialysis_no=?", orgID, dialysisNo).First(&patient).Error
  96. return
  97. }
  98. func FindPatientByIdCardNo(orgID int64, idCardNo string) (patient models.Patients, err error) {
  99. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and id_card_no=?", orgID, idCardNo).First(&patient).Error
  100. return
  101. }
  102. func FindPatientByMobile(orgID int64, mobile string) (patient models.Patients, err error) {
  103. err = readDb.Model(&models.Patients{}).Where("phone=? and user_org_id=? and status=1", mobile, orgID).First(&patient).Error
  104. return
  105. }
  106. func FindPatientById(orgID int64, id int64) (patient models.Patients, err error) {
  107. err = readDb.Model(&models.Patients{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  108. return
  109. }
  110. func FindPatientByIdTwo(orgID int64, id int64) (patient models.XtPatientsNew, err error) {
  111. err = readDb.Model(&models.XtPatientsNew{}).Where("blood_id = ? and user_org_id = ? and status =1", id, orgID).First(&patient).Error
  112. return
  113. }
  114. func FindPatientByIdWithDiseases(orgID int64, id int64) (patient models.Patients, err error) {
  115. 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
  116. return
  117. }
  118. func FindPatientWithDeviceByNo(orgID int64, no string, time int64) (patient models.SchedualPatient2, err error) {
  119. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  120. return db.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  121. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  122. Where("user_org_id = ? AND schedule_date = ? ", orgID, time)
  123. }).Preload("InfectiousDiseases","status = 1").Where("user_org_id=? and dialysis_no = ? and status=1", orgID, no).First(&patient).Error
  124. return
  125. }
  126. func CreatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  127. user, _ := GetSgjUserByMobild(patient.Phone)
  128. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  129. utx := writeDb.Begin()
  130. btx := writeUserDb.Begin()
  131. if user.ID == 0 {
  132. user.Mobile = patient.Phone
  133. user.Avatar = patient.Avatar
  134. user.AvatarThumb = patient.Avatar
  135. user.Birthday = patient.Birthday
  136. user.Username = patient.Name
  137. user.Gender = patient.Gender
  138. user.Sources = 11
  139. user.Introduce = patient.Remark
  140. user.Status = 1
  141. user.UpdatedTime = patient.UpdatedTime
  142. user.CreatedTime = patient.CreatedTime
  143. err = btx.Create(&user).Error
  144. if err != nil {
  145. utx.Rollback()
  146. btx.Rollback()
  147. return
  148. }
  149. }
  150. patient.UserId = user.ID
  151. if customer == nil {
  152. err = btx.Create(&models.SgjCustomer{
  153. UserOrgId: patient.UserOrgId,
  154. UserId: user.ID,
  155. Mobile: patient.Phone,
  156. Name: patient.Name,
  157. Gender: patient.Gender,
  158. Birthday: patient.Birthday,
  159. Sources: 11,
  160. Status: 1,
  161. CreatedTime: patient.CreatedTime,
  162. UpdatedTime: patient.UpdatedTime,
  163. Avatar: patient.Avatar,
  164. Remark: patient.Remark,
  165. }).Error
  166. if err != nil {
  167. utx.Rollback()
  168. btx.Rollback()
  169. return
  170. }
  171. }
  172. err = utx.Create(patient).Error
  173. if err != nil {
  174. utx.Rollback()
  175. btx.Rollback()
  176. return
  177. }
  178. var lapseto models.PatientLapseto
  179. lapseto.PatientId = patient.ID
  180. lapseto.LapsetoType = patient.Lapseto
  181. lapseto.CreatedTime = patient.CreatedTime
  182. lapseto.UpdatedTime = patient.CreatedTime
  183. lapseto.Status = 1
  184. lapseto.LapsetoTime = patient.CreatedTime
  185. err = utx.Create(&lapseto).Error
  186. if err != nil {
  187. utx.Rollback()
  188. btx.Rollback()
  189. return
  190. }
  191. if len(contagions) > 0 {
  192. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  193. insertParams := make([]string, 0)
  194. insertData := make([]interface{}, 0)
  195. for _, contagion := range contagions {
  196. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  197. insertData = append(insertData, patient.ID)
  198. insertData = append(insertData, contagion)
  199. insertData = append(insertData, 1)
  200. insertData = append(insertData, patient.CreatedTime)
  201. insertData = append(insertData, patient.UpdatedTime)
  202. }
  203. thisSQL += strings.Join(insertParams, ", ")
  204. err = utx.Exec(thisSQL, insertData...).Error
  205. if err != nil {
  206. utx.Rollback()
  207. btx.Rollback()
  208. return
  209. }
  210. }
  211. if len(diseases) > 0 {
  212. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  213. insertParams := make([]string, 0)
  214. insertData := make([]interface{}, 0)
  215. for _, disease := range diseases {
  216. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  217. insertData = append(insertData, patient.ID)
  218. insertData = append(insertData, disease)
  219. insertData = append(insertData, 1)
  220. insertData = append(insertData, patient.CreatedTime)
  221. insertData = append(insertData, patient.UpdatedTime)
  222. }
  223. thisSQL += strings.Join(insertParams, ", ")
  224. err = utx.Exec(thisSQL, insertData...).Error
  225. if err != nil {
  226. utx.Rollback()
  227. btx.Rollback()
  228. return
  229. }
  230. }
  231. utx.Commit()
  232. btx.Commit()
  233. return
  234. }
  235. func GetLastPatientData(orgid int64) (models.Patients, error) {
  236. patients := models.Patients{}
  237. err := XTReadDB().Model(&patients).Where("user_org_id = ? and status = 1", orgid).Last(&patients).Error
  238. return patients, err
  239. }
  240. func CreatePatientsNew(patientsNew *models.XtPatientsNew) error {
  241. err := XTWriteDB().Model(&patientsNew).Create(&patientsNew).Error
  242. return err
  243. }
  244. func EditPatientLapseto(patient *models.Patients, lapseto *models.PatientLapseto) (err error) {
  245. utx := writeDb.Begin()
  246. err = utx.Model(&models.Patients{}).Where("id=?", patient.ID).Update(map[string]interface{}{"Lapseto": patient.Lapseto}).Error
  247. if err != nil {
  248. utx.Rollback()
  249. return
  250. }
  251. err = utx.Create(lapseto).Error
  252. if err != nil {
  253. utx.Rollback()
  254. return
  255. }
  256. // 删除排班和排班模板信息
  257. if lapseto.LapsetoType == 2 {
  258. now := time.Now()
  259. deleteScheduleErr := utx.Model(&models.PatientSchedule{}).Where("patient_id = ? AND schedule_date >= ? AND status = 1", patient.ID, lapseto.LapsetoTime).Updates(map[string]interface{}{
  260. "status": 0,
  261. "updated_time": now.Unix(),
  262. }).Error
  263. if deleteScheduleErr != nil {
  264. utx.Rollback()
  265. err = deleteScheduleErr
  266. return
  267. }
  268. deleteSchTempItemErr := utx.Model(&models.PatientScheduleTemplateItem{}).Where("patient_id = ? AND status = 1", patient.ID).Updates(map[string]interface{}{
  269. "status": 0,
  270. "mtime": now.Unix(),
  271. }).Error
  272. if deleteSchTempItemErr != nil {
  273. utx.Rollback()
  274. err = deleteSchTempItemErr
  275. return
  276. }
  277. }
  278. utx.Commit()
  279. return
  280. }
  281. func UpdatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  282. // if len(contagions) > 0 || len(diseases) > 0 {
  283. utx := writeDb.Begin()
  284. err = utx.Save(patient).Error
  285. if err != nil {
  286. utx.Rollback()
  287. return
  288. }
  289. err = utx.Model(&models.InfectiousDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  290. fmt.Println("err", err)
  291. if err != nil {
  292. utx.Rollback()
  293. return
  294. }
  295. if len(contagions) > 0 && patient.IsInfectious == 2{
  296. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  297. insertParams := make([]string, 0)
  298. insertData := make([]interface{}, 0)
  299. for _, contagion := range contagions {
  300. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  301. insertData = append(insertData, patient.ID)
  302. insertData = append(insertData, contagion)
  303. insertData = append(insertData, 1)
  304. insertData = append(insertData, patient.CreatedTime)
  305. insertData = append(insertData, patient.UpdatedTime)
  306. }
  307. thisSQL += strings.Join(insertParams, ", ")
  308. err = utx.Exec(thisSQL, insertData...).Error
  309. if err != nil {
  310. utx.Rollback()
  311. return
  312. }
  313. }
  314. err = utx.Model(&models.ChronicDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  315. if err != nil {
  316. utx.Rollback()
  317. return
  318. }
  319. if len(diseases) > 0 {
  320. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  321. insertParams := make([]string, 0)
  322. insertData := make([]interface{}, 0)
  323. for _, disease := range diseases {
  324. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  325. insertData = append(insertData, patient.ID)
  326. insertData = append(insertData, disease)
  327. insertData = append(insertData, 1)
  328. insertData = append(insertData, patient.CreatedTime)
  329. insertData = append(insertData, patient.UpdatedTime)
  330. }
  331. thisSQL += strings.Join(insertParams, ", ")
  332. err = utx.Exec(thisSQL, insertData...).Error
  333. if err != nil {
  334. utx.Rollback()
  335. return
  336. }
  337. }
  338. utx.Commit()
  339. // } else {
  340. // err = writeDb.Save(patient).Error
  341. // }
  342. return
  343. }
  344. func UpdatepatientTwo(patientsNew *models.XtPatientsNew, id int64) error {
  345. 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,
  346. "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
  347. return err
  348. }
  349. func GetLastInfectionRecord(id int64, org_id int64, project_id int64, date int64) (inspection models.Inspection, err error) {
  350. 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
  351. return
  352. }
  353. func GetAllInfectionRecord(date int64, org_id int64, patient_id int64, project_id int64) (inspection []*models.Inspection, err error) {
  354. 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
  355. return
  356. }
  357. func GetPatientDiseases(id int64) []int64 {
  358. var dis []models.ChronicDiseases
  359. ids := make([]int64, 0)
  360. err := readDb.Model(&models.ChronicDiseases{}).Where("patient_id=? and status=1", id).Find(&dis).Error
  361. if err != nil || len(dis) == 0 {
  362. return ids
  363. }
  364. for _, item := range dis {
  365. ids = append(ids, item.DiseaseId)
  366. }
  367. return ids
  368. }
  369. func GetPatientContagions(id int64) []int64 {
  370. var cis []models.InfectiousDiseases
  371. ids := make([]int64, 0)
  372. err := readDb.Model(&models.InfectiousDiseases{}).Where("patient_id=? and status=1", id).Find(&cis).Error
  373. if err != nil || len(cis) == 0 {
  374. return ids
  375. }
  376. for _, item := range cis {
  377. ids = append(ids, item.DiseaseId)
  378. }
  379. return ids
  380. }
  381. func FindPatientDialysisSolutionByMode(orgID int64, patientID, modeId int64) (solution models.DialysisSolution, err error) {
  382. 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
  383. return
  384. }
  385. func FindPatientDialysisSolution(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  386. err = readDb.Model(&models.DialysisSolution{}).Where("id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  387. return
  388. }
  389. func FindPatientDialysisSolutionChild(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  390. err = readDb.Model(&models.DialysisSolution{}).Where("parent_id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  391. return
  392. }
  393. func CreatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  394. err = writeDb.Create(solution).Error
  395. return
  396. }
  397. func UpdatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  398. err = writeDb.Save(solution).Error
  399. return
  400. }
  401. //GetPatientDialysisSolutionList 返回患者透析方案的列表
  402. func GetPatientDialysisSolutionList(orgID int64, patientID int64, page, limit int64) (solutions []*models.DialysisSolution, total int64, err error) {
  403. offset := (page - 1) * limit
  404. db := readDb.Table("xt_dialysis_solution as ds").Where("ds.status=1")
  405. if orgID > 0 {
  406. db = db.Where("ds.user_org_id=?", orgID)
  407. }
  408. if patientID > 0 {
  409. db = db.Where("ds.patient_id=?", patientID)
  410. }
  411. db = db.Count(&total).Offset(offset).Limit(limit)
  412. err = db.Order("id desc").Find(&solutions).Error
  413. if err != nil {
  414. return
  415. }
  416. if len(solutions) > 0 {
  417. nilNameIds := make([]int64, 0)
  418. for _, solution := range solutions {
  419. if len(solution.ModeName) == 0 {
  420. nilNameIds = append(nilNameIds, solution.ModeId)
  421. }
  422. }
  423. if len(nilNameIds) > 0 {
  424. var modes []*models.TreatmentMode
  425. err = readDb.Model(&models.TreatmentMode{}).Where("id IN (?)", nilNameIds).Find(&modes).Error
  426. if err != nil {
  427. return
  428. }
  429. modesMap := make(map[int64]models.TreatmentMode, 0)
  430. for _, mode := range modes {
  431. modesMap[mode.ID] = *mode
  432. }
  433. for index, solution := range solutions {
  434. if _, exixt := modesMap[solution.ModeId]; exixt && len(solution.ModeName) == 0 {
  435. solutions[index].ModeName = modesMap[solution.ModeId].Name
  436. }
  437. }
  438. }
  439. }
  440. return
  441. }
  442. //GetPatientDryWeightAdjustList 返回患者调整干体重的列表
  443. func GetPatientDryWeightAdjustList(orgID int64, patientID int64, page int64, limit int64) (weights []*models.DryWeightAdjust, total int64, err error) {
  444. db := readDb.Table("xt_dry_weight_adjust as dwa").Where("dwa.status=1")
  445. if orgID > 0 {
  446. db = db.Where("dwa.user_org_id=?", orgID)
  447. }
  448. if patientID > 0 {
  449. db = db.Where("dwa.patient_id=?", patientID)
  450. }
  451. 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")
  452. if page > 0 && limit > 0 {
  453. offset := (page - 1) * limit
  454. db = db.Offset(offset).Limit(limit)
  455. }
  456. err = db.Find(&weights).Error
  457. return
  458. }
  459. func CreateDryWeightAdjust(m *models.DryWeightAdjust) (err error) {
  460. err = writeDb.Create(m).Error
  461. return
  462. }
  463. func FindPatientLastDryWeightAdjust(orgID int64, id int64) (weight models.DryWeightAdjust, err error) {
  464. err = readDb.Model(&models.DryWeightAdjust{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  465. return
  466. }
  467. func CreateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  468. return writeDb.Create(m).Error
  469. }
  470. func GetMaxAdviceGroupID(orgId int64) (group int64) {
  471. var advice models.DoctorAdvice
  472. err := readDb.Table("xt_doctor_advice").Where("user_org_id=?", orgId).Select("max(groupno) as groupno").First(&advice).Error
  473. if err != nil {
  474. fmt.Println(err)
  475. group = 0
  476. }
  477. group = advice.GroupNo
  478. return
  479. }
  480. func CreateGroupAdvice(orgId int64, group int64, advices []*models.GroupAdvice) (err error) {
  481. if group == 0 {
  482. group = GetMaxAdviceGroupID(orgId) + 1
  483. }
  484. tx := writeDb.Begin()
  485. defer func() {
  486. if r := recover(); r != nil {
  487. tx.Rollback()
  488. }
  489. }()
  490. for _, advice := range advices {
  491. advice.GroupNo = group
  492. if err = tx.Create(advice).Error; err != nil {
  493. tx.Rollback()
  494. return
  495. }
  496. }
  497. tx.Commit()
  498. return
  499. }
  500. func CreateMGroupAdvice(orgId int64, advices []*models.GroupAdvice, groupNo int64) (list []*models.GroupAdvice, err error) {
  501. if groupNo <= 0 {
  502. group := GetMaxAdviceGroupID(orgId)
  503. groupNo = group + 1
  504. }
  505. tx := writeDb.Begin()
  506. defer func() {
  507. if r := recover(); r != nil {
  508. tx.Rollback()
  509. }
  510. }()
  511. for _, advice := range advices {
  512. advice.GroupNo = groupNo
  513. if err = tx.Create(advice).Error; err != nil {
  514. tx.Rollback()
  515. return
  516. }
  517. list = append(list, advice)
  518. if len(advice.Children) > 0 {
  519. for _, child := range advice.Children {
  520. child.GroupNo = groupNo
  521. child.ParentId = advice.ID
  522. fmt.Println(child)
  523. if err = tx.Create(&child).Error; err != nil {
  524. tx.Rollback()
  525. return
  526. }
  527. list = append(list, child)
  528. }
  529. }
  530. }
  531. tx.Commit()
  532. return
  533. }
  534. func UpdateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  535. return writeDb.Save(m).Error
  536. }
  537. func StopGroupAdvice(orgId int64, groupNo int64, m *models.DoctorAdvice) (err error) {
  538. 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
  539. if err != nil {
  540. return
  541. }
  542. return
  543. }
  544. func StopDoctorAdvice(m *models.DoctorAdvice) (err error) {
  545. ut := writeDb.Begin()
  546. err = ut.Save(m).Error
  547. if err != nil {
  548. ut.Rollback()
  549. return
  550. }
  551. 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
  552. if err != nil {
  553. ut.Rollback()
  554. return
  555. }
  556. ut.Commit()
  557. return
  558. }
  559. func DeleteSolution(m *models.DialysisSolution) (err error) {
  560. if m.ParentId > 0 {
  561. return writeDb.Save(m).Error
  562. } else {
  563. ut := writeDb.Begin()
  564. err = ut.Save(m).Error
  565. if err != nil {
  566. ut.Rollback()
  567. return
  568. }
  569. err = ut.Model(&models.DialysisSolution{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0}).Error
  570. if err != nil {
  571. ut.Rollback()
  572. return
  573. }
  574. ut.Commit()
  575. }
  576. return
  577. }
  578. func DeleteDoctorAdvice(m *models.DoctorAdvice) (err error) {
  579. if m.ParentId > 0 {
  580. return writeDb.Save(m).Error
  581. } else {
  582. ut := writeDb.Begin()
  583. err = ut.Save(m).Error
  584. if err != nil {
  585. ut.Rollback()
  586. return
  587. }
  588. err = ut.Model(&models.DoctorAdvice{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0, "Modifier": m.Modifier}).Error
  589. if err != nil {
  590. ut.Rollback()
  591. return
  592. }
  593. ut.Commit()
  594. }
  595. return
  596. }
  597. func DeleteGroupAdvice(orgId int64, groupNo int64, admin_user_id int64) (err error) {
  598. 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
  599. if err != nil {
  600. return
  601. }
  602. return
  603. }
  604. func FindDoctorAdvice(orgID, id int64) (advice models.DoctorAdvice, err error) {
  605. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  606. return
  607. }
  608. func FindDoctorAdviceByGroupNo(orgID, groupNo int64) (advice models.DoctorAdvice, err error) {
  609. err = readDb.Model(&models.DoctorAdvice{}).Where("groupno = ? and user_org_id=? and status = 1", groupNo, orgID).First(&advice).Error
  610. return
  611. }
  612. func GetDoctorAdviceList(orgID, patientID, advice_type, stop, start, end int64, keywords string) (advices []*models.DoctorAdvices, total int64, err error) {
  613. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  614. table := UserReadDB().Table("sgj_user_admin_role as r")
  615. fmt.Print("table", table)
  616. if orgID > 0 {
  617. db = db.Where("x.user_org_id=?", orgID)
  618. }
  619. if patientID > 0 {
  620. db = db.Where("x.patient_id = ?", patientID)
  621. }
  622. if advice_type > 0 {
  623. db = db.Where("x.advice_type = ?", advice_type)
  624. } else if advice_type == 0 {
  625. db = db.Where("x.advice_type in (?)", []int{1, 3})
  626. }
  627. if stop == 1 {
  628. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  629. } else if stop == 2 {
  630. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  631. }
  632. if start != 0 {
  633. db = db.Where("x.start_time>=?", start)
  634. }
  635. if end != 0 {
  636. db = db.Where("start_time<=?", end)
  637. }
  638. if len(keywords) > 0 {
  639. likeKey := "%" + keywords + "%"
  640. db = db.Where("x.advice_name LIKE ?", likeKey)
  641. }
  642. 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
  643. fmt.Print("err", err)
  644. return
  645. }
  646. func GetDoctorAdviceListOne(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64) (advices []*models.DoctorAdvices, total int64, err error) {
  647. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  648. table := UserReadDB().Table("sgj_user_admin_role as r")
  649. fmt.Print("table", table)
  650. if orgID > 0 {
  651. db = db.Where("x.user_org_id=?", orgID)
  652. }
  653. if patientID > 0 {
  654. db = db.Where("x.patient_id = ?", patientID)
  655. }
  656. if advice_type == 1 && advice_type > 0 {
  657. db = db.Where("x.advice_type = ?", advice_type)
  658. }
  659. if advice_type == 3 && advice_type > 0 {
  660. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  661. }
  662. if stop == 1 {
  663. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  664. } else if stop == 2 {
  665. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  666. }
  667. if start != 0 {
  668. db = db.Where("x.start_time>=?", start)
  669. }
  670. if end != 0 {
  671. db = db.Where("start_time<=?", end)
  672. }
  673. if len(keywords) > 0 {
  674. likeKey := "%" + keywords + "%"
  675. db = db.Where("x.advice_name LIKE ?", likeKey)
  676. }
  677. offset := (page - 1) * limit
  678. 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
  679. fmt.Print("err", err)
  680. return
  681. }
  682. 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) {
  683. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  684. table := UserReadDB().Table("sgj_user_admin_role as r")
  685. fmt.Println(table)
  686. if orgID > 0 {
  687. db = db.Where("x.user_org_id=?", orgID)
  688. }
  689. if patientID > 0 {
  690. db = db.Where("x.patient_id = ?", patientID)
  691. }
  692. if advice_type == 1 {
  693. db = db.Where("x.advice_type = ?", advice_type)
  694. }
  695. if advice_type == 3 {
  696. db = db.Where("x.advice_type <> 1")
  697. }
  698. if stop == 1 {
  699. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  700. } else if stop == 2 {
  701. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  702. }
  703. if start != 0 {
  704. db = db.Where("x.start_time>=?", start)
  705. }
  706. if end != 0 {
  707. db = db.Where("start_time<=?", end)
  708. }
  709. if len(keywords) > 0 {
  710. likeKey := "%" + keywords + "%"
  711. db = db.Where("x.advice_name LIKE ?", likeKey)
  712. }
  713. offset := (page - 1) * limit
  714. 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
  715. fmt.Print("错误是什么", err)
  716. return
  717. }
  718. func GetDoctorAdvicePageList(orgID, patientID, advice_type, stop, start, end int64, keywords string, page, limit int64) (advices []*models.DoctorAdvice, total int64, err error) {
  719. offset := (page - 1) * limit
  720. db := readDb.Model(&models.DoctorAdvice{}).Where("status=1")
  721. if orgID > 0 {
  722. db = db.Where("user_org_id=?", orgID)
  723. }
  724. if patientID > 0 {
  725. db = db.Where("patient_id = ?", patientID)
  726. }
  727. if advice_type > 0 {
  728. db = db.Where("advice_type = ?", advice_type)
  729. }
  730. if stop == 1 {
  731. db = db.Where("(stop_state=? or execution_state=?) and parent_id=0", stop, stop)
  732. } else if stop == 2 {
  733. db = db.Where("stop_state=? and execution_state=?", stop, stop)
  734. }
  735. if start != 0 {
  736. db = db.Where("start_time>=?", start)
  737. }
  738. if end != 0 {
  739. db = db.Where("start_time<=?", end)
  740. }
  741. if len(keywords) > 0 {
  742. likeKey := "%" + keywords + "%"
  743. db = db.Where("advice_name LIKE ?", likeKey)
  744. }
  745. 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
  746. return
  747. }
  748. func CreateSubDoctorAdvice(advices []*models.DoctorAdvice) (err error) {
  749. if len(advices) > 0 {
  750. utx := writeDb.Begin()
  751. if len(advices) > 0 {
  752. 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 "
  753. insertParams := make([]string, 0)
  754. insertData := make([]interface{}, 0)
  755. for _, advice := range advices {
  756. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  757. insertData = append(insertData, advice.SingleDoseUnit)
  758. insertData = append(insertData, advice.PrescribingNumber)
  759. insertData = append(insertData, advice.PrescribingNumberUnit)
  760. insertData = append(insertData, advice.AdviceName)
  761. insertData = append(insertData, advice.AdviceDesc)
  762. insertData = append(insertData, advice.SingleDose)
  763. insertData = append(insertData, advice.CreatedTime)
  764. insertData = append(insertData, advice.UpdatedTime)
  765. insertData = append(insertData, advice.PatientId)
  766. insertData = append(insertData, advice.ParentId)
  767. insertData = append(insertData, advice.UserOrgId)
  768. insertData = append(insertData, advice.RecordDate)
  769. insertData = append(insertData, 2)
  770. insertData = append(insertData, advice.DrugSpecUnit)
  771. }
  772. thisSQL += strings.Join(insertParams, ", ")
  773. err = utx.Exec(thisSQL, insertData...).Error
  774. if err != nil {
  775. utx.Rollback()
  776. return
  777. }
  778. }
  779. utx.Commit()
  780. }
  781. return
  782. }
  783. func GetPatientDialysisRecord(orgID, patientID int64, page, limit, start, end, mode_id int64) ([]*models.PatientDialysisRecord, int64, error) {
  784. offset := (page - 1) * limit
  785. var total int64
  786. var err error
  787. var orders []*models.PatientDialysisRecord
  788. // err = readDb.Table("xt_dialysis_order as do").
  789. // Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  790. // Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  791. // Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  792. // Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  793. // 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).
  794. // Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  795. // 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
  796. db := readDb.Table("xt_dialysis_order as do").
  797. Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  798. Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  799. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  800. return readDb.Where("patient_id=? and user_org_id=? and status=1", patientID, orgID).Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  801. return readUserDb.Where("status = 1")
  802. })
  803. }).
  804. Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  805. Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  806. Preload("Device", "org_id=? and status=1", orgID).
  807. Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  808. return readUserDb.Where("org_id=? and status = 1", orgID)
  809. }).
  810. 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).
  811. Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  812. Where("do.patient_id=? and do.user_org_id=? and do.stage = 2 and do.status=1", patientID, orgID).Group("s.schedule_date")
  813. if start != 0 {
  814. db = db.Where("do.dialysis_date>=?", start)
  815. }
  816. if end != 0 {
  817. db = db.Where("do.dialysis_date<=?", end)
  818. }
  819. if mode_id > 0 {
  820. db = db.Joins("JOIN xt_dialysis_prescription as dp ON dp.record_id=do.id")
  821. db = db.Where("dp.mode_id=?", mode_id)
  822. }
  823. 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
  824. if len(orders) > 0 {
  825. ids := make([]int64, 0)
  826. for _, order := range orders {
  827. dialyzer := order.DialysisPrescription.Dialyzer
  828. ids = append(ids, dialyzer)
  829. }
  830. if len(ids) > 0 {
  831. var dialyzers []*models.DeviceNumber
  832. err = readDb.Model(&models.DeviceNumber{}).Where("id IN (?) and org_id=? and status=1", ids, orgID).Find(&dialyzers).Error
  833. if err != nil {
  834. return nil, 0, err
  835. }
  836. dialyzerMap := make(map[int64]models.DeviceNumber, 0)
  837. for _, item := range dialyzers {
  838. dialyzerMap[item.ID] = *item
  839. }
  840. for orderIndex, order := range orders {
  841. if _, exist := dialyzerMap[order.DialysisPrescription.Dialyzer]; exist {
  842. orders[orderIndex].DeviceNumber = dialyzerMap[order.DialysisPrescription.Dialyzer].Number
  843. }
  844. }
  845. }
  846. }
  847. return orders, total, err
  848. }
  849. func GetPatientTreatmentSummaryList(orgID, patientID, page, limit, start, end int64) (list []*models.TreatmentSummary, total int, err error) {
  850. offset := (page - 1) * limit
  851. fmt.Println(offset)
  852. fmt.Println(limit)
  853. db := readDb.Model(&models.TreatmentSummary{}).Where("user_org_id = ? and patient_id=?", orgID, patientID)
  854. if start != 0 {
  855. db = db.Where("assessment_date >= ?", start)
  856. }
  857. if end != 0 {
  858. db = db.Where("assessment_date <= ?", end)
  859. }
  860. db = db.Where("status=1")
  861. err = db.Count(&total).Offset(offset).Limit(limit).Order("assessment_date desc, id desc").Find(&list).Error
  862. return
  863. }
  864. func GetPatientScheduleList(orgID int64, patientID int64, page int64, limit int64, start int64) (schedules []*models.PatientSchedule, err error) {
  865. offset := (page - 1) * limit
  866. err = readDb.Table("xt_schedule as s").
  867. Preload("DeviceZone", "org_id=? and status=1", orgID).
  868. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  869. Preload("TreatmentMode", "status=1").
  870. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, start).
  871. Limit(limit).Offset(offset).Find(&schedules).Error
  872. return
  873. }
  874. func GetMonitorRecord(orgID int64, date int64, partition int64) ([]*models.MonitorDialysisSchedule, error) {
  875. var mds []*models.MonitorDialysisSchedule
  876. db := readDb.
  877. Model(&models.MonitorDialysisSchedule{}).
  878. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  879. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  880. Preload("TreatmentMode", "status = 1").
  881. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  882. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  883. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  884. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  885. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  886. Preload("DialysisOrder.DeviceNumber", "status = 1 AND org_id = ?", orgID).
  887. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  888. Where("status = 1 AND user_org_id = ?", orgID)
  889. if date != 0 {
  890. db = db.Where("schedule_date = ? ", date)
  891. }
  892. if partition != 0 {
  893. db = db.Where("partition_id = ? ", partition)
  894. }
  895. err := db.Find(&mds).Error
  896. return mds, err
  897. }
  898. func MobileGetMonitorsWithPatient(orgID int64, keyword string, page int) ([]*models.MonitorDialysisSchedule, error) {
  899. var patients []*models.Patients
  900. 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
  901. if getPatientErr != nil {
  902. return nil, getPatientErr
  903. }
  904. patientIDs := make([]int64, len(patients))
  905. for index, patient := range patients {
  906. patientIDs[index] = patient.ID
  907. }
  908. db := readDb.
  909. Model(&models.MonitorDialysisSchedule{}).
  910. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  911. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  912. Preload("TreatmentMode", "status = 1").
  913. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  914. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  915. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  916. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  917. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  918. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  919. Where("status = 1 AND user_org_id = ? AND patient_id in (?)", orgID, patientIDs)
  920. var schedules []*models.MonitorDialysisSchedule
  921. err := db.Offset(20 * (page - 1)).Limit(20).Order("schedule_date desc").Find(&schedules).Error
  922. return schedules, err
  923. }
  924. func GetPatientByKeyWord(orgID int64, keywords string) (patient []*models.Patients, err error) {
  925. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1", orgID)
  926. if len(keywords) > 0 {
  927. likekey := "%" + keywords + "%"
  928. err = db.Where("name LIKE ? OR dialysis_no LIKE ? ", likekey, likekey).Find(&patient).Error
  929. } else {
  930. err = db.Find(&patient).Error
  931. }
  932. return
  933. }
  934. func FindDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice models.DoctorAdvice, err error) {
  935. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).First(&advice).Error
  936. return
  937. }
  938. func FindOldDoctorAdvice(orgID int64, advice_id int64) (advice models.DoctorAdvice, err error) {
  939. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND user_org_id=? AND status = 1", advice_id, orgID).First(&advice).Error
  940. return
  941. }
  942. func UpdateAdviceGroupStartTime(orgID int64, groupNO int64, startTime int64, admin_user_id int64) error {
  943. now := time.Now().Unix()
  944. 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{}{
  945. "start_time": startTime,
  946. "updated_time": now,
  947. "modifier": admin_user_id,
  948. }).Error
  949. return err
  950. }
  951. func QueryPatientById(id int64) *models.Patients {
  952. var pat models.Patients
  953. err := readDb.Model(&models.Patients{}).Where("id=?", id).First(&pat).Error
  954. if err != nil {
  955. }
  956. return &pat
  957. }
  958. func FindAllDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  959. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).Find(&advice).Error
  960. return
  961. }
  962. func FindDoctorAdviceByIds(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  963. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND user_org_id = ? AND status = 1", ids, orgID).Find(&advice).Error
  964. return
  965. }
  966. func BatchDeleteDoctorAdvice(ids []string, user_id int64) (err error) {
  967. ut := writeDb.Begin()
  968. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix(), "modifier": user_id}).Error
  969. if err != nil {
  970. ut.Rollback()
  971. return
  972. }
  973. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix(), "modifier": user_id}).Error
  974. if err != nil {
  975. ut.Rollback()
  976. return
  977. }
  978. ut.Commit()
  979. return err
  980. }
  981. func FindAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  982. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND parent_id = 0", orgID, groupno).Find(&advice).Error
  983. return
  984. }
  985. func UpdateDoctorAdviceAndSubAdvice(m *models.DoctorAdvice) (err error) {
  986. ut := writeDb.Begin()
  987. err = ut.Save(m).Error
  988. if err != nil {
  989. ut.Rollback()
  990. return
  991. }
  992. 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
  993. if err != nil {
  994. ut.Rollback()
  995. return
  996. }
  997. ut.Commit()
  998. return err
  999. }
  1000. func GetAllWaitRemindPatient(org_id int64, page int64, limit int64) (total int64, patient []*models.Patients, err error) {
  1001. type Total struct {
  1002. Count int64
  1003. }
  1004. var totals Total
  1005. offset := (page - 1) * limit
  1006. err = readDb.Raw("select * from xt_patients where user_org_id = ? 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
  1007. readDb.Raw("select Count(id) as count from xt_patients where user_org_id = ? 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)
  1008. return totals.Count, patient, err
  1009. }
  1010. func UpdatePatientRemindStatus(patient_id int64, remind int64, org_id int64) (err error) {
  1011. 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
  1012. return
  1013. }
  1014. func CreatePatientWeightAdjust(m *models.SgjPatientDryweight) (err error) {
  1015. err = writeDb.Create(m).Error
  1016. return
  1017. }
  1018. func FindLastDryWeightAdjust(orgID int64, id int64) (weight models.SgjPatientDryweight, err error) {
  1019. err = readDb.Model(&models.SgjPatientDryweight{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  1020. return
  1021. }
  1022. func GetSchedualPatientByKeyWord(orgID int64, keywords string, date int64) (patient []*models.Patients, err error) {
  1023. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1024. if len(keywords) > 0 {
  1025. likekey := "%" + keywords + "%"
  1026. 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
  1027. } else {
  1028. err = db.Find(&patient).Error
  1029. }
  1030. return
  1031. }
  1032. func GetPatientScheduleOne(patientid int64, nowdate int64, orgid int64) (models.XtSchedule, error) {
  1033. schedule := models.XtSchedule{}
  1034. err := XTReadDB().Model(&schedule).Where("patient_id = ? and schedule_date = ? and user_org_id = ? and status =1", patientid, nowdate, orgid).Find(&schedule).Error
  1035. return schedule, err
  1036. }
  1037. func CreateExportPatient(patient *models.Patients, contagions []int64, org_creator int64) (err error) {
  1038. user, _ := GetSgjUserByMobild(patient.Phone)
  1039. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  1040. utx := writeDb.Begin()
  1041. btx := writeUserDb.Begin()
  1042. if user.ID == 0 {
  1043. user.Mobile = patient.Phone
  1044. user.Avatar = patient.Avatar
  1045. user.AvatarThumb = patient.Avatar
  1046. user.Birthday = patient.Birthday
  1047. user.Username = patient.Name
  1048. user.Gender = patient.Gender
  1049. user.Sources = 11
  1050. user.Introduce = patient.Remark
  1051. user.Status = 1
  1052. user.UpdatedTime = patient.UpdatedTime
  1053. user.CreatedTime = patient.CreatedTime
  1054. err = btx.Create(&user).Error
  1055. if err != nil {
  1056. utx.Rollback()
  1057. btx.Rollback()
  1058. return
  1059. }
  1060. }
  1061. patient.UserId = user.ID
  1062. if customer == nil {
  1063. err = btx.Create(&models.SgjCustomer{
  1064. UserOrgId: patient.UserOrgId,
  1065. UserId: user.ID,
  1066. Mobile: patient.Phone,
  1067. Name: patient.Name,
  1068. Gender: patient.Gender,
  1069. Birthday: patient.Birthday,
  1070. Sources: 11,
  1071. Status: 1,
  1072. CreatedTime: patient.CreatedTime,
  1073. UpdatedTime: patient.UpdatedTime,
  1074. Avatar: patient.Avatar,
  1075. Remark: patient.Remark,
  1076. }).Error
  1077. if err != nil {
  1078. utx.Rollback()
  1079. btx.Rollback()
  1080. return
  1081. }
  1082. }
  1083. err = utx.Create(patient).Error
  1084. if err != nil {
  1085. utx.Rollback()
  1086. btx.Rollback()
  1087. return
  1088. }
  1089. if patient.DryWeight > 0 {
  1090. var dryWeight models.SgjPatientDryweight
  1091. dryWeight.PatientId = patient.ID
  1092. dryWeight.UserOrgId = patient.UserOrgId
  1093. dryWeight.Status = 1
  1094. dryWeight.AdjustedValue = "/"
  1095. dryWeight.Creator = org_creator
  1096. dryWeight.UserId = org_creator
  1097. dryWeight.Ctime = time.Now().Unix()
  1098. dryWeight.Mtime = time.Now().Unix()
  1099. dryWeight.DryWeight = patient.DryWeight
  1100. dryWeight.Remakes = ""
  1101. err = utx.Create(&dryWeight).Error
  1102. }
  1103. var lapseto models.PatientLapseto
  1104. lapseto.PatientId = patient.ID
  1105. lapseto.LapsetoType = patient.Lapseto
  1106. lapseto.CreatedTime = patient.CreatedTime
  1107. lapseto.UpdatedTime = patient.CreatedTime
  1108. lapseto.Status = 1
  1109. lapseto.LapsetoTime = patient.CreatedTime
  1110. err = utx.Create(&lapseto).Error
  1111. if err != nil {
  1112. utx.Rollback()
  1113. btx.Rollback()
  1114. return
  1115. }
  1116. if len(contagions) > 0 {
  1117. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  1118. insertParams := make([]string, 0)
  1119. insertData := make([]interface{}, 0)
  1120. for _, contagion := range contagions {
  1121. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  1122. insertData = append(insertData, patient.ID)
  1123. insertData = append(insertData, contagion)
  1124. insertData = append(insertData, 1)
  1125. insertData = append(insertData, patient.CreatedTime)
  1126. insertData = append(insertData, patient.UpdatedTime)
  1127. }
  1128. thisSQL += strings.Join(insertParams, ", ")
  1129. err = utx.Exec(thisSQL, insertData...).Error
  1130. if err != nil {
  1131. utx.Rollback()
  1132. btx.Rollback()
  1133. return
  1134. }
  1135. }
  1136. patientsNew := models.XtPatientsNew{
  1137. Name: patient.Name,
  1138. Gender: patient.Gender,
  1139. Phone: patient.Phone,
  1140. IdCardNo: patient.IdCardNo,
  1141. FirstDialysisDate: patient.FirstDialysisDate,
  1142. Source: patient.Source,
  1143. Lapseto: patient.Lapseto,
  1144. IsInfectious: patient.IsInfectious,
  1145. DialysisNo: patient.DialysisNo,
  1146. Height: patient.Height,
  1147. HomeAddress: patient.HomeAddress,
  1148. IsExcelExport: 1,
  1149. BloodPatients: 1,
  1150. Status: 1,
  1151. CreatedTime: time.Now().Unix(),
  1152. UserOrgId: patient.UserOrgId,
  1153. BloodId: patient.ID,
  1154. Avatar: "https://images.shengws.com/201809182128111.png",
  1155. }
  1156. err = utx.Create(&patientsNew).Error
  1157. if err != nil {
  1158. utx.Rollback()
  1159. btx.Rollback()
  1160. return
  1161. }
  1162. utx.Commit()
  1163. btx.Commit()
  1164. return
  1165. }
  1166. func FindPatientPhoneIsExist(phone string, org_id int64) (count int64) {
  1167. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND phone = ? AND status = 1", org_id, phone).Count(&count)
  1168. return
  1169. }
  1170. func FindPatientIdCardNoIsExist(id_card_no string, org_id int64) (count int64) {
  1171. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND id_card_no = ? AND status = 1", org_id, id_card_no).Count(&count)
  1172. return
  1173. }
  1174. func CreateExportErrLog(log *models.ExportErrLog) {
  1175. writeDb.Create(&log)
  1176. return
  1177. }
  1178. func FindPatientExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1179. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 1", org_id, export_time).Find(&errLogs).Error
  1180. return
  1181. }
  1182. func CreateExportLog(log *models.ExportLog) {
  1183. writeDb.Create(&log)
  1184. }
  1185. func UpdateDoctorEditAdvice(advice models.XtDoctorAdvice, orgid int64, groupno int64, date int64, patientid int64) error {
  1186. 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
  1187. return err
  1188. }
  1189. func GetPatientsByKey(orgID int64, keywords string) (patient []*models.Patients, err error) {
  1190. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1191. if len(keywords) > 0 {
  1192. likekey := "%" + keywords + "%"
  1193. err = db.Where("(name LIKE ? OR dialysis_no LIKE ?)", likekey, likekey).Find(&patient).Error
  1194. } else {
  1195. err = db.Find(&patient).Error
  1196. }
  1197. return
  1198. }
  1199. func UpdatePatientLapseto(patientid int64, lapseto models.PatientLapseto) error {
  1200. 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
  1201. return err
  1202. }