patient_service.go 57KB

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