index_evaluation_service.go 81KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779
  1. package statistics_service
  2. import (
  3. "Xcx_New/models"
  4. "Xcx_New/service"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "math"
  8. "strconv"
  9. "strings"
  10. )
  11. func GetOrgFirstPatientInfo(user_org_id int64) (patient *models.Patients, err error) {
  12. db := service.XTReadDB()
  13. var tempPatient models.Patients
  14. findErr := db.Model(&models.Patients{}).Where("user_org_id = ? AND status = 1 AND lapseto = 1", user_org_id).Last(&tempPatient).Error
  15. if findErr == gorm.ErrRecordNotFound {
  16. return nil, nil
  17. }
  18. if findErr != nil {
  19. return nil, findErr
  20. }
  21. return &tempPatient, nil
  22. }
  23. type ProcessIndexDataStruct struct {
  24. Date string `json:"date"`
  25. Value float64 `json:"value"`
  26. }
  27. //透析过程统计
  28. func GetDialysisProcessIndexChartData(user_org_id int64, patient_id int64, start_time int64, end_time int64, statistics_type int) (datas []*ProcessIndexDataStruct, err error) {
  29. db := service.XTReadDB()
  30. switch statistics_type {
  31. case 1:
  32. err = db.Raw("select from_unixtime(assessment_date,'%Y-%m-%d') as date, weight_before as value from xt_assessment_before_dislysis Where status = 1 AND user_org_id = ? AND patient_id = ? AND assessment_date <= ? AND assessment_date >= ? ", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  33. break
  34. case 2:
  35. err = db.Raw("select from_unixtime(assessment_date,'%Y-%m-%d') as date, weight_after as value from xt_assessment_after_dislysis Where status = 1 AND user_org_id = ? AND patient_id = ? AND assessment_date <= ? AND assessment_date >= ? ", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  36. break
  37. case 3:
  38. err = db.Raw("select from_unixtime(assessment_date,'%Y-%m-%d') as date, systolic_blood_pressure as value from xt_assessment_before_dislysis Where status = 1 AND user_org_id = ? AND patient_id = ? AND assessment_date <= ? AND assessment_date >= ? AND systolic_blood_pressure <> 0 ", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  39. break
  40. case 4:
  41. err = db.Raw("select from_unixtime(assessment_date,'%Y-%m-%d') as date, diastolic_blood_pressure as value from xt_assessment_before_dislysis Where status = 1 AND user_org_id = ? AND patient_id = ? AND assessment_date <= ? AND assessment_date >= ? AND diastolic_blood_pressure <> 0", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  42. break
  43. case 5:
  44. err = db.Raw("select from_unixtime(assessment_date,'%Y-%m-%d') as date, pulse_frequency as value from xt_assessment_before_dislysis Where status = 1 AND user_org_id = ? AND patient_id = ? AND assessment_date <= ? AND assessment_date >= ? ", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  45. break
  46. case 6:
  47. err = db.Raw("select from_unixtime(assessment_date,'%Y-%m-%d') as date, systolic_blood_pressure as value from xt_assessment_after_dislysis Where status = 1 AND user_org_id = ? AND patient_id = ? AND assessment_date <= ? AND assessment_date >= ? AND systolic_blood_pressure <> 0", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  48. break
  49. case 7:
  50. err = db.Raw("select from_unixtime(assessment_date,'%Y-%m-%d') as date, diastolic_blood_pressure as value from xt_assessment_after_dislysis Where status = 1 AND user_org_id = ? AND patient_id = ? AND assessment_date <= ? AND assessment_date >= ? AND diastolic_blood_pressure <> 0 ", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  51. break
  52. case 8:
  53. err = db.Raw("select from_unixtime(assessment_date,'%Y-%m-%d') as date, pulse_frequency as value from xt_assessment_after_dislysis Where status = 1 AND user_org_id = ? AND patient_id = ? AND assessment_date <= ? AND assessment_date >= ? ", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  54. break
  55. }
  56. if err != nil {
  57. return nil, err
  58. }
  59. return datas, nil
  60. }
  61. type otherItemAmount struct {
  62. Total int64 `json:"total"`
  63. Name string `json:"name"`
  64. Ratio string `json:"ratio"`
  65. }
  66. func GetDialysisProcessIndexOtherChartData(user_org_id int64, patient_id int64, start_time int64, end_time int64, statistics_type int) (item []*otherItemAmount, err error) {
  67. db := service.XTReadDB()
  68. var items []*otherItemAmount
  69. var tempErr error
  70. var total int64
  71. switch statistics_type {
  72. case 9:
  73. tempErr = db.Table("xt_dialysis_prescription as p").Where("p.user_org_id=? and p.status=1 and p.record_date >= ? and p.record_date <= ? AND p.patient_id = ? ", user_org_id, start_time, end_time, patient_id).
  74. Select("CASE WHEN p.mode_id = 1 THEN 'HD'" +
  75. " WHEN p.mode_id = 2 THEN 'HDF'" +
  76. " WHEN p.mode_id = 3 THEN 'HD+HP'" +
  77. " WHEN p.mode_id = 4 THEN 'HP'" +
  78. " WHEN p.mode_id = 5 THEN 'HF'" +
  79. " WHEN p.mode_id = 6 THEN 'SCUF'" +
  80. " WHEN p.mode_id = 7 THEN 'IUF'" +
  81. " WHEN p.mode_id = 8 THEN 'HFHD'" +
  82. " WHEN p.mode_id = 9 THEN 'HFHD+HP'" +
  83. " WHEN p.mode_id = 10 THEN 'PHF'" +
  84. " WHEN p.mode_id = 11 THEN 'HFR'" +
  85. " WHEN p.mode_id = 12 THEN 'HDF+HP'" +
  86. " WHEN p.mode_id = 13 THEN 'CRRT'" +
  87. " WHEN p.mode_id = 14 THEN '腹水回输'" +
  88. " WHEN p.mode_id = 19 THEN 'IUF+HD'" +
  89. " ELSE '未知' END AS name, COUNT(*) AS total",
  90. ).Group("p.mode_id").Scan(&items).Error
  91. break
  92. case 10:
  93. tempErr = db.Table("xt_dialysis_prescription as p").Where("p.user_org_id=? and p.status=1 and p.record_date >= ? and p.record_date <= ? AND p.patient_id = ? ", user_org_id, start_time, end_time, patient_id).
  94. Select("CASE WHEN p.anticoagulant = 1 THEN '无肝素'" +
  95. " WHEN p.anticoagulant = 2 THEN '普通肝素'" +
  96. " WHEN p.anticoagulant = 3 THEN '低分子肝素'" +
  97. " WHEN p.anticoagulant = 4 THEN '阿加曲班'" +
  98. " WHEN p.anticoagulant = 5 THEN '枸橼酸钠'" +
  99. " WHEN p.anticoagulant = 6 THEN '低分子肝素钙'" +
  100. " WHEN p.anticoagulant = 7 THEN '低分子肝素钠'" +
  101. " ELSE '未知' END AS name, COUNT(*) AS total",
  102. ).Group("p.anticoagulant").Scan(&items).Error
  103. break
  104. }
  105. db.Table("xt_dialysis_prescription as p").Where("p.user_org_id=? AND p.status = 1 AND p.patient_id = ? AND p.record_date >= ? and p.record_date <= ? ", user_org_id, patient_id, start_time, end_time).Count(&total)
  106. for _, item := range items {
  107. if math.IsNaN(float64(item.Total) / float64(total)) {
  108. item.Ratio = "0.0"
  109. } else {
  110. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(total))*100), 64)
  111. item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
  112. }
  113. }
  114. if tempErr != nil {
  115. return nil, err
  116. }
  117. return items, nil
  118. }
  119. type PatientDialysisRecord struct {
  120. models.VMSchedule
  121. DeviceNumber VMDeviceNumber `gorm:"foreignkey:ID;AssociationForeignKey:BedId;" json:"device_number"`
  122. DialysisPrescription VMDialysisPrescription `json:"prescription" gorm:"foreignkey:RecordDate;AssociationForeignKey:ScheduleDate;"`
  123. PredialysisEvaluation VMPredialysisEvaluation `json:"predialysis_evaluation" gorm:"foreignkey:AssessmentDate;AssociationForeignKey:ScheduleDate;"`
  124. AssessmentAfterDislysis models.AssessmentAfterDislysis `json:"assessment_after_dislysis" gorm:"foreignkey:AssessmentDate;AssociationForeignKey:ScheduleDate;"`
  125. Patient VMPatients `json:"patient" gorm:"foreignkey:ID;AssociationForeignKey:PatientID;"`
  126. }
  127. func GetDialysisProcessIndexTableData(orgID, patientID int64, page, limit, start, end int64) ([]*PatientDialysisRecord, int64, error) {
  128. offset := (page - 1) * limit
  129. var total int64
  130. var err error
  131. var orders []*PatientDialysisRecord
  132. readDb := service.XTReadDB()
  133. db := readDb.Table("xt_schedule as sch").
  134. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  135. Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  136. Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  137. Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  138. Preload("Patient", "user_org_id=? and status=1", orgID).
  139. Where("sch.patient_id=? and sch.user_org_id=? and sch.status=1", patientID, orgID).Group("sch.schedule_date")
  140. if start != 0 {
  141. db = db.Where("sch.schedule_date>=?", start)
  142. }
  143. if end != 0 {
  144. db = db.Where("sch.schedule_date<=?", end)
  145. }
  146. err = db.Count(&total).Offset(offset).Limit(limit).Order("sch.schedule_date desc").Select("sch.patient_id,sch.schedule_date,sch.bed_id,sch.user_org_id").Find(&orders).Error
  147. return orders, total, err
  148. }
  149. func GetAnticoagulantData(user_org_id int64) (config []*models.Dataconfig, tempErr error) {
  150. var tempDataConfig []*models.Dataconfig
  151. var tempConfig models.Dataconfig
  152. db := service.XTReadDB()
  153. db.Model(&models.Dataconfig{}).Where("name = '抗凝剂' AND field_name = 'anticoagulant' AND parent_id = 0").First(&tempConfig)
  154. err := db.Model(&models.Dataconfig{}).Where("(org_id = 0 OR org_id = ?) AND parent_id = ?", user_org_id, tempConfig.ID).Find(&tempDataConfig).Error
  155. return tempDataConfig, err
  156. }
  157. //透析过程统计
  158. //透析监测统计
  159. func GetDialysisWatchChartData(user_org_id int64, patient_id int64, start_time int64, end_time int64, statistics_type int) (datas []*ProcessIndexDataStruct, err error) {
  160. db := service.XTReadDB()
  161. switch statistics_type {
  162. case 1:
  163. if patient_id == 0 {
  164. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.systolic_blood_pressure as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND operate_time <= ? AND operate_time >= ? AND systolic_blood_pressure <> 0 Order by operate_time asc", user_org_id, end_time, start_time).Scan(&datas).Error
  165. } else {
  166. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.systolic_blood_pressure as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND patient_id = ? AND operate_time <= ? AND operate_time >= ? AND systolic_blood_pressure <> 0 Order by operate_time asc", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  167. }
  168. break
  169. case 2:
  170. if patient_id == 0 {
  171. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.diastolic_blood_pressure as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND operate_time <= ? AND operate_time >= ? AND diastolic_blood_pressure <> 0 Order by operate_time asc", user_org_id, end_time, start_time).Scan(&datas).Error
  172. } else {
  173. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.diastolic_blood_pressure as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND patient_id = ? AND operate_time <= ? AND operate_time >= ? AND diastolic_blood_pressure <> 0 Order by operate_time asc", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  174. }
  175. break
  176. case 3:
  177. if patient_id == 0 {
  178. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.pulse_frequency as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, end_time, start_time).Scan(&datas).Error
  179. } else {
  180. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.pulse_frequency as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND patient_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  181. }
  182. break
  183. case 4:
  184. if patient_id == 0 {
  185. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.breathing_rate as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, end_time, start_time).Scan(&datas).Error
  186. } else {
  187. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.breathing_rate as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND patient_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  188. }
  189. break
  190. case 5:
  191. if patient_id == 0 {
  192. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.temperature as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, end_time, start_time).Scan(&datas).Error
  193. } else {
  194. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.temperature as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND patient_id = ? AND operate_time <= ? AND operate_time >= ?Order by operate_time asc", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  195. }
  196. break
  197. case 6:
  198. if patient_id == 0 {
  199. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.blood_flow_volume as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, end_time, start_time).Scan(&datas).Error
  200. } else {
  201. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.blood_flow_volume as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND patient_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  202. }
  203. break
  204. case 7:
  205. if patient_id == 0 {
  206. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.venous_pressure as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, end_time, start_time).Scan(&datas).Error
  207. } else {
  208. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.venous_pressure as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND patient_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  209. }
  210. break
  211. case 8:
  212. if patient_id == 0 {
  213. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.arterial_pressure as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, end_time, start_time).Scan(&datas).Error
  214. } else {
  215. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.arterial_pressure as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND patient_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  216. }
  217. break
  218. case 9:
  219. if patient_id == 0 {
  220. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.transmembrane_pressure as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, end_time, start_time).Scan(&datas).Error
  221. } else {
  222. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.transmembrane_pressure as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND patient_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  223. }
  224. break
  225. case 10:
  226. if patient_id == 0 {
  227. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.dialysate_temperature as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, end_time, start_time).Scan(&datas).Error
  228. } else {
  229. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.dialysate_temperature as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND patient_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  230. }
  231. break
  232. case 11:
  233. if patient_id == 0 {
  234. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.ultrafiltration_rate as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, end_time, start_time).Scan(&datas).Error
  235. } else {
  236. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.ultrafiltration_rate as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND patient_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  237. }
  238. break
  239. case 12:
  240. if patient_id == 0 {
  241. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.ultrafiltration_volume as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, end_time, start_time).Scan(&datas).Error
  242. } else {
  243. err = db.Raw("select from_unixtime(monitor.operate_time,'%Y-%m-%d') as date, monitor.ultrafiltration_volume as value from xt_monitoring_record as monitor Where status = 1 AND user_org_id = ? AND patient_id = ? AND operate_time <= ? AND operate_time >= ? Order by operate_time asc", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  244. }
  245. break
  246. }
  247. if err != nil {
  248. return nil, err
  249. }
  250. return datas, nil
  251. }
  252. func GetDialysisWatchTableData(orgID, patientID int64, page, limit, start, end int64) ([]*VMMonitoringRecord, int64, error) {
  253. offset := (page - 1) * limit
  254. var total int64
  255. var err error
  256. var orders []*VMMonitoringRecord
  257. readDb := service.XTReadDB()
  258. db := readDb.Table("xt_monitoring_record as mo").
  259. Where(" mo.user_org_id=? and mo.status=1", orgID)
  260. if patientID != 0 {
  261. db = db.Where("mo.patient_id =?", patientID)
  262. }
  263. if start != 0 {
  264. db = db.Where("mo.operate_time>=?", start)
  265. }
  266. if end != 0 {
  267. db = db.Where("mo.operate_time<=?", end)
  268. }
  269. err = db.Count(&total).Offset(offset).Limit(limit).Order("mo.operate_time desc").Select("mo.patient_id,mo.operate_time,mo.systolic_blood_pressure, mo.diastolic_blood_pressure, mo.pulse_frequency,mo.breathing_rate,mo.temperature,mo.blood_flow_volume,mo.venous_pressure,mo.arterial_pressure,mo.transmembrane_pressure,mo.dialysate_temperature,mo.ultrafiltration_rate,mo.ultrafiltration_volume").Find(&orders).Error
  270. return orders, total, err
  271. }
  272. //透析监测统计
  273. //透析体重统计
  274. func GetDialysisWeightChartData(user_org_id int64, start_time int64, end_time int64, statistics_type int) (item []*otherItemAmount, err error) {
  275. db := service.XTReadDB()
  276. var items []*otherItemAmount
  277. var tempErr error
  278. var Total int64
  279. switch statistics_type {
  280. case 1:
  281. db.Table("xt_assessment_before_dislysis ").Where("user_org_id=? and status=1 and created_time >= ? and created_time <= ?", user_org_id, start_time, end_time).Count(&Total)
  282. tempErr = db.Table("xt_assessment_before_dislysis ").Where("user_org_id=? and status=1 and created_time >= ? and created_time <= ?", user_org_id, start_time, end_time).
  283. Select("CASE WHEN dry_weight < 40 THEN '小于40kg'" +
  284. " WHEN dry_weight >= 40 AND dry_weight < 50 THEN '40~50kg'" +
  285. " WHEN dry_weight >= 50 AND dry_weight < 60 THEN '50~60kg'" +
  286. " WHEN dry_weight >= 60 AND dry_weight < 70 THEN '60~70kg'" +
  287. " WHEN dry_weight >= 70 THEN '大于70kg'" +
  288. " ELSE '未知' END AS name, COUNT(*) AS total",
  289. ).Group("name").Scan(&items).Error
  290. var isHasConditionOne bool = false
  291. var isHasConditionTwo bool = false
  292. var isHasConditionThree bool = false
  293. var isHasConditionFour bool = false
  294. var isHasConditionFive bool = false
  295. for _, item := range items {
  296. if item.Name == "小于40kg" {
  297. isHasConditionOne = true
  298. }
  299. if item.Name == "40~50kg" {
  300. isHasConditionTwo = true
  301. }
  302. if item.Name == "60~70kg" {
  303. isHasConditionThree = true
  304. }
  305. if item.Name == "大于70kg" {
  306. isHasConditionFour = true
  307. }
  308. if item.Name == "未知" {
  309. isHasConditionFive = true
  310. }
  311. }
  312. if !isHasConditionOne {
  313. newItem := &otherItemAmount{
  314. Total: 0,
  315. Name: "小于40kg",
  316. Ratio: "0",
  317. }
  318. items = append(items, newItem)
  319. }
  320. if !isHasConditionTwo {
  321. newItem := &otherItemAmount{
  322. Total: 0,
  323. Name: "40~50kg",
  324. Ratio: "0",
  325. }
  326. items = append(items, newItem)
  327. }
  328. if !isHasConditionThree {
  329. newItem := &otherItemAmount{
  330. Total: 0,
  331. Name: "60~70kg",
  332. Ratio: "0",
  333. }
  334. items = append(items, newItem)
  335. }
  336. if !isHasConditionFour {
  337. newItem := &otherItemAmount{
  338. Total: 0,
  339. Name: "大于70kg",
  340. Ratio: "0",
  341. }
  342. items = append(items, newItem)
  343. }
  344. if !isHasConditionFive {
  345. newItem := &otherItemAmount{
  346. Total: 0,
  347. Name: "未知",
  348. Ratio: "0",
  349. }
  350. items = append(items, newItem)
  351. }
  352. for _, item := range items {
  353. if math.IsNaN(float64(item.Total) / float64(Total)) {
  354. item.Ratio = "0.0"
  355. } else {
  356. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(Total))*100), 64)
  357. item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
  358. }
  359. }
  360. break
  361. case 2:
  362. db.Table("xt_assessment_before_dislysis ").Where("user_org_id=? and status=1 and created_time >= ? and created_time <= ?", user_org_id, start_time, end_time).Count(&Total)
  363. tempErr = db.Table("xt_assessment_before_dislysis").Where("user_org_id=? and status=1 and created_time >= ? and created_time <= ?", user_org_id, start_time, end_time).
  364. Select("CASE WHEN weight_before < 40 THEN '小于40kg'" +
  365. " WHEN weight_before >= 40 AND weight_before < 50 THEN '40~50kg'" +
  366. " WHEN weight_before >= 50 AND weight_before < 60 THEN '50~60kg'" +
  367. " WHEN weight_before >= 60 AND weight_before < 70 THEN '60~70kg'" +
  368. " WHEN weight_before >= 70 THEN '大于70kg'" +
  369. " ELSE '未知' END AS name, COUNT(*) AS total",
  370. ).Group("name").Scan(&items).Error
  371. var isHasConditionOne bool = false
  372. var isHasConditionTwo bool = false
  373. var isHasConditionThree bool = false
  374. var isHasConditionFour bool = false
  375. var isHasConditionFive bool = false
  376. for _, item := range items {
  377. if item.Name == "小于40kg" {
  378. isHasConditionOne = true
  379. }
  380. if item.Name == "40~50kg" {
  381. isHasConditionTwo = true
  382. }
  383. if item.Name == "60~70kg" {
  384. isHasConditionThree = true
  385. }
  386. if item.Name == "大于70kg" {
  387. isHasConditionFour = true
  388. }
  389. if item.Name == "未知" {
  390. isHasConditionFive = true
  391. }
  392. }
  393. if !isHasConditionOne {
  394. newItem := &otherItemAmount{
  395. Total: 0,
  396. Name: "小于40kg",
  397. Ratio: "0",
  398. }
  399. items = append(items, newItem)
  400. }
  401. if !isHasConditionTwo {
  402. newItem := &otherItemAmount{
  403. Total: 0,
  404. Name: "40~50kg",
  405. Ratio: "0",
  406. }
  407. items = append(items, newItem)
  408. }
  409. if !isHasConditionThree {
  410. newItem := &otherItemAmount{
  411. Total: 0,
  412. Name: "60~70kg",
  413. Ratio: "0",
  414. }
  415. items = append(items, newItem)
  416. }
  417. if !isHasConditionFour {
  418. newItem := &otherItemAmount{
  419. Total: 0,
  420. Name: "大于70kg",
  421. Ratio: "0",
  422. }
  423. items = append(items, newItem)
  424. }
  425. if !isHasConditionFive {
  426. newItem := &otherItemAmount{
  427. Total: 0,
  428. Name: "未知",
  429. Ratio: "0",
  430. }
  431. items = append(items, newItem)
  432. }
  433. for _, item := range items {
  434. if math.IsNaN(float64(item.Total) / float64(Total)) {
  435. item.Ratio = "0.0"
  436. } else {
  437. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(Total))*100), 64)
  438. item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
  439. }
  440. }
  441. break
  442. case 3:
  443. //db.Table("xt_assessment_before_dislysis ").Where("user_org_id=? and status=1 and created_time >= ? and created_time <= ?", user_org_id, start_time, end_time).Count(&Total)
  444. //db.Raw("Select count(*) from `xt_assessment_before_dislysis` LEFT JOIN `xt_assessment_after_dislysis` On xt_assessment_before_dislysis.`patient_id` = xt_assessment_after_dislysis.`patient_id` AND xt_assessment_before_dislysis.assessment_date = xt_assessment_after_dislysis.assessment_date AND xt_assessment_after_dislysis.weight_after != 0 Where xt_assessment_before_dislysis.user_org_id = ? AND xt_assessment_before_dislysis.created_time >= ? AND xt_assessment_before_dislysis.created_time <= ? Order by xt_assessment_before_dislysis.assessment_date desc", user_org_id, start_time, end_time).Count(&Total)
  445. var newTotal int64
  446. type weight struct {
  447. WeightAfter float64 `json:"weight_after"`
  448. WeightBefore float64 `json:"weight_before"`
  449. }
  450. var weights []*weight
  451. var weightAdd []float64
  452. db.Raw("Select xt_assessment_after_dislysis.weight_after as weight_after ,xt_assessment_before_dislysis.weight_before as weight_before from `xt_assessment_before_dislysis` LEFT JOIN `xt_assessment_after_dislysis` On xt_assessment_before_dislysis.`patient_id` = xt_assessment_after_dislysis.`patient_id` AND xt_assessment_before_dislysis.assessment_date = xt_assessment_after_dislysis.assessment_date AND xt_assessment_after_dislysis.weight_after <> 0 AND xt_assessment_after_dislysis.user_org_id = ? Where xt_assessment_before_dislysis.user_org_id = ? AND xt_assessment_before_dislysis.created_time >= ? AND xt_assessment_before_dislysis.created_time <= ? Order by xt_assessment_before_dislysis.assessment_date desc", user_org_id, user_org_id, start_time, end_time).Scan(&weights)
  453. for index, _ := range weights {
  454. if index+1 < len(weights) {
  455. if weights[index+1].WeightAfter == 0 || weights[index].WeightBefore == 0 {
  456. continue
  457. }
  458. weightAdd = append(weightAdd, weights[index].WeightBefore-weights[index+1].WeightAfter)
  459. }
  460. }
  461. var total_one int64
  462. var total_two int64
  463. var total_three int64
  464. var total_four int64
  465. var total_five int64
  466. for _, item := range weightAdd {
  467. if item < 1 {
  468. total_one++
  469. }
  470. if item >= 1 && item < 2 {
  471. total_two++
  472. }
  473. if item >= 2 && item < 3 {
  474. total_three++
  475. }
  476. if item >= 3 && item <= 4 {
  477. total_four++
  478. }
  479. if item > 4 {
  480. total_five++
  481. }
  482. }
  483. newTotal = total_one + total_two + total_three + total_four + total_five
  484. newItem := &otherItemAmount{
  485. Total: total_one,
  486. Name: "小于1kg",
  487. Ratio: "0",
  488. }
  489. newItem1 := &otherItemAmount{
  490. Total: total_two,
  491. Name: "1-2kg",
  492. Ratio: "0",
  493. }
  494. newItem2 := &otherItemAmount{
  495. Total: total_three,
  496. Name: "2-3kg",
  497. Ratio: "0",
  498. }
  499. newItem3 := &otherItemAmount{
  500. Total: total_four,
  501. Name: "3-4kg",
  502. Ratio: "0",
  503. }
  504. newItem4 := &otherItemAmount{
  505. Total: total_five,
  506. Name: "大于4kg",
  507. Ratio: "0",
  508. }
  509. items = append(items, newItem)
  510. items = append(items, newItem1)
  511. items = append(items, newItem2)
  512. items = append(items, newItem3)
  513. items = append(items, newItem4)
  514. for _, item := range items {
  515. if math.IsNaN(float64(item.Total) / float64(newTotal)) {
  516. item.Ratio = "0.0"
  517. } else {
  518. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(newTotal))*100), 64)
  519. item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
  520. }
  521. }
  522. break
  523. case 4:
  524. db.Table("xt_assessment_after_dislysis ").Where("user_org_id=? and status=1 and created_time >= ? and created_time <= ?", user_org_id, start_time, end_time).Count(&Total)
  525. tempErr = db.Table("xt_assessment_after_dislysis").Where("user_org_id=? and status=1 and created_time >= ? and created_time <= ?", user_org_id, start_time, end_time).
  526. Select("CASE WHEN weight_after < 40 THEN '小于40kg'" +
  527. " WHEN weight_after >= 40 AND weight_after < 50 THEN '40~50kg'" +
  528. " WHEN weight_after >= 50 AND weight_after < 60 THEN '50~60kg'" +
  529. " WHEN weight_after >= 60 AND weight_after < 70 THEN '60~70kg'" +
  530. " WHEN weight_after >= 70 THEN '大于70kg'" +
  531. " ELSE '未知' END AS name, COUNT(*) AS total",
  532. ).Group("name").Scan(&items).Error
  533. var isHasConditionOne bool = false
  534. var isHasConditionTwo bool = false
  535. var isHasConditionThree bool = false
  536. var isHasConditionFour bool = false
  537. var isHasConditionFive bool = false
  538. for _, item := range items {
  539. if item.Name == "小于40kg" {
  540. isHasConditionOne = true
  541. }
  542. if item.Name == "40~50kg" {
  543. isHasConditionTwo = true
  544. }
  545. if item.Name == "60~70kg" {
  546. isHasConditionThree = true
  547. }
  548. if item.Name == "大于70kg" {
  549. isHasConditionFour = true
  550. }
  551. if item.Name == "未知" {
  552. isHasConditionFive = true
  553. }
  554. }
  555. if !isHasConditionOne {
  556. newItem := &otherItemAmount{
  557. Total: 0,
  558. Name: "小于40kg",
  559. Ratio: "0",
  560. }
  561. items = append(items, newItem)
  562. }
  563. if !isHasConditionTwo {
  564. newItem := &otherItemAmount{
  565. Total: 0,
  566. Name: "40~50kg",
  567. Ratio: "0",
  568. }
  569. items = append(items, newItem)
  570. }
  571. if !isHasConditionThree {
  572. newItem := &otherItemAmount{
  573. Total: 0,
  574. Name: "60~70kg",
  575. Ratio: "0",
  576. }
  577. items = append(items, newItem)
  578. }
  579. if !isHasConditionFour {
  580. newItem := &otherItemAmount{
  581. Total: 0,
  582. Name: "大于70kg",
  583. Ratio: "0",
  584. }
  585. items = append(items, newItem)
  586. }
  587. if !isHasConditionFive {
  588. newItem := &otherItemAmount{
  589. Total: 0,
  590. Name: "未知",
  591. Ratio: "0",
  592. }
  593. items = append(items, newItem)
  594. }
  595. for _, item := range items {
  596. if math.IsNaN(float64(item.Total) / float64(Total)) {
  597. item.Ratio = "0.0"
  598. } else {
  599. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(Total))*100), 64)
  600. item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
  601. }
  602. }
  603. break
  604. }
  605. if tempErr != nil {
  606. return nil, err
  607. }
  608. fmt.Println(items)
  609. return items, nil
  610. }
  611. func GetDialysisWeightTableData(orgID, patientID int64, page, limit, start, end int64) ([]*VMPredialysisEvaluation, int64, error) {
  612. offset := (page - 1) * limit
  613. var total int64
  614. var err error
  615. var orders []*VMPredialysisEvaluation
  616. readDb := service.XTReadDB()
  617. db := readDb.Table("xt_assessment_before_dislysis")
  618. db = db.Where(" user_org_id=? and status=1", orgID)
  619. if patientID > 0 {
  620. db = db.Where("patient_id=?", patientID)
  621. }
  622. if start != 0 {
  623. db = db.Where("assessment_date>=?", start)
  624. }
  625. if end != 0 {
  626. db = db.Where("assessment_date<=?", end)
  627. }
  628. db = db.Preload("VMAssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).Preload("VMPatients", "status = 1 AND user_org_id = ?", orgID)
  629. err = db.Count(&total).Offset(offset).Limit(limit).Order("assessment_date desc").Select("dry_weight, weight_before, patient_id, assessment_date").Find(&orders).Error
  630. for _, item := range orders {
  631. after, _ := GetLastAfterWeight(orgID, item.AssessmentDate, item.PatientId)
  632. if after.ID > 0 {
  633. fmt.Println(after.WeightAfter)
  634. item.LastAfterWeight = after.WeightAfter
  635. }
  636. }
  637. return orders, total, err
  638. }
  639. type LastAfter struct {
  640. ID int64 `json:"id"`
  641. WeightAfter float64 `json:"weight_after"`
  642. }
  643. func GetLastAfterWeight(user_org_id int64, before_assessment_date int64, patient_id int64) (after LastAfter, err error) {
  644. readDb := service.XTReadDB()
  645. err = readDb.Raw("SELECT weight_after as weight_after,id as id from xt_assessment_after_dislysis WHERE (user_org_id = ? AND patient_id = ? AND assessment_date < ?) ORDER BY id desc,`xt_assessment_after_dislysis`.`id` ASC LIMIT 1 ", user_org_id, patient_id, before_assessment_date).Scan(&after).Error
  646. return
  647. }
  648. func GetPatientDialysisWeightChartData(user_org_id int64, patient_id int64, start_time int64, end_time int64, statistics_type int) (datas []*ProcessIndexDataStruct, err error) {
  649. db := service.XTReadDB()
  650. switch statistics_type {
  651. case 1:
  652. err = db.Raw("select from_unixtime(assessment_date, '%Y-%m-%d') as date, dry_weight as value from xt_assessment_before_dislysis Where status = 1 AND user_org_id = ? AND patient_id = ? AND created_time <= ? AND created_time >= ? ", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  653. break
  654. case 2:
  655. err = db.Raw("select from_unixtime(assessment_date, '%Y-%m-%d') as date, weight_before as value from xt_assessment_before_dislysis Where status = 1 AND user_org_id = ? AND patient_id = ? AND created_time <= ? AND created_time >= ? ", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  656. break
  657. case 3:
  658. type weight struct {
  659. WeightAfter float64 `json:"weight_after"`
  660. WeightBefore float64 `json:"weight_before"`
  661. Date string `json:"date"`
  662. }
  663. var weights []*weight
  664. db.Raw("Select xt_assessment_after_dislysis.weight_after as weight_after ,xt_assessment_before_dislysis.weight_before as weight_before,from_unixtime(xt_assessment_before_dislysis.assessment_date,'%Y-%m-%d') as date from `xt_assessment_before_dislysis` LEFT JOIN `xt_assessment_after_dislysis` On xt_assessment_before_dislysis.`patient_id` = xt_assessment_after_dislysis.`patient_id` AND xt_assessment_before_dislysis.assessment_date = xt_assessment_after_dislysis.assessment_date AND xt_assessment_after_dislysis.weight_after <> 0 Where xt_assessment_before_dislysis.user_org_id = ? AND xt_assessment_before_dislysis.created_time >= ? AND xt_assessment_before_dislysis.created_time <= ? AND xt_assessment_before_dislysis.patient_id = ? Order by date desc", user_org_id, start_time, end_time, patient_id).Scan(&weights)
  665. fmt.Println(len(weights))
  666. for index, _ := range weights {
  667. if index+1 < len(weights) {
  668. var weight_add ProcessIndexDataStruct
  669. fmt.Println(weights[index+1].WeightAfter)
  670. fmt.Println(weights[index].WeightBefore)
  671. if weights[index+1].WeightAfter == 0 || weights[index].WeightBefore == 0 {
  672. continue
  673. }
  674. add := weights[index].WeightBefore - weights[index+1].WeightAfter
  675. weight_add.Value = add
  676. weight_add.Date = weights[index].Date
  677. datas = append(datas, &weight_add)
  678. }
  679. }
  680. break
  681. case 4:
  682. err = db.Raw("select from_unixtime(assessment_date, '%Y-%m-%d') as date, weight_after as value from xt_assessment_after_dislysis Where status = 1 AND user_org_id = ? AND patient_id = ? AND created_time <= ? AND created_time >= ? ", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  683. break
  684. }
  685. if err != nil {
  686. return nil, err
  687. }
  688. return datas, nil
  689. }
  690. //透析体重统计
  691. //透析血压统计
  692. func GetBloodPressureChartData(user_org_id int64, start_time int64, end_time int64, statistics_type int, patient_id int64) (item []*otherItemAmount, err error) {
  693. db := service.XTReadDB()
  694. var items []*otherItemAmount
  695. var tempErr error
  696. var TotalOne int64
  697. var TotalTwo int64
  698. var Total int64
  699. switch statistics_type {
  700. case 1:
  701. type bp struct {
  702. SystolicBloodPressure float64 `json:"systolic_blood_pressure"`
  703. DiastolicBloodPressure float64 `json:"diastolic_blood_pressure"`
  704. }
  705. var bps []*bp
  706. if patient_id == 0 {
  707. db.Raw("SELECT systolic_blood_pressure,diastolic_blood_pressure FROM xt_assessment_before_dislysis Where user_org_id = ? AND created_time >= ? AND created_time <= ? AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0 UNION ALL SELECT systolic_blood_pressure,diastolic_blood_pressure FROM xt_assessment_after_dislysis Where user_org_id = ? AND created_time >= ? AND created_time <= ? AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0 ", user_org_id, start_time, end_time, user_org_id, start_time, end_time).Scan(&bps)
  708. db.Raw("SELECT count(id) FROM xt_assessment_before_dislysis Where user_org_id = ? AND created_time >= ? AND created_time <= ? AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0 ", user_org_id, start_time, end_time).Count(&TotalOne)
  709. db.Raw("SELECT count(id) FROM xt_assessment_after_dislysis Where user_org_id = ? AND created_time >= ? AND created_time <= ? AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0 ", user_org_id, start_time, end_time).Count(&TotalTwo)
  710. Total = TotalOne + TotalTwo
  711. fmt.Println("----Total-----")
  712. fmt.Println(TotalOne)
  713. fmt.Println(TotalTwo)
  714. fmt.Println(Total)
  715. } else {
  716. db.Raw("SELECT systolic_blood_pressure,diastolic_blood_pressure FROM xt_assessment_before_dislysis Where user_org_id = ? AND created_time >= ? AND created_time <= ? AND patient_id = ? AND AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0 UNION ALL SELECT systolic_blood_pressure,diastolic_blood_pressure FROM xt_assessment_after_dislysis Where user_org_id = ? AND created_time >= ? AND created_time <= ? AND patient_id = ? AND AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0 ", user_org_id, start_time, end_time, patient_id, user_org_id, start_time, end_time, patient_id).Scan(&bps)
  717. db.Raw("SELECT count(id) FROM xt_assessment_before_dislysis Where user_org_id = ? AND created_time >= ? AND created_time <= ? AND patient_id = ? AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0 ", user_org_id, start_time, end_time, patient_id).Count(&TotalOne)
  718. db.Raw("SELECT count(id) FROM xt_assessment_after_dislysis Where user_org_id = ? AND created_time >= ? AND created_time <= ? AND patient_id = ? AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0 ", user_org_id, start_time, end_time, patient_id).Count(&TotalTwo)
  719. Total = TotalOne + TotalTwo
  720. fmt.Println(TotalOne)
  721. fmt.Println(TotalTwo)
  722. fmt.Println(Total)
  723. }
  724. var total_one int64
  725. var total_two int64
  726. var total_three int64
  727. var total_four int64
  728. for _, item := range bps {
  729. if item.SystolicBloodPressure > 160 && item.DiastolicBloodPressure > 100 {
  730. total_one++
  731. } else if item.SystolicBloodPressure < 140 && item.DiastolicBloodPressure < 90 {
  732. total_two++
  733. } else if item.SystolicBloodPressure >= 140 && item.SystolicBloodPressure <= 160 && item.DiastolicBloodPressure >= 90 && item.DiastolicBloodPressure <= 100 {
  734. total_three++
  735. } else {
  736. total_four++
  737. }
  738. }
  739. newItem := &otherItemAmount{
  740. Total: total_one,
  741. Name: "大于160/100mmHg",
  742. Ratio: "0",
  743. }
  744. newItem1 := &otherItemAmount{
  745. Total: total_two,
  746. Name: "小于140/90mmHg",
  747. Ratio: "0",
  748. }
  749. newItem2 := &otherItemAmount{
  750. Total: total_three,
  751. Name: "140-160mmHg/90-100mmHg",
  752. Ratio: "0",
  753. }
  754. newItem3 := &otherItemAmount{
  755. Total: total_four,
  756. Name: "其他",
  757. Ratio: "0",
  758. }
  759. items = append(items, newItem)
  760. items = append(items, newItem1)
  761. items = append(items, newItem2)
  762. items = append(items, newItem3)
  763. for _, item := range items {
  764. if math.IsNaN(float64(item.Total) / float64(Total)) {
  765. item.Ratio = "0.0"
  766. } else {
  767. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(Total))*100), 64)
  768. item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
  769. }
  770. }
  771. break
  772. case 2:
  773. if patient_id == 0 {
  774. tempErr = db.Table("xt_assessment_before_dislysis ").Where("user_org_id=? and status=1 and created_time >= ? and created_time <= ? AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0 ", user_org_id, start_time, end_time).
  775. Select("CASE WHEN systolic_blood_pressure > 160 AND diastolic_blood_pressure > 100 THEN '大于160/100mmHg'" +
  776. " WHEN systolic_blood_pressure < 140 AND diastolic_blood_pressure < 90 THEN '小于140/90mmHg'" +
  777. " WHEN systolic_blood_pressure >= 140 AND systolic_blood_pressure <= 160 AND diastolic_blood_pressure >= 90 AND diastolic_blood_pressure <= 100 THEN '140-160mmHg/90-100mmHg'" +
  778. " ELSE '其他' END AS name, COUNT(*) AS total",
  779. ).Group("name").Scan(&items).Error
  780. db.Table("xt_assessment_before_dislysis ").Where("user_org_id=? AND status = 1 AND created_time >= ? AND created_time <= ? AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0 ", user_org_id, start_time, end_time).Count(&Total)
  781. } else {
  782. tempErr = db.Table("xt_assessment_before_dislysis ").Where("user_org_id=? and status=1 and created_time >= ? and created_time <= ? and patient_id = ? AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0 ", user_org_id, start_time, end_time, patient_id).
  783. Select("CASE WHEN systolic_blood_pressure > 160 AND diastolic_blood_pressure > 100 THEN '大于160/100mmHg'" +
  784. " WHEN systolic_blood_pressure < 140 AND diastolic_blood_pressure < 90 THEN '小于140/90mmHg'" +
  785. " WHEN systolic_blood_pressure >= 140 AND systolic_blood_pressure <= 160 AND diastolic_blood_pressure >= 90 AND diastolic_blood_pressure <= 100 THEN '140-160mmHg/90-100mmHg'" +
  786. " ELSE '其他' END AS name, COUNT(*) AS total",
  787. ).Group("name").Scan(&items).Error
  788. db.Table("xt_assessment_before_dislysis ").Where("user_org_id=? AND status = 1 AND created_time >= ? AND created_time <= ? AND patient_id = ? AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0", user_org_id, start_time, end_time, patient_id).Count(&Total)
  789. }
  790. var isHasConditionOne bool = false
  791. var isHasConditionTwo bool = false
  792. var isHasConditionThree bool = false
  793. var isHasConditionFour bool = false
  794. for _, item := range items {
  795. if item.Name == "大于160/100mmHg" {
  796. isHasConditionOne = true
  797. }
  798. if item.Name == "小于140/90mmHg" {
  799. isHasConditionTwo = true
  800. }
  801. if item.Name == "140-160mmHg/90-100mmHg" {
  802. isHasConditionThree = true
  803. }
  804. if item.Name == "其他" {
  805. isHasConditionFour = true
  806. }
  807. }
  808. if !isHasConditionOne {
  809. newItem := &otherItemAmount{
  810. Total: 0,
  811. Name: "大于160/100mmHg",
  812. Ratio: "0",
  813. }
  814. items = append(items, newItem)
  815. }
  816. if !isHasConditionTwo {
  817. newItem := &otherItemAmount{
  818. Total: 0,
  819. Name: "小于140/90mmHg",
  820. Ratio: "0",
  821. }
  822. items = append(items, newItem)
  823. }
  824. if !isHasConditionThree {
  825. newItem := &otherItemAmount{
  826. Total: 0,
  827. Name: "140-160mmHg/90-100mmHg",
  828. Ratio: "0",
  829. }
  830. items = append(items, newItem)
  831. }
  832. if !isHasConditionFour {
  833. newItem := &otherItemAmount{
  834. Total: 0,
  835. Name: "其他",
  836. Ratio: "0",
  837. }
  838. items = append(items, newItem)
  839. }
  840. for _, item := range items {
  841. if math.IsNaN(float64(item.Total) / float64(Total)) {
  842. item.Ratio = "0.0"
  843. } else {
  844. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(Total))*100), 64)
  845. item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
  846. }
  847. }
  848. break
  849. case 3:
  850. if patient_id == 0 {
  851. tempErr = db.Table("xt_assessment_after_dislysis ").Where("user_org_id=? and status=1 and created_time >= ? and created_time <= ? AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0 ", user_org_id, start_time, end_time).
  852. Select("CASE WHEN systolic_blood_pressure > 160 AND diastolic_blood_pressure > 100 THEN '大于160/100mmHg'" +
  853. " WHEN systolic_blood_pressure < 140 AND diastolic_blood_pressure < 90 THEN '小于140/90mmHg'" +
  854. " WHEN systolic_blood_pressure >= 140 AND systolic_blood_pressure <= 160 AND diastolic_blood_pressure >= 90 AND diastolic_blood_pressure <= 100 THEN '140-160mmHg/90-100mmHg'" +
  855. " ELSE '其他' END AS name, COUNT(*) AS total",
  856. ).Group("name").Scan(&items).Error
  857. db.Table("xt_assessment_after_dislysis").Where("user_org_id=? AND status = 1 AND created_time >= ? AND created_time <= ? AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0 ", user_org_id, start_time, end_time).Count(&Total)
  858. } else {
  859. tempErr = db.Table("xt_assessment_after_dislysis ").Where("user_org_id=? and status=1 and created_time >= ? and created_time <= ? and patient_id = ? AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0", user_org_id, start_time, end_time, patient_id).
  860. Select("CASE WHEN systolic_blood_pressure > 160 AND diastolic_blood_pressure > 100 THEN '大于160/100mmHg'" +
  861. " WHEN systolic_blood_pressure < 140 AND diastolic_blood_pressure < 90 THEN '小于140/90mmHg'" +
  862. " WHEN systolic_blood_pressure >= 140 AND systolic_blood_pressure <= 160 AND diastolic_blood_pressure >= 90 AND diastolic_blood_pressure <= 100 THEN '140-160mmHg/90-100mmHg'" +
  863. " ELSE '其他' END AS name, COUNT(*) AS total",
  864. ).Group("name").Scan(&items).Error
  865. db.Table("xt_assessment_after_dislysis ").Where("user_org_id=? AND status = 1 AND created_time >= ? AND created_time <= ? AND patient_id = ? AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0 ", user_org_id, start_time, end_time, patient_id).Count(&Total)
  866. }
  867. var isHasConditionOne bool = false
  868. var isHasConditionTwo bool = false
  869. var isHasConditionThree bool = false
  870. var isHasConditionFour bool = false
  871. for _, item := range items {
  872. if item.Name == "大于160/100mmHg" {
  873. isHasConditionOne = true
  874. }
  875. if item.Name == "小于140/90mmHg" {
  876. isHasConditionTwo = true
  877. }
  878. if item.Name == "140-160mmHg/90-100mmHg" {
  879. isHasConditionThree = true
  880. }
  881. if item.Name == "其他" {
  882. isHasConditionFour = true
  883. }
  884. }
  885. if !isHasConditionOne {
  886. newItem := &otherItemAmount{
  887. Total: 0,
  888. Name: "大于160/100mmHg",
  889. Ratio: "0",
  890. }
  891. items = append(items, newItem)
  892. }
  893. if !isHasConditionTwo {
  894. newItem := &otherItemAmount{
  895. Total: 0,
  896. Name: "小于140/90mmHg",
  897. Ratio: "0",
  898. }
  899. items = append(items, newItem)
  900. }
  901. if !isHasConditionThree {
  902. newItem := &otherItemAmount{
  903. Total: 0,
  904. Name: "140-160mmHg/90-100mmHg",
  905. Ratio: "0",
  906. }
  907. items = append(items, newItem)
  908. }
  909. if !isHasConditionFour {
  910. newItem := &otherItemAmount{
  911. Total: 0,
  912. Name: "其他",
  913. Ratio: "0",
  914. }
  915. items = append(items, newItem)
  916. }
  917. for _, item := range items {
  918. if math.IsNaN(float64(item.Total) / float64(Total)) {
  919. item.Ratio = "0.0"
  920. } else {
  921. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(Total))*100), 64)
  922. item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
  923. }
  924. }
  925. break
  926. }
  927. if tempErr != nil {
  928. return nil, err
  929. }
  930. return items, nil
  931. }
  932. func GetBloodPressureTableData(orgID, patientID int64, page, limit, start, end int64) ([]*VMSchedule, int64, error) {
  933. offset := (page - 1) * limit
  934. var total int64
  935. var err error
  936. var orders []*VMSchedule
  937. readDb := service.XTReadDB()
  938. db := readDb.Table("xt_schedule as sch")
  939. db = db.Where(" sch.user_org_id=? and sch.status=1", orgID)
  940. if patientID > 0 {
  941. db = db.Where("sch.patient_id=?", patientID)
  942. }
  943. if start != 0 {
  944. db = db.Where("sch.schedule_date>=?", start)
  945. }
  946. if end != 0 {
  947. db = db.Where("sch.schedule_date<=?", end)
  948. }
  949. db = db.Preload("VMPredialysisEvaluation", "status = 1 AND user_org_id = ?", orgID).
  950. Preload("VMAssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  951. Preload("VMMinMonitoringRecord", func(db *gorm.DB) *gorm.DB {
  952. return db.Where("user_org_id = ? AND status = 1", orgID).Order("diastolic_blood_pressure ASC")
  953. }).
  954. Preload("VMMaxMonitoringRecord", func(db *gorm.DB) *gorm.DB {
  955. return db.Where("user_org_id = ? AND status = 1 ", orgID).Order("systolic_blood_pressure DESC")
  956. }).
  957. Preload("VMPatients", "status = 1 AND user_org_id = ?", orgID)
  958. err = db.Count(&total).Offset(offset).Limit(limit).Order("sch.schedule_date desc").Select(" sch.schedule_date,sch.patient_id,sch.user_org_id").Find(&orders).Error
  959. return orders, total, err
  960. }
  961. type BPDataStruct struct {
  962. Date string `json:"date"`
  963. SystolicBloodPressure float64 `json:"systolic_blood_pressure"`
  964. DiastolicBloodPressure float64 `json:"diastolic_blood_pressure"`
  965. }
  966. func GetPatientBloodPressureChartData(user_org_id int64, patient_id int64, start_time int64, end_time int64, statistics_type int) (datas []*BPDataStruct, err error) {
  967. db := service.XTReadDB()
  968. switch statistics_type {
  969. case 1:
  970. err = db.Raw("select from_unixtime(assessment_date, '%Y-%m-%d') as date, systolic_blood_pressure as systolic_blood_pressure, diastolic_blood_pressure as diastolic_blood_pressure from xt_assessment_before_dislysis Where status = 1 AND user_org_id = ? AND patient_id = ? AND created_time <= ? AND created_time >= ? AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0 ", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  971. break
  972. case 2:
  973. err = db.Raw("select from_unixtime(assessment_date, '%Y-%m-%d') as date, systolic_blood_pressure as systolic_blood_pressure, diastolic_blood_pressure as diastolic_blood_pressure from xt_assessment_after_dislysis Where status = 1 AND user_org_id = ? AND patient_id = ? AND created_time <= ? AND created_time >= ? AND systolic_blood_pressure <> 0 AND diastolic_blood_pressure <> 0 ", user_org_id, patient_id, end_time, start_time).Scan(&datas).Error
  974. break
  975. }
  976. if err != nil {
  977. return nil, err
  978. }
  979. return datas, nil
  980. }
  981. //透析血压统计
  982. //工作量统计
  983. func GetDoctorWorkloadChartData(user_org_id int64, start_time int64, end_time int64, statistics_type int, admin_user_id int64) (datas []*VMUserAdminRole, err error) {
  984. userDb := service.UserReadDB()
  985. db := service.XTReadDB()
  986. if admin_user_id == 0 {
  987. userDb.Raw("select admin_user_id, user_name,org_id from sgj_user_admin_role Where status = 1 AND org_id = ? AND user_type = 2 ", user_org_id).Scan(&datas)
  988. } else {
  989. userDb.Raw("select admin_user_id, user_name,org_id from sgj_user_admin_role Where status = 1 AND org_id = ? AND user_type = 2 AND admin_user_id = ?", user_org_id, admin_user_id).Scan(&datas)
  990. }
  991. switch statistics_type {
  992. case 1:
  993. var Total int64
  994. db.Table("xt_dialysis_prescription as p").Joins("join xt_schedule sch On sch.schedule_date = p.record_date AND sch.patient_id = p.patient_id AND sch.user_org_id = ? AND sch.status = 1", user_org_id).Where("p.user_org_id = ? AND p.status = 1 AND p.record_date >= ? AND p.record_date <= ? AND p.creater <> 0 ", user_org_id, start_time, end_time).Count(&Total)
  995. for _, item := range datas {
  996. var total int64
  997. db.Table("xt_dialysis_prescription as p").Joins("join xt_schedule sch On sch.schedule_date = p.record_date AND sch.patient_id = p.patient_id AND sch.user_org_id = ? AND sch.status = 1", user_org_id).Where("p.user_org_id = ? AND p.status = 1 AND p.creater = ? AND p.record_date >= ? AND p.record_date <= ?", user_org_id, item.AdminUserId, start_time, end_time).Count(&total)
  998. item.PCount = total
  999. if math.IsNaN(float64(item.PCount) / float64(Total)) {
  1000. item.PRatio = "0.0"
  1001. } else {
  1002. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.PCount)/float64(Total))*100), 64)
  1003. item.PRatio = strconv.FormatFloat(float_value, 'f', 1, 32)
  1004. }
  1005. }
  1006. break
  1007. case 2:
  1008. var Total int64
  1009. db.Table("xt_patient_course_of_disease as course").Where("org_id = ? AND status = 1 AND record_time >= ? AND record_time <= ?", user_org_id, start_time, end_time).Count(&Total)
  1010. for _, item := range datas {
  1011. var total int64
  1012. db.Table("xt_patient_course_of_disease as course").Where("org_id = ? AND status = 1 AND recorder = ? AND record_time >= ? AND record_time <= ?", user_org_id, item.AdminUserId, start_time, end_time).Count(&total)
  1013. item.CourseCount = total
  1014. if math.IsNaN(float64(item.CourseCount) / float64(Total)) {
  1015. item.CourseRatio = "0.0"
  1016. } else {
  1017. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.CourseCount)/float64(Total))*100), 64)
  1018. item.CourseRatio = strconv.FormatFloat(float_value, 'f', 1, 32)
  1019. }
  1020. }
  1021. break
  1022. case 3:
  1023. var Total int64
  1024. db.Table("xt_patient_rescue_record as rescue").Where("org_id = ? AND status = 1 AND record_time >= ? AND record_time <= ?", user_org_id, start_time, end_time).Count(&Total)
  1025. for _, item := range datas {
  1026. var total int64
  1027. db.Table("xt_patient_rescue_record as rescue").Where("org_id = ? AND status = 1 AND recorder = ? AND record_time >= ? AND record_time <= ?", user_org_id, item.AdminUserId, start_time, end_time).Count(&total)
  1028. item.RescueCount = total
  1029. if math.IsNaN(float64(item.RescueCount) / float64(Total)) {
  1030. item.RescueRatio = "0.0"
  1031. } else {
  1032. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.RescueCount)/float64(Total))*100), 64)
  1033. item.RescueRatio = strconv.FormatFloat(float_value, 'f', 1, 32)
  1034. }
  1035. }
  1036. break
  1037. }
  1038. if err != nil {
  1039. return nil, err
  1040. }
  1041. fmt.Println(datas)
  1042. return datas, nil
  1043. }
  1044. func GetDoctorWorkloadTableData(user_org_id int64, start_time int64, end_time int64, admin_user_id int64) (datas []*VMUserAdminRole, err error) {
  1045. userDb := service.UserReadDB()
  1046. db := service.XTReadDB()
  1047. if admin_user_id == 0 {
  1048. userDb.Raw("select admin_user_id, user_name,org_id from sgj_user_admin_role Where status = 1 AND org_id = ? AND user_type = 2 ", user_org_id).Scan(&datas)
  1049. } else {
  1050. userDb.Raw("select admin_user_id, user_name,org_id from sgj_user_admin_role Where status = 1 AND org_id = ? AND user_type = 2 AND admin_user_id = ?", user_org_id, admin_user_id).Scan(&datas)
  1051. }
  1052. for _, item := range datas {
  1053. var Total int64
  1054. var TotalOne int64
  1055. var TotalTwo int64
  1056. db.Table("xt_dialysis_prescription as p").Joins("join xt_schedule sch On sch.schedule_date = p.record_date AND sch.patient_id = p.patient_id AND sch.user_org_id = ? AND sch.status = 1", user_org_id).Where("p.user_org_id = ? AND p.status = 1 AND p.creater = ? AND p.record_date >= ? AND p.record_date <= ?", user_org_id, item.AdminUserId, start_time, end_time).Count(&Total)
  1057. db.Table("xt_patient_course_of_disease as course").Where("org_id = ? AND status = 1 AND recorder = ? AND record_time >= ? AND record_time <= ?", user_org_id, item.AdminUserId, start_time, end_time).Count(&TotalOne)
  1058. db.Table("xt_patient_rescue_record as rescue").Where("org_id = ? AND status = 1 AND recorder = ? AND record_time >= ? AND record_time <= ?", user_org_id, item.AdminUserId, start_time, end_time).Count(&TotalTwo)
  1059. item.PCount = Total
  1060. item.CourseCount = TotalOne
  1061. item.RescueCount = TotalTwo
  1062. }
  1063. if err != nil {
  1064. return nil, err
  1065. }
  1066. return datas, nil
  1067. }
  1068. func GetNurseWorkloadChartData(user_org_id int64, start_time int64, end_time int64, statistics_type int, admin_user_id int64) (datas []*VMUserAdminRole, err error) {
  1069. userDb := service.UserReadDB()
  1070. db := service.XTReadDB()
  1071. if admin_user_id == 0 {
  1072. userDb.Raw("select admin_user_id, user_name,org_id from sgj_user_admin_role Where status = 1 AND org_id = ? AND user_type = 3 ", user_org_id).Scan(&datas)
  1073. } else {
  1074. userDb.Raw("select admin_user_id, user_name,org_id from sgj_user_admin_role Where status = 1 AND org_id = ? AND user_type = 3 AND admin_user_id = ?", user_org_id, admin_user_id).Scan(&datas)
  1075. }
  1076. var NewTotalOne int64
  1077. var NewTotalThree int64
  1078. var NewTotalFour int64
  1079. db.Table("xt_dialysis_order as o").Joins("join xt_schedule sch On sch.schedule_date = o.dialysis_date AND sch.patient_id = o.patient_id AND sch.user_org_id = ? AND sch.status = 1", user_org_id).Where("o.user_org_id = ? AND o.status = 1 AND o.dialysis_date >= ? AND o.dialysis_date <= ?", user_org_id, start_time, end_time).Count(&NewTotalOne)
  1080. db.Table("xt_monitoring_record as monitor ").Joins("join xt_schedule sch On sch.schedule_date = monitor.monitoring_date AND sch.patient_id = monitor.patient_id AND sch.user_org_id = ? AND sch.status = 1", user_org_id).Where("monitor.user_org_id = ? AND monitor.status = 1 AND monitor.monitoring_date >= ? AND monitor.monitoring_date <= ?", user_org_id, start_time, end_time).Count(&NewTotalThree)
  1081. db.Table("xt_treatment_summary as summary").Joins("join xt_schedule sch On sch.schedule_date = summary.assessment_date AND sch.patient_id = summary.patient_id AND sch.user_org_id = ? AND sch.status = 1", user_org_id).Where("summary.user_org_id = ? AND summary.status = 1 AND summary.assessment_date >= ? AND summary.assessment_date <= ?", user_org_id, start_time, end_time).Count(&NewTotalFour)
  1082. switch statistics_type {
  1083. case 2:
  1084. for _, item := range datas {
  1085. var Total int64
  1086. db.Table("xt_dialysis_order as o").Joins("join xt_schedule sch On sch.schedule_date = o.dialysis_date AND sch.patient_id = o.patient_id AND sch.user_org_id = ? AND sch.status = 1", user_org_id).Where("o.user_org_id = ? AND o.status = 1 AND o.dialysis_date >= ? AND o.dialysis_date <= ? AND o.puncture_nurse = ?", user_org_id, start_time, end_time, item.AdminUserId).Count(&Total)
  1087. item.PunctureCount = Total
  1088. if math.IsNaN(float64(item.PunctureCount) / float64(NewTotalOne)) {
  1089. item.PunctureRatio = "0.0"
  1090. } else {
  1091. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.PunctureCount)/float64(NewTotalOne))*100), 64)
  1092. item.PunctureRatio = strconv.FormatFloat(float_value, 'f', 1, 32)
  1093. }
  1094. }
  1095. break
  1096. case 1:
  1097. for _, item := range datas {
  1098. var Total int64
  1099. db.Table("xt_monitoring_record as monitor").Joins("join xt_schedule sch On sch.schedule_date = monitor.monitoring_date AND sch.patient_id = monitor.patient_id AND sch.user_org_id = ? AND sch.status = 1", user_org_id).Where("monitor.user_org_id = ? AND monitor.status = 1 AND monitor.monitoring_nurse = ? AND monitor.monitoring_date >= ? AND monitor.monitoring_date <= ?", user_org_id, item.AdminUserId, start_time, end_time).Count(&Total)
  1100. item.CureCount = Total
  1101. if math.IsNaN(float64(item.CureCount) / float64(NewTotalThree)) {
  1102. item.CureRatio = "0.0"
  1103. } else {
  1104. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.CureCount)/float64(NewTotalThree))*100), 64)
  1105. item.CureRatio = strconv.FormatFloat(float_value, 'f', 1, 32)
  1106. }
  1107. }
  1108. break
  1109. case 3:
  1110. for _, item := range datas {
  1111. var Total int64
  1112. db.Table("xt_treatment_summary as summary ").Joins("join xt_schedule sch On sch.schedule_date = summary.assessment_date AND sch.patient_id = summary.patient_id AND sch.user_org_id = ? AND sch.status = 1", user_org_id).Where("summary.user_org_id = ? AND summary.status = 1 AND summary.creater = ? AND summary.assessment_date >= ? AND summary.assessment_date <= ?", user_org_id, item.AdminUserId, start_time, end_time).Count(&Total)
  1113. item.MissionCount = Total
  1114. if math.IsNaN(float64(item.MissionCount) / float64(NewTotalFour)) {
  1115. item.MissionRatio = "0.0"
  1116. } else {
  1117. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.MissionCount)/float64(NewTotalFour))*100), 64)
  1118. item.MissionRatio = strconv.FormatFloat(float_value, 'f', 1, 32)
  1119. }
  1120. }
  1121. break
  1122. }
  1123. if err != nil {
  1124. return nil, err
  1125. }
  1126. return datas, nil
  1127. }
  1128. func GetNurseWorkloadTableData(user_org_id int64, start_time int64, end_time int64, admin_user_id int64, page int64, limit int64) (datas []*VMUserAdminRole, AdminTotal int64, err error) {
  1129. userDb := service.UserReadDB()
  1130. db := service.XTReadDB()
  1131. //var tempConfig models.Dataconfig
  1132. offset := (page - 1) * limit
  1133. //db.Model(&models.Dataconfig{}).Where("name = '血管通路' AND module = 'hemodialysis' AND field_name = 'vascular_access_desc' AND parent_id = 0").First(&tempConfig)
  1134. if admin_user_id == 0 {
  1135. userDb.Raw("select admin_user_id, user_name,org_id from sgj_user_admin_role Where status = 1 AND org_id = ? AND user_type = 3 ", user_org_id).Offset(offset).Limit(limit).Scan(&datas)
  1136. } else {
  1137. userDb.Raw("select admin_user_id, user_name,org_id from sgj_user_admin_role Where status = 1 AND org_id = ? AND user_type = 3 AND admin_user_id = ?", user_org_id, admin_user_id).Scan(&datas)
  1138. }
  1139. for _, item := range datas {
  1140. var Total int64
  1141. var TotalTwo int64
  1142. var TotalThree int64
  1143. //db.Raw("select count(*) as total from (select d.patient_id as patient, d.id, d.`assessment_date` as date,config.name as name from xt_assessment_before_dislysis as d JOIN `xt_data_config` config on d.`blood_access_part_opera_id` = config.value AND config.org_id = ? AND config.parent_id = ? AND FIND_IN_SET('内瘘',config.name) > 0 Where d.user_org_id = ? AND d.assessment_date >=? AND d.assessment_date <= ? Group by d.id) b JOIN `xt_dialysis_order` on xt_dialysis_order.`dialysis_date` = b.date AND xt_dialysis_order.user_org_id = ? AND xt_dialysis_order.status = 1 AND xt_dialysis_order.start_nurse = ? AND xt_dialysis_order.dialysis_date >= ? AND xt_dialysis_order.dialysis_date <= ? ", user_org_id, tempConfig.ID, user_org_id, start_time, end_time, user_org_id, item.AdminUserId, start_time, end_time).Count(&Total)
  1144. //db.Raw("select count(*) as total_one from (select d.patient_id as patient, d.id, d.`assessment_date` as date,config.name as name from xt_assessment_before_dislysis as d JOIN `xt_data_config` config on d.`blood_access_part_opera_id` = config.value AND config.org_id = ? AND config.parent_id = ? AND FIND_IN_SET('导管',config.name) > 0 Where d.user_org_id = ? AND d.assessment_date >=? AND d.assessment_date <= ? Group by d.id) b JOIN `xt_dialysis_order` on xt_dialysis_order.`dialysis_date` = b.date AND xt_dialysis_order.user_org_id = ? AND xt_dialysis_order.status = 1 AND xt_dialysis_order.start_nurse = ? AND xt_dialysis_order.dialysis_date >= ? AND xt_dialysis_order.dialysis_date <= ? ", user_org_id, tempConfig.ID, user_org_id, start_time, end_time, user_org_id, item.AdminUserId, start_time, end_time).Count(&TotalOne)
  1145. db.Table("xt_monitoring_record as monitor ").Joins("join xt_schedule sch On sch.schedule_date = monitor.monitoring_date AND sch.patient_id = monitor.patient_id AND sch.user_org_id = ? AND sch.status = 1", user_org_id).Where("monitor.user_org_id = ? AND monitor.status = 1 AND monitor.monitoring_nurse = ? AND monitor.monitoring_date >= ? AND monitor.monitoring_date <= ?", user_org_id, item.AdminUserId, start_time, end_time).Count(&TotalTwo)
  1146. db.Table("xt_dialysis_order as o").Joins("join xt_schedule sch On sch.schedule_date = o.dialysis_date AND sch.patient_id = o.patient_id AND sch.user_org_id = ? AND sch.status = 1", user_org_id).Where("o.user_org_id = ? AND o.status = 1 AND o.dialysis_date >= ? AND o.dialysis_date <= ? AND o.puncture_nurse = ?", user_org_id, start_time, end_time, item.AdminUserId).Count(&Total)
  1147. db.Table("xt_treatment_summary as summary").Joins("join xt_schedule sch On sch.schedule_date = summary.assessment_date AND sch.patient_id = summary.patient_id AND sch.user_org_id = ? AND sch.status = 1", user_org_id).Where("summary.user_org_id = ? AND summary.status = 1 AND summary.creater = ? AND summary.assessment_date >= ? AND summary.assessment_date <= ?", user_org_id, item.AdminUserId, start_time, end_time).Count(&TotalThree)
  1148. item.PunctureCount = Total
  1149. item.CureCount = TotalTwo
  1150. item.MissionCount = TotalThree
  1151. }
  1152. if err != nil {
  1153. return nil, AdminTotal, err
  1154. }
  1155. return datas, AdminTotal, nil
  1156. }
  1157. //工作量统计
  1158. type AdminUser struct {
  1159. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1160. AdminUserId int64 `gorm:"column:admin_user_id" json:"admin_user_id" form:"admin_user_id"`
  1161. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1162. UserName string `gorm:"column:user_name" json:"user_name" form:"user_name"`
  1163. UserType int64 `gorm:"column:user_type" json:"user_type" form:"user_type"`
  1164. }
  1165. func GetAllAdminUser(user_org_id int64) (datas []*AdminUser, err error) {
  1166. userDb := service.UserReadDB()
  1167. userDb.Raw("select id, admin_user_id, user_name,org_id, user_type from sgj_user_admin_role Where status = 1 AND org_id = ?", user_org_id).Scan(&datas)
  1168. return datas, nil
  1169. }
  1170. //func FindOrgInspectionCount(user_org_id int64) (count int64, err error) {
  1171. // db := service.XTReadDB()
  1172. // err = db.Model(&models.QualityControlStandard{}).Where("status = 1 AND org_id = ? ", user_org_id).Count(&count).Error
  1173. // return
  1174. //}
  1175. type InspectionReference struct {
  1176. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1177. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1178. ProjectName string `gorm:"column:project_name" json:"project_name" form:"project_name"`
  1179. Project string `gorm:"column:project" json:"project" form:"project"`
  1180. ProjectId int64 `gorm:"column:project_id" json:"project_id" form:"project_id"`
  1181. ItemName string `gorm:"column:item_name" json:"item_name" form:"item_name"`
  1182. RangeType int64 `gorm:"column:range_type" json:"range_type" form:"range_type"`
  1183. RangeMin string `gorm:"column:range_min" json:"range_min" form:"range_min"`
  1184. RangeMax string `gorm:"column:range_max" json:"range_max" form:"range_max"`
  1185. RangeValue string `gorm:"column:range_value" json:"range_value" form:"range_value"`
  1186. Unit string `gorm:"column:unit" json:"unit" form:"unit"`
  1187. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1188. }
  1189. func FindOrgQualityControlStandardInspectionReference(user_org_id int64) (references []*InspectionReference, err error) {
  1190. db := service.XTReadDB()
  1191. err = db.Raw("SELECT reference.project_name AS project_name,qcs.inspection_major AS project_id, qcs.inspection_minor AS id,reference.item_name AS item_name,qcs.range_type AS range_type,reference.range_min AS range_min,reference.range_max AS range_max,reference.unit AS unit,qcs.range_value AS range_value FROM xt_quality_control_standard qcs LEFT JOIN xt_inspection_reference reference ON qcs.inspection_major = reference.project_id AND ( qcs.inspection_minor = reference.item_id AND reference.org_id = ?) OR (org_id = 0 AND qcs.inspection_minor = reference.id ) WHERE qcs.user_org_id = ? AND qcs.status = 1 AND reference.status = 1", user_org_id, user_org_id).Scan(&references).Error
  1192. return
  1193. }
  1194. func FindOrgInspectionReference(user_org_id int64, range_type int) (references []*models.InspectionReference, err error) {
  1195. db := service.XTReadDB()
  1196. if range_type == 1 {
  1197. err = db.Model(&models.InspectionReference{}).Where("status = 1 AND org_id = ? AND range_type = 1 AND (range_min REGEXP '[^0-9.]') = 0 AND (range_max REGEXP '[^0-9.]') = 0 ", user_org_id).Find(&references).Error
  1198. } else {
  1199. err = db.Model(&models.InspectionReference{}).Where("status = 1 AND org_id = ? AND range_type = 2", user_org_id).Find(&references).Error
  1200. }
  1201. return
  1202. }
  1203. func FindProcjectInspectionReference(user_org_id int64, project_id int64, item_id int64) (references models.InspectionReference, err error) {
  1204. db := service.XTReadDB()
  1205. err = db.Model(&models.InspectionReference{}).Where("status = 1 AND org_id = ? AND project_id = ? AND id = ?", user_org_id, project_id, item_id).First(&references).Error
  1206. return
  1207. }
  1208. func FindOrgInspectionReferenceRangeTypeValue(user_org_id int64, project_id int64, item_id int64) (inspection []*models.Inspection, err error) {
  1209. db := service.XTReadDB()
  1210. err = db.Model(&models.Inspection{}).Where("status = 1 AND org_id = ? AND project_id = ? AND item_id = ? ", user_org_id, project_id, item_id).Group("inspect_value").Find(&inspection).Error
  1211. return
  1212. }
  1213. func FindOrgConfigRangeTypeValue(user_org_id int64, project_id int64, item_id int64) (qcs models.InspectionReference, err error) {
  1214. db := service.XTReadDB()
  1215. err = db.Model(&models.InspectionReference{}).Where("status = 1 AND org_id = ? AND project_id = ? AND id = ?", user_org_id, project_id, item_id).First(&qcs).Error
  1216. return
  1217. }
  1218. func GetInspectionChartData(user_org_id int64, start_time int64, end_time int64, project_id int64, item_id int64, range_type int, range_value string) (item []*otherItemAmount, err error) {
  1219. db := service.XTReadDB()
  1220. var items []*otherItemAmount
  1221. var tempErr error
  1222. var Total int64
  1223. selectContent := "CASE"
  1224. range_value_arr := strings.Split(range_value, ",")
  1225. db.Table("xt_inspection").Where("org_id=? and status=1 and inspect_date >= ? and inspect_date <= ? AND project_id = ? AND item_id = ? AND inspect_value <> '' ", user_org_id, start_time, end_time, project_id, item_id).Count(&Total)
  1226. if range_type == 1 {
  1227. tempErr = db.Table("xt_inspection").Where("org_id=? and status=1 and inspect_date >= ? and inspect_date <= ? AND project_id = ? AND item_id = ? AND inspect_value <> ''", user_org_id, start_time, end_time, project_id, item_id).
  1228. Select("CASE WHEN inspect_value < " + range_value_arr[0] + " THEN " + "'" + "小于" + range_value_arr[0] + "'" +
  1229. " WHEN inspect_value >= " + range_value_arr[0] + " AND inspect_value <= " + range_value_arr[1] + " THEN " + "'" + range_value_arr[0] + "~" + range_value_arr[1] + "'" +
  1230. " WHEN inspect_value > " + range_value_arr[1] + " THEN " + "'" + "大于" + range_value_arr[1] + "'" +
  1231. " ELSE '未知' END AS name, COUNT(*) AS total",
  1232. ).Group("name").Scan(&items).Error
  1233. var isHasConditionOne bool = false
  1234. var isHasConditionTwo bool = false
  1235. var isHasConditionThree bool = false
  1236. for _, item := range items {
  1237. if item.Name == "大于"+range_value_arr[1] {
  1238. isHasConditionOne = true
  1239. }
  1240. if item.Name == "小于"+range_value_arr[0] {
  1241. isHasConditionTwo = true
  1242. }
  1243. if item.Name == range_value_arr[0]+"~"+range_value_arr[1] {
  1244. isHasConditionThree = true
  1245. }
  1246. }
  1247. if !isHasConditionOne {
  1248. newItem := &otherItemAmount{
  1249. Total: 0,
  1250. Name: "大于" + range_value_arr[1],
  1251. Ratio: "0",
  1252. }
  1253. items = append(items, newItem)
  1254. }
  1255. if !isHasConditionTwo {
  1256. newItem := &otherItemAmount{
  1257. Total: 0,
  1258. Name: "小于" + range_value_arr[0],
  1259. Ratio: "0",
  1260. }
  1261. items = append(items, newItem)
  1262. }
  1263. if !isHasConditionThree {
  1264. newItem := &otherItemAmount{
  1265. Total: 0,
  1266. Name: range_value_arr[0] + "~" + range_value_arr[1],
  1267. Ratio: "0",
  1268. }
  1269. items = append(items, newItem)
  1270. }
  1271. for _, item := range items {
  1272. if math.IsNaN(float64(item.Total) / float64(Total)) {
  1273. item.Ratio = "0.0"
  1274. } else {
  1275. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(Total))*100), 64)
  1276. item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
  1277. }
  1278. }
  1279. return items, tempErr
  1280. } else {
  1281. for _, item := range range_value_arr {
  1282. //selectContent = selectContent + " WHEN inspect_value =" + item + " THEN " + "'" + item + "'"
  1283. selectContent = selectContent + " WHEN inspect_value =" + "'" + item + "'" + " THEN " + "'" + item + "'"
  1284. }
  1285. tempErr = db.Table("xt_inspection").Where("org_id=? and status=1 and inspect_date >= ? and inspect_date <= ? AND project_id = ? AND item_id = ? AND inspect_value <> '' ", user_org_id, start_time, end_time, project_id, item_id).
  1286. Select(selectContent +
  1287. " ELSE '未知' END AS name, COUNT(*) AS total",
  1288. ).Group("name").Scan(&items).Error
  1289. for _, item := range items {
  1290. if math.IsNaN(float64(item.Total) / float64(Total)) {
  1291. item.Ratio = "0.0"
  1292. } else {
  1293. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(Total))*100), 64)
  1294. item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
  1295. }
  1296. }
  1297. return items, tempErr
  1298. }
  1299. }
  1300. type Inspection struct {
  1301. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1302. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1303. OrgId int64 `gorm:"column:org_id" json:"org_id" form:"org_id"`
  1304. ProjectId int64 `gorm:"column:project_id" json:"project_id" form:"project_id"`
  1305. ItemId int64 `gorm:"column:item_id" json:"item_id" form:"item_id"`
  1306. ItemName string `gorm:"column:item_name" json:"item_name" form:"item_name"`
  1307. ProjectName string `gorm:"column:project_name" json:"project_name" form:"project_name"`
  1308. InspectType int64 `gorm:"column:inspect_type" json:"inspect_type" form:"inspect_type"`
  1309. InspectValue string `gorm:"column:inspect_value" json:"inspect_value" form:"inspect_value"`
  1310. InspectDate int64 `gorm:"column:inspect_date" json:"inspect_date" form:"inspect_date"`
  1311. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1312. CreatedTime int64 `gorm:"column:created_time" json:"created_time" form:"created_time"`
  1313. UpdatedTime int64 `gorm:"column:updated_time" json:"updated_time" form:"updated_time"`
  1314. Patient models.Patients `gorm:"ForeignKey:ID;AssociationForeignKey:PatientId" json:"patient"`
  1315. }
  1316. func (Inspection) TableName() string {
  1317. return "xt_inspection"
  1318. }
  1319. func GetInspectionChartTableData(user_org_id int64, start_time int64, end_time int64, project_id int64, item_id int64, patient_id int64, page, limit int64, range_type int, range_value string) (datas []*Inspection, total int64, err error) {
  1320. if range_type == 1 {
  1321. index := strings.Index(range_value, "小于")
  1322. indexTwo := strings.Index(range_value, "大于")
  1323. indexThree := strings.Index(range_value, "~")
  1324. if index != -1 {
  1325. value, _ := strconv.ParseFloat(strings.Replace(range_value, "小于", "", 1), 64)
  1326. db := service.XTReadDB()
  1327. offset := (page - 1) * limit
  1328. err = db.Model(&Inspection{}).Where("org_id = ? AND status = 1 AND inspect_date >= ? AND inspect_date <= ? AND project_id = ? AND item_id = ? AND status = 1 AND inspect_value < ?", user_org_id, start_time, end_time, project_id, item_id, value).Preload("Patient", func(db *gorm.DB) *gorm.DB {
  1329. return db.Where("status = 1 AND user_org_id = ?", user_org_id)
  1330. }).Count(&total).Offset(offset).Limit(limit).Find(&datas).Error
  1331. return datas, total, err
  1332. } else if indexTwo != -1 {
  1333. value, _ := strconv.ParseFloat(strings.Replace(range_value, "大于", "", 1), 64)
  1334. db := service.XTReadDB()
  1335. offset := (page - 1) * limit
  1336. err = db.Model(&Inspection{}).Where("org_id = ? AND status = 1 AND inspect_date >= ? AND inspect_date <= ? AND project_id = ? AND item_id = ? AND status = 1 AND inspect_value > ?", user_org_id, start_time, end_time, project_id, item_id, value).Preload("Patient", func(db *gorm.DB) *gorm.DB {
  1337. return db.Where("status = 1 AND user_org_id = ?", user_org_id)
  1338. }).Count(&total).Offset(offset).Limit(limit).Find(&datas).Error
  1339. return datas, total, err
  1340. } else if indexThree != -1 {
  1341. range_value_arr := strings.Split(range_value, "~")
  1342. fmt.Println(range_value_arr)
  1343. float1, err := strconv.ParseFloat(range_value_arr[0], 64)
  1344. float2, err := strconv.ParseFloat(range_value_arr[1], 64)
  1345. fmt.Println(err)
  1346. fmt.Println(float1)
  1347. fmt.Println(float2)
  1348. db := service.XTReadDB()
  1349. offset := (page - 1) * limit
  1350. err = db.Model(&Inspection{}).Where("org_id = ? AND status = 1 AND inspect_date >= ? AND inspect_date <= ? AND project_id = ? AND item_id = ? AND status = 1 AND inspect_value >= ? AND inspect_value <= ?", user_org_id, start_time, end_time, project_id, item_id, float1, float2).Preload("Patient", func(db *gorm.DB) *gorm.DB {
  1351. return db.Where("status = 1 AND user_org_id = ?", user_org_id)
  1352. }).Count(&total).Offset(offset).Limit(limit).Find(&datas).Error
  1353. return datas, total, err
  1354. } else {
  1355. db := service.XTReadDB()
  1356. offset := (page - 1) * limit
  1357. err = db.Model(&Inspection{}).Where("org_id = ? AND status = 1 AND inspect_date >= ? AND inspect_date <= ? AND project_id = ? AND item_id = ? AND status = 1 ", user_org_id, start_time, end_time, project_id, item_id).Preload("Patient", func(db *gorm.DB) *gorm.DB {
  1358. return db.Where("status = 1 AND user_org_id = ?", user_org_id)
  1359. }).Count(&total).Offset(offset).Limit(limit).Find(&datas).Error
  1360. return datas, total, err
  1361. }
  1362. } else {
  1363. if range_value == "全部" {
  1364. db := service.XTReadDB()
  1365. offset := (page - 1) * limit
  1366. err = db.Model(&Inspection{}).Where("org_id = ? AND status = 1 AND inspect_date >= ? AND inspect_date <= ? AND project_id = ? AND item_id = ? AND status = 1 ", user_org_id, start_time, end_time, project_id, item_id).Preload("Patient", func(db *gorm.DB) *gorm.DB {
  1367. return db.Where("status = 1 AND user_org_id = ?", user_org_id)
  1368. }).Count(&total).Offset(offset).Limit(limit).Find(&datas).Error
  1369. return datas, total, err
  1370. } else {
  1371. db := service.XTReadDB()
  1372. offset := (page - 1) * limit
  1373. err = db.Model(&Inspection{}).Where("org_id = ? AND status = 1 AND inspect_date >= ? AND inspect_date <= ? AND project_id = ? AND item_id = ? AND status = 1 AND inspect_value = ?", user_org_id, start_time, end_time, project_id, item_id, range_value).Preload("Patient", func(db *gorm.DB) *gorm.DB {
  1374. return db.Where("status = 1 AND user_org_id = ?", user_org_id)
  1375. }).Count(&total).Offset(offset).Limit(limit).Find(&datas).Error
  1376. return datas, total, err
  1377. }
  1378. }
  1379. }
  1380. type NewIndexDataStruct struct {
  1381. Date string `json:"date"`
  1382. Value string `json:"value"`
  1383. }
  1384. func GetPatientInspectionChartData(user_org_id int64, start_time int64, end_time int64, project_id int64, item_id int64, patient_id int64) (datas []*NewIndexDataStruct, err error) {
  1385. db := service.XTReadDB()
  1386. err = db.Raw("select from_unixtime(inspect_date, '%Y-%m-%d') as date, inspect_value as value from xt_inspection Where status = 1 AND org_id = ? AND patient_id = ? AND project_id = ? AND item_id = ? AND inspect_date <= ? AND inspect_date >= ? Order by inspect_date asc ", user_org_id, patient_id, project_id, item_id, end_time, start_time).Scan(&datas).Error
  1387. if err != nil {
  1388. return nil, err
  1389. }
  1390. return datas, nil
  1391. }
  1392. func GetPatientInspectionBarChartData(user_org_id int64, start_time int64, end_time int64, project_id int64, item_id int64, range_type int, range_value string, patient_id int64) (item []*otherItemAmount, err error) {
  1393. db := service.XTReadDB()
  1394. var items []*otherItemAmount
  1395. var tempErr error
  1396. selectContent := "CASE"
  1397. range_value_arr := strings.Split(range_value, ",")
  1398. var Total int64
  1399. db.Table("xt_inspection").Where("org_id=? and status=1 and inspect_date >= ? and inspect_date <= ? AND project_id = ? AND item_id = ? AND patient_id = ? AND inspect_value <> ''", user_org_id, start_time, end_time, project_id, item_id, patient_id).Count(&Total)
  1400. if range_type == 1 {
  1401. tempErr = db.Table("xt_inspection").Where("org_id=? and status=1 and inspect_date >= ? and inspect_date <= ? AND project_id = ? AND item_id = ? AND patient_id = ? AND inspect_value <> ''", user_org_id, start_time, end_time, project_id, item_id, patient_id).
  1402. Select("CASE WHEN inspect_value < " + range_value_arr[0] + " THEN " + "'" + "小于" + range_value_arr[0] + "'" +
  1403. " WHEN inspect_value >= " + range_value_arr[0] + " AND inspect_value <= " + range_value_arr[1] + " THEN " + "'" + range_value_arr[0] + "~" + range_value_arr[1] + "'" +
  1404. " WHEN inspect_value > " + range_value_arr[1] + " THEN " + "'" + "大于" + range_value_arr[1] + "'" +
  1405. " ELSE '未知' END AS name, COUNT(*) AS total",
  1406. ).Group("name").Scan(&items).Error
  1407. var isHasConditionOne bool = false
  1408. var isHasConditionTwo bool = false
  1409. var isHasConditionThree bool = false
  1410. for _, item := range items {
  1411. if item.Name == "大于"+range_value_arr[1] {
  1412. isHasConditionOne = true
  1413. }
  1414. if item.Name == "小于"+range_value_arr[0] {
  1415. isHasConditionTwo = true
  1416. }
  1417. if item.Name == range_value_arr[0]+"~"+range_value_arr[1] {
  1418. isHasConditionThree = true
  1419. }
  1420. }
  1421. if !isHasConditionOne {
  1422. newItem := &otherItemAmount{
  1423. Total: 0,
  1424. Name: "大于" + range_value_arr[1],
  1425. Ratio: "0",
  1426. }
  1427. items = append(items, newItem)
  1428. }
  1429. if !isHasConditionTwo {
  1430. newItem := &otherItemAmount{
  1431. Total: 0,
  1432. Name: "小于" + range_value_arr[0],
  1433. Ratio: "0",
  1434. }
  1435. items = append(items, newItem)
  1436. }
  1437. if !isHasConditionThree {
  1438. newItem := &otherItemAmount{
  1439. Total: 0,
  1440. Name: range_value_arr[0] + "~" + range_value_arr[1],
  1441. Ratio: "0",
  1442. }
  1443. items = append(items, newItem)
  1444. }
  1445. for _, item := range items {
  1446. if math.IsNaN(float64(item.Total) / float64(Total)) {
  1447. item.Ratio = "0.0"
  1448. } else {
  1449. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(Total))*100), 64)
  1450. item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
  1451. }
  1452. }
  1453. return items, tempErr
  1454. } else {
  1455. for _, item := range range_value_arr {
  1456. selectContent = selectContent + " WHEN inspect_value =" + "'" + item + "'" + " THEN " + "'" + item + "'"
  1457. }
  1458. tempErr = db.Table("xt_inspection").Where("org_id=? and status=1 and inspect_date >= ? and inspect_date <= ? AND patient_id = ? AND inspect_value <> '' AND project_id = ? AND item_id = ?", user_org_id, start_time, end_time, patient_id, project_id, item_id).
  1459. Select(selectContent +
  1460. " ELSE '未知' END AS name, COUNT(*) AS total",
  1461. ).Group("name").Scan(&items).Error
  1462. for _, item := range items {
  1463. if math.IsNaN(float64(item.Total) / float64(Total)) {
  1464. item.Ratio = "0.0"
  1465. } else {
  1466. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(Total))*100), 64)
  1467. item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
  1468. }
  1469. }
  1470. return items, tempErr
  1471. }
  1472. }
  1473. type VMPatient struct {
  1474. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1475. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  1476. UserId int64 `gorm:"column:user_id" json:"user_id" form:"user_id"`
  1477. Name string `gorm:"column:name" json:"name" form:"name"`
  1478. Lapseto int64 `gorm:"column:lapseto" json:"lapseto" form:"lapseto"`
  1479. Status int64 `gorm:"column:status" json:"status" form:"status"`
  1480. }
  1481. func (VMPatient) TableName() string {
  1482. return "xt_patients"
  1483. }
  1484. func GetOrgPatients(user_org_id int64) (patient *[]VMPatient, err error) {
  1485. db := service.XTReadDB()
  1486. var tempPatient []VMPatient
  1487. findErr := db.Model(&models.Patients{}).Where("user_org_id = ? AND status = 1 AND lapseto = 1", user_org_id).Find(&tempPatient).Error
  1488. if findErr == gorm.ErrRecordNotFound {
  1489. return nil, nil
  1490. }
  1491. if findErr != nil {
  1492. return nil, findErr
  1493. }
  1494. return &tempPatient, nil
  1495. }
  1496. func FindOrgQualityControlStandardInspectionReferenceById(user_org_id int64, project_id int64, item_id int64) (references InspectionReference, err error) {
  1497. db := service.XTReadDB()
  1498. err = db.Raw("Select reference.project_name as project_name,qcs.inspection_major as project_id, qcs.inspection_minor as id,reference.item_name as item_name,qcs.range_type as range_type,reference.range_min as range_min, reference.range_max as range_max,reference.unit as unit,qcs.range_value as range_value from xt_quality_control_standard qcs Left JOIN xt_inspection_reference reference ON qcs.inspection_major = reference.project_id and qcs.inspection_minor = reference.id and (reference.org_id = ? OR org_id = 0) Where qcs.user_org_id = ? AND qcs.status = 1 AND qcs.inspection_major = ? AND qcs.inspection_minor = ?", user_org_id, user_org_id, project_id, item_id).Scan(&references).Error
  1499. return
  1500. }
  1501. func FindOrgInspectionCount(user_org_id int64) (count int64, err error) {
  1502. db := service.XTReadDB()
  1503. err = db.Model(&models.InspectionReference{}).Where("status = 1 AND org_id = ?", user_org_id).Count(&count).Error
  1504. return
  1505. }