patientmanage_service.go 83KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810
  1. package service
  2. import (
  3. "Xcx_New/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func GetIllnessListTwo() (ills []*models.Illness, err error) {
  11. err = readUserDb.Where("status=1").Find(&ills).Error
  12. return
  13. }
  14. func GetBloodDialysisPatient(orgid int64, page int64, limit int64) (patients []*models.Patients, total int64, err error) {
  15. db := XTReadDB().Table("xt_patients_new as x").Where("x.status = 1")
  16. if orgid > 0 {
  17. db = db.Where("x.user_org_id = ?", orgid)
  18. }
  19. offset := (page - 1) * limit
  20. err = db.Count(&total).Order("x.created_time desc").Offset(offset).Limit(limit).
  21. Select("x.id,x.user_org_id,x.user_id,x.avatar,x.patient_type,x.dialysis_no,x.admission_number,x.source,x.lapseto,x.partition_id,x.bed_id,x.name,x.alias,x.gender,x.marital_status,x.id_card_no,x.birthday,x.reimbursement_way_id,x.health_care_type,x.health_care_no,x.health_care_due_date,x.height,x.blood_type,x.rh,x.health_care_due_alert_date,x.education_level,x.profession,x.phone,x.home_telephone,x.relative_phone,x.relative_relations,x.home_address,x.work_unit,x.unit_address,x.children,x.receiving_date,x.is_hospital_first_dialysis,x.first_dialysis_date,x.first_dialysis_hospital,x.predialysis_condition,x.pre_hospital_dialysis_frequency,x.pre_hospital_dialysis_times,x.hospital_first_dialysis_date,x.induction_period,x.initial_dialysis,x.total_dialysis,x.attending_doctor_id,x.head_nurse_id,x.evaluate,x.diagnose,x.remark,x.registrars_id,x.registrars,x.qr_code,x.binding_state,x.patient_complains,x.present_history,x.past_history,x.temperature,x.pulse,x.respiratory,x.sbp,x.dbp,x.status,x.created_time,x.updated_time,x.nation,x.native_place,x.age,x.blood_patients,x.slow_patients,x.member_patients,x.ecommer_patients,x.blood_id,x.slow_id,x.member_id").Find(&patients).Error
  22. fmt.Println("err是什么", err)
  23. return
  24. }
  25. func GetAllBloodDialysisPatient(orgid int64, page int64, limit int64, lapseto int64, source int64) (patients []*models.XtPatientsNew, total int64, err error) {
  26. db := XTReadDB().Table("xt_patients_new as x").Where("x.status = 1")
  27. if orgid > 0 {
  28. db = db.Where("x.user_org_id = ? and x.blood_patients = 1", orgid)
  29. }
  30. if lapseto > 0 {
  31. db = db.Where("x.lapseto = ?", lapseto)
  32. }
  33. if source > 0 {
  34. db = db.Where("x.source = ?", source)
  35. }
  36. offset := (page - 1) * limit
  37. err = db.Count(&total).Order("x.created_time desc").Offset(offset).Limit(limit).
  38. Select("x.id,x.user_org_id,x.user_id,x.avatar,x.patient_type,x.dialysis_no,x.admission_number,x.source,x.lapseto,x.partition_id,x.bed_id,x.name,x.alias,x.gender,x.marital_status,x.id_card_no,x.birthday,x.reimbursement_way_id,x.health_care_type,x.health_care_no,x.health_care_due_date,x.height,x.blood_type,x.rh,x.health_care_due_alert_date,x.education_level,x.profession,x.phone,x.home_telephone,x.relative_phone,x.relative_relations,x.home_address,x.work_unit,x.unit_address,x.children,x.receiving_date,x.is_hospital_first_dialysis,x.first_dialysis_date,x.first_dialysis_hospital,x.predialysis_condition,x.pre_hospital_dialysis_frequency,x.pre_hospital_dialysis_times,x.hospital_first_dialysis_date,x.induction_period,x.initial_dialysis,x.total_dialysis,x.attending_doctor_id,x.head_nurse_id,x.evaluate,x.diagnose,x.remark,x.registrars_id,x.registrars,x.qr_code,x.binding_state,x.patient_complains,x.present_history,x.past_history,x.temperature,x.pulse,x.respiratory,x.sbp,x.dbp,x.status,x.created_time,x.updated_time,x.nation,x.native_place,x.age,x.blood_patients,x.slow_patients,x.member_patients,x.ecommer_patients,x.blood_id,x.slow_id,x.member_id").Find(&patients).Error
  39. fmt.Println("err是什么", err)
  40. return
  41. }
  42. func GetAllSlowPatient(orgid int64, page int64, limit int64) (patients []*models.XtPatientsNew, total int64, err error) {
  43. db := XTReadDB().Table("xt_patients_new as x").Where("x.status = 1")
  44. if orgid > 0 {
  45. db = db.Where("x.user_org_id = ? and x.slow_patients = 1", orgid)
  46. }
  47. offset := (page - 1) * limit
  48. err = db.Count(&total).Order("x.created_time desc").Offset(offset).Limit(limit).
  49. Select("x.id,x.user_org_id,x.user_id,x.avatar,x.patient_type,x.dialysis_no,x.admission_number,x.source,x.lapseto,x.partition_id,x.bed_id,x.name,x.alias,x.gender,x.marital_status,x.id_card_no,x.birthday,x.reimbursement_way_id,x.health_care_type,x.health_care_no,x.health_care_due_date,x.height,x.blood_type,x.rh,x.health_care_due_alert_date,x.education_level,x.profession,x.phone,x.home_telephone,x.relative_phone,x.relative_relations,x.home_address,x.work_unit,x.unit_address,x.children,x.receiving_date,x.is_hospital_first_dialysis,x.first_dialysis_date,x.first_dialysis_hospital,x.predialysis_condition,x.pre_hospital_dialysis_frequency,x.pre_hospital_dialysis_times,x.hospital_first_dialysis_date,x.induction_period,x.initial_dialysis,x.total_dialysis,x.attending_doctor_id,x.head_nurse_id,x.evaluate,x.diagnose,x.remark,x.registrars_id,x.registrars,x.qr_code,x.binding_state,x.patient_complains,x.present_history,x.past_history,x.temperature,x.pulse,x.respiratory,x.sbp,x.dbp,x.status,x.created_time,x.updated_time,x.nation,x.native_place,x.age,x.blood_patients,x.slow_patients,x.member_patients,x.ecommer_patients,x.blood_id,x.slow_id,x.member_id").Find(&patients).Error
  50. fmt.Println("err是什么", err)
  51. return
  52. }
  53. func GetAllMemberPatient(orgid int64, page int64, limit int64) (patients []*models.XtPatientsNew, total int64, err error) {
  54. db := XTReadDB().Table("xt_patients_new as x").Where("x.status = 1")
  55. if orgid > 0 {
  56. db = db.Where("x.user_org_id = ? and x.member_patients = 1", orgid)
  57. }
  58. offset := (page - 1) * limit
  59. err = db.Count(&total).Order("x.created_time desc").Offset(offset).Limit(limit).
  60. Select("x.id,x.user_org_id,x.user_id,x.avatar,x.patient_type,x.dialysis_no,x.admission_number,x.source,x.lapseto,x.partition_id,x.bed_id,x.name,x.alias,x.gender,x.marital_status,x.id_card_no,x.birthday,x.reimbursement_way_id,x.health_care_type,x.health_care_no,x.health_care_due_date,x.height,x.blood_type,x.rh,x.health_care_due_alert_date,x.education_level,x.profession,x.phone,x.home_telephone,x.relative_phone,x.relative_relations,x.home_address,x.work_unit,x.unit_address,x.children,x.receiving_date,x.is_hospital_first_dialysis,x.first_dialysis_date,x.first_dialysis_hospital,x.predialysis_condition,x.pre_hospital_dialysis_frequency,x.pre_hospital_dialysis_times,x.hospital_first_dialysis_date,x.induction_period,x.initial_dialysis,x.total_dialysis,x.attending_doctor_id,x.head_nurse_id,x.evaluate,x.diagnose,x.remark,x.registrars_id,x.registrars,x.qr_code,x.binding_state,x.patient_complains,x.present_history,x.past_history,x.temperature,x.pulse,x.respiratory,x.sbp,x.dbp,x.status,x.created_time,x.updated_time,x.nation,x.native_place,x.age,x.blood_patients,x.slow_patients,x.member_patients,x.ecommer_patients,x.blood_id,x.slow_id,x.member_id").Find(&patients).Error
  61. fmt.Println("err是什么", err)
  62. return
  63. }
  64. func GetAllPatient(orgid int64) (patients []*models.PatientsNew, err error) {
  65. db := XTReadDB().Table("xt_patients_new as x")
  66. err = db.Raw("select id,name from xt_patients_new where user_org_id = ? and status =1", orgid).Scan(&patients).Error
  67. return
  68. }
  69. func ChechLastDialysisNoTwo(orgID int64) (dialysisNo int64) {
  70. var patient models.Patients
  71. err := readDb.Model(&models.Patients{}).Where("user_org_id=? and status = 1", orgID).Order("dialysis_no desc").First(&patient).Error
  72. if err != nil {
  73. return
  74. }
  75. if patient.ID == 0 {
  76. return
  77. }
  78. dialysisNo, _ = strconv.ParseInt(patient.DialysisNo, 10, 64)
  79. return
  80. }
  81. func GetBloodPatientInfo(orgid int64, phone string) (*models.Patients, error) {
  82. patients := models.Patients{}
  83. var err error
  84. err = XTReadDB().Model(&patients).Where("user_org_id = ? and phone = ? and status = 1", orgid, phone).Find(&patients).Error
  85. if err == gorm.ErrRecordNotFound {
  86. return nil, err
  87. }
  88. if err != nil {
  89. return nil, err
  90. }
  91. return &patients, nil
  92. }
  93. func GetSlowPatientInfo(orgid int64, phone string) (*models.CdmPatients, error) {
  94. patients := models.CdmPatients{}
  95. var err error
  96. err = PatientReadDB().Model(&patients).Where("user_org_id = ? and phone = ? and status = 1", orgid, phone).Find(&patients).Error
  97. if err == gorm.ErrRecordNotFound {
  98. return nil, err
  99. }
  100. if err != nil {
  101. return nil, err
  102. }
  103. return &patients, nil
  104. }
  105. func GetMemberPatientInfo(orgid int64, phone string) (*models.SgjUserCustomer, error) {
  106. customer := models.SgjUserCustomer{}
  107. err := UserReadDB().Model(&customer).Where("user_org_id = ? and mobile = ? and status =1", orgid, phone).Find(&customer).Error
  108. if err == gorm.ErrRecordNotFound {
  109. return nil, err
  110. }
  111. if err != nil {
  112. return nil, err
  113. }
  114. return &customer, nil
  115. }
  116. func CreateOldPatient(patients *models.Patients) error {
  117. err := XTWriteDB().Create(&patients).Error
  118. return err
  119. }
  120. func GetLastOldPatient(orgid int64) (models.Patients, error) {
  121. patients := models.Patients{}
  122. err := XTReadDB().Model(&patients).Where("user_org_id = ? and status =1", orgid).Last(&patients).Error
  123. return patients, err
  124. }
  125. func AddContagions(patienid int64, createdtime int64, updatedtime int64, contagions []int64) (err error) {
  126. utx := writeDb.Begin()
  127. if len(contagions) > 0 {
  128. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  129. insertParams := make([]string, 0)
  130. insertData := make([]interface{}, 0)
  131. for _, contagion := range contagions {
  132. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  133. insertData = append(insertData, patienid)
  134. insertData = append(insertData, contagion)
  135. insertData = append(insertData, 1)
  136. insertData = append(insertData, createdtime)
  137. insertData = append(insertData, updatedtime)
  138. }
  139. thisSQL += strings.Join(insertParams, ",")
  140. err = utx.Exec(thisSQL, insertData...).Error
  141. fmt.Println("这个错误err", err)
  142. }
  143. utx.Commit()
  144. return
  145. }
  146. func AddSlowContagions(patienid int64, createdtime int64, updatedtime int64, contagions []int64, orgid int64) (err error) {
  147. utx := PatientWriteDB().Begin()
  148. if len(contagions) > 0 {
  149. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time,user_org_id) VALUES "
  150. insertParams := make([]string, 0)
  151. insertData := make([]interface{}, 0)
  152. for _, contagion := range contagions {
  153. insertParams = append(insertParams, "(?, ?, ?, ?, ?,?)")
  154. insertData = append(insertData, patienid)
  155. insertData = append(insertData, contagion)
  156. insertData = append(insertData, 1)
  157. insertData = append(insertData, createdtime)
  158. insertData = append(insertData, updatedtime)
  159. insertData = append(insertData, orgid)
  160. }
  161. thisSQL += strings.Join(insertParams, ", ")
  162. err = utx.Exec(thisSQL, insertData...).Error
  163. if err != nil {
  164. utx.Rollback()
  165. utx.Rollback()
  166. return
  167. }
  168. }
  169. utx.Commit()
  170. return
  171. }
  172. func AddSlowDiseases(patienid int64, createdtime int64, updatedtime int64, diseases []int64, orgid int64) (err error) {
  173. utx := PatientWriteDB().Begin()
  174. if len(diseases) > 0 {
  175. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time,user_org_id) VALUES "
  176. insertParams := make([]string, 0)
  177. insertData := make([]interface{}, 0)
  178. for _, disease := range diseases {
  179. insertParams = append(insertParams, "(?, ?, ?, ?, ?,?)")
  180. insertData = append(insertData, patienid)
  181. insertData = append(insertData, disease)
  182. insertData = append(insertData, 1)
  183. insertData = append(insertData, createdtime)
  184. insertData = append(insertData, updatedtime)
  185. insertData = append(insertData, orgid)
  186. }
  187. thisSQL += strings.Join(insertParams, ", ")
  188. err = utx.Exec(thisSQL, insertData...).Error
  189. if err != nil {
  190. utx.Rollback()
  191. return
  192. }
  193. }
  194. utx.Commit()
  195. return
  196. }
  197. func CreateNewPatient(patientsNew *models.XtPatientsNew) error {
  198. err := XTWriteDB().Create(&patientsNew).Error
  199. return err
  200. }
  201. func CreateCdmPatient(cdmpatient *models.CdmPatients) error {
  202. err := PatientWriteDB().Create(&cdmpatient).Error
  203. return err
  204. }
  205. func CreateMemberPatient(customer *models.SgjUserCustomer) error {
  206. err := UserWriteDB().Create(&customer).Error
  207. return err
  208. }
  209. func GetOldCdmPatient(orgid int64) (models.CdmPatients, error) {
  210. patients := models.CdmPatients{}
  211. err := PatientReadDB().Model(&patients).Where("user_org_id = ? and status =1", orgid).Last(&patients).Error
  212. return patients, err
  213. }
  214. func GetPatientDetailTwo(id int64) (models.XtPatientsNew, error) {
  215. patients := models.XtPatientsNew{}
  216. err := XTReadDB().Where("id=? and status = 1", id).Find(&patients).Error
  217. return patients, err
  218. }
  219. func GetPatientNewId(id int64) (models.XtPatientsNew, error) {
  220. patientsNew := models.XtPatientsNew{}
  221. err := XTReadDB().Where("blood_id = ? and status = 1", id).Find(&patientsNew).Error
  222. return patientsNew, err
  223. }
  224. func GetPatientDiseasesTwo(id int64) []int64 {
  225. var dis []models.XtPatientsInfectiousDiseases
  226. ids := make([]int64, 0)
  227. err := XTReadDB().Model(&models.XtPatientsInfectiousDiseases{}).Where("patient_id = ? and status = 1", id).Find(&dis).Error
  228. if err != nil || len(dis) == 0 {
  229. return ids
  230. }
  231. for _, item := range dis {
  232. ids = append(ids, item.DiseaseId)
  233. }
  234. return ids
  235. }
  236. func GetSlowPatientDiseases(id int64) (infeciousdisease []*models.CdmPatientsInfectiousDiseases, err error) {
  237. //var dis []models.CdmPatientsInfectiousDiseases
  238. //diseases := models.CdmPatientsChronicDiseases{}
  239. //ids := make([]int64, 0)
  240. //err := PatientReadDB().Model(&diseases).Where("patient_id = ? and status =1", id).Find(&ids).Error
  241. //if err != nil || len(dis) == 0 {
  242. // return ids
  243. //}
  244. //for _, item := range dis {
  245. // ids = append(ids, item.DiseaseId)
  246. //}
  247. //return ids
  248. err = PatientReadDB().Model(&infeciousdisease).Where("patient_id = ? and status =1", id).Find(&infeciousdisease).Error
  249. return infeciousdisease, err
  250. }
  251. func GetCronicDiseases(id int64) (chronic []*models.CdmPatientsChronicDiseases, err error) {
  252. //var dis []models.CdmPatientsChronicDiseases
  253. //ids := make([]int64, 0)
  254. //err := PatientReadDB().Model(&models.CdmPatientsChronicDiseases{}).Where("patient_id = ? and status =1", id).Find(&ids).Error
  255. //if err != nil || len(dis) == 0 {
  256. // return ids
  257. //}
  258. //for _, item := range dis {
  259. // ids = append(ids, item.DiseaseId)
  260. //}
  261. //return ids
  262. err = PatientReadDB().Model(&chronic).Where("patient_id = ? and status =1", id).Find(&chronic).Error
  263. return chronic, err
  264. }
  265. func CreatePatientTwo(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  266. user, _ := GetSgjUserByMobild(patient.Phone)
  267. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  268. utx := writeDb.Begin()
  269. btx := writeUserDb.Begin()
  270. if user.ID == 0 {
  271. user.Mobile = patient.Phone
  272. user.Avatar = patient.Avatar
  273. user.AvatarThumb = patient.Avatar
  274. user.Birthday = patient.Birthday
  275. user.Username = patient.Name
  276. user.Gender = patient.Gender
  277. user.Sources = 11
  278. user.Introduce = patient.Remark
  279. user.Status = 1
  280. user.UpdatedTime = patient.UpdatedTime
  281. user.CreatedTime = patient.CreatedTime
  282. err = btx.Create(&user).Error
  283. if err != nil {
  284. utx.Rollback()
  285. btx.Rollback()
  286. return
  287. }
  288. }
  289. patient.UserId = user.ID
  290. if customer == nil {
  291. err = btx.Create(&models.SgjCustomer{
  292. UserOrgId: patient.UserOrgId,
  293. UserId: user.ID,
  294. Mobile: patient.Phone,
  295. Name: patient.Name,
  296. Gender: patient.Gender,
  297. Birthday: patient.Birthday,
  298. Sources: 11,
  299. Status: 1,
  300. CreatedTime: patient.CreatedTime,
  301. UpdatedTime: patient.UpdatedTime,
  302. Avatar: patient.Avatar,
  303. Remark: patient.Remark,
  304. }).Error
  305. if err != nil {
  306. utx.Rollback()
  307. btx.Rollback()
  308. return
  309. }
  310. }
  311. err = utx.Create(patient).Error
  312. if err != nil {
  313. utx.Rollback()
  314. btx.Rollback()
  315. return
  316. }
  317. var lapseto models.PatientLapseto
  318. lapseto.PatientId = patient.ID
  319. lapseto.LapsetoType = patient.Lapseto
  320. lapseto.CreatedTime = patient.CreatedTime
  321. lapseto.UpdatedTime = patient.CreatedTime
  322. lapseto.Status = 1
  323. lapseto.LapsetoTime = patient.CreatedTime
  324. err = utx.Create(&lapseto).Error
  325. if err != nil {
  326. utx.Rollback()
  327. btx.Rollback()
  328. return
  329. }
  330. if len(contagions) > 0 {
  331. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  332. insertParams := make([]string, 0)
  333. insertData := make([]interface{}, 0)
  334. for _, contagion := range contagions {
  335. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  336. insertData = append(insertData, patient.ID)
  337. insertData = append(insertData, contagion)
  338. insertData = append(insertData, 1)
  339. insertData = append(insertData, patient.CreatedTime)
  340. insertData = append(insertData, patient.UpdatedTime)
  341. }
  342. thisSQL += strings.Join(insertParams, ", ")
  343. err = utx.Exec(thisSQL, insertData...).Error
  344. if err != nil {
  345. utx.Rollback()
  346. btx.Rollback()
  347. return
  348. }
  349. }
  350. if len(diseases) > 0 {
  351. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  352. insertParams := make([]string, 0)
  353. insertData := make([]interface{}, 0)
  354. for _, disease := range diseases {
  355. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  356. insertData = append(insertData, patient.ID)
  357. insertData = append(insertData, disease)
  358. insertData = append(insertData, 1)
  359. insertData = append(insertData, patient.CreatedTime)
  360. insertData = append(insertData, patient.UpdatedTime)
  361. }
  362. thisSQL += strings.Join(insertParams, ", ")
  363. err = utx.Exec(thisSQL, insertData...).Error
  364. if err != nil {
  365. utx.Rollback()
  366. btx.Rollback()
  367. return
  368. }
  369. }
  370. utx.Commit()
  371. btx.Commit()
  372. return
  373. }
  374. func FindPatientByMobileFive(mobile string, orgID int64) (patient models.XtPatientsNew, err error) {
  375. err = readDb.Model(&models.Patients{}).Where("phone=? and user_org_id=? and status=1", mobile, orgID).First(&patient).Error
  376. return
  377. }
  378. func FindPatientByDialysisNoFive(orgID int64, dialysisNo string) (patient models.Patients, err error) {
  379. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and dialysis_no=?", orgID, dialysisNo).First(&patient).Error
  380. return
  381. }
  382. func FindPatientByIdCardNoFive(orgID int64, idCardNo string) (patient models.XtPatientsNew, err error) {
  383. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and id_card_no=?", orgID, idCardNo).First(&patient).Error
  384. return
  385. }
  386. func FindPatientByIdCardNoOne(orgID int64, idCardNo string, id int64) (patient models.XtPatientsNew, err error) {
  387. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and id_card_no=? and id<>?", orgID, idCardNo, id).First(&patient).Error
  388. return
  389. }
  390. func FindPatientByDialysisNoOne(orgID int64, dialysisNo string, id int64) (patient models.Patients, err error) {
  391. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and dialysis_no=? and id<>?", orgID, dialysisNo, id).First(&patient).Error
  392. return
  393. }
  394. func FindPatientByMobileOne(mobile string, orgID int64, id int64) (patient models.XtPatientsNew, err error) {
  395. //err = readDb.Model(&models.Patients{}).Where(" status=1 and user_org_id=? and phone=? and id<>?", orgID, mobile, id).First(&patient).Error
  396. //return
  397. db := readDb.Table("xt_patients_new as x")
  398. err = db.Not("phone = ?", mobile).Where("user_org_id = ? and id = ? and status =1", orgID, id).Find(&patient).Error
  399. return
  400. }
  401. func GetPatientData(phone string, orgid int64) (*models.XtPatientsNew, error) {
  402. var patientnew models.XtPatientsNew
  403. var err error
  404. err = XTReadDB().Model(&patientnew).Where("user_org_id = ? and phone = ? and status =? and blood_patients = ?", orgid, phone, 1, 1).Find(&patientnew).Error
  405. if err == gorm.ErrRecordNotFound {
  406. return nil, err
  407. }
  408. if err != nil {
  409. return nil, err
  410. }
  411. return &patientnew, nil
  412. }
  413. func GetSlowPatientData(phone string, orgid int64) (*models.CdmPatients, error) {
  414. var patientnew models.CdmPatients
  415. err = PatientReadDB().Model(&patientnew).Where("user_org_id = ? and phone = ? and status = ?", orgid, phone, 1).Find(&patientnew).Error
  416. if err == gorm.ErrRecordNotFound {
  417. return nil, err
  418. }
  419. if err != nil {
  420. return nil, err
  421. }
  422. return &patientnew, nil
  423. }
  424. func GetMemberNewPatient(phone string, orgid int64) (*models.XtPatientsNew, error) {
  425. var patientnew models.XtPatientsNew
  426. err = XTReadDB().Model(&patientnew).Where("user_org_id = ? and phone = ? and status = ? and member_patients = ?", orgid, phone, 1, 1).Find(&patientnew).Error
  427. if err == gorm.ErrRecordNotFound {
  428. return nil, err
  429. }
  430. if err != nil {
  431. return nil, err
  432. }
  433. return &patientnew, nil
  434. }
  435. func GetLastMemberPatient(orgid int64) (models.SgjUserCustomer, error) {
  436. customer := models.SgjUserCustomer{}
  437. err := UserReadDB().Model(&customer).Where("user_org_id = ? and status = 1", orgid).Last(&customer).Error
  438. return customer, err
  439. }
  440. func GetLastNewSlowPatient(phone string, orgid int64) (*models.XtPatientsNew, error) {
  441. var patientnew models.XtPatientsNew
  442. err = XTReadDB().Model(&patientnew).Where("user_org_id = ? and phone = ? and status = ? and slow_patients = ?", orgid, phone, 1, 1).Find(&patientnew).Error
  443. if err == gorm.ErrRecordNotFound {
  444. return nil, err
  445. }
  446. if err != nil {
  447. return nil, err
  448. }
  449. return &patientnew, nil
  450. }
  451. func GetNewDoctorAdvice(patientID int64, advice_type int64, start int64, end int64, limit int64, page int64, orgID int64) (doctoradvice []*models.DoctorAdvices, total int64, err error) {
  452. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  453. table := UserReadDB().Table("sgj_user_admin_role as r")
  454. tabletwo := readDb.Table("xt_dialysis_prescription as s").Where("s.status = 1")
  455. fmt.Print("table", table, tabletwo)
  456. if orgID > 0 {
  457. db = db.Where("x.user_org_id=?", orgID)
  458. }
  459. if patientID > 0 {
  460. db = db.Where("x.patient_id = ?", patientID)
  461. }
  462. if advice_type == 1 {
  463. db = db.Where("x.advice_type = ?", advice_type)
  464. }
  465. if advice_type == 3 {
  466. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  467. }
  468. if advice_type == 4 {
  469. db = db.Where("x.advice_type = ?", advice_type)
  470. }
  471. if start > 0 {
  472. db = db.Where("x.start_time>=?", start)
  473. }
  474. if end > 0 {
  475. db = db.Where("x.start_time<=?", end)
  476. }
  477. //offset := (page - 1) * limit
  478. 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,s.dialyzer_perfusion_apparatus,s.anticoagulant,s.anticoagulant_shouji,s.anticoagulant_weichi,s.anticoagulant_zongliang,s.dialysis_dialyszers,s.dialysis_irrigation, 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").Joins("left join xt_dialysis_prescription as s on s.patient_id = x.patient_id and s.record_date = x.advice_date and s.user_org_id = ?", orgID).Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&doctoradvice).Error
  479. fmt.Print("err", err)
  480. return
  481. }
  482. func GetNewDoctorAdviceOne(patientID int64, advice_type int64, start int64, end int64, limit int64, page int64, orgID int64) (doctoradvice []*models.DoctorAdvice, total int64, err error) {
  483. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  484. table := UserReadDB().Table("sgj_user_admin_role as r")
  485. fmt.Println("table", table)
  486. tabletwo := readDb.Table("xt_dialysis_prescription as s").Where("s.status = 1")
  487. fmt.Print("table", table, tabletwo)
  488. if orgID > 0 {
  489. db = db.Where("x.user_org_id=?", orgID)
  490. }
  491. if patientID > 0 {
  492. db = db.Where("x.patient_id = ?", patientID)
  493. }
  494. if advice_type == 1 {
  495. db = db.Where("x.advice_type = ?", advice_type)
  496. }
  497. if advice_type == 3 {
  498. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  499. }
  500. if advice_type == 4 {
  501. db = db.Where("x.advice_type = ?", advice_type)
  502. }
  503. if start > 0 {
  504. db = db.Where("x.start_time>=?", start)
  505. }
  506. if end > 0 {
  507. db = db.Where("x.start_time<=?", end)
  508. }
  509. offset := (page - 1) * limit
  510. 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,s.dialyzer_perfusion_apparatus,s.anticoagulant,s.anticoagulant_shouji,s.anticoagulant_weichi,s.anticoagulant_zongliang,s.dialysis_dialyszers,s.dialysis_irrigation, 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").Joins("left join xt_dialysis_prescription as s on s.patient_id = x.patient_id and s.record_date = x.advice_date and s.user_org_id = ?", orgID).Scan(&doctoradvice).Error
  511. fmt.Print("错误是什么", err)
  512. return
  513. }
  514. func GetDryWeight(patientid int64, startime int64, endtime int64, limit int64, page int64, orgId int64) (dryWeight []*models.XtPatientDryweight, total int64, err error) {
  515. db := XTReadDB().Table("xt_patient_dryweight as x").Where("x.status = 1")
  516. if orgId > 0 {
  517. db = db.Where("x.user_org_id = ?", orgId)
  518. }
  519. if patientid > 0 {
  520. db = db.Where("x.patient_id = ?", patientid)
  521. }
  522. if startime > 0 {
  523. db = db.Where("x.ctime >= ?", startime)
  524. }
  525. if endtime > 0 {
  526. db = db.Where("x.ctime <=?", endtime)
  527. }
  528. offset := (page - 1) * limit
  529. err = db.Count(&total).Order("x.ctime desc").Offset(offset).Limit(limit).Group("x.id").
  530. Select("x.id,x.dry_weight,x.creator,x.remakes,x.patient_id,x.ctime,x.adjusted_value,x.user_id,x.user_org_id,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creator").Scan(&dryWeight).Error
  531. return dryWeight, total, err
  532. }
  533. func ToSearch(orgId int64, name string) (patient []*models.XtPatientsNew, err error) {
  534. likeKey := "%" + name + "%"
  535. err = XTReadDB().Where("name like ? and user_org_id = ?", likeKey, orgId).Find(&patient).Error
  536. return patient, err
  537. }
  538. func GetCourseManagement(patientid int64, startime int64, endtime int64, limit int64, page int64, orgid int64) (patientCourse []*models.PatientCourseOfDiseases, total int64, err error) {
  539. db := XTReadDB().Table("xt_patient_course_of_disease as x").Where("x.status = 1")
  540. if patientid > 0 {
  541. db = db.Where("x.patient_id = ?", patientid)
  542. }
  543. if orgid > 0 {
  544. db = db.Where("x.org_id = ?", orgid)
  545. }
  546. if startime > 0 {
  547. db = db.Where("x.record_time >=?", startime)
  548. }
  549. if endtime > 0 {
  550. db = db.Where("x.record_time <= ?", endtime)
  551. }
  552. offset := (page - 1) * limit
  553. err = db.Count(&total).Order("x.ctime desc").Offset(offset).Limit(limit).Group("x.id").
  554. Select("x.id,x.org_id,x.patient_id,x.recorder,x.record_time,x.content,x.title,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.recorder").Scan(&patientCourse).Error
  555. return patientCourse, total, err
  556. }
  557. func DeleteCouseManagement(patientid int64) error {
  558. err := XTWriteDB().Model(models.PatientCourseOfDisease{}).Where("id = ?", patientid).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  559. return err
  560. }
  561. func GetCouseManagentDetail(id int64) (models.PatientCourseOfDiseasess, error) {
  562. disease := models.PatientCourseOfDiseasess{}
  563. db := XTReadDB().Table("xt_patient_course_of_disease as x")
  564. err := db.Select("x.id,x.org_id,x.patient_id,x.recorder,x.record_time,x.content,x.title,s.name,r.user_name").Joins("left join xt_patients as s on s.id = x.patient_id ").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.recorder").Where("x.id = ?", id).Scan(&disease).Error
  565. return disease, err
  566. }
  567. func DeleteDryWeight(id int64) error {
  568. err := XTWriteDB().Model(models.SgjPatientDryweight{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  569. return err
  570. }
  571. func GetDryWeightDetail(id int64) (models.SgjPatientDryweights, error) {
  572. dryweight := models.SgjPatientDryweights{}
  573. db := XTReadDB().Table("xt_patient_dryweight as x")
  574. err := db.Select("x.id,x.dry_weight,x.creator,x.remakes,x.patient_id,x.adjusted_value,x.user_org_id,x.user_id,x.ctime,s.name,r.user_name").Joins("left join xt_patients as s on s.id = x.patient_id").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.user_id").Where("x.id = ?", id).Scan(&dryweight).Error
  575. return dryweight, err
  576. }
  577. func GetlongDialysisrecord(patientid int64, startime int64, endtime int64, limit int64, page int64, orgid int64) (prescription []*models.XtDialysisSolution, total int64, err error) {
  578. db := XTReadDB().Table("xt_dialysis_solution as x").Where("x.status = 1")
  579. if patientid > 0 {
  580. db = db.Where("x.patient_id = ?", patientid)
  581. }
  582. if orgid > 0 {
  583. db = db.Where("x.user_org_id = ?", orgid)
  584. }
  585. if startime > 0 {
  586. db = db.Where("x.created_time >=?", startime)
  587. }
  588. if endtime > 0 {
  589. db = db.Where("x.created_time <= ?", endtime)
  590. }
  591. offset := (page - 1) * limit
  592. err = db.Count(&total).Order("x.created_time desc").Offset(offset).Limit(limit).Group("x.id").
  593. Select("x.id,x.name,x.sub_name,x.user_org_id,x.patient_id,x.parent_id,x.type,x.period,x.times,x.anticoagulant,x.anticoagulant_shouji,x.anticoagulant_weichi,x.anticoagulant_zongliang,x.anticoagulant_gaimingcheng,x.anticoagulant_gaijiliang,x.mode_name,x.mode_id,x.dialysis_duration,x.replacement_way,x.hemodialysis_machine,x.blood_filter,x.perfusion_apparatus,x.blood_flow_volume,x.dewater,x.displace_liqui,x.glucose,x.dry_weight,x.dialysate_flow,x.kalium,x.sodium,x.calcium,x.bicarbonate,x.doctor,x.first_dialysis,x.remark,x.initiate_mode,x.affirm_state,x.use_state,x.status,x.registrars_id,x.created_time,x.updated_time,x.solution_type,x.dialysate_temperature,x.conductivity,x.dialysis_duration_hour,x.dialysis_duration_minute,x.target_ultrafiltration,x.dialysate_formulation,x.dialyzer,x.replacement_total,x.dialyzer_perfusion_apparatus,x.body_fluid,x.special_medicine,x.special_medicine_other,x.displace_liqui_part,x.displace_liqui_value,x.blood_access,x.ultrafiltration,x.body_fluid_other,x.target_ktv,x.dialysis_dialyszers,x.dialysis_irrigation").Find(&prescription).Error
  594. return
  595. }
  596. func GetRescuerecord(patientid int64, startime int64, endtime int64, limit int64, page int64, orgid int64) (rescuerecord []*models.XtPatientRescueRecords, total int64, err error) {
  597. db := XTReadDB().Table("xt_patient_rescue_record as x").Where("x.status = 1")
  598. if patientid > 0 {
  599. db = db.Where("x.patient_id = ?", patientid)
  600. }
  601. if orgid > 0 {
  602. db = db.Where("x.org_id = ?", orgid)
  603. }
  604. if startime > 0 {
  605. db = db.Where("x.record_time >=?", startime)
  606. }
  607. if endtime > 0 {
  608. db = db.Where("x.record_time <= ?", endtime)
  609. }
  610. offset := (page - 1) * limit
  611. err = db.Count(&total).Order("x.record_time desc").Offset(offset).Limit(limit).Group("x.id").
  612. Select("x.id,x.org_id,x.patient_id,x.recorder,x.record_time,x.content,x.status,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.recorder").Scan(&rescuerecord).Error
  613. return
  614. }
  615. func GetScheduling(patientid int64, startime int64, endtime int64, limit int64, page int64, orgid int64, modeType int64) (schedules []*models.XtSchedules, total int64, err error) {
  616. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  617. if patientid > 0 {
  618. db = db.Where("x.patient_id = ?", patientid)
  619. }
  620. if orgid > 0 {
  621. db = db.Where("x.user_org_id = ?", orgid)
  622. }
  623. if startime > 0 {
  624. db = db.Where("x.schedule_date >=?", startime)
  625. }
  626. if endtime > 0 {
  627. db = db.Where("x.schedule_date <= ?", endtime)
  628. }
  629. if modeType > 0 {
  630. db = db.Where("x.mode_id = ?", modeType)
  631. }
  632. offset := (page - 1) * limit
  633. err = db.Count(&total).Order("x.schedule_date desc").Offset(offset).Limit(limit).Group("x.id").
  634. Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,x.status,x.created_time,x.updated_time,s.number,z.name").Joins("left join xt_device_number as s on s.id = x.bed_id").Joins("left join xt_device_zone as z on z.id = x.partition_id").Scan(&schedules).Error
  635. return
  636. }
  637. func GetEducation(patientid int64, startime int64, endtime int64, limit int64, page int64, orgid int64) (treatment []*models.XtTreatmentSummary, total int64, err error) {
  638. db := XTReadDB().Table("xt_treatment_summary as x").Where("x.status = 1")
  639. if patientid > 0 {
  640. db = db.Where("x.patient_id = ?", patientid)
  641. }
  642. if orgid > 0 {
  643. db = db.Where("x.user_org_id = ?", orgid)
  644. }
  645. if startime > 0 {
  646. db = db.Where("x.assessment_date >=?", startime)
  647. }
  648. if endtime > 0 {
  649. db = db.Where("x.assessment_date <= ?", endtime)
  650. }
  651. offset := (page - 1) * limit
  652. err = db.Count(&total).Order("x.assessment_date desc").Offset(offset).Limit(limit).Group("x.id").
  653. Select("x.id,x.user_org_id,x.patient_id,x.assessment_date,x.dialysis_order_id,x.mission,x.dialysis_summary,x.change,x.sj_nurse,x.zl_nurse,x.hd_nurse,x.xj_nurse,x.zl_doctor,x.channel_image,x.puncture,x.puncture_needle,x.puncture_direction,x.status,x.created_time,x.updated_time,x.creater,x.modifier").Scan(&treatment).Error
  654. return
  655. }
  656. func GetSchedulingDetail(id int64) (models.XtScheduless, error) {
  657. schedules := models.XtScheduless{}
  658. db := XTReadDB().Table("xt_schedule as x")
  659. err := db.Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,n.number,z.name").Joins("left join xt_device_number as n on n.id = x.bed_id").Joins("left join xt_device_zone as z on z.id = x.partition_id").Where("x.id =?", id).Scan(&schedules).Error
  660. return schedules, err
  661. }
  662. func GetSchedulingTwo(id int64) (models.XtScheduless, error) {
  663. schedules := models.XtScheduless{}
  664. db := XTReadDB().Table("xt_schedule as x")
  665. err := db.Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,z.name").Joins("left join xt_patients as z on z.id = x.patient_id").Where("x.id =?", id).Scan(&schedules).Error
  666. return schedules, err
  667. }
  668. func DeleteScheduling(id int64) error {
  669. err := XTWriteDB().Model(models.XtSchedules{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  670. return err
  671. }
  672. func GetEducationDetailOne(id int64) (models.XtTreatmentSummarys, error) {
  673. summarys := models.XtTreatmentSummarys{}
  674. db := XTReadDB().Table("xt_treatment_summary as x")
  675. err := db.Select("x.id,x.user_org_id,x.patient_id,x.assessment_date,x.dialysis_order_id,x.mission,x.dialysis_summary,x.change,x.sj_nurse,x.zl_nurse,x.hd_nurse,x.xj_nurse,x.zl_doctor,x.channel_image,x.puncture,x.puncture_needle,x.puncture_direction,x.status,x.created_time,x.creater,x.modifier,s.name").Joins("left join xt_patients as s on s.id = x.patient_id").Where("x.id = ?", id).Scan(&summarys).Error
  676. return summarys, err
  677. }
  678. func DeleteEducation(id int64) error {
  679. err := XTWriteDB().Model(models.XtTreatmentSummary{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  680. return err
  681. }
  682. func GetRescueRecordDetail(id int64) (models.XtPatientRescueRecords, error) {
  683. records := models.XtPatientRescueRecords{}
  684. db := XTReadDB().Table("xt_patient_rescue_record as x")
  685. err := db.Select("x.id,x.org_id,x.patient_id,x.recorder,x.record_time,x.content,x.status,r.user_name,s.name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.recorder").Joins("left join xt_patients as s on s.id = x.patient_id").Where("x.id=?", id).Scan(&records).Error
  686. return records, err
  687. }
  688. func DeleteRescueRecord(id int64) error {
  689. err := XTWriteDB().Model(models.XtPatientRescueRecord{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  690. return err
  691. }
  692. func ToSearchePatient(keyword string, orgId int64) (paitents []*models.XtPatientsNew, err error) {
  693. likeKey := "%" + keyword + "%"
  694. err = XTReadDB().Where("user_org_id = ? and name like ? or dialysis_no like ?", orgId, likeKey, likeKey).Find(&paitents).Error
  695. return paitents, err
  696. }
  697. func SearchallPatient(keyword string, orgid int64) (patients []*models.XtPatientsNew, err error) {
  698. db := XTReadDB().Table("xt_patients_new as x").Where("x.status = 1")
  699. if len(keyword) > 0 {
  700. likeKey := "%" + keyword + "%"
  701. db = db.Where("x.name LIKE ? OR x.dialysis_no LIKE ?", likeKey, likeKey)
  702. }
  703. if orgid > 0 {
  704. db = db.Where("x.user_org_id = ?", orgid)
  705. }
  706. err = db.Select("x.id,x.user_org_id,x.user_id,x.avatar,x.patient_type,x.dialysis_no,x.admission_number,x.source,x.lapseto,x.partition_id,x.bed_id,x.name,x.alias,x.gender,x.marital_status,x.id_card_no,x.birthday,x.reimbursement_way_id,x.health_care_type,x.health_care_no,x.health_care_due_date,x.height,x.blood_type,x.rh,x.health_care_due_alert_date,x.education_level,x.profession,x.phone,x.home_telephone,x.relative_phone,x.relative_relations,x.home_address,x.work_unit,x.unit_address,x.children,x.receiving_date,x.is_hospital_first_dialysis,x.first_dialysis_date,x.first_dialysis_hospital,x.predialysis_condition,x.pre_hospital_dialysis_frequency,x.pre_hospital_dialysis_times,x.hospital_first_dialysis_date,x.induction_period,x.initial_dialysis,x.total_dialysis,x.attending_doctor_id,x.head_nurse_id,x.evaluate,x.diagnose,x.remark,x.registrars_id,x.registrars,x.qr_code,x.binding_state,x.patient_complains,x.present_history,x.past_history,x.temperature,x.pulse,x.respiratory,x.sbp,x.dbp,x.status,x.created_time,x.updated_time,x.nation,x.native_place,x.age,x.infectious_next_record_time,x.is_infectious,x.remind_cycle,x.response_result,x.is_open_remind,x.first_treatment_date,x.dialysis_age,x.expense_kind,x.tell_phone,x.contact_name,x.blood_patients,x.slow_patients,x.member_patients,x.ecommer_patients,x.blood_id,x.slow_id,x.member_id,x.member_fistdate,x.member_patienttype,x.member_treatement,x.equitment_id").Find(&patients).Error
  707. return patients, err
  708. }
  709. func GetSearchallBlood(keyword string, orgid int64) (patients []*models.XtPatientsNew, err error) {
  710. db := XTReadDB().Table("xt_patients_new as x").Where("x.status = 1 and x.blood_patients = 1")
  711. if len(keyword) > 0 {
  712. likeKey := "%" + keyword + "%"
  713. db = db.Where("x.name LIKE ? OR x.dialysis_no LIKE ?", likeKey, likeKey)
  714. }
  715. if orgid > 0 {
  716. db = db.Where("x.user_org_id = ?", orgid)
  717. }
  718. err = db.Select("x.id,x.user_org_id,x.user_id,x.avatar,x.patient_type,x.dialysis_no,x.admission_number,x.source,x.lapseto,x.partition_id,x.bed_id,x.name,x.alias,x.gender,x.marital_status,x.id_card_no,x.birthday,x.reimbursement_way_id,x.health_care_type,x.health_care_no,x.health_care_due_date,x.height,x.blood_type,x.rh,x.health_care_due_alert_date,x.education_level,x.profession,x.phone,x.home_telephone,x.relative_phone,x.relative_relations,x.home_address,x.work_unit,x.unit_address,x.children,x.receiving_date,x.is_hospital_first_dialysis,x.first_dialysis_date,x.first_dialysis_hospital,x.predialysis_condition,x.pre_hospital_dialysis_frequency,x.pre_hospital_dialysis_times,x.hospital_first_dialysis_date,x.induction_period,x.initial_dialysis,x.total_dialysis,x.attending_doctor_id,x.head_nurse_id,x.evaluate,x.diagnose,x.remark,x.registrars_id,x.registrars,x.qr_code,x.binding_state,x.patient_complains,x.present_history,x.past_history,x.temperature,x.pulse,x.respiratory,x.sbp,x.dbp,x.status,x.created_time,x.updated_time,x.nation,x.native_place,x.age,x.infectious_next_record_time,x.is_infectious,x.remind_cycle,x.response_result,x.is_open_remind,x.first_treatment_date,x.dialysis_age,x.expense_kind,x.tell_phone,x.contact_name,x.blood_patients,x.slow_patients,x.member_patients,x.ecommer_patients,x.blood_id,x.slow_id,x.member_id,x.member_fistdate,x.member_patienttype,x.member_treatement,x.equitment_id").Find(&patients).Error
  719. return patients, err
  720. }
  721. func GetSlowSearchePatient(keyword string, orgid int64) (patients []*models.XtPatientsNew, err error) {
  722. db := XTReadDB().Table("xt_patients_new as x").Where("x.status = 1 and x.slow_patients = 1")
  723. if len(keyword) > 0 {
  724. likeKey := "%" + keyword + "%"
  725. db = db.Where("x.name LIKE ? OR x.dialysis_no LIKE ?", likeKey, likeKey)
  726. }
  727. if orgid > 0 {
  728. db = db.Where("x.user_org_id = ?", orgid)
  729. }
  730. err = db.Select("x.id,x.user_org_id,x.user_id,x.avatar,x.patient_type,x.dialysis_no,x.admission_number,x.source,x.lapseto,x.partition_id,x.bed_id,x.name,x.alias,x.gender,x.marital_status,x.id_card_no,x.birthday,x.reimbursement_way_id,x.health_care_type,x.health_care_no,x.health_care_due_date,x.height,x.blood_type,x.rh,x.health_care_due_alert_date,x.education_level,x.profession,x.phone,x.home_telephone,x.relative_phone,x.relative_relations,x.home_address,x.work_unit,x.unit_address,x.children,x.receiving_date,x.is_hospital_first_dialysis,x.first_dialysis_date,x.first_dialysis_hospital,x.predialysis_condition,x.pre_hospital_dialysis_frequency,x.pre_hospital_dialysis_times,x.hospital_first_dialysis_date,x.induction_period,x.initial_dialysis,x.total_dialysis,x.attending_doctor_id,x.head_nurse_id,x.evaluate,x.diagnose,x.remark,x.registrars_id,x.registrars,x.qr_code,x.binding_state,x.patient_complains,x.present_history,x.past_history,x.temperature,x.pulse,x.respiratory,x.sbp,x.dbp,x.status,x.created_time,x.updated_time,x.nation,x.native_place,x.age,x.infectious_next_record_time,x.is_infectious,x.remind_cycle,x.response_result,x.is_open_remind,x.first_treatment_date,x.dialysis_age,x.expense_kind,x.tell_phone,x.contact_name,x.blood_patients,x.slow_patients,x.member_patients,x.ecommer_patients,x.blood_id,x.slow_id,x.member_id,x.member_fistdate,x.member_patienttype,x.member_treatement,x.equitment_id").Find(&patients).Error
  731. return patients, err
  732. }
  733. func GetMemberSearchPatient(keyword string, orgid int64) (patients []*models.XtPatientsNew, err error) {
  734. db := XTReadDB().Table("xt_patients_new as x").Where("x.status = 1 and x.member_patients = 1")
  735. if len(keyword) > 0 {
  736. likeKey := "%" + keyword + "%"
  737. db = db.Where("x.name LIKE ? OR x.dialysis_no LIKE ?", likeKey, likeKey)
  738. }
  739. if orgid > 0 {
  740. db = db.Where("x.user_org_id = ?", orgid)
  741. }
  742. err = db.Select("x.id,x.user_org_id,x.user_id,x.avatar,x.patient_type,x.dialysis_no,x.admission_number,x.source,x.lapseto,x.partition_id,x.bed_id,x.name,x.alias,x.gender,x.marital_status,x.id_card_no,x.birthday,x.reimbursement_way_id,x.health_care_type,x.health_care_no,x.health_care_due_date,x.height,x.blood_type,x.rh,x.health_care_due_alert_date,x.education_level,x.profession,x.phone,x.home_telephone,x.relative_phone,x.relative_relations,x.home_address,x.work_unit,x.unit_address,x.children,x.receiving_date,x.is_hospital_first_dialysis,x.first_dialysis_date,x.first_dialysis_hospital,x.predialysis_condition,x.pre_hospital_dialysis_frequency,x.pre_hospital_dialysis_times,x.hospital_first_dialysis_date,x.induction_period,x.initial_dialysis,x.total_dialysis,x.attending_doctor_id,x.head_nurse_id,x.evaluate,x.diagnose,x.remark,x.registrars_id,x.registrars,x.qr_code,x.binding_state,x.patient_complains,x.present_history,x.past_history,x.temperature,x.pulse,x.respiratory,x.sbp,x.dbp,x.status,x.created_time,x.updated_time,x.nation,x.native_place,x.age,x.infectious_next_record_time,x.is_infectious,x.remind_cycle,x.response_result,x.is_open_remind,x.first_treatment_date,x.dialysis_age,x.expense_kind,x.tell_phone,x.contact_name,x.blood_patients,x.slow_patients,x.member_patients,x.ecommer_patients,x.blood_id,x.slow_id,x.member_id,x.member_fistdate,x.member_patienttype,x.member_treatement,x.equitment_id").Find(&patients).Error
  743. return patients, err
  744. }
  745. func GetLongDialysisDetail(id int64) (models.XtDialysisSolution, error) {
  746. solution := models.XtDialysisSolution{}
  747. err := XTReadDB().Model(&solution).Where("id=? and status =1", id).Find(&solution).Error
  748. return solution, err
  749. }
  750. func GetPatientName(id int64) (models.Patients, error) {
  751. patientsNew := models.Patients{}
  752. err := XTReadDB().Model(&patientsNew).Where("id=? and status = 1", id).Find(&patientsNew).Error
  753. return patientsNew, err
  754. }
  755. func DeleteLongDialysis(id int64) error {
  756. err := XTWriteDB().Model(models.XtDialysisSolution{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  757. return err
  758. }
  759. func GetDialysisrecord(patientID int64, start int64, end int64, limit int64, page int64, orgID int64, mode_id int64) ([]*models.PatientDialysisRecord, int64, error) {
  760. offset := (page - 1) * limit
  761. var total int64
  762. var err error
  763. var orders []*models.PatientDialysisRecord
  764. db := readDb.Table("xt_dialysis_order as do").
  765. Preload("MonitoringRecord", "patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).
  766. Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  767. Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  768. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  769. return readDb.Where("patient_id=? and user_org_id=? and status=1", patientID, orgID).Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  770. return readUserDb.Where("status = 1")
  771. })
  772. }).
  773. Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  774. Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  775. Preload("Device", "org_id=? and status=1", orgID).
  776. Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  777. return readUserDb.Where("org_id=? and status = 1", orgID)
  778. }).
  779. 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).
  780. Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id and s.status = 1", orgID).
  781. Where("do.patient_id=? and do.user_org_id=? and do.stage = 2 and do.status=1", patientID, orgID).Group("s.schedule_date")
  782. if start > 0 {
  783. db = db.Where("do.dialysis_date>=?", start)
  784. }
  785. if end > 0 {
  786. db = db.Where("do.dialysis_date<=?", end)
  787. }
  788. if mode_id > 0 {
  789. db = db.Joins("JOIN xt_dialysis_prescription as dp ON dp.record_id=do.id")
  790. db = db.Where("dp.mode_id=?", mode_id)
  791. }
  792. 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
  793. if len(orders) > 0 {
  794. ids := make([]int64, 0)
  795. for _, order := range orders {
  796. dialyzer := order.DialysisPrescription.Dialyzer
  797. ids = append(ids, dialyzer)
  798. }
  799. if len(ids) > 0 {
  800. var dialyzers []*models.DeviceNumber
  801. err = readDb.Model(&models.DeviceNumber{}).Where("id IN (?) and org_id=? and status=1", ids, orgID).Find(&dialyzers).Error
  802. if err != nil {
  803. return nil, 0, err
  804. }
  805. dialyzerMap := make(map[int64]models.DeviceNumber, 0)
  806. for _, item := range dialyzers {
  807. dialyzerMap[item.ID] = *item
  808. }
  809. for orderIndex, order := range orders {
  810. if _, exist := dialyzerMap[order.DialysisPrescription.Dialyzer]; exist {
  811. orders[orderIndex].DeviceNumber = dialyzerMap[order.DialysisPrescription.Dialyzer].Number
  812. }
  813. }
  814. }
  815. }
  816. return orders, total, err
  817. }
  818. func GetAllDoctorTwo(orgid int64, appid int64) (appRole []*models.SgjUserAdminRole, err error) {
  819. err = UserReadDB().Where("org_id = ? and app_id = ? and status = ? ", orgid, appid, 1).Find(&appRole).Error
  820. return appRole, err
  821. }
  822. func GetDoctorAdviceDetail(state int64, id int64, orgid int64) (advices []*models.DoctorAdvices, err error) {
  823. db := XTReadDB().Table("xt_doctor_advice as x")
  824. err = db.Order("x.id desc").Select("x.id,x.user_org_id,x.patient_id,x.advice_type,x.advice_date,x.start_time,x.advice_name,x.advice_desc,x.reminder_date,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.record_date,x.dialysis_order_id,x.check_time,x.check_state,x.drug_spec,x.drug_spec_unit,x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.template_id,x.modifier,s.name").Joins("left join xt_patients as s on s.id=x.patient_id").Where("x.start_time = ? and x.patient_id = ? and x.user_org_id = ? and x.status = 1", state, id, orgid).Scan(&advices).Error
  825. return advices, err
  826. }
  827. func DeleteManagement(groupid int64, orgid int64, patientid int64) error {
  828. err := XTWriteDB().Model(models.DoctorAdvice{}).Where("start_time =? and user_org_id = ? and patient_id =?", groupid, orgid, patientid).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  829. return err
  830. }
  831. func DeleteChild(id int64) error {
  832. err := XTWriteDB().Model(models.DoctorAdvice{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  833. return err
  834. }
  835. func DeletePatient(id int64) error {
  836. err = XTWriteDB().Model(models.DoctorAdvice{}).Where("id=? or parent_id = ?", id, id).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  837. return err
  838. }
  839. func GetNewPatient(id int64) (models.XtPatientsNew, error) {
  840. patientsNew := models.XtPatientsNew{}
  841. err := XTReadDB().Model(&patientsNew).Where("id=?", id).Find(&patientsNew).Error
  842. return patientsNew, err
  843. }
  844. func UpdatedPatient(patients models.Patients, id int64) error {
  845. err := XTWriteDB().Model(&patients).Where("id=?", id).Update(map[string]interface{}{"name": patients.Name, "gender": patients.Gender, "birthday": patients.Birthday, "age": patients.Age, "phone": patients.Phone, "lapseto": patients.Lapseto, "admission_number": patients.AdmissionNumber, "first_dialysis_date": patients.FirstDialysisDate, "diagnose": patients.Diagnose, "source": patients.Source, "dialysis_no": patients.DialysisNo, "avatar": patients.Avatar, "id_card_no": patients.IdCardNo, "is_infectious": patients.IsInfectious, "out_reason": patients.OutReason, "death_time": patients.DeathTime, "relative_phone": patients.RelativePhone}).Error
  846. return err
  847. }
  848. func UpdateXtPatientNew(patients models.XtPatientsNew, id int64) error {
  849. err := XTWriteDB().Model(&patients).Where("id=?", id).Update(map[string]interface{}{"name": patients.Name, "gender": patients.Gender, "birthday": patients.Birthday, "age": patients.Age, "phone": patients.Phone, "lapseto": patients.Lapseto, "admission_number": patients.AdmissionNumber, "first_dialysis_date": patients.FirstDialysisDate, "diagnose": patients.Diagnose, "source": patients.Source, "dialysis_no": patients.DialysisNo, "avatar": patients.Avatar, "id_card_no": patients.IdCardNo, "is_infectious": patients.IsInfectious, "out_reason": patients.OutReason, "death_time": patients.DeathTime, "relative_phone": patients.RelativePhone}).Error
  850. return err
  851. }
  852. func UpdateXtPatientNewOne(patients models.XtPatientsNew, id int64) error {
  853. err := XTWriteDB().Model(&patients).Where("id=?", id).Update(map[string]interface{}{"name": patients.Name, "gender": patients.Gender, "birthday": patients.Birthday, "age": patients.Age, "phone": patients.Phone, "lapseto": patients.Lapseto, "adminssion_number": patients.AdmissionNumber, "first_dialysis_date": patients.FirstDialysisDate, "diagnose": patients.Diagnose, "source": patients.Source, "dialysis_no": patients.DialysisNo, "avatar": patients.Avatar, "id_card_no": patients.IdCardNo, "is_infectious": patients.IsInfectious, "member_patienttype": patients.MemberPatienttype, "member_treatement": patients.MemberTreatement}).Error
  854. return err
  855. }
  856. func UpdateContagions(patientid int64) (models.XtPatientsInfectiousDiseases, error) {
  857. diseases := models.XtPatientsInfectiousDiseases{}
  858. err := XTWriteDB().Model(&diseases).Where("patient_id = ?", patientid).Update(map[string]interface{}{"status": 2, "updated_time": time.Now().Unix()}).Error
  859. return diseases, err
  860. }
  861. func UpdateCustomer(cdmpatient models.CdmPatients, id int64) error {
  862. err := PatientWriteDB().Model(&cdmpatient).Where("id=?", id).Update(map[string]interface{}{"name": cdmpatient.Name, "phone": cdmpatient.Phone, "gender": cdmpatient.Gender, "birthday": cdmpatient.Birthday, "diagnose": cdmpatient.Diagnose, "source": cdmpatient.Source, "avatar": cdmpatient.Avatar, "id_card_no": cdmpatient.IdCardNo, "updated_time": cdmpatient.UpdatedTime}).Error
  863. return err
  864. }
  865. func UpdatedCustomer(customer models.SgjUserCustomer, id int64) error {
  866. err := UserWriteDB().Model(&customer).Where("id = ?", id).Update(map[string]interface{}{"name": customer.Name, "mobile": customer.Mobile, "gender": customer.Gender, "birthday": customer.Birthday, "ill_date": customer.IllDate, "avatar": customer.Avatar, "illness_id": customer.IllnessId, "treat_type": customer.TreatType}).Error
  867. return err
  868. }
  869. func GetInspectionByOrgId(orgid int64) (*models.XtInspectionReference, error) {
  870. diseases := models.XtInspectionReference{}
  871. err := XTReadDB().Model(&diseases).Where("org_id = ? and status = 1", orgid).Find(&diseases).Error
  872. if err == gorm.ErrRecordNotFound {
  873. return nil, err
  874. }
  875. if err != nil {
  876. return nil, err
  877. }
  878. return &diseases, nil
  879. }
  880. func GetAllProjectName(date int64, orgid int64, patientid int64) (inspection []*models.XtInspection, err error) {
  881. err = XTReadDB().Model(&inspection).Where("inspect_date = ? and org_id = ? and patient_id = ? and status = 1", date, orgid, patientid).Order("inspect_date desc").Group("project_name").Find(&inspection).Error
  882. return inspection, err
  883. }
  884. func GetAllInspection(orgid int64) (inspection []*models.XtInspectionReference, err error) {
  885. err = XTReadDB().Model(&inspection).Where("org_id = ? and status = 1", orgid).Group("project_name").Find(&inspection).Error
  886. return inspection, err
  887. }
  888. func GetInspection(patientid int64, startime int64, endtime int64, limit int64, page int64, orgid int64, projectid int64) (inspection []*models.Inspection, total int64, err error) {
  889. db := XTReadDB().Table("xt_inspection as x").Where("x.status = 1")
  890. if patientid > 0 {
  891. db = db.Where("x.patient_id = ?", patientid)
  892. }
  893. if startime > 0 {
  894. db = db.Where("x.inspect_date >= ?", startime)
  895. }
  896. if endtime > 0 {
  897. db = db.Where("x.inspect_date<=?", endtime)
  898. }
  899. if orgid > 0 {
  900. db = db.Where("x.org_id = ?", orgid)
  901. }
  902. if projectid > 0 {
  903. db = db.Where("x.project_id = ?", projectid)
  904. }
  905. err = db.Count(&total).Order("x.inspect_date desc").Group("x.id").
  906. Select("x.id,x.patient_id,x.org_id,x.project_id,x.item_id,x.item_name,x.project_name,x.inspect_type,x.inspect_value,x.inspect_date,x.created_time").Find(&inspection).Error
  907. return inspection, total, err
  908. }
  909. func GetInspectionDetail(patientid int64, date int64, orgid int64, projectid int64) (inspection []*models.Inspections, err error) {
  910. db := XTReadDB().Table("xt_inspection as x").Where("x.status =1")
  911. table := XTReadDB().Table("xt_inspection_reference as r")
  912. fmt.Print("table", table)
  913. if patientid > 0 {
  914. db = db.Where("x.patient_id = ?", patientid)
  915. }
  916. if date > 0 {
  917. db = db.Where("x.inspect_date = ?", date)
  918. }
  919. if orgid > 0 {
  920. db = db.Where("x.org_id = ?", orgid)
  921. }
  922. if projectid > 0 {
  923. db = db.Where("x.project_id = ?", projectid)
  924. }
  925. err = db.Group("x.id").Select("x.id,x.patient_id,x.org_id,x.project_id,x.item_id,x.item_name,x.project_name,x.inspect_type,x.inspect_value,x.inspect_date,x.status,x.created_time,x.updated_time,r.range_type,r.range_min,r.range_max,r.range_value,r.range_options,r.unit").Joins("left join xt_inspection_reference as r on (r.item_id = x.item_id AND r.org_id > 0) OR ( x.item_id = r.id AND r.org_id = 0) ").Scan(&inspection).Error
  926. return inspection, err
  927. }
  928. func CreateFeedBack(feedback *models.XtPatientFeedback) error {
  929. err := XTWriteDB().Model(&feedback).Create(&feedback).Error
  930. return err
  931. }
  932. func DeleteInspection(date int64, orgid int64) error {
  933. err := XTWriteDB().Model(models.Inspection{}).Where("inspect_date = ? and org_id = ?", date, orgid).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  934. return err
  935. }
  936. func DeleteChildInspection(name string, date int64, orgid int64) error {
  937. err := XTWriteDB().Model(models.Inspection{}).Where("project_name = ? and inspect_date = ? and org_id = ?", name, date, orgid).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  938. return err
  939. }
  940. func SaveMessage(id int64, orgid int64, role *models.SgjUserAdminRole) error {
  941. err := UserWriteDB().Model(models.SgjUserAdminRole{}).Where("admin_user_id = ? and org_id = ?", id, orgid).Update(map[string]interface{}{"message": role.Message, "mtime": time.Now().Unix()}).Error
  942. return err
  943. }
  944. func SaveSex(id int64, role *models.SgjUserAdminRole) error {
  945. err := UserWriteDB().Model(models.SgjUserAdminRole{}).Where("admin_user_id = ?", id).Update(map[string]interface{}{"sex": role.Sex, "mtime": time.Now().Unix()}).Error
  946. return err
  947. }
  948. func SaveBirthday(id int64, role *models.SgjUserAdminRole) error {
  949. err := UserWriteDB().Model(models.SgjUserAdminRole{}).Where("admin_user_id = ?", id).Update(map[string]interface{}{"birthday": role.Birthday, "mtime": time.Now().Unix()}).Error
  950. return err
  951. }
  952. func GetPartitionByName(name string, orgid int64) (*models.DeviceZone, error) {
  953. zone := models.DeviceZone{}
  954. err := XTReadDB().Model(&zone).Where("name = ? and org_id = ? and status = 1", name, orgid).Find(&zone).Error
  955. if err == gorm.ErrRecordNotFound {
  956. return nil, err
  957. }
  958. if err != nil {
  959. return nil, err
  960. }
  961. return &zone, nil
  962. }
  963. func GetPartitionByNameOne(name string, orgid int64) (zone models.DeviceZone, err error) {
  964. err = XTReadDB().Model(&zone).Where("name = ? and org_id = ? and status = 1", name, orgid).First(&zone).Error
  965. return zone, err
  966. }
  967. func CreatedZone(zone *models.DeviceZone) error {
  968. err := XTWriteDB().Model(&zone).Create(&zone).Error
  969. return err
  970. }
  971. func GetAllZone(orgid int64, page int64, limit int64) (zone []*models.DeviceZones, total int64, err error) {
  972. db := XTReadDB().Table("xt_device_zone as x").Where("x.status =1")
  973. table := XTReadDB().Table("xt_device_number as n")
  974. fmt.Println("table", table)
  975. if orgid > 0 {
  976. db = db.Where("x.org_id = ?", orgid)
  977. }
  978. offset := (page - 1) * limit
  979. err = db.Group("x.id").Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Select("x.id,x.org_id,x.name,x.type,n.number").Joins("left join xt_device_number as n on n.zone_id = x.id AND n.status = 1").Scan(&zone).Error
  980. return zone, total, err
  981. }
  982. func DeleteZoneById(id int64) error {
  983. err := XTWriteDB().Model(models.DeviceZone{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  984. return err
  985. }
  986. func GetGroupByName(name string, orgid int64) (*models.DeviceGroup, error) {
  987. group := models.DeviceGroup{}
  988. err := XTReadDB().Model(&group).Where("name = ? and org_id = ? and status = 1", name, orgid).Find(&group).Error
  989. if err == gorm.ErrRecordNotFound {
  990. return nil, err
  991. }
  992. if err != nil {
  993. return nil, err
  994. }
  995. return &group, nil
  996. }
  997. func GetGroupByNameOne(name string, orgid int64) (group models.DeviceGroup, err error) {
  998. err = XTReadDB().Model(&group).Where("name = ? and org_id = ? and status =1", name, orgid).First(&group).Error
  999. return group, err
  1000. }
  1001. func SaveGroup(group *models.DeviceGroup) error {
  1002. err := XTWriteDB().Model(&group).Create(&group).Error
  1003. return err
  1004. }
  1005. func GetAllGroup(orgid int64, page int64, limit int64) (group []*models.DeviceGroups, total int64, err error) {
  1006. db := XTReadDB().Table("xt_device_group as x").Where("x.status = 1")
  1007. table := XTReadDB().Table("xt_device_number as n")
  1008. fmt.Println("table", table)
  1009. if orgid > 0 {
  1010. db = db.Where("x.org_id = ?", orgid)
  1011. }
  1012. offset := (page - 1) * limit
  1013. err = db.Group("x.id").Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Select("x.id,x.org_id,x.name,x.status,x.ctime,x.mtime,n.number").Joins("left join xt_device_number as n on n.group_id = x.id And n.status = 1").Scan(&group).Error
  1014. return group, total, err
  1015. }
  1016. func DeleteGroup(id int64) error {
  1017. err := XTWriteDB().Model(models.DeviceGroup{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  1018. return err
  1019. }
  1020. func GetAllZoneOne(orgid int64) (zone []*models.DeviceZone, err error) {
  1021. err = XTReadDB().Model(&zone).Where("org_id = ? and status =1", orgid).Find(&zone).Error
  1022. return zone, err
  1023. }
  1024. func GetAllGroupOne(orgid int64) (group []*models.DeviceGroup, err error) {
  1025. err = XTReadDB().Model(&group).Where("org_id =?", orgid).Find(&group).Error
  1026. return group, err
  1027. }
  1028. func GetBedByName(name string, orgid int64) (*models.DeviceNumber, error) {
  1029. number := models.DeviceNumber{}
  1030. err := XTReadDB().Model(&number).Where("number=? and org_id = ? and status =1", name, orgid).Find(&number).Error
  1031. if err == gorm.ErrRecordNotFound {
  1032. return nil, err
  1033. }
  1034. if err != nil {
  1035. return nil, err
  1036. }
  1037. return &number, nil
  1038. }
  1039. func GetBedByNameOne(name string, orgid int64) (number models.DeviceNumber, err error) {
  1040. err = XTReadDB().Model(&number).Where("number=? and org_id = ? and status = 1", name, orgid).First(&number).Error
  1041. return number, err
  1042. }
  1043. func CreateNumber(number *models.DeviceNumber) error {
  1044. err := XTWriteDB().Model(&number).Create(&number).Error
  1045. return err
  1046. }
  1047. func GetAllNumber(page int64, limit int64, orgid int64) (number []*models.DeviceNumber, total int64, err error) {
  1048. db := XTReadDB().Table("xt_device_number as x").Where("x.status = 1")
  1049. if orgid > 0 {
  1050. db = db.Where("x.org_id = ?", orgid)
  1051. }
  1052. offset := (page - 1) * limit
  1053. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime asc").Select("x.id,x.org_id,x.number,x.group_id,x.zone_id,x.status").Find(&number).Error
  1054. return number, total, err
  1055. }
  1056. func GetSchedu(bedid int64, thetime int64, orgid int64) (*models.XtSchedule, error) {
  1057. schedule := models.XtSchedule{}
  1058. err := XTReadDB().Model(&schedule).Where("bed_id = ? and schedule_date = ? and user_org_id = ? and status = 1", bedid, thetime, orgid).Find(&schedule).Error
  1059. if err == gorm.ErrRecordNotFound {
  1060. return nil, err
  1061. }
  1062. if err != nil {
  1063. return nil, err
  1064. }
  1065. return &schedule, nil
  1066. }
  1067. func DeleteBed(id int64) error {
  1068. err := XTWriteDB().Model(models.DeviceNumber{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "ctime": time.Now().Unix()}).Error
  1069. return err
  1070. }
  1071. func GetMemberPatient(orgid int64) (patients []*models.XtPatientsNew, err error) {
  1072. err = XTReadDB().Model(&patients).Where("user_org_id = ? AND member_patients = ? AND status = 1", orgid, 1).Find(&patients).Error
  1073. return patients, err
  1074. }
  1075. func GetSlowPatient(orgid int64) (patients []*models.XtPatientsNew, err error) {
  1076. err = XTReadDB().Model(&patients).Where("user_org_id = ? AND slow_patients = ? AND status = 1", orgid, 1).Find(&patients).Error
  1077. return patients, err
  1078. }
  1079. func GetBloodPatient(orgid int64) (patients []*models.XtPatientsNew, err error) {
  1080. err = XTReadDB().Model(&patients).Where("user_org_id = ? AND blood_patients = ? AND status = 1", orgid, 1).Find(&patients).Error
  1081. return patients, err
  1082. }
  1083. func SaveName(id int64, admin *models.SgjUserAdmin) error {
  1084. err = UserWriteDB().Model(models.SgjUserAdmin{}).Where("id=?", id).Update(map[string]interface{}{"name": admin.Name, "mtime": time.Now().Unix()}).Error
  1085. return err
  1086. }
  1087. func GetAdminUserId(id int64) (models.SgjUserAdminRole, error) {
  1088. role := models.SgjUserAdminRole{}
  1089. err := UserReadDB().Where("id=? and status =1", id).Find(&role).Error
  1090. return role, err
  1091. }
  1092. func GetMyOrganaziton(id int64) (org []*models.SgjUserOrg, err error) {
  1093. db := UserReadDB().Table("sgj_user_org as x").Where("x.status = 1")
  1094. table := UserReadDB().Table("sgj_user_admin as a")
  1095. fmt.Println("table", table)
  1096. err = db.Select("x.id,x.creator,x.org_name,x.org_short_name,x.org_logo,x.org_introduction,x.illness,x.province,x.city,x.district,x.address,x.longitude,x.latitude,x.org_type,x.comments,x.evaluate,x.gallery,x.business_week,x.business_time,x.contact_name,x.telephone,x.claim,x.operating_state,x.source,x.ttype,x.ttid,x.tuid,x.reason,x.level,x.website,x.import,x.sortno,a.is_super_admin").Joins("left join sgj_user_admin as a on a.id = x.creator").Where("x.creator = ?", id).Scan(&org).Error
  1097. return org, err
  1098. }
  1099. func GetOrgInformation(id int64) (models.SgjUserOrg, error) {
  1100. org := models.SgjUserOrg{}
  1101. err := UserReadDB().Where("id=? and status = 1", id).Find(&org).Error
  1102. return org, err
  1103. }
  1104. func GetShortName(orgtype int64) (models.SgjUserOrgType, error) {
  1105. orgType := models.SgjUserOrgType{}
  1106. err := UserReadDB().Model(&orgType).Where("id=? and status = 1", orgtype).Find(&orgType).Error
  1107. return orgType, err
  1108. }
  1109. func GetTypeName(id int64) (models.SgjUserOrgType, error) {
  1110. orgType := models.SgjUserOrgType{}
  1111. err := UserReadDB().Model(&orgType).Where("id=? and status = 1", id).Find(&orgType).Error
  1112. return orgType, err
  1113. }
  1114. func GetOrgProvince(id int64) (models.SgjUserDistrict, error) {
  1115. district := models.SgjUserDistrict{}
  1116. err := UserReadDB().Model(&district).Where("id=?", id).Find(&district).Error
  1117. return district, err
  1118. }
  1119. func GetAllOrgType() (orgType []*models.SgjUserOrgType, err error) {
  1120. err = UserReadDB().Model(&models.SgjUserOrgType{}).Where("status= ? AND id = ? OR id = ? OR id = ? OR id = ? OR id = ? OR id = ?", 1, 1, 7, 12, 16, 21, 26).Find(&orgType).Error
  1121. return
  1122. }
  1123. func UpdateOrg(id int64, org models.SgjUserOrg) error {
  1124. err := UserWriteDB().Model(&org).Where("id = ?", id).Update(map[string]interface{}{"org_name": org.OrgName, "org_short_name": org.OrgShortName, "province": org.Province, "city": org.City, "district": org.District, "address": org.Address, "org_type": org.OrgType, "telephone": org.Telephone, "contact_name": org.ContactName, "mtime": time.Now().Unix()}).Error
  1125. return err
  1126. }
  1127. func GetMyInfoName(id int64) (models.SgjUserAdminRole, error) {
  1128. role := models.SgjUserAdminRole{}
  1129. err := UserReadDB().Model(&role).Where("id=? and status = 1", id).Find(&role).Error
  1130. return role, err
  1131. }
  1132. func GetAdminName(id int64, orgid int64) (models.AdminUsers, error) {
  1133. user := models.AdminUsers{}
  1134. //err := UserReadDB().Model(&user).Where("id=? and status =1", id).Find(&user).Error
  1135. //return user, err
  1136. db := UserReadDB().Table("sgj_user_admin as x").Where("x.status = 1")
  1137. table := UserReadDB().Table("sgj_user_admin_role as r")
  1138. fmt.Println("table", table)
  1139. err := db.Group("x.id").Select("x.id,x.mobile,x.password,x.is_super_admin,x.source,x.name,r.admin_user_id,r.org_id,r.app_id,r.role_id,r.user_name,r.avatar,r.message,r.sex,r.birthday").Where("x.id = ?", id).Joins("left join sgj_user_admin_role as r on r.admin_user_id = x.id and r.org_id = ?", orgid).Scan(&user).Error
  1140. return user, err
  1141. }
  1142. func GetCreatByOrgId(orgid int64) (models.SgjUserOrg, error) {
  1143. org := models.SgjUserOrg{}
  1144. err := UserReadDB().Model(&org).Where("id=? and status = 1", orgid).Find(&org).Error
  1145. return org, err
  1146. }
  1147. func GetMyOrgInformationFour(creator int64, orgid int64) (models.SgjUserAdminRole, error) {
  1148. role := models.SgjUserAdminRole{}
  1149. err := UserReadDB().Model(&role).Where("admin_user_id = ? and org_id = ? and status = 1", creator, orgid).Find(&role).Error
  1150. return role, err
  1151. }
  1152. func GetPartionDetail(id int64) (models.DeviceZone, error) {
  1153. zone := models.DeviceZone{}
  1154. err := XTReadDB().Model(&zone).Where("id=? and status = 1", id).Find(&zone).Error
  1155. return zone, err
  1156. }
  1157. func UpdatePartition(id int64, zone *models.DeviceZone) error {
  1158. err := XTWriteDB().Model(&zone).Where("id=?", id).Update(map[string]interface{}{"name": zone.Name, "type": zone.Type, "mtime": time.Now().Unix()}).Error
  1159. return err
  1160. }
  1161. func GetGroupDetail(id int64) (models.DeviceGroup, error) {
  1162. group := models.DeviceGroup{}
  1163. err := XTReadDB().Model(&group).Where("id=? and status = 1", id).Find(&group).Error
  1164. return group, err
  1165. }
  1166. func UpdateGroup(id int64, group *models.DeviceGroup) error {
  1167. err := XTWriteDB().Model(&group).Where("id=?", id).Update(map[string]interface{}{"name": group.Name, "mtime": time.Now().Unix()}).Error
  1168. return err
  1169. }
  1170. func GetBedDetail(id int64) (models.DeviceNumber, error) {
  1171. number := models.DeviceNumber{}
  1172. err := XTReadDB().Model(&number).Where("id=? and status =1", id).Find(&number).Error
  1173. return number, err
  1174. }
  1175. func UpdatedBed(id int64, number *models.DeviceNumber) error {
  1176. err = XTWriteDB().Model(&number).Where("id=?", id).Update(map[string]interface{}{"number": number.Number, "zone_id": number.ZoneID, "group_id": number.GroupID, "mtime": time.Now().Unix()}).Error
  1177. return err
  1178. }
  1179. func CreateLoginLod(log *models.SgjUserAdminLoginLog) error {
  1180. err := UserWriteDB().Model(&log).Create(&log).Error
  1181. return err
  1182. }
  1183. func GetAppId(orgid int64) (models.OrgApp, error) {
  1184. app := models.OrgApp{}
  1185. err := UserReadDB().Model(&app).Where("org_id = ? and status = 1 and app_type = 3", orgid).Find(&app).Error
  1186. return app, err
  1187. }
  1188. func GetRoleName(adminid int64, orgid int64, appid int64) (models.Roles, error) {
  1189. role := models.Roles{}
  1190. err := UserReadDB().Model(&role).Where("creator = ? and org_id = ? and app_id = ? and status =1", adminid, orgid, appid).Find(&role).Error
  1191. return role, err
  1192. }
  1193. func GetRole(id int64) (models.Role, error) {
  1194. role := models.Role{}
  1195. err := UserReadDB().Model(&role).Where("id = ? and status =1", id).Find(&role).Error
  1196. return role, err
  1197. }
  1198. func GetPositionName(adminid int64, orgid int64, appid int64) (models.App_Role, error) {
  1199. role := models.App_Role{}
  1200. err := UserReadDB().Model(&role).Where("admin_user_id = ? and org_id = ? and app_id = ?", adminid, orgid, appid).Find(&role).Error
  1201. return role, err
  1202. }
  1203. func GetAdmin(id int64) (models.SgjUserAdmin, error) {
  1204. admin := models.SgjUserAdmin{}
  1205. err := UserReadDB().Model(&admin).Where("id=? and status =1", id).Find(&admin).Error
  1206. return admin, err
  1207. }
  1208. func GetPosition(id int64) (models.SgjUserAdminRole, error) {
  1209. roles := models.SgjUserAdminRole{}
  1210. err = UserReadDB().Model(&roles).Where("id=? and status = 1", id).Find(&roles).Error
  1211. return roles, err
  1212. }
  1213. func GetPositionOne(id int64) (models.SgjUserAdmin, error) {
  1214. admin := models.SgjUserAdmin{}
  1215. err = UserReadDB().Model(&admin).Where("id=? and status = 1", id).Find(&admin).Error
  1216. return admin, err
  1217. }
  1218. func GetMyOrgInformation(id int64, appid int64) (models.SgjUserAdminRole, error) {
  1219. admin := models.SgjUserAdminRole{}
  1220. err := UserReadDB().Model(&admin).Where("admin_user_id=? and app_id = ? and status = 1", id, appid).First(&admin).Error
  1221. return admin, err
  1222. }
  1223. func SaveNameTwo(id int64, orgid int64, role *models.SgjUserAdminRole) error {
  1224. err := UserWriteDB().Model(models.SgjUserAdminRole{}).Where("admin_user_id =? and org_id = ?", id, orgid).Update(map[string]interface{}{"user_name": role.UserName, "mtime": time.Now().Unix()}).Error
  1225. return err
  1226. }
  1227. func AddPatientLapseto(lapseto *models.PatientLapseto) error {
  1228. err := XTWriteDB().Create(&lapseto).Error
  1229. return err
  1230. }
  1231. func GetDialysAfterRecord(patientid int64, startime int64, endtime int64, orgid int64) (dislysis []*models.XtAssessmentAfterDislysis, err error) {
  1232. db := XTReadDB().Model(&dislysis)
  1233. if startime > 0 {
  1234. db = db.Where("assessment_date >= ?", startime)
  1235. }
  1236. if endtime > 0 {
  1237. db = db.Where("assessment_date <= ?", endtime)
  1238. }
  1239. if patientid > 0 {
  1240. db = db.Where("patient_id = ?", patientid)
  1241. }
  1242. if orgid > 0 {
  1243. db = db.Where("user_org_id = ?", orgid)
  1244. }
  1245. err = db.Find(dislysis).Error
  1246. return dislysis, err
  1247. }
  1248. func GetDialysisDateByDate(starTime int64, patientId int64, orgId int64) (*models.XtPatientVascularAccess, error) {
  1249. access := models.XtPatientVascularAccess{}
  1250. err := XTReadDB().Model(&access).Where("start_time = ? and patient_id = ? and user_org_id = ? and status = 1", starTime, patientId, orgId).Find(&access).Error
  1251. if err == gorm.ErrRecordNotFound {
  1252. return nil, err
  1253. }
  1254. if err != nil {
  1255. return nil, err
  1256. }
  1257. return &access, nil
  1258. }
  1259. func GetDialysisDateByDateOne(starTime int64, patientId int64, orgId int64, id int64) (*models.XtPatientVascularAccess, error) {
  1260. access := models.XtPatientVascularAccess{}
  1261. err := XTReadDB().Model(&access).Where("start_time = ? and patient_id = ? and user_org_id = ? and status = 1 and id <> ?", starTime, patientId, orgId, id).Find(&access).Error
  1262. if err == gorm.ErrRecordNotFound {
  1263. return nil, err
  1264. }
  1265. if err != nil {
  1266. return nil, err
  1267. }
  1268. return &access, nil
  1269. }
  1270. func SaveVascularAccess(access *models.XtPatientVascularAccess) error {
  1271. err := XTWriteDB().Create(&access).Error
  1272. return err
  1273. }
  1274. func GetAllVacualAccessList(orgid int64, limit int64, page int64, patientId int64) (vascular []*models.XtPatientVascularAccess, total int64, err error) {
  1275. offset := (page - 1) * limit
  1276. err = XTReadDB().Model(&vascular).Where("user_org_id = ? and status = 1 and patient_id = ?", orgid, patientId).Count(&total).Offset(offset).Limit(limit).Order("start_time desc").Find(&vascular).Error
  1277. return vascular, total, err
  1278. }
  1279. func GetVasularAccessByDetail(id int64) (models.XtPatientVascularAccess, error) {
  1280. access := models.XtPatientVascularAccess{}
  1281. err := XTReadDB().Where("id = ? and status =1", id).Find(&access).Error
  1282. return access, err
  1283. }
  1284. func UpdateVascularAccess(access *models.XtPatientVascularAccess, id int64) error {
  1285. err := XTWriteDB().Model(&access).Where("id= ?", id).Updates(map[string]interface{}{"access_project": access.AccessProject, "blood_access_part_id": access.BloodAccessPartId, "inflow_pass": access.InflowPass, "start_time": access.StartTime, "first_start_time": access.FirstStartTime, "blood_access_part_opera_id": access.BloodAccessPartOperaId, "stop_reason": access.StopReason, "remark": access.Remark, "user_status": access.UserStatus, "modify": access.Modify, "stop_time": access.StopTime, "other_vascular": access.OtherVascular, "ci_type": access.CiType, "blood_cultupe": access.BloodCultupe, "sequelae_type": access.SequelaeType}).Error
  1286. return err
  1287. }
  1288. func DeleteVasularAccess(id int64) error {
  1289. access := models.XtPatientVascularAccess{}
  1290. err := XTWriteDB().Model(&access).Where("id = ?", id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1291. return err
  1292. }
  1293. func CreatePatientWayAssessment(assessment *models.XtPatientPasswayAssessment) error {
  1294. err := XTWriteDB().Create(&assessment).Error
  1295. return err
  1296. }
  1297. func GetAllPassWayAssessment(parent_id int64, patient_id int64, page int64, limit int64, orgid int64) (assessment []*models.XtPatientPasswayAssessment, total int64, err error) {
  1298. offset := (page - 1) * limit
  1299. err = XTReadDB().Model(&assessment).Where("parent_id = ? and patient_id = ? and user_org_id = ? and status =1", parent_id, patient_id, orgid).Count(&total).Offset(offset).Limit(limit).Order("start_time desc").Find(&assessment).Error
  1300. return assessment, total, err
  1301. }
  1302. func GetPasswayAssesmentById(id int64) (models.XtPatientPasswayAssessment, error) {
  1303. assessment := models.XtPatientPasswayAssessment{}
  1304. err := XTReadDB().Model(&assessment).Where("id=? and status =1", id).Find(&assessment).Error
  1305. return assessment, err
  1306. }
  1307. func UpdatePassWayAssesment(assessment *models.XtPatientPasswayAssessment, id int64) error {
  1308. err := XTWriteDB().Model(&assessment).Where("id = ?", id).Updates(map[string]interface{}{"start_time": assessment.StartTime, "creator": assessment.Creator, "blood_project": assessment.BloodProject, "blood_result": assessment.BloodResult, "blood_dealwith": assessment.BloodDealwith, "modify": assessment.Modify, "updated_time": time.Now().Unix()}).Error
  1309. return err
  1310. }
  1311. func DeleteWayAssessment(id int64) error {
  1312. assessment := models.XtPatientPasswayAssessment{}
  1313. err := XTWriteDB().Model(&assessment).Where("id = ?", id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  1314. return err
  1315. }
  1316. func GetLastPassWayAssessment(orgid int64, patientId int64) (models.XtPatientVascularAccess, error) {
  1317. assessment := models.XtPatientVascularAccess{}
  1318. err := XTReadDB().Model(&assessment).Where("user_org_id = ? and patient_id = ? and status =6 and user_status = 1", orgid, patientId).Order("start_time desc").Last(&assessment).Error
  1319. return assessment, err
  1320. }
  1321. func GetAccessList(orgid int64) (*models.Dataconfig, error) {
  1322. dataconfig := models.Dataconfig{}
  1323. err := XTReadDB().Model(&dataconfig).Where("org_id = 0 and name = '血管通路' and status = ?", 1).Find(&dataconfig).Error
  1324. return &dataconfig, err
  1325. }
  1326. func GetParentAccessList(orgid int64, parentid int64) (dataconfig []*models.Dataconfig, err error) {
  1327. err = XTReadDB().Model(&dataconfig).Where("org_id in(0,?) and parent_id = ? and status =1", orgid, parentid).Find(&dataconfig).Error
  1328. return dataconfig, err
  1329. }
  1330. func GetBloodAccess(orgid int64) (*models.Dataconfig, error) {
  1331. dataconfig := models.Dataconfig{}
  1332. err := XTReadDB().Model(&dataconfig).Where("org_id = 0 and name = '血管通路部位' and status = 1 ").Find(&dataconfig).Error
  1333. return &dataconfig, err
  1334. }
  1335. func GetPatientScheduleCount(orgid int64, startTime int64, endTime int64, ids []string) (schedule []*models.VmXtSchedule, err error) {
  1336. db := XTReadDB().Table("xt_schedule as x").Where("x.status =1")
  1337. if orgid > 0 {
  1338. db = db.Where("x.user_org_id = ?", orgid)
  1339. }
  1340. if startTime > 0 {
  1341. db = db.Where("x.schedule_date >=?", startTime)
  1342. }
  1343. if endTime > 0 {
  1344. db = db.Where("x.schedule_date<=?", endTime)
  1345. }
  1346. if len(ids) > 0 {
  1347. db = db.Where("x.partition_id in (?)", ids)
  1348. }
  1349. err = db.Select("count(x.id) as count,x.schedule_type,x.schedule_week").Group("x.schedule_week,x.schedule_type").Scan(&schedule).Error
  1350. return schedule, err
  1351. }
  1352. func GetTotalBedNumber(orgid int64, ids []string) (list []*models.XtDeviceNumber, total int64, err error) {
  1353. //err = XTReadDB().Model(&list).Where("org_id = ? and status = 1 ",orgid).Count(&total).Find(&list).Error
  1354. //return list,total,err
  1355. db := XTReadDB().Table("xt_device_number as x").Where("x.status = 1")
  1356. if orgid > 0 {
  1357. db = db.Where("x.org_id = ?", orgid)
  1358. }
  1359. if len(ids) > 0 {
  1360. db = db.Where("x.zone_id in (?)", ids)
  1361. }
  1362. err = db.Count(&total).Find(&list).Error
  1363. return list, total, err
  1364. }
  1365. func GetInspectionMajorItem(startime int64, lasttime int64, orgid int64, patientid int64) (inspection []*models.XtInspection, err error) {
  1366. db := XTReadDB().Table("xt_inspection as x").Where("x.status = 1")
  1367. if startime > 0 {
  1368. db = db.Where("x.inspect_date<=?", startime)
  1369. }
  1370. if lasttime > 0 {
  1371. db = db.Where("x.inspect_date>=?", lasttime)
  1372. }
  1373. if orgid > 0 {
  1374. db = db.Where("x.org_id = ?", orgid)
  1375. }
  1376. if patientid > 0 {
  1377. db = db.Where("x.patient_id = ?", patientid)
  1378. }
  1379. err = db.Group("x.project_id").Find(&inspection).Error
  1380. return inspection, err
  1381. }
  1382. func GetInspectionDetailByProject(project_id int64, patientid int64, inspect_date int64, orgid int64) (inspection []*models.VmInspection, err error) {
  1383. db := XTReadDB().Table("xt_inspection as x").Where("x.status = 1")
  1384. table := XTReadDB().Table("xt_inspection_reference as t").Where("t.status = 1")
  1385. fmt.Println(table)
  1386. if inspect_date > 0 {
  1387. db = db.Where("x.inspect_date=?", inspect_date)
  1388. }
  1389. if orgid > 0 {
  1390. db = db.Where("x.org_id = ?", orgid)
  1391. }
  1392. if patientid > 0 {
  1393. db = db.Where("x.patient_id = ?", patientid)
  1394. }
  1395. if project_id > 0 {
  1396. db = db.Where("x.project_id = ?", project_id)
  1397. }
  1398. err = db.Select("x.id,x.patient_id,x.org_id,x.project_id,x.item_id,x.item_name,x.project_name,x.inspect_type,x.inspect_value,x.inspect_date,t.range_max,t.range_min,t.range_value,t.unit").Joins("left join xt_inspection_reference as t on t.id = x.item_id").Scan(&inspection).Error
  1399. return inspection, err
  1400. }
  1401. func GetInspectionItemlist(patientid int64, inspect_date []string, projectid []string) (inspection []*models.VmInspection, err error) {
  1402. db := XTReadDB().Table("xt_inspection as x").Where("x.status = 1")
  1403. table := XTReadDB().Table("xt_inspection_reference as s").Where("s.status = 1")
  1404. fmt.Println(table)
  1405. if patientid > 0 {
  1406. db = db.Where("x.patient_id = ?", patientid)
  1407. }
  1408. if len(inspect_date) > 0 {
  1409. db = db.Where("x.inspect_date in(?)", inspect_date)
  1410. }
  1411. if len(projectid) > 0 {
  1412. db = db.Where("x.project_id in(?)", projectid)
  1413. }
  1414. err = db.Select("x.id,x.patient_id,x.org_id,x.project_id,x.item_id,x.item_name,x.project_name,x.inspect_type,x.inspect_value,x.inspect_date,t.range_max,t.range_min,t.range_value,t.unit").Joins("left join xt_inspection_reference as t on t.id = x.item_id").Scan(&inspection).Error
  1415. return inspection, err
  1416. }
  1417. func GetDialysisBeforInitDateList(patient_id int64, startime int64, endtime int64) (models.PredialysisEvaluation, error) {
  1418. befor := models.PredialysisEvaluation{}
  1419. db := XTReadDB().Model(&befor).Where("status = 1")
  1420. if patient_id > 0 {
  1421. db = db.Where("patient_id = ? and assessment_date>=? and assessment_date<=? ", patient_id, startime, endtime)
  1422. }
  1423. err = db.Last(&befor).Error
  1424. return befor, err
  1425. }
  1426. func GetDialysisPrescriptionDataList(patient_id int64, startime int64, endtime int64) (models.DialysisPrescription, error) {
  1427. prescription := models.DialysisPrescription{}
  1428. db := XTReadDB().Model(&prescription).Where("status = 1")
  1429. if patient_id > 0 {
  1430. db = db.Where("patient_id = ? and record_date>=? and record_date<=?", patient_id, startime, endtime)
  1431. }
  1432. err = db.Last(&prescription).Error
  1433. return prescription, err
  1434. }
  1435. func GetDialysisAssementAfter(patient_id int64, startime int64, endtime int64) (models.AssessmentAfterDislysis, error) {
  1436. after := models.AssessmentAfterDislysis{}
  1437. db := XTReadDB().Model(&after).Where("status = 1")
  1438. if patient_id > 0 {
  1439. db = db.Where("patient_id = ? and assessment_date>=? and assessment_date<=?", patient_id, startime, endtime)
  1440. }
  1441. err = db.Last(&after).Error
  1442. return after, err
  1443. }
  1444. func GetDialysisDialysisMode(patient_id int64, startime int64, endtime int64, orgid int64) (counts []*models.PatientPrescriptionCountStruct, err error) {
  1445. db := readDb.Table("xt_dialysis_order as o").Where("o.status = 1")
  1446. table := readDb.Table("xt_schedule as s").Where("s.status = 1")
  1447. fmt.Println(table)
  1448. p := readDb.Table("xt_patients as p").Where("p.status = 1")
  1449. fmt.Println(p)
  1450. if startime > 0 {
  1451. db = db.Where("o.dialysis_date >=?", startime)
  1452. }
  1453. if endtime > 0 {
  1454. db = db.Where("o.dialysis_date<=?", endtime)
  1455. }
  1456. if orgid > 0 {
  1457. db = db.Where("o.user_org_id = ? and o.stage = 2", orgid)
  1458. }
  1459. if patient_id > 0 {
  1460. db = db.Where("o.patient_id = ?", patient_id)
  1461. }
  1462. err = db.Select("s.mode_id,count(s.mode_id) as count").Joins("left join xt_schedule as s on s.patient_id = o.patient_id and s.schedule_date = o.dialysis_date and s.status= 1").Joins("left join xt_patients as p on o.patient_id = p.id").Group("s.mode_id").Scan(&counts).Error
  1463. return counts, err
  1464. }
  1465. func CreateSummary(summary *models.XtTemplateSummary) error {
  1466. err = XTWriteDB().Create(&summary).Error
  1467. return err
  1468. }
  1469. func UpdateTempalteSummary(summary *models.XtTemplateSummary) error {
  1470. err = XTWriteDB().Save(&summary).Error
  1471. return err
  1472. }
  1473. func GetTemplateSummaryList(patientid int64, orgid int64, startime int64, endtime int64) (summary []*models.XtTemplateSummary, err error) {
  1474. db := XTReadDB().Model(&summary).Where("status = 1")
  1475. if startime > 0 {
  1476. db = db.Where("record_time >=?", startime)
  1477. }
  1478. if endtime > 0 {
  1479. db = db.Where("record_time<=?", endtime)
  1480. }
  1481. if patientid > 0 {
  1482. db = db.Where("patient_id = ?", patientid)
  1483. }
  1484. if orgid > 0 {
  1485. db = db.Where("user_org_id = ?", orgid)
  1486. }
  1487. err = db.Order("record_time desc").Find(&summary).Error
  1488. return summary, err
  1489. }
  1490. func GetTemplateSummaryDetail(id int64) (models.XtTemplateSummary, error) {
  1491. summary := models.XtTemplateSummary{}
  1492. err := XTReadDB().Model(&summary).Where("id=?", id).Find(&summary).Error
  1493. return summary, err
  1494. }
  1495. func GetTemplateSummaryPrintDetail(id int64) (models.XtTemplateSummary, error) {
  1496. summary := models.XtTemplateSummary{}
  1497. err := XTReadDB().Model(&summary).Where("id=? and status = 1", id).Preload("Patients", "status = 1").Find(&summary).Error
  1498. return summary, err
  1499. }
  1500. func GetTreateSummary(startime int64, endtime int64, limit int64, page int64, creater int64, patient_id int64) (list []*models.XtTemplateSummary, total int64, err error) {
  1501. db := XTReadDB().Model(&list).Where("status = 1")
  1502. offset := (page - 1) * limit
  1503. if startime > 0 {
  1504. db = db.Where("record_time >=?", startime)
  1505. }
  1506. if endtime > 0 {
  1507. db = db.Where("record_time<=?", endtime)
  1508. }
  1509. if creater > 0 {
  1510. db = db.Where("admin_user_id = ?", creater)
  1511. }
  1512. if patient_id > 0 {
  1513. db = db.Where("patient_id = ?", patient_id)
  1514. }
  1515. err = db.Count(&total).Offset(offset).Limit(limit).Order("record_time desc").Find(&list).Error
  1516. return list, total, err
  1517. }
  1518. func DeleteSummary(ids []string) error {
  1519. summary := models.XtTemplateSummary{}
  1520. err := XTWriteDB().Model(&summary).Where("id in(?)", ids).Updates(map[string]interface{}{"status": 0}).Error
  1521. return err
  1522. }