statistis_qc_service.go 150KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033
  1. package service
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "log"
  6. "math"
  7. "reflect"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "XT_New/models"
  12. "github.com/jinzhu/gorm"
  13. )
  14. func GetDialysisTotalDatas() {
  15. }
  16. func GetNewDialysiTotal(startime int64, endtime int64, orgid int64, origin int64) (int64, error) {
  17. counts := models.PatientPrescriptionCountStruct{}
  18. var err error
  19. if origin == 1 { //透析记录,上机为准
  20. db := XTReadDB().Table("xt_dialysis_order as x").Joins("join xt_dialysis_prescription p on p.patient_id = x.patient_id and p.record_date = x.dialysis_date")
  21. err = db.Select("count(x.id) as count").Where("x.dialysis_date >= ? and x.dialysis_date<=? and x.user_org_id = ? AND x.status = 1", startime, endtime, orgid).Scan(&counts).Error
  22. return counts.Count, err
  23. } else {
  24. var count int64
  25. var schs []models.Schedule
  26. db := XTReadDB().Model(&models.Schedule{})
  27. err := db.
  28. Group("patient_id, schedule_date").
  29. Where("schedule_date >= ? AND schedule_date <= ? and user_org_id = ? and status = 1", startime, endtime, orgid).
  30. Find(&schs).
  31. Error
  32. count = int64(len(schs))
  33. return count, err
  34. }
  35. }
  36. type TreatmentMode struct {
  37. ModeID int `json:"mode_id"`
  38. ModeName string `json:"mode_name"`
  39. }
  40. type DialysisStat struct {
  41. Date string `json:"日期"`
  42. Counts map[string]int `json:"counts"`
  43. Total int `json:"合计"`
  44. }
  45. func GetTreatmentModes(org_id int64, start string, end string) (mode []models.MewTreatmentMode) {
  46. //XTReadDB().Model(&models.HisPrescription{}).Joins("join xt_treatment_mode mode on").Where("user_org_id = ?", org_id).Find(&modes).Error
  47. XTReadDB().Table("xt_dialysis_prescription p").Joins("join xt_treatment_mode mode on mode.id = p.mode_id").Where("p.user_org_id = ? and FROM_UNIXTIME(p.record_date) >= ? AND FROM_UNIXTIME(p.record_date) <= ?", org_id, start, end).Select("mode.name as name, p.mode_id as mode_id").Group("p.mode_id").Scan(&mode)
  48. return
  49. }
  50. func GetDialysisStats(start int64, end int64, mode int64, org_id int64, time_way int64) ([]map[string]interface{}, error) {
  51. // 将时间戳转换为time.Time类型
  52. t := time.Unix(start, 0)
  53. // 使用布局定义格式化时间
  54. layout := "2006-01-02"
  55. // 将时间格式化为字符串
  56. startDate := t.Format(layout) + " 00:00:00"
  57. t2 := time.Unix(end, 0)
  58. // 使用布局定义格式化时间
  59. //layout := "2006-01-02"
  60. // 将时间格式化为字符串
  61. endDate := t2.Format(layout) + " 00:00:00"
  62. //var stats []DialysisStat
  63. var modes []models.MewTreatmentMode
  64. fmt.Println(mode)
  65. if mode == 0 {
  66. modes = GetTreatmentModes(org_id, startDate, endDate)
  67. } else {
  68. modes_two := GetTreatmentModes(org_id, startDate, endDate)
  69. for _, item := range modes_two {
  70. if item.ModeID == mode {
  71. modes = append(modes, item)
  72. }
  73. }
  74. }
  75. // 获取透析模式
  76. if err != nil {
  77. return nil, err
  78. }
  79. var selectClauses []string
  80. // 构建动态查询语句
  81. if time_way == 1 {
  82. //selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0])}
  83. selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0])}
  84. } else if time_way == 3 {
  85. selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0]+"~"+strings.Split(endDate, " ")[0])}
  86. //selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(strings.Split(startDate, " ")[0], "-")[0]+"-"+strings.Split(strings.Split(startDate, " ")[0], "-")[1])}
  87. } else if time_way == 4 {
  88. selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0]+"~"+strings.Split(endDate, " ")[0])}
  89. //selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(strings.Split(startDate, " ")[0], "-")[0])}
  90. } else {
  91. selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0]+"~"+strings.Split(endDate, " ")[0])}
  92. }
  93. for _, mode := range modes {
  94. fmt.Println(mode)
  95. selectClauses = append(selectClauses, fmt.Sprintf("COALESCE(SUM(CASE WHEN t.name = '%s' THEN 1 ELSE 0 END), 0) AS `%s`", mode.Name, mode.Name))
  96. }
  97. selectClauses = append(selectClauses, "COUNT(*) AS `合计`")
  98. var rows *sql.Rows
  99. var query string
  100. if mode > 0 {
  101. query = fmt.Sprintf(`
  102. SELECT
  103. %s
  104. FROM
  105. xt_dialysis_prescription p
  106. JOIN
  107. xt_treatment_mode t ON p.mode_id = t.mode_id
  108. WHERE
  109. FROM_UNIXTIME(p.record_date) >= ? AND FROM_UNIXTIME(p.record_date) <= ? AND p.user_org_id = ? and p.mode_id = ?
  110. GROUP BY
  111. 日期
  112. `, strings.Join(selectClauses, ", "))
  113. rows, _ = readDb.Raw(query, startDate, endDate, org_id, mode).Rows()
  114. } else {
  115. query = fmt.Sprintf(`
  116. SELECT
  117. %s
  118. FROM
  119. xt_dialysis_prescription p
  120. JOIN
  121. xt_treatment_mode t ON p.mode_id = t.mode_id
  122. WHERE
  123. FROM_UNIXTIME(p.record_date) >= ? AND FROM_UNIXTIME(p.record_date) <= ? AND p.user_org_id = ?
  124. GROUP BY
  125. 日期
  126. `, strings.Join(selectClauses, ", "))
  127. rows, _ = readDb.Raw(query, startDate, endDate, org_id).Rows()
  128. }
  129. if err != nil {
  130. return nil, err
  131. }
  132. defer rows.Close()
  133. columns, err := rows.Columns()
  134. if err != nil {
  135. return nil, err
  136. }
  137. var results []map[string]interface{}
  138. for rows.Next() {
  139. // 创建一个长度为列数的切片,用于存储每一行的值
  140. columnValues := make([]interface{}, len(columns))
  141. columnPointers := make([]interface{}, len(columns))
  142. for i := range columnValues {
  143. columnPointers[i] = &columnValues[i]
  144. }
  145. // 扫描当前行的值
  146. if err := rows.Scan(columnPointers...); err != nil {
  147. return nil, err
  148. }
  149. // 将扫描到的值放入结果 map 中
  150. result := make(map[string]interface{})
  151. for i, colName := range columns {
  152. val := columnPointers[i].(*interface{})
  153. result[colName] = *val
  154. }
  155. results = append(results, result)
  156. }
  157. //// 解析查询结果
  158. //for rows.Next() {
  159. // var stat DialysisStat
  160. // stat.Counts = make(map[string]int)
  161. // cols := []interface{}{&stat.Date}
  162. // for _, mode := range modes {
  163. // var count int
  164. // cols = append(cols, &count)
  165. // stat.Counts[mode.Name] = count
  166. // }
  167. // var total int
  168. // cols = append(cols, &total)
  169. // stat.Total = total
  170. // if err := rows.Scan(cols...); err != nil {
  171. // return nil, err
  172. // }
  173. // stats = append(stats, stat)
  174. //}
  175. return results, nil
  176. }
  177. func GetScheduleStats(start int64, end int64, mode int64, org_id int64, time_way int64) ([]map[string]interface{}, error) {
  178. // 将时间戳转换为time.Time类型
  179. t := time.Unix(start, 0)
  180. // 使用布局定义格式化时间
  181. layout := "2006-01-02"
  182. // 将时间格式化为字符串
  183. startDate := t.Format(layout) + " 00:00:00"
  184. t2 := time.Unix(end, 0)
  185. // 使用布局定义格式化时间
  186. //layout := "2006-01-02"
  187. // 将时间格式化为字符串
  188. endDate := t2.Format(layout) + " 00:00:00"
  189. //var stats []DialysisStat
  190. var modes []models.MewTreatmentMode
  191. fmt.Println(mode)
  192. if mode == 0 {
  193. modes = GetTreatmentModes(org_id, startDate, endDate)
  194. } else {
  195. modes_two := GetTreatmentModes(org_id, startDate, endDate)
  196. for _, item := range modes_two {
  197. if item.ModeID == mode {
  198. modes = append(modes, item)
  199. }
  200. }
  201. }
  202. // 获取透析模式
  203. if err != nil {
  204. return nil, err
  205. }
  206. var selectClauses []string
  207. // 构建动态查询语句
  208. if time_way == 1 {
  209. selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0])}
  210. } else if time_way == 3 {
  211. selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(strings.Split(startDate, " ")[0], "-")[0]+strings.Split(strings.Split(startDate, " ")[0], "-")[1])}
  212. } else if time_way == 4 {
  213. selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(strings.Split(startDate, " ")[0], "-")[0])}
  214. } else {
  215. selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0]+"~"+strings.Split(endDate, " ")[0])}
  216. }
  217. for _, mode := range modes {
  218. fmt.Println(mode)
  219. selectClauses = append(selectClauses, fmt.Sprintf("COALESCE(SUM(CASE WHEN t.name = '%s' THEN 1 ELSE 0 END), 0) AS `%s`", mode.Name, mode.Name))
  220. }
  221. selectClauses = append(selectClauses, "COUNT(*) AS `合计`")
  222. var rows *sql.Rows
  223. var query string
  224. if mode > 0 {
  225. query = fmt.Sprintf(`
  226. SELECT
  227. %s
  228. FROM
  229. xt_schedule p
  230. JOIN
  231. xt_treatment_mode t ON p.mode_id = t.mode_id
  232. WHERE
  233. FROM_UNIXTIME(p.schedule_date) >= ? AND FROM_UNIXTIME(p.schedule_date) <= ? AND p.user_org_id = ? and p.mode_id = ? and p.status = 1
  234. GROUP BY
  235. 日期
  236. `, strings.Join(selectClauses, ", "))
  237. rows, _ = readDb.Raw(query, startDate, endDate, org_id, mode).Rows()
  238. } else {
  239. query = fmt.Sprintf(`
  240. SELECT
  241. %s
  242. FROM
  243. xt_schedule p
  244. JOIN
  245. xt_treatment_mode t ON p.mode_id = t.mode_id
  246. WHERE
  247. FROM_UNIXTIME(p.schedule_date) >= ? AND FROM_UNIXTIME(p.schedule_date) <= ? AND p.user_org_id = ? and p.status = 1
  248. GROUP BY
  249. 日期
  250. `, strings.Join(selectClauses, ", "))
  251. rows, _ = readDb.Raw(query, startDate, endDate, org_id).Rows()
  252. }
  253. if err != nil {
  254. return nil, err
  255. }
  256. defer rows.Close()
  257. columns, err := rows.Columns()
  258. if err != nil {
  259. return nil, err
  260. }
  261. var results []map[string]interface{}
  262. for rows.Next() {
  263. // 创建一个长度为列数的切片,用于存储每一行的值
  264. columnValues := make([]interface{}, len(columns))
  265. columnPointers := make([]interface{}, len(columns))
  266. for i := range columnValues {
  267. columnPointers[i] = &columnValues[i]
  268. }
  269. // 扫描当前行的值
  270. if err := rows.Scan(columnPointers...); err != nil {
  271. return nil, err
  272. }
  273. // 将扫描到的值放入结果 map 中
  274. result := make(map[string]interface{})
  275. for i, colName := range columns {
  276. val := columnPointers[i].(*interface{})
  277. result[colName] = *val
  278. }
  279. results = append(results, result)
  280. }
  281. //// 解析查询结果
  282. //for rows.Next() {
  283. // var stat DialysisStat
  284. // stat.Counts = make(map[string]int)
  285. // cols := []interface{}{&stat.Date}
  286. // for _, mode := range modes {
  287. // var count int
  288. // cols = append(cols, &count)
  289. // stat.Counts[mode.Name] = count
  290. // }
  291. // var total int
  292. // cols = append(cols, &total)
  293. // stat.Total = total
  294. // if err := rows.Scan(cols...); err != nil {
  295. // return nil, err
  296. // }
  297. // stats = append(stats, stat)
  298. //}
  299. return results, nil
  300. }
  301. //func GetScheduleStats(startDate int64, endDate int64, groupBy int64, mode int64, org_id int64) ([]DialysisStat, error) {
  302. // var stats []DialysisStat
  303. // var modes []models.TreatmentMode
  304. // if mode > 0 {
  305. // modes, _ = GetTreatmentModes()
  306. // } else {
  307. // modes_two, _ := GetTreatmentModes()
  308. // for _, item := range modes_two {
  309. // if item.ModeID == mode {
  310. // modes = append(modes, item)
  311. // }
  312. // }
  313. // }
  314. // // 获取透析模式
  315. // if err != nil {
  316. // return nil, err
  317. // }
  318. //
  319. // // 构建日期格式
  320. // dateFormat := "%Y-%m-%d"
  321. // switch groupBy {
  322. // case 2:
  323. // dateFormat = "%Y-%m-%d"
  324. // case 1:
  325. // dateFormat = "%Y-%u"
  326. // case 3:
  327. // dateFormat = "%Y-%m"
  328. // case 4:
  329. // dateFormat = "%Y"
  330. // default:
  331. // return nil, fmt.Errorf("invalid group by option")
  332. // }
  333. // // 构建动态查询语句
  334. // selectClauses := []string{fmt.Sprintf("DATE_FORMAT(p.schudle_date, '%s') AS `日期`", dateFormat)}
  335. // for _, mode := range modes {
  336. // selectClauses = append(selectClauses, fmt.Sprintf("SUM(CASE WHEN t.mode_name = '%s' THEN 1 ELSE 0 END) AS `%s`", mode.Name, mode.Name))
  337. // }
  338. // selectClauses = append(selectClauses, "COUNT(*) AS `合计`")
  339. //
  340. // query := fmt.Sprintf(`
  341. // SELECT
  342. // %s
  343. // FROM
  344. // xt_schedule p
  345. // JOIN
  346. // xt_treatment_mode t ON p.mode_id = t.mode_id
  347. // WHERE
  348. // p.schedule_date >= ? AND p.schedule_date <= ?
  349. // GROUP BY
  350. // DATE_FORMAT(p.schedule_date, '%s')
  351. // `, dateFormat, dateFormat, selectClauses)
  352. //
  353. // rows, err := readDb.Raw(query, startDate, endDate).Rows()
  354. // if err != nil {
  355. // return nil, err
  356. // }
  357. // defer rows.Close()
  358. //
  359. // // 解析查询结果
  360. // for rows.Next() {
  361. // var stat DialysisStat
  362. // stat.Counts = make(map[string]int)
  363. // cols := []interface{}{&stat.Date}
  364. // for _, mode := range modes {
  365. // var count int
  366. // cols = append(cols, &count)
  367. // stat.Counts[mode.Name] = count
  368. // }
  369. // var total int
  370. // cols = append(cols, &total)
  371. // stat.Total = total
  372. // if err := rows.Scan(cols...); err != nil {
  373. // return nil, err
  374. // }
  375. // stats = append(stats, stat)
  376. // }
  377. //
  378. // return stats, nil
  379. //}
  380. func GetNewDialysisCountMode(starttime int64, endtime int64, orgid int64, origin int64, mode_id int64) (counts []*models.PatientPrescriptionCountStruct, err error) {
  381. if origin == 1 {
  382. db := readDb.Table("xt_dialysis_order as o").Where("o.status = 1")
  383. if starttime > 0 {
  384. db = db.Where("o.dialysis_date >=?", starttime)
  385. }
  386. if endtime > 0 {
  387. db = db.Where("o.dialysis_date<=?", endtime)
  388. }
  389. if orgid > 0 {
  390. db = db.Where("o.user_org_id = ?", orgid)
  391. }
  392. if mode_id == 0 {
  393. err = db.Select("s.mode_id,count(s.mode_id) as count").Joins("join xt_dialysis_prescription as s on s.patient_id = o.patient_id and s.record_date = o.dialysis_date and s.status= 1 AND s.record_date >= ? AND s.record_date <= ? AND s.mode_id > 0 ", starttime, endtime).Group("s.mode_id").Scan(&counts).Error
  394. } else {
  395. err = db.Select("s.mode_id,count(s.mode_id) as count").Joins("join xt_dialysis_prescription as s on s.patient_id = o.patient_id and s.record_date = o.dialysis_date and s.status= 1 AND s.record_date >= ? AND s.record_date <= ? AND s.mode_id = ? ", starttime, endtime, mode_id).Group("s.mode_id").Scan(&counts).Error
  396. }
  397. return counts, err
  398. } else {
  399. err = readDb.Raw("select mode_id, count(aa.`mode_id`) as count from xt_schedule aa join (SELECT DISTINCT o.patient_id, o.`schedule_date` FROM xt_schedule AS o WHERE o.status = 1 AND o.schedule_date >= ? AND o.schedule_date <= ? AND o.user_org_id = ?) as b on aa.`patient_id` = b.`patient_id` and aa.`schedule_date` = b.`schedule_date` where aa.`status` = 1 and aa.schedule_date >= ? AND aa.schedule_date <= ? AND aa.user_org_id = ? Group by aa.`mode_id`", starttime, endtime, orgid, starttime, endtime, orgid).Find(&counts).Error
  400. return counts, err
  401. }
  402. }
  403. func GetNewDialysisCountModeTwo(starttime int64, endtime int64, orgid int64, origin int64, mode_id int64) (counts models.CustomDialysisData, err error) {
  404. // 将时间戳转换为time.Time类型
  405. t := time.Unix(starttime, 0)
  406. // 使用布局定义格式化时间
  407. layout := "2006-01-02"
  408. // 将时间格式化为字符串
  409. startDate := t.Format(layout) + " 00:00:00"
  410. t2 := time.Unix(endtime, 0)
  411. // 使用布局定义格式化时间
  412. //layout := "2006-01-02"
  413. // 将时间格式化为字符串
  414. endDate := t2.Format(layout) + " 00:00:00"
  415. //fmt.Println(formattedDate)
  416. // 将日期字符串解析为time.Time类型
  417. //start, _ := time.Parse("2006-01-02 15:04:05", formattedDate)
  418. //starttime = start.Unix()
  419. //fmt.Println(starttime)
  420. if origin == 1 {
  421. db := readDb.Table("xt_dialysis_order as o").Where("o.status = 1")
  422. if starttime > 0 {
  423. db = db.Where("DATE_FORMAT(FROM_UNIXTIME(o.dialysis_date), '%Y-%m-%d %H:%i:%s') >= ?", startDate)
  424. }
  425. if endtime > 0 {
  426. db = db.Where("DATE_FORMAT(FROM_UNIXTIME(o.dialysis_date), '%Y-%m-%d %H:%i:%s') <= ?", endDate)
  427. }
  428. if orgid > 0 {
  429. db = db.Where("o.user_org_id = ?", orgid)
  430. }
  431. err = db.Select("s.mode_id,count(s.mode_id) as count").Joins("join xt_dialysis_prescription as s on s.patient_id = o.patient_id and s.record_date = o.dialysis_date and s.status= 1 AND DATE_FORMAT(FROM_UNIXTIME(s.record_date), '%Y-%m-%d %H:%i:%s') >= ? AND DATE_FORMAT(FROM_UNIXTIME(s.record_date), '%Y-%m-%d %H:%i:%s') <= ? AND s.mode_id = ? ", startDate, endDate, mode_id).Group("s.mode_id").Scan(&counts).Error
  432. return counts, err
  433. } else {
  434. db := readDb.Table("xt_schedule as o").Where("o.status = 1").Select("patient_id, schedule_date").Group("mode_id")
  435. if starttime > 0 {
  436. db = db.Where("DATE_FORMAT(FROM_UNIXTIME(o.schedule_date),'%Y-%m-%d %H:%i:%s') >= ?", startDate)
  437. }
  438. if endtime > 0 {
  439. db = db.Where("DATE_FORMAT(FROM_UNIXTIME(o.schedule_date),'%Y-%m-%d %H:%i:%s') <= ?", endDate)
  440. }
  441. if orgid > 0 {
  442. db = db.Where("o.user_org_id = ?", orgid)
  443. }
  444. if mode_id > 0 {
  445. db = db.Where("o.mode_id = ?", mode_id)
  446. }
  447. err = db.Select("o.mode_id,count(o.mode_id) as count").Scan(&counts).Error
  448. return counts, err
  449. }
  450. }
  451. var anticoagulantMap = map[int]string{
  452. 1: "无肝素",
  453. 2: "普通肝素",
  454. 3: "低分子肝素",
  455. 4: "阿加曲班",
  456. 5: "枸橼酸钠",
  457. 6: "低分子肝素钙",
  458. 7: "低分子肝素钠",
  459. 8: "依诺肝素",
  460. 9: "达肝素",
  461. 10: "体外抗凝",
  462. 11: "那屈肝素",
  463. 12: "无抗凝剂",
  464. 13: "那屈肝素钙",
  465. 14: "肝素钙注射液",
  466. 15: "甲磺酸萘莫司他",
  467. 16: "低分子量肝素钙",
  468. 17: "肝素钠",
  469. }
  470. func GetAnticoagulantData(start_time int64, end_time int64, org_id int64) (map[string]int, error) {
  471. var results []struct {
  472. Anticoagulant int
  473. Count int
  474. }
  475. err := XTReadDB().Model(&models.DialysisPrescription{}).Joins("join xt_dialysis_order oo on oo.patient_id = xt_dialysis_prescription.patient_id and oo.dialysis_date = xt_dialysis_prescription.record_date and oo.status = 1").
  476. Select("xt_dialysis_prescription.anticoagulant, COUNT(*) as count").
  477. Where("xt_dialysis_prescription.status = 1 and xt_dialysis_prescription.record_date >= ? and xt_dialysis_prescription.record_date <= ? and xt_dialysis_prescription.user_org_id = ?", start_time, end_time, org_id).
  478. Group("xt_dialysis_prescription.anticoagulant").
  479. Scan(&results).Error
  480. if err != nil {
  481. return nil, err
  482. }
  483. anticoagulantData := make(map[string]int)
  484. for _, result := range results {
  485. if name, ok := anticoagulantMap[result.Anticoagulant]; ok {
  486. anticoagulantData[name] = result.Count
  487. }
  488. //else {
  489. // anticoagulantData[fmt.Sprintf("Unknown (%d)", result.Anticoagulant)] = result.Count
  490. //}z
  491. }
  492. return anticoagulantData, nil
  493. }
  494. func GetAnticoagulantTotal(start_time int64, end_time int64, org_id int64) (total int64, err error) {
  495. err = XTReadDB().Model(&models.DialysisPrescription{}).Joins("join xt_dialysis_order oo on oo.patient_id = xt_dialysis_prescription.patient_id and oo.dialysis_date = xt_dialysis_prescription.record_date and oo.status = 1").
  496. Where("xt_dialysis_prescription.record_date >= ? and xt_dialysis_prescription.record_date <= ? and xt_dialysis_prescription.user_org_id = ? and xt_dialysis_prescription.status = 1", start_time, end_time, org_id).
  497. Count(&total).Error
  498. return
  499. }
  500. func GetDialyzerSummary(org_id int64) ([]models.DialysisPrescription, error) {
  501. var Dialyzer []models.DialysisPrescription
  502. err := XTReadDB().Model(&models.DialysisPrescription{}).
  503. Select("dialysis_dialyszers").
  504. Where(" user_org_id = ? and status = 1 and dialysis_dialyszers <> ''", org_id).
  505. Group("dialysis_dialyszers").
  506. Find(&Dialyzer).Error
  507. if err != nil {
  508. return nil, err
  509. }
  510. return Dialyzer, nil
  511. }
  512. func GetIrrigationSummary(org_id int64) ([]models.DialysisPrescription, error) {
  513. var Dialyzer []models.DialysisPrescription
  514. err := XTReadDB().Model(&models.DialysisPrescription{}).
  515. Select("dialysis_irrigation").
  516. Where(" user_org_id = ? and status = 1 and dialysis_irrigation <> ''", org_id).
  517. Group("dialysis_irrigation").
  518. Find(&Dialyzer).Error
  519. if err != nil {
  520. return nil, err
  521. }
  522. return Dialyzer, nil
  523. }
  524. func GetDialysisStrainerSummary(org_id int64) ([]models.DialysisPrescription, error) {
  525. var Dialyzer []models.DialysisPrescription
  526. err := XTReadDB().Model(&models.DialysisPrescription{}).
  527. Select("dialysis_strainer").
  528. Where(" user_org_id = ? and status = 1 and dialysis_strainer <> ''", org_id).
  529. Group("dialysis_strainer").
  530. Find(&Dialyzer).Error
  531. if err != nil {
  532. return nil, err
  533. }
  534. return Dialyzer, nil
  535. }
  536. type DialyzerResult struct {
  537. Dialyzer string
  538. Count int
  539. }
  540. func GetDialyzerData(start_time int64, end_time int64, org_id int64) (dr []DialyzerResult, err error) {
  541. err = XTReadDB().Model(&models.DialysisPrescription{}).Joins("join xt_dialysis_order oo on oo.patient_id = xt_dialysis_prescription.patient_id and oo.dialysis_date = xt_dialysis_prescription.record_date and oo.status = 1").
  542. Select("xt_dialysis_prescription.dialysis_dialyszers as dialyzer, COUNT(*) as count").
  543. Where("xt_dialysis_prescription.record_date >= ? and xt_dialysis_prescription.record_date <= ? and xt_dialysis_prescription.user_org_id = ? and xt_dialysis_prescription.status = 1 and xt_dialysis_prescription.dialysis_dialyszers <> ''", start_time, end_time, org_id).
  544. Group("xt_dialysis_prescription.dialysis_dialyszers").
  545. Scan(&dr).Error
  546. return
  547. }
  548. func GetDialysisIrrigationData(start_time int64, end_time int64, org_id int64) (dr []DialyzerResult, err error) {
  549. err = XTReadDB().Model(&models.DialysisPrescription{}).Joins("join xt_dialysis_order oo on oo.patient_id = xt_dialysis_prescription.patient_id and oo.dialysis_date = xt_dialysis_prescription.record_date and oo.status = 1").
  550. Select("xt_dialysis_prescription.dialysis_irrigation as dialyzer, COUNT(*) as count").
  551. Where("xt_dialysis_prescription.record_date >= ? and xt_dialysis_prescription.record_date <= ? and xt_dialysis_prescription.user_org_id = ? and xt_dialysis_prescription.status = 1 and xt_dialysis_prescription.dialysis_irrigation <> '' ", start_time, end_time, org_id).
  552. Group("xt_dialysis_prescription.dialysis_irrigation").
  553. Scan(&dr).Error
  554. return
  555. }
  556. func GetDialysisStrainerData(start_time int64, end_time int64, org_id int64) (dr []DialyzerResult, err error) {
  557. err = XTReadDB().Model(&models.DialysisPrescription{}).Joins("join xt_dialysis_order oo on oo.patient_id = xt_dialysis_prescription.patient_id and oo.dialysis_date = xt_dialysis_prescription.record_date and oo.status = 1").
  558. Select("xt_dialysis_prescription.dialysis_strainer as dialyzer, COUNT(*) as count").
  559. Where("xt_dialysis_prescription.record_date >= ? and xt_dialysis_prescription.record_date <= ? and xt_dialysis_prescription.user_org_id = ? and xt_dialysis_prescription.status = 1 and xt_dialysis_prescription.dialysis_strainer <> ''", start_time, end_time, org_id).
  560. Group("xt_dialysis_prescription.dialysis_strainer").
  561. Scan(&dr).Error
  562. return
  563. }
  564. func GetIrrigationData(start_time int64, end_time int64, org_id int64) (dr []DialyzerResult, err error) {
  565. err = XTReadDB().Model(&models.DialysisPrescription{}).
  566. Select("dialysis_irrigation as dialyzer, COUNT(*) as count").
  567. Where("record_date >= ? and record_date <= ? and user_org_id = ? and status = 1", start_time, end_time, org_id).
  568. Group("dialysis_irrigation").
  569. Scan(&dr).Error
  570. return
  571. }
  572. func GetDialyzerTotal(start_time int64, end_time int64, org_id int64) (total int64, err error) {
  573. err = XTReadDB().Model(&models.DialysisPrescription{}).Joins("join xt_dialysis_order oo on oo.patient_id = xt_dialysis_prescription.patient_id and oo.dialysis_date = xt_dialysis_prescription.record_date and oo.status = 1").
  574. Where("xt_dialysis_prescription.status = 1 and xt_dialysis_prescription.record_date >= ? and xt_dialysis_prescription.record_date <= ? and xt_dialysis_prescription.user_org_id = ?", start_time, end_time, org_id).
  575. Count(&total).Error
  576. return
  577. }
  578. func GetPrescriptionByAnticoagulant(page int64, limit int64, orgid int64, anticoagulant int64, start_time int64, end_time int64) (solution []*models.DialysisPrescription, total int64, err error) {
  579. db := XTReadDB().Model(&models.DialysisPrescription{}).Joins("join xt_dialysis_order oo on oo.patient_id = xt_dialysis_prescription.patient_id and oo.dialysis_date = xt_dialysis_prescription.record_date and oo.status = 1").Preload("QCPatients", "xt_patients.status = 1 and xt_patients.user_org_id = ?", orgid).Where("xt_dialysis_prescription.status = 1")
  580. if anticoagulant > 0 {
  581. db = db.Where("xt_dialysis_prescription.anticoagulant = ?", anticoagulant)
  582. }
  583. if orgid > 0 {
  584. db = db.Where("xt_dialysis_prescription.user_org_id = ?", orgid)
  585. }
  586. db = db.Where("xt_dialysis_prescription.record_date >= ? and xt_dialysis_prescription.record_date <= ?", start_time, end_time)
  587. offset := (page - 1) * limit
  588. err = db.Count(&total).Offset(offset).Limit(limit).Find(&solution).Error
  589. return solution, total, err
  590. }
  591. func GetPrescriptionByDialyzer(page int64, limit int64, orgid int64, dialyzer string, start_time int64, end_time int64) (solution []*models.DialysisPrescription, total int64, err error) {
  592. db := XTReadDB().Model(&models.DialysisPrescription{}).Joins("join xt_dialysis_order oo on oo.patient_id = xt_dialysis_prescription.patient_id and oo.dialysis_date = xt_dialysis_prescription.record_date and oo.status = 1").Preload("QCPatients", "xt_patients.status = 1 and xt_patients.user_org_id = ?", orgid).Where("xt_dialysis_prescription.status = 1")
  593. if len(dialyzer) > 0 {
  594. db = db.Where("xt_dialysis_prescription.dialysis_dialyszers = ? or xt_dialysis_prescription.dialysis_irrigation = ? or xt_dialysis_prescription.dialysis_strainer = ?", dialyzer, dialyzer, dialyzer)
  595. }
  596. if orgid > 0 {
  597. db = db.Where("xt_dialysis_prescription.user_org_id = ?", orgid)
  598. }
  599. db = db.Where("xt_dialysis_prescription.record_date >= ? and xt_dialysis_prescription.record_date <= ? and xt_dialysis_prescription.status = 1", start_time, end_time)
  600. offset := (page - 1) * limit
  601. err = db.Count(&total).Offset(offset).Limit(limit).Find(&solution).Error
  602. return solution, total, err
  603. }
  604. type DialysisData struct {
  605. DialysisStatus string `json:"dialysis_status"`
  606. Count int `json:"count"`
  607. }
  608. type CustomData struct {
  609. DialysisNo string
  610. PatientName string
  611. DialysisDate string
  612. DialysisDuration string
  613. ActualDuration string
  614. Diff string
  615. Doctor string
  616. Nurse string
  617. }
  618. func GetDialysisCompletionRate(org_id int64, start_time int64, end_time int64) (map[string]int, error) {
  619. var results []DialysisData
  620. query := `
  621. SELECT
  622. CASE
  623. WHEN ABS(TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0))) <= 15 THEN '达到透析处方时间'
  624. WHEN TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0)) < -15 THEN '超出处方时间'
  625. ELSE '未达到处方时间'
  626. END AS dialysis_status,
  627. COUNT(*) AS count
  628. FROM
  629. xt_dialysis_order o
  630. JOIN
  631. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  632. JOIN
  633. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  634. WHERE
  635. o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ?
  636. GROUP BY
  637. dialysis_status;
  638. `
  639. if err := readDb.Raw(query, org_id, org_id, org_id, start_time, end_time).Scan(&results).Error; err != nil {
  640. return nil, err
  641. }
  642. dialysisCompletionRate := make(map[string]int)
  643. for _, result := range results {
  644. dialysisCompletionRate[result.DialysisStatus] = result.Count
  645. }
  646. return dialysisCompletionRate, nil
  647. }
  648. func GetDialysisCompletionTotal(org_id int64, start_time int64, end_time int64) (int64, error) {
  649. var Count int64
  650. query := `
  651. SELECT
  652. COUNT(*) AS count
  653. FROM
  654. xt_dialysis_order o
  655. JOIN
  656. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  657. JOIN
  658. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  659. WHERE
  660. o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ?
  661. `
  662. if err := readDb.Raw(query, org_id, org_id, org_id, start_time, end_time).Count(&Count).Error; err != nil {
  663. return 0, err
  664. }
  665. return Count, nil
  666. }
  667. func GetDialysisCompletionDetail(org_id int64, start_time int64, end_time int64, mode int64, limit int64, page int64) (results []interface{}, total int64, err error) {
  668. var query string
  669. if mode == 0 {
  670. query = `
  671. SELECT
  672. pp.dialysis_no as dialysis_no,
  673. pp.name as patient_name,
  674. FROM_UNIXTIME(o.dialysis_date) as dialysis_date,
  675. CONCAT(p.dialysis_duration_hour, 'h', p.dialysis_duration_minute,'min') as dialysis_duration,
  676. CONCAT(a.actual_treatment_hour, 'h', a.actual_treatment_minute,'min') as actual_duration,
  677. ABS(TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0))) as diff,
  678. p.prescription_doctor as doctor,
  679. o.finish_nurse as nurse,
  680. pp.id_card_no as id_card_no
  681. FROM
  682. xt_dialysis_order o
  683. JOIN
  684. xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?
  685. JOIN
  686. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  687. JOIN
  688. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  689. WHERE
  690. o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ?
  691. `
  692. readDb.Table("xt_dialysis_order o").
  693. Joins("JOIN xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?", org_id).
  694. Joins("JOIN xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?", org_id).
  695. Joins("JOIN xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?", org_id).
  696. Where("o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ?", org_id, start_time, end_time).Count(&total)
  697. } else if mode == 1 {
  698. query = `
  699. SELECT
  700. pp.dialysis_no as dialysis_no,
  701. pp.name as patient_name,
  702. FROM_UNIXTIME(o.dialysis_date) as dialysis_date,
  703. CONCAT(p.dialysis_duration_hour, 'h', p.dialysis_duration_minute,'min') as dialysis_duration,
  704. CONCAT(a.actual_treatment_hour, 'h', a.actual_treatment_minute,'min') as actual_duration,
  705. ABS(TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0))) as diff,
  706. p.prescription_doctor as doctor,
  707. o.finish_nurse as nurse,
  708. pp.id_card_no as id_card_no
  709. FROM
  710. xt_dialysis_order o
  711. JOIN
  712. xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?
  713. JOIN
  714. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  715. JOIN
  716. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  717. WHERE
  718. o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ? AND ABS(TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0))) <= 15
  719. `
  720. readDb.Table("xt_dialysis_order o").
  721. Joins("JOIN xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?", org_id).
  722. Joins("JOIN xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?", org_id).
  723. Joins("JOIN xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?", org_id).
  724. Where("o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ? and ABS(TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0))) <= 15", org_id, start_time, end_time).Count(&total)
  725. } else if mode == 2 {
  726. query = `
  727. SELECT
  728. pp.dialysis_no as dialysis_no,
  729. pp.name as patient_name,
  730. FROM_UNIXTIME(o.dialysis_date) as dialysis_date,
  731. CONCAT(p.dialysis_duration_hour, 'h', p.dialysis_duration_minute,'min') as dialysis_duration,
  732. CONCAT(a.actual_treatment_hour, 'h', a.actual_treatment_minute,'min') as actual_duration,
  733. ABS(TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0))) as diff,
  734. p.prescription_doctor as doctor,
  735. o.finish_nurse as nurse,
  736. pp.id_card_no as id_card_no
  737. FROM
  738. xt_dialysis_order o
  739. JOIN
  740. xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?
  741. JOIN
  742. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  743. JOIN
  744. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  745. WHERE
  746. o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ? AND TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0)) < -15
  747. `
  748. readDb.Table("xt_dialysis_order o").
  749. Joins("JOIN xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?", org_id).
  750. Joins("JOIN xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?", org_id).
  751. Joins("JOIN xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?", org_id).
  752. Where("o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ? and TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0)) < 0", org_id, start_time, end_time).Count(&total)
  753. } else if mode == 3 {
  754. query = `
  755. SELECT
  756. pp.dialysis_no as dialysis_no,
  757. pp.name as patient_name,
  758. FROM_UNIXTIME(o.dialysis_date) as dialysis_date,
  759. CONCAT(p.dialysis_duration_hour, 'h', p.dialysis_duration_minute,'min') as dialysis_duration,
  760. CONCAT(a.actual_treatment_hour, 'h', a.actual_treatment_minute,'min') as actual_duration,
  761. ABS(TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0))) as diff,
  762. p.prescription_doctor as doctor,
  763. o.finish_nurse as nurse,
  764. pp.id_card_no as id_card_no
  765. FROM
  766. xt_dialysis_order o
  767. JOIN
  768. xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?
  769. JOIN
  770. xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
  771. JOIN
  772. xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
  773. WHERE
  774. o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ? AND TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0)) > 15
  775. `
  776. readDb.Table("xt_dialysis_order o").
  777. Joins("JOIN xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?", org_id).
  778. Joins("JOIN xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?", org_id).
  779. Joins("JOIN xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?", org_id).
  780. Where("o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ? and TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0)) > 15", org_id, start_time, end_time).Count(&total)
  781. }
  782. offset := (page - 1) * limit
  783. rows, err := readDb.Raw(query, org_id, org_id, org_id, org_id, start_time, end_time).Offset(offset).Limit(limit).Rows()
  784. if err != nil {
  785. fmt.Println(err)
  786. }
  787. defer rows.Close()
  788. // Define a map to hold the dynamic fields
  789. fields := map[string]interface{}{
  790. "dialysis_no": "",
  791. "patient_name": "",
  792. "dialysis_date": "",
  793. "dialysis_duration": "",
  794. "actual_duration": "",
  795. "diff": 0,
  796. "doctor": "",
  797. "nurse": "",
  798. "id_card_no": "",
  799. }
  800. // Create the dynamic struct type
  801. dynamicStructType := createDynamicStruct(fields)
  802. // Slice to hold the results
  803. //var results []interface{}
  804. // Iterate over the rows and scan into the dynamic struct
  805. for rows.Next() {
  806. // Create a new instance of the dynamic struct
  807. result := reflect.New(dynamicStructType).Interface()
  808. // Create a slice of pointers to the fields in the struct
  809. fieldPtrs := []interface{}{
  810. reflect.ValueOf(result).Elem().FieldByName("Dialysis_no").Addr().Interface(),
  811. reflect.ValueOf(result).Elem().FieldByName("Patient_name").Addr().Interface(),
  812. reflect.ValueOf(result).Elem().FieldByName("Dialysis_date").Addr().Interface(),
  813. reflect.ValueOf(result).Elem().FieldByName("Dialysis_duration").Addr().Interface(),
  814. reflect.ValueOf(result).Elem().FieldByName("Actual_duration").Addr().Interface(),
  815. reflect.ValueOf(result).Elem().FieldByName("Diff").Addr().Interface(),
  816. reflect.ValueOf(result).Elem().FieldByName("Doctor").Addr().Interface(),
  817. reflect.ValueOf(result).Elem().FieldByName("Nurse").Addr().Interface(),
  818. reflect.ValueOf(result).Elem().FieldByName("Id_card_no").Addr().Interface(),
  819. }
  820. // Scan the row into the struct
  821. if err := rows.Scan(fieldPtrs...); err != nil {
  822. //log.Fatalf("failed to scan row: %v", err)
  823. fmt.Println(err)
  824. }
  825. // Append the result to the slice
  826. results = append(results, result)
  827. }
  828. return results, total, err
  829. }
  830. type QualityControlStandard struct {
  831. ItemName string `json:"item_name"`
  832. ProjectMame string `json:"project_name"`
  833. InspectionMajor int64 `json:"inspection_major"`
  834. InspectionMinor int64 `json:"inspection_minor"`
  835. }
  836. func getQualityControlStandards(org_id int64) ([]QualityControlStandard, error) {
  837. var standards []QualityControlStandard
  838. if err := readDb.Model(&models.XtQualityControlStandard{}).Select("re.project_name as project_name,re.item_name as item_name,xt_quality_control_standard.inspection_major as inspection_major,xt_quality_control_standard.inspection_minor as inspection_minor").Joins("join xt_inspection_reference re on re.project_id = xt_quality_control_standard.inspection_major and re.item_id = xt_quality_control_standard.inspection_minor and re.org_id = ?", org_id).Where("xt_quality_control_standard.user_org_id = ? and xt_quality_control_standard.status = 1", org_id).Scan(&standards).Error; err != nil {
  839. return nil, err
  840. }
  841. return standards, nil
  842. }
  843. type DialysisQualityStat struct {
  844. Month string `json:"月"`
  845. Name string `json:"姓名"`
  846. Counts map[string]float64 `json:"counts"`
  847. }
  848. //func GetLatestInspectionValues(org_id int64, yearMonth string) ([]map[string]interface{}, error) {
  849. // standards, err := getQualityControlStandards(org_id)
  850. // if err != nil {
  851. // return nil, err
  852. // }
  853. //
  854. // var selectFields []string
  855. // for _, standard := range standards {
  856. // field := fmt.Sprintf("IFNULL(MAX(CASE WHEN i.item_name = %s THEN i.inspect_value END),'') AS `%s`",
  857. // "'"+standard.ItemName+"'", standard.ItemName)
  858. // selectFields = append(selectFields, field)
  859. // }
  860. //
  861. // query := fmt.Sprintf(`
  862. // SELECT
  863. // p.name as '姓名',
  864. // %s
  865. // FROM xt_inspection i
  866. // JOIN xt_patients p On p.id = i.patient_id
  867. // WHERE DATE_FORMAT(FROM_UNIXTIME(i.inspect_date), '%%Y-%%m') = ? and org_id = ?
  868. //
  869. // GROUP BY i.patient_id
  870. // `, strings.Join(selectFields, ", "))
  871. //
  872. // var results []map[string]interface{}
  873. // rows, err := readDb.Raw(query, yearMonth, org_id).Rows()
  874. // if err != nil {
  875. // return nil, err
  876. // }
  877. // defer rows.Close()
  878. //
  879. // columns, err := rows.Columns()
  880. // if err != nil {
  881. // return nil, err
  882. // }
  883. //
  884. // for rows.Next() {
  885. // // 创建一个长度为列数的切片,用于存储每一行的值
  886. // columnValues := make([]interface{}, len(columns))
  887. // columnPointers := make([]interface{}, len(columns))
  888. // for i := range columnValues {
  889. // columnPointers[i] = &columnValues[i]
  890. // }
  891. //
  892. // // 扫描当前行的值
  893. // if err := rows.Scan(columnPointers...); err != nil {
  894. // return nil, err
  895. // }
  896. //
  897. // // 将扫描到的值放入结果 map 中
  898. // result := make(map[string]interface{})
  899. // for i, colName := range columns {
  900. // val := columnPointers[i].(*interface{})
  901. // result[colName] = *val
  902. // }
  903. //
  904. // results = append(results, result)
  905. // }
  906. //
  907. // return results, nil
  908. //}
  909. func GetLatestInspectionValues(org_id int64, yearMonth string) ([]map[string]interface{}, error) {
  910. standards, err := getQualityControlStandards(org_id)
  911. if err != nil {
  912. return nil, err
  913. }
  914. var selectFields []string
  915. for _, standard := range standards {
  916. field := fmt.Sprintf("IFNULL(MAX(CASE WHEN latest_inspections.item_name = '%s' THEN latest_inspections.inspect_value END),'') AS `%s`",
  917. standard.ItemName, standard.ItemName)
  918. selectFields = append(selectFields, field)
  919. }
  920. query := fmt.Sprintf(`
  921. SELECT
  922. p.name as '姓名',
  923. %s
  924. FROM xt_patients p
  925. JOIN (
  926. SELECT
  927. i1.patient_id,
  928. i1.item_name,
  929. i1.inspect_value
  930. FROM
  931. xt_inspection i1
  932. JOIN (
  933. SELECT
  934. patient_id,
  935. item_name,
  936. MAX(inspect_date) AS latest_inspect_date
  937. FROM
  938. xt_inspection
  939. WHERE
  940. DATE_FORMAT(FROM_UNIXTIME(inspect_date), '%%Y-%%m') = ?
  941. AND org_id = ?
  942. GROUP BY
  943. patient_id, item_name
  944. ) latest
  945. ON
  946. i1.patient_id = latest.patient_id
  947. AND i1.item_name = latest.item_name
  948. AND i1.inspect_date = latest.latest_inspect_date
  949. ) latest_inspections
  950. ON p.id = latest_inspections.patient_id
  951. WHERE p.user_org_id = ? and p.lapseto = 1
  952. GROUP BY p.id, p.name
  953. `, strings.Join(selectFields, ", "))
  954. var results []map[string]interface{}
  955. rows, err := readDb.Raw(query, yearMonth, org_id, org_id).Rows() // 注意这里多传一个org_id参数
  956. if err != nil {
  957. return nil, err
  958. }
  959. defer rows.Close()
  960. columns, err := rows.Columns()
  961. if err != nil {
  962. return nil, err
  963. }
  964. for rows.Next() {
  965. // 创建一个长度为列数的切片,用于存储每一行的值
  966. columnValues := make([]interface{}, len(columns))
  967. columnPointers := make([]interface{}, len(columns))
  968. for i := range columnValues {
  969. columnPointers[i] = &columnValues[i]
  970. }
  971. // 扫描当前行的值
  972. if err := rows.Scan(columnPointers...); err != nil {
  973. return nil, err
  974. }
  975. // 将扫描到的值放入结果 map 中
  976. result := make(map[string]interface{})
  977. for i, colName := range columns {
  978. val := columnPointers[i].(*interface{})
  979. result[colName] = *val
  980. }
  981. results = append(results, result)
  982. }
  983. return results, nil
  984. }
  985. //func GetLatestInspectionValues(org_id int64, yearMonth string) ([]map[string]interface{}, error) {
  986. // standards, err := getQualityControlStandards(org_id)
  987. // if err != nil {
  988. // return nil, err
  989. // }
  990. //
  991. // var selectFields []string
  992. // for _, standard := range standards {
  993. // field := fmt.Sprintf("IFNULL(MAX(CASE WHEN latest_inspections.item_name = '%s' THEN latest_inspections.inspect_value END),'') AS `%s`",
  994. // standard.ItemName, standard.ItemName)
  995. // selectFields = append(selectFields, field)
  996. // }
  997. //
  998. // query := fmt.Sprintf(`
  999. // SELECT
  1000. // p.name AS '姓名',
  1001. // %s
  1002. // FROM xt_patients p
  1003. // LEFT JOIN (
  1004. // SELECT
  1005. // i1.patient_id,
  1006. // i1.item_name,
  1007. // i1.inspect_value
  1008. // FROM
  1009. // xt_inspection i1
  1010. // JOIN (
  1011. // SELECT
  1012. // patient_id,
  1013. // item_name,
  1014. // MAX(inspect_date) AS latest_inspect_date
  1015. // FROM
  1016. // xt_inspection
  1017. // WHERE
  1018. // DATE_FORMAT(FROM_UNIXTIME(inspect_date), '%%Y-%%m') = ?
  1019. // AND org_id = ?
  1020. // GROUP BY
  1021. // patient_id, item_name
  1022. // ) latest
  1023. // ON
  1024. // i1.patient_id = latest.patient_id
  1025. // AND i1.item_name = latest.item_name
  1026. // AND i1.inspect_date = latest.latest_inspect_date
  1027. // ) latest_inspections
  1028. // ON p.id = latest_inspections.patient_id
  1029. // WHERE p.user_org_id = ?
  1030. // GROUP BY p.id, p.name
  1031. // `, strings.Join(selectFields, ", "))
  1032. //
  1033. // var results []map[string]interface{}
  1034. // rows, err := readDb.Raw(query, yearMonth, org_id, org_id).Rows() // 注意这里多传一个org_id参数
  1035. // if err != nil {
  1036. // return nil, err
  1037. // }
  1038. // defer rows.Close()
  1039. //
  1040. // columns, err := rows.Columns()
  1041. // if err != nil {
  1042. // return nil, err
  1043. // }
  1044. //
  1045. // for rows.Next() {
  1046. // // 创建一个长度为列数的切片,用于存储每一行的值
  1047. // columnValues := make([]interface{}, len(columns))
  1048. // columnPointers := make([]interface{}, len(columns))
  1049. // for i := range columnValues {
  1050. // columnPointers[i] = &columnValues[i]
  1051. // }
  1052. //
  1053. // // 扫描当前行的值
  1054. // if err := rows.Scan(columnPointers...); err != nil {
  1055. // return nil, err
  1056. // }
  1057. //
  1058. // // 将扫描到的值放入结果 map 中
  1059. // result := make(map[string]interface{})
  1060. // for i, colName := range columns {
  1061. // val := columnPointers[i].(*interface{})
  1062. // result[colName] = *val
  1063. // }
  1064. //
  1065. // results = append(results, result)
  1066. // }
  1067. //
  1068. // return results, nil
  1069. //}
  1070. func createDynamicStruct(fields map[string]interface{}) reflect.Type {
  1071. var structFields []reflect.StructField
  1072. for name, value := range fields {
  1073. structFields = append(structFields, reflect.StructField{
  1074. Name: strings.Title(name),
  1075. Type: reflect.TypeOf(value),
  1076. Tag: reflect.StructTag(fmt.Sprintf(`json:"%s"`, name)),
  1077. })
  1078. }
  1079. return reflect.StructOf(structFields)
  1080. }
  1081. func GetDialysisPrescriptionInfo(start_date string, end_date string, mode int64, org_id int64, page int64, limit int64) (qcp []models.QCPrescription, total int64, err error) {
  1082. offset := (page - 1) * limit
  1083. err = readDb.Model(&models.QCPrescription{}).Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1084. return readUserDb.Where("status = 1 and org_id=?", org_id)
  1085. }).Preload("QCPatients", "status = 1 and user_org_id = ?", org_id).Preload("QCDialysisOrder", func(db *gorm.DB) *gorm.DB {
  1086. return db.Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1087. return readUserDb.Where("status = 1 and org_id=?", org_id)
  1088. }).Preload("DeviceNumber", "status = 1 and org_id = ?", org_id).Where("status = 1 AND user_org_id = ?", org_id)
  1089. }).Preload("QCAssessmentAfterDislysis", "status = 1 and user_org_id = ?", org_id).Where("FROM_UNIXTIME(record_date, '%Y-%m-%d') >= ? and FROM_UNIXTIME(record_date, '%Y-%m-%d') <= ? and user_org_id = ? and mode_id = ? and status = 1", start_date, end_date, org_id, mode).Count(&total).Offset(offset).Limit(limit).Find(&qcp).Error
  1090. return
  1091. }
  1092. func GetScheduleInfo(start_date string, end_date string, mode int64, org_id int64, page int64, limit int64) (qcp []models.QCSchedule, total int64, err error) {
  1093. offset := (page - 1) * limit
  1094. err = readDb.Model(&models.QCSchedule{}).Preload("QCPatients", "status = 1 and user_org_id =?", org_id).Preload("QCSPrescription", func(db *gorm.DB) *gorm.DB {
  1095. return db.Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1096. return readUserDb.Where("status = 1 and org_id=?", org_id)
  1097. }).Where("status = 1 AND user_org_id = ?", org_id)
  1098. }).Preload("QCAssessmentAfterDislysis", "status = 1 and user_org_id = ?", org_id).Preload("QCDialysisOrder", func(db *gorm.DB) *gorm.DB {
  1099. return db.Preload("DeviceNumber", "status = 1").Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1100. return readUserDb.Where("status = 1 and org_id=?", org_id)
  1101. }).Where("status = 1 AND user_org_id = ?", org_id)
  1102. }).Where("FROM_UNIXTIME(schedule_date, '%Y-%m-%d') >= ? and FROM_UNIXTIME(schedule_date, '%Y-%m-%d') <= ? and user_org_id = ? and mode_id = ? and status = 1", start_date, end_date, org_id, mode).Count(&total).Offset(offset).Limit(limit).Find(&qcp).Error
  1103. return
  1104. }
  1105. type otherItemAmount struct {
  1106. Total int64 `json:"total"`
  1107. Name string `json:"name"`
  1108. Ratio string `json:"ratio"`
  1109. }
  1110. // xt_patients model
  1111. type Patient struct {
  1112. ID uint `gorm:"primaryKey"`
  1113. IDCardNo string `gorm:"column:id_card_no"`
  1114. }
  1115. func (Patient) TableName() string {
  1116. return "xt_patients"
  1117. }
  1118. // xt_assessment_before_dislysis model
  1119. type Assessment struct {
  1120. ID uint `gorm:"id"`
  1121. PatientID uint `gorm:"column:patient_id"`
  1122. SystolicBP float64 `gorm:"column:systolic_blood_pressure"`
  1123. DiastolicBP float64 `gorm:"column:diastolic_blood_pressure"`
  1124. AssessmentDate int64 `gorm:"column:assessment_date"`
  1125. UserOrgID int `gorm:"column:user_org_id"`
  1126. }
  1127. func (Assessment) TableName() string {
  1128. return "xt_assessment_before_dislysis"
  1129. }
  1130. // xt_assessment_before_dislysis model
  1131. type AssessmentAfter struct {
  1132. ID uint `gorm:"id"`
  1133. PatientID uint `gorm:"column:patient_id"`
  1134. SystolicBP float64 `gorm:"column:systolic_blood_pressure"`
  1135. DiastolicBP float64 `gorm:"column:diastolic_blood_pressure"`
  1136. AssessmentDate int64 `gorm:"column:assessment_date"`
  1137. UserOrgID int `gorm:"column:user_org_id"`
  1138. }
  1139. func (AssessmentAfter) TableName() string {
  1140. return "xt_assessment_after_dislysis"
  1141. }
  1142. type WeightDetail struct {
  1143. DialysisNo string
  1144. PatientName string
  1145. Gender int64
  1146. IDCardNo string
  1147. DialysisDate string
  1148. DryWeight float64
  1149. LastWeightAfter float64
  1150. WeightBefore float64
  1151. }
  1152. func GetNewDialysisWeightChartData(user_org_id int64, start_time int64, end_time int64, statistics_type int) (item []*otherItemAmount, err error) {
  1153. //db := XTReadDB()
  1154. var items []*otherItemAmount
  1155. var tempErr error
  1156. var Total int64
  1157. switch statistics_type {
  1158. case 1:
  1159. newItemOne := &otherItemAmount{
  1160. Total: 0,
  1161. Name: "小于40kg",
  1162. Ratio: "0",
  1163. }
  1164. newItemTwo := &otherItemAmount{
  1165. Total: 0,
  1166. Name: "40~50kg",
  1167. Ratio: "0",
  1168. }
  1169. newItemThree := &otherItemAmount{
  1170. Total: 0,
  1171. Name: "50~60kg",
  1172. Ratio: "0",
  1173. }
  1174. newItemfour := &otherItemAmount{
  1175. Total: 0,
  1176. Name: "60~70kg",
  1177. Ratio: "0",
  1178. }
  1179. newItemfive := &otherItemAmount{
  1180. Total: 0,
  1181. Name: "大于70kg",
  1182. Ratio: "0",
  1183. }
  1184. newItemsix := &otherItemAmount{
  1185. Total: 0,
  1186. Name: "未知",
  1187. Ratio: "0",
  1188. }
  1189. data, total, _ := GetNewDialysisWeightDetailTableTenThree(user_org_id, start_time, end_time)
  1190. Total = total
  1191. for _, item := range data {
  1192. fmt.Println(item.DryWeight < 40)
  1193. if item.DryWeight < 40 {
  1194. newItemOne.Total++
  1195. } else if item.DryWeight >= 40 && item.DryWeight < 50 {
  1196. newItemTwo.Total++
  1197. } else if item.DryWeight >= 50 && item.DryWeight < 60 {
  1198. newItemThree.Total++
  1199. } else if item.DryWeight >= 60 && item.DryWeight < 70 {
  1200. newItemfour.Total++
  1201. } else if item.DryWeight >= 70 {
  1202. newItemfive.Total++
  1203. //isHasConditionFour = true
  1204. } else {
  1205. newItemsix.Total++
  1206. }
  1207. }
  1208. items = append(items, newItemOne)
  1209. items = append(items, newItemTwo)
  1210. items = append(items, newItemThree)
  1211. items = append(items, newItemfour)
  1212. items = append(items, newItemfive)
  1213. items = append(items, newItemsix)
  1214. for _, item := range items {
  1215. if math.IsNaN(float64(item.Total) / float64(Total)) {
  1216. item.Ratio = "0.0"
  1217. } else {
  1218. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(Total))*100), 64)
  1219. item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
  1220. }
  1221. }
  1222. break
  1223. case 2:
  1224. var newTotal int64
  1225. _, totalone, _ := GetNewDialysisWeightDetailTableTenTenOne(user_org_id, start_time, end_time, 1)
  1226. _, totaltwo, _ := GetNewDialysisWeightDetailTableTenTenOne(user_org_id, start_time, end_time, 2)
  1227. _, totalthree, _ := GetNewDialysisWeightDetailTableTenTenOne(user_org_id, start_time, end_time, 3)
  1228. _, totalfour, _ := GetNewDialysisWeightDetailTableTenTenOne(user_org_id, start_time, end_time, 4)
  1229. newTotal = totalone + totaltwo + totalthree + totalfour
  1230. newItem := &otherItemAmount{
  1231. Total: totalone,
  1232. Name: "体重增长<=3%",
  1233. Ratio: "0",
  1234. }
  1235. newItem1 := &otherItemAmount{
  1236. Total: totaltwo,
  1237. Name: "3%<体重增长<5%",
  1238. Ratio: "0",
  1239. }
  1240. newItem2 := &otherItemAmount{
  1241. Total: totalthree,
  1242. Name: "体重增长>=5%",
  1243. Ratio: "0",
  1244. }
  1245. newItem3 := &otherItemAmount{
  1246. Total: totalfour,
  1247. Name: "其他",
  1248. Ratio: "0",
  1249. }
  1250. items = append(items, newItem)
  1251. items = append(items, newItem1)
  1252. items = append(items, newItem2)
  1253. items = append(items, newItem3)
  1254. for _, item := range items {
  1255. if math.IsNaN(float64(item.Total) / float64(newTotal)) {
  1256. item.Ratio = "0.0"
  1257. } else {
  1258. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(newTotal))*100), 64)
  1259. item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
  1260. }
  1261. }
  1262. break
  1263. case 3:
  1264. var newTotal int64
  1265. _, totalone, _ := GetNewDialysisWeightDetailTableTenTenTwo(user_org_id, start_time, end_time, 1)
  1266. _, totaltwo, _ := GetNewDialysisWeightDetailTableTenTenTwo(user_org_id, start_time, end_time, 2)
  1267. _, totalthree, _ := GetNewDialysisWeightDetailTableTenTenTwo(user_org_id, start_time, end_time, 3)
  1268. _, totalfour, _ := GetNewDialysisWeightDetailTableTenTenTwo(user_org_id, start_time, end_time, 4)
  1269. newTotal = totalone + totaltwo + totalthree + totalfour
  1270. newItem := &otherItemAmount{
  1271. Total: totalone,
  1272. Name: "体重增长<=3%",
  1273. Ratio: "0",
  1274. }
  1275. newItem1 := &otherItemAmount{
  1276. Total: totaltwo,
  1277. Name: "3%<体重增长<5%",
  1278. Ratio: "0",
  1279. }
  1280. newItem2 := &otherItemAmount{
  1281. Total: totalthree,
  1282. Name: "体重增长>=5%",
  1283. Ratio: "0",
  1284. }
  1285. newItem3 := &otherItemAmount{
  1286. Total: totalfour,
  1287. Name: "其他",
  1288. Ratio: "0",
  1289. }
  1290. items = append(items, newItem)
  1291. items = append(items, newItem1)
  1292. items = append(items, newItem2)
  1293. items = append(items, newItem3)
  1294. for _, item := range items {
  1295. if math.IsNaN(float64(item.Total) / float64(newTotal)) {
  1296. item.Ratio = "0.0"
  1297. } else {
  1298. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(newTotal))*100), 64)
  1299. item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
  1300. }
  1301. }
  1302. break
  1303. case 4:
  1304. var newTotal int64
  1305. var total_one int64
  1306. var total_two int64
  1307. var total_three int64
  1308. data, total, _ := GetNewDialysisWeightDetailTableTenTenOne(user_org_id, start_time, end_time, 0)
  1309. newTotal = total
  1310. for _, item := range data {
  1311. if item.WeightStatus == "达标" {
  1312. total_one++
  1313. } else if item.WeightStatus == "不达标" {
  1314. total_two++
  1315. } else if item.WeightStatus == "其他" {
  1316. total_three++
  1317. }
  1318. }
  1319. newTotal = total_one + total_two + total_three
  1320. newItem := &otherItemAmount{
  1321. Total: total_one,
  1322. Name: "达到干体重",
  1323. Ratio: "0",
  1324. }
  1325. newItem1 := &otherItemAmount{
  1326. Total: total_two,
  1327. Name: "未达到干体重",
  1328. Ratio: "0",
  1329. }
  1330. newItem2 := &otherItemAmount{
  1331. Total: total_three,
  1332. Name: "其他或卧床",
  1333. Ratio: "0",
  1334. }
  1335. items = append(items, newItem)
  1336. items = append(items, newItem1)
  1337. items = append(items, newItem2)
  1338. for _, item := range items {
  1339. if math.IsNaN(float64(item.Total) / float64(newTotal)) {
  1340. item.Ratio = "0.0"
  1341. } else {
  1342. float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(newTotal))*100), 64)
  1343. item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
  1344. }
  1345. }
  1346. break
  1347. }
  1348. if tempErr != nil {
  1349. return nil, err
  1350. }
  1351. fmt.Println(items)
  1352. return items, nil
  1353. }
  1354. func GetNewDialysisBloodChartData(user_org_id int64, start_time int64, end_time int64, statistics_type int) (item []otherItemAmount, err error) {
  1355. db := XTReadDB()
  1356. switch statistics_type {
  1357. case 1:
  1358. var assessments []Assessment
  1359. err = db.Joins("JOIN xt_patients ON xt_patients.id = xt_assessment_before_dislysis.patient_id").
  1360. Where("xt_assessment_before_dislysis.assessment_date >= ? AND xt_assessment_before_dislysis.assessment_date <= ? AND xt_assessment_before_dislysis.user_org_id = ?", start_time, end_time, user_org_id).
  1361. Find(&assessments).Error
  1362. if err != nil {
  1363. log.Fatal("Query execution error:", err)
  1364. }
  1365. var total, metStandard, unmetStandard int
  1366. // Iterate through the result set
  1367. for _, assessment := range assessments {
  1368. // Fetch patient information
  1369. var patient Patient
  1370. err = db.First(&patient, assessment.PatientID).Error
  1371. if err != nil {
  1372. log.Printf("Skipping record due to missing patient data for ID: %v", assessment.PatientID)
  1373. continue
  1374. }
  1375. // Calculate the patient's age
  1376. age, err := calculateAge(patient.IDCardNo)
  1377. if err != nil {
  1378. log.Printf("Skipping record due to invalid ID card number: %v", patient.IDCardNo)
  1379. continue
  1380. }
  1381. // Update the total count of patients
  1382. total++
  1383. // Determine if the patient meets the standard based on their age and blood pressure
  1384. if (age < 60 && assessment.SystolicBP < 140 && assessment.DiastolicBP < 90) || (age >= 60 && assessment.SystolicBP < 160 && assessment.DiastolicBP < 90) {
  1385. metStandard++
  1386. } else {
  1387. unmetStandard++
  1388. }
  1389. }
  1390. // Calculate and print the results
  1391. if total > 0 {
  1392. metPercentage := float64(metStandard) / float64(total) * 100
  1393. unmetPercentage := float64(unmetStandard) / float64(total) * 100
  1394. fmt.Printf("Total patients: %d\n", total)
  1395. fmt.Printf("Met standard: %d (%.2f%%)\n", metStandard, metPercentage)
  1396. fmt.Printf("Unmet standard: %d (%.2f%%)\n", unmetStandard, unmetPercentage)
  1397. var items otherItemAmount
  1398. items.Name = "达标患者"
  1399. items.Ratio = strconv.FormatFloat(metPercentage, 'f', 1, 32)
  1400. items.Total = int64(metStandard)
  1401. item = append(item, items)
  1402. var items2 otherItemAmount
  1403. items2.Name = "未达标患者"
  1404. items2.Ratio = strconv.FormatFloat(unmetPercentage, 'f', 1, 32)
  1405. items2.Total = int64(unmetStandard)
  1406. item = append(item, items2)
  1407. }
  1408. case 2:
  1409. var assessments []AssessmentAfter
  1410. err = db.Joins("JOIN xt_patients ON xt_patients.id = xt_assessment_after_dislysis.patient_id").
  1411. Where("xt_assessment_after_dislysis.assessment_date >= ? AND xt_assessment_after_dislysis.assessment_date <= ? AND xt_assessment_after_dislysis.user_org_id = ?", start_time, end_time, user_org_id).
  1412. Find(&assessments).Error
  1413. if err != nil {
  1414. log.Fatal("Query execution error:", err)
  1415. }
  1416. var total, metStandard, unmetStandard int
  1417. // Iterate through the result set
  1418. for _, assessment := range assessments {
  1419. // Fetch patient information
  1420. var patient Patient
  1421. err = db.First(&patient, assessment.PatientID).Error
  1422. if err != nil {
  1423. log.Printf("Skipping record due to missing patient data for ID: %v", assessment.PatientID)
  1424. continue
  1425. }
  1426. // Calculate the patient's age
  1427. age, err := calculateAge(patient.IDCardNo)
  1428. if err != nil {
  1429. log.Printf("Skipping record due to invalid ID card number: %v", patient.IDCardNo)
  1430. continue
  1431. }
  1432. // Update the total count of patients
  1433. total++
  1434. // Determine if the patient meets the standard based on their age and blood pressure
  1435. if (age < 60 && assessment.SystolicBP < 140 && assessment.DiastolicBP < 90) || (age >= 60 && assessment.SystolicBP < 160 && assessment.DiastolicBP < 90) {
  1436. metStandard++
  1437. } else {
  1438. unmetStandard++
  1439. }
  1440. }
  1441. // Calculate and print the results
  1442. if total > 0 {
  1443. metPercentage := float64(metStandard) / float64(total) * 100
  1444. unmetPercentage := float64(unmetStandard) / float64(total) * 100
  1445. fmt.Printf("Total patients: %d\n", total)
  1446. fmt.Printf("Met standard: %d (%.2f%%)\n", metStandard, metPercentage)
  1447. fmt.Printf("Unmet standard: %d (%.2f%%)\n", unmetStandard, unmetPercentage)
  1448. var items otherItemAmount
  1449. items.Name = "达标患者"
  1450. items.Ratio = strconv.FormatFloat(metPercentage, 'f', 1, 32)
  1451. items.Total = int64(metStandard)
  1452. item = append(item, items)
  1453. var items2 otherItemAmount
  1454. items2.Name = "未达标患者"
  1455. items2.Ratio = strconv.FormatFloat(unmetPercentage, 'f', 1, 32)
  1456. items2.Total = int64(unmetStandard)
  1457. item = append(item, items2)
  1458. }
  1459. break
  1460. }
  1461. return
  1462. }
  1463. func calculateAge(idCardNo string) (int, error) {
  1464. if len(idCardNo) < 14 {
  1465. return 0, fmt.Errorf("invalid ID card number")
  1466. }
  1467. // Extract birth year, month, and day from ID card number
  1468. year := idCardNo[6:10]
  1469. month := idCardNo[10:12]
  1470. day := idCardNo[12:14]
  1471. // Parse the birth date
  1472. birthDate, err := time.Parse("20060102", year+month+day)
  1473. if err != nil {
  1474. return 0, err
  1475. }
  1476. // Calculate age
  1477. now := time.Now()
  1478. age := now.Year() - birthDate.Year()
  1479. if now.YearDay() < birthDate.YearDay() {
  1480. age--
  1481. }
  1482. return age, nil
  1483. }
  1484. // 定义结果结构体
  1485. type DialysisDataTwo struct {
  1486. DialysisNo string `json:"dialysis_no"`
  1487. PatientName string `json:"patient_name"`
  1488. Gender string `json:"gender"`
  1489. Age int `json:"age"`
  1490. AssessmentDate string `json:"assessment_date"` // 使用 string 存储日期,或使用 time.Time
  1491. DryWeight float64 `json:"dry_weight"`
  1492. WeightBefore float64 `json:"weight_before"`
  1493. WeightAfter float64 `json:"weight_after"`
  1494. LastWeightAfter float64 `json:"last_weight_after"` // 使用指针处理可能为空的值
  1495. WeightAdd float64 `json:"weight_add"` // 使用指针处理可能为空的值
  1496. DryWeightCategory string `json:"dry_weight_category"`
  1497. WeightStatus string `json:"weight_status"`
  1498. }
  1499. // func GetNewDialysisWeightDetailTableTen(user_org_id int64, start_time int64, end_time int64, addType, dryType, afterType, page, limit int64) ([]DialysisDataTwo, int64, error) {
  1500. // offset := (page - 1) * limit
  1501. // var dialysisData []DialysisDataTwo
  1502. // var total int64
  1503. // // 构建基本查询
  1504. // query := readDb.Table("xt_assessment_before_dislysis b").
  1505. // Select(`
  1506. // p.dialysis_no AS dialysis_no,
  1507. // p.name AS patient_name,
  1508. // p.gender AS gender,
  1509. // FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
  1510. // b.assessment_date AS assessment_date,
  1511. // b.dry_weight AS dry_weight,
  1512. // b.weight_before AS weight_before,
  1513. // a.weight_after AS weight_after,
  1514. // (
  1515. // SELECT a2.weight_after
  1516. // FROM xt_assessment_after_dislysis a2
  1517. // JOIN xt_assessment_before_dislysis b2
  1518. // ON a2.patient_id = b2.patient_id
  1519. // AND a2.assessment_date = b2.assessment_date
  1520. // WHERE b2.patient_id = b.patient_id
  1521. // AND b2.assessment_date < b.assessment_date
  1522. // ORDER BY b2.assessment_date DESC LIMIT 1
  1523. // ) AS last_weight_after,
  1524. // CASE
  1525. // WHEN (
  1526. // SELECT a2.weight_after
  1527. // FROM xt_assessment_after_dislysis a2
  1528. // JOIN xt_assessment_before_dislysis b2
  1529. // ON a2.patient_id = b2.patient_id
  1530. // AND a2.assessment_date = b2.assessment_date
  1531. // WHERE b2.patient_id = b.patient_id
  1532. // AND b2.assessment_date < b.assessment_date
  1533. // ORDER BY b2.assessment_date DESC LIMIT 1
  1534. // ) IS NOT NULL
  1535. // THEN (b.weight_before - (
  1536. // SELECT a2.weight_after
  1537. // FROM xt_assessment_after_dislysis a2
  1538. // JOIN xt_assessment_before_dislysis b2
  1539. // ON a2.patient_id = b2.patient_id
  1540. // AND a2.assessment_date = b2.assessment_date
  1541. // WHERE b2.patient_id = b.patient_id
  1542. // AND b2.assessment_date < b.assessment_date
  1543. // ORDER BY b2.assessment_date DESC LIMIT 1
  1544. // )) / (
  1545. // SELECT a2.weight_after
  1546. // FROM xt_assessment_after_dislysis a2
  1547. // JOIN xt_assessment_before_dislysis b2
  1548. // ON a2.patient_id = b2.patient_id
  1549. // AND a2.assessment_date = b2.assessment_date
  1550. // WHERE b2.patient_id = b.patient_id
  1551. // AND b2.assessment_date < b.assessment_date
  1552. // ORDER BY b2.assessment_date DESC LIMIT 1
  1553. // )
  1554. // ELSE NULL
  1555. // END AS weight_add,
  1556. // CASE
  1557. // WHEN b.dry_weight IS NOT NULL THEN
  1558. // CASE
  1559. // WHEN b.dry_weight < 40 THEN '小于40'
  1560. // WHEN b.dry_weight BETWEEN 40 AND 50 THEN '40~50'
  1561. // WHEN b.dry_weight BETWEEN 50 AND 60 THEN '50~60'
  1562. // WHEN b.dry_weight BETWEEN 60 AND 70 THEN '60~70'
  1563. // WHEN b.dry_weight > 70 THEN '大于70'
  1564. // END
  1565. // ELSE '其他'
  1566. // END AS dry_weight_category,
  1567. // CASE
  1568. // WHEN b.dry_weight IS NOT NULL THEN
  1569. // CASE
  1570. // WHEN ABS(a.weight_after - b.dry_weight) <= 0.3 THEN '达标'
  1571. // ELSE '不达标'
  1572. // END
  1573. // ELSE '其他'
  1574. // END AS weight_status
  1575. // `).
  1576. // Joins("JOIN xt_patients p ON p.id = b.patient_id").
  1577. // Joins("JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date AND a.status = 1").
  1578. // Where("b.user_org_id = ?", user_org_id).
  1579. // Where("b.assessment_date >= ?", start_time).
  1580. // Where("b.assessment_date <= ?", end_time).
  1581. // Where("b.status = ?", 1)
  1582. //
  1583. // fmt.Println("dry_type")
  1584. // fmt.Println(dryType)
  1585. // switch dryType {
  1586. // case 1:
  1587. // query.Where("dry_weight_category = '小于40'")
  1588. // case 2:
  1589. // query.Where("dry_weight_category = '40~50'")
  1590. // case 3:
  1591. // query.Where("dry_weight_category = '50~60'")
  1592. // case 4:
  1593. // query.Where("dry_weight_category = '60~70'")
  1594. // case 5:
  1595. // query.Where("dry_weight_category = '大于70'")
  1596. // }
  1597. //
  1598. // // 根据传入的 add_type 值动态构建条件
  1599. // addTypeCondition(query, addType)
  1600. // // 根据传入的 dry_type 值动态构建条件
  1601. // //fmt.Println("dry_type")
  1602. // //fmt.Println(dryType)
  1603. //
  1604. // //dryTypeCondition(query, dryType)
  1605. // // 根据传入的 after_type 值动态构建条件
  1606. // afterTypeCondition(query, afterType)
  1607. // // 获取总记录数
  1608. // if err := query.Count(&total).Error; err != nil {
  1609. // return nil, 0, fmt.Errorf("count query failed: %v", err)
  1610. // }
  1611. // // 获取分页数据
  1612. // rows, err := query.Order("p.id, b.assessment_date").Offset(int(offset)).Limit(int(limit)).Rows()
  1613. // if err != nil {
  1614. // return nil, 0, fmt.Errorf("pagination query failed: %v", err)
  1615. // }
  1616. // defer rows.Close()
  1617. // // 处理结果集
  1618. // for rows.Next() {
  1619. // var data DialysisDataTwo
  1620. // if err := rows.Scan(
  1621. // &data.DialysisNo,
  1622. // &data.PatientName,
  1623. // &data.Gender,
  1624. // &data.Age,
  1625. // &data.AssessmentDate,
  1626. // &data.DryWeight,
  1627. // &data.WeightBefore,
  1628. // &data.WeightAfter,
  1629. // &data.LastWeightAfter,
  1630. // &data.WeightAdd,
  1631. // &data.DryWeightCategory,
  1632. // &data.WeightStatus,
  1633. // ); err != nil {
  1634. // return nil, 0, fmt.Errorf("row scan failed: %v", err)
  1635. // }
  1636. // // 将每一行结果追加到切片中
  1637. // dialysisData = append(dialysisData, data)
  1638. // }
  1639. // return dialysisData, total, nil
  1640. // }
  1641. func GetNewDialysisWeightDetailTableTenFour(
  1642. user_org_id int64,
  1643. start_time int64,
  1644. end_time int64,
  1645. addType, dryType, afterType, page, limit int64,
  1646. ) ([]DialysisDataTwo, int64, error) {
  1647. offset := (page - 1) * limit
  1648. var total int64
  1649. // 构建基本查询
  1650. query := readDb.Table("xt_assessment_before_dislysis b").
  1651. Select(`
  1652. p.dialysis_no AS dialysis_no,
  1653. p.name AS patient_name,
  1654. p.gender AS gender,
  1655. FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
  1656. b.assessment_date AS assessment_date,
  1657. b.dry_weight AS dry_weight,
  1658. b.weight_before AS weight_before,
  1659. a.weight_after AS weight_after,
  1660. (
  1661. SELECT a2.weight_after
  1662. FROM xt_assessment_after_dislysis a2
  1663. JOIN xt_assessment_before_dislysis b2
  1664. ON a2.patient_id = b2.patient_id
  1665. AND a2.assessment_date = b2.assessment_date
  1666. WHERE b2.patient_id = b.patient_id
  1667. AND b2.assessment_date < b.assessment_date
  1668. ORDER BY b2.assessment_date DESC LIMIT 1
  1669. ) AS last_weight_after,
  1670. CASE
  1671. WHEN (
  1672. SELECT a2.weight_after
  1673. FROM xt_assessment_after_dislysis a2
  1674. JOIN xt_assessment_before_dislysis b2
  1675. ON a2.patient_id = b2.patient_id
  1676. AND a2.assessment_date = b2.assessment_date
  1677. WHERE b2.patient_id = b.patient_id
  1678. AND b2.assessment_date < b.assessment_date
  1679. ORDER BY b2.assessment_date DESC LIMIT 1
  1680. ) IS NOT NULL
  1681. THEN (b.weight_before - (
  1682. SELECT a2.weight_after
  1683. FROM xt_assessment_after_dislysis a2
  1684. JOIN xt_assessment_before_dislysis b2
  1685. ON a2.patient_id = b2.patient_id
  1686. AND a2.assessment_date = b2.assessment_date
  1687. WHERE b2.patient_id = b.patient_id
  1688. AND b2.assessment_date < b.assessment_date
  1689. ORDER BY b2.assessment_date DESC LIMIT 1
  1690. )) / (
  1691. SELECT a2.weight_after
  1692. FROM xt_assessment_after_dislysis a2
  1693. JOIN xt_assessment_before_dislysis b2
  1694. ON a2.patient_id = b2.patient_id
  1695. AND a2.assessment_date = b2.assessment_date
  1696. WHERE b2.patient_id = b.patient_id
  1697. AND b2.assessment_date < b.assessment_date
  1698. ORDER BY b2.assessment_date DESC LIMIT 1
  1699. )
  1700. ELSE NULL
  1701. END AS weight_add,
  1702. CASE
  1703. WHEN b.dry_weight IS NOT NULL THEN
  1704. CASE
  1705. WHEN ABS(a.weight_after - b.dry_weight) <= 0.3 THEN '达标'
  1706. ELSE '不达标'
  1707. END
  1708. ELSE '其他'
  1709. END AS weight_status
  1710. `).
  1711. Joins("left JOIN xt_patients p ON p.id = b.patient_id").
  1712. Joins("left JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date AND a.status = 1").
  1713. Where("b.user_org_id = ?", user_org_id).
  1714. Where("b.assessment_date >= ?", start_time).
  1715. Where("b.assessment_date <= ?", end_time).
  1716. Where("b.status = ?", 1)
  1717. // 处理干重过滤
  1718. switch dryType {
  1719. case 5:
  1720. query = query.Where("b.dry_weight < ?", 40)
  1721. case 1:
  1722. query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 40, 50)
  1723. case 2:
  1724. query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 50, 60)
  1725. case 3:
  1726. query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 60, 70)
  1727. case 4:
  1728. query = query.Where("b.dry_weight >= ?", 70)
  1729. case 6:
  1730. query = query.Where("b.dry_weight IS NULL OR b.dry_weight = ''")
  1731. }
  1732. // 处理加重过滤
  1733. switch addType {
  1734. case 1:
  1735. query = query.Where("weight_add <= 0.03")
  1736. case 2:
  1737. query = query.Where("weight_add > 0.03 AND weight_add < 0.05")
  1738. case 3:
  1739. query = query.Where("weight_add >= 0.05")
  1740. case 4:
  1741. query = query.Where("weight_add IS NULL")
  1742. }
  1743. // 处理状态过滤
  1744. switch afterType {
  1745. case 1:
  1746. query = query.Having("CASE WHEN b.dry_weight IS NOT NULL THEN CASE WHEN ABS(a.weight_after - b.dry_weight) <= 0.3 THEN '达标' ELSE '不达标' END ELSE '其他' END = ?", "达标")
  1747. case 2:
  1748. query = query.Having("CASE WHEN b.dry_weight IS NOT NULL THEN CASE WHEN ABS(a.weight_after - b.dry_weight) <= 0.3 THEN '达标' ELSE '不达标' END ELSE '其他' END = ?", "不达标")
  1749. case 3:
  1750. query = query.Having("CASE WHEN b.dry_weight IS NOT NULL THEN CASE WHEN ABS(a.weight_after - b.dry_weight) <= 0.3 THEN '达标' ELSE '不达标' END ELSE '其他' END = ?", "其他")
  1751. }
  1752. // 获取总记录数
  1753. if err := query.Count(&total).Error; err != nil {
  1754. return nil, 0, fmt.Errorf("count query failed: %v", err)
  1755. }
  1756. var data []DialysisDataTwo
  1757. query.Order("p.id, b.assessment_date").Offset(int(offset)).Limit(int(limit)).Find(&data)
  1758. fmt.Println(data)
  1759. return data, total, nil
  1760. }
  1761. // func GetNewDialysisWeightDetailTableTen(
  1762. //
  1763. // user_org_id int64,
  1764. // start_time int64,
  1765. // end_time int64,
  1766. // addType, dryType, afterType, page, limit int64, keyword string) ([]DialysisDataTwo, int64, error) {
  1767. // offset := (page - 1) * limit
  1768. // //var dialysisData []DialysisDataTwo
  1769. // var total int64
  1770. // keyword = "%" + keyword + "%"
  1771. // // 构建基本查询
  1772. // query := readDb.Table("xt_assessment_before_dislysis b").
  1773. // Select(`
  1774. // p.dialysis_no AS dialysis_no,
  1775. // p.name AS patient_name,
  1776. // p.gender AS gender,
  1777. // FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
  1778. // b.assessment_date AS assessment_date,
  1779. // latest_dry_weight.dry_weight AS dry_weight,
  1780. // b.dry_weight AS dry_weight,
  1781. // b.weight_before AS weight_before,
  1782. // a.weight_after AS weight_after,
  1783. // (
  1784. // SELECT a2.weight_after
  1785. // FROM xt_assessment_after_dislysis a2
  1786. // JOIN xt_assessment_before_dislysis b2
  1787. // ON a2.patient_id = b2.patient_id
  1788. // AND a2.assessment_date = b2.assessment_date
  1789. // WHERE b2.patient_id = b.patient_id
  1790. // AND b2.assessment_date < b.assessment_date
  1791. // ORDER BY b2.assessment_date DESC LIMIT 1
  1792. // ) AS last_weight_after,
  1793. // CASE
  1794. // WHEN (
  1795. // SELECT a2.weight_after
  1796. // FROM xt_assessment_after_dislysis a2
  1797. // JOIN xt_assessment_before_dislysis b2
  1798. // ON a2.patient_id = b2.patient_id
  1799. // AND a2.assessment_date = b2.assessment_date
  1800. // WHERE b2.patient_id = b.patient_id
  1801. // AND b2.assessment_date < b.assessment_date
  1802. // ORDER BY b2.assessment_date DESC LIMIT 1
  1803. // ) IS NOT NULL
  1804. // THEN (b.weight_before - (
  1805. // SELECT a2.weight_after
  1806. // FROM xt_assessment_after_dislysis a2
  1807. // JOIN xt_assessment_before_dislysis b2
  1808. // ON a2.patient_id = b2.patient_id
  1809. // AND a2.assessment_date = b2.assessment_date
  1810. // WHERE b2.patient_id = b.patient_id
  1811. // AND b2.assessment_date < b.assessment_date
  1812. // ORDER BY b2.assessment_date DESC LIMIT 1
  1813. // )) / (
  1814. // SELECT a2.weight_after
  1815. // FROM xt_assessment_after_dislysis a2
  1816. // JOIN xt_assessment_before_dislysis b2
  1817. // ON a2.patient_id = b2.patient_id
  1818. // AND a2.assessment_date = b2.assessment_date
  1819. // WHERE b2.patient_id = b.patient_id
  1820. // AND b2.assessment_date < b.assessment_date
  1821. // ORDER BY b2.assessment_date DESC LIMIT 1
  1822. // )
  1823. // ELSE NULL
  1824. // END AS weight_add,
  1825. // CASE
  1826. // WHEN a.weight_after IS NULL THEN '其他'
  1827. // WHEN b.dry_weight IS NOT NULL THEN
  1828. // CASE
  1829. // WHEN ABS(a.weight_after - b.dry_weight) <= 0.3 THEN '达标'
  1830. // ELSE '不达标'
  1831. // END
  1832. // ELSE '其他'
  1833. // END AS weight_status
  1834. // `).
  1835. // Joins("JOIN xt_patients p ON p.id = b.patient_id and p.name like ?", keyword).
  1836. // Joins("left JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date AND a.status = 1").
  1837. // Where("b.user_org_id = ?", user_org_id).
  1838. // Where("b.assessment_date >= ?", start_time).
  1839. // Where("b.assessment_date <= ?", end_time).
  1840. // Where("b.status = ?", 1)
  1841. //
  1842. // switch dryType {
  1843. // case 5:
  1844. // query = query.Where("b.dry_weight < ?", 40)
  1845. // case 1:
  1846. // query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 40, 50)
  1847. // case 2:
  1848. // query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 50, 60)
  1849. // case 3:
  1850. // query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 60, 70)
  1851. // case 4:
  1852. // query = query.Where("b.dry_weight >= ?", 70)
  1853. // case 6:
  1854. // query = query.Where("b.dry_weight IS NULL OR b.dry_weight = ''")
  1855. // }
  1856. //
  1857. // switch addType {
  1858. // case 1:
  1859. // query = query.Where(`
  1860. // (
  1861. // CASE
  1862. // WHEN (
  1863. // SELECT a2.weight_after
  1864. // FROM xt_assessment_after_dislysis a2
  1865. // JOIN xt_assessment_before_dislysis b2
  1866. // ON a2.patient_id = b2.patient_id
  1867. // AND a2.assessment_date = b2.assessment_date
  1868. // WHERE b2.patient_id = b.patient_id
  1869. // AND b2.assessment_date < b.assessment_date
  1870. // ORDER BY b2.assessment_date DESC LIMIT 1
  1871. // ) IS NOT NULL
  1872. // THEN (b.weight_before - (
  1873. // SELECT a2.weight_after
  1874. // FROM xt_assessment_after_dislysis a2
  1875. // JOIN xt_assessment_before_dislysis b2
  1876. // ON a2.patient_id = b2.patient_id
  1877. // AND a2.assessment_date = b2.assessment_date
  1878. // WHERE b2.patient_id = b.patient_id
  1879. // AND b2.assessment_date < b.assessment_date
  1880. // ORDER BY b2.assessment_date DESC LIMIT 1
  1881. // )) / (
  1882. // SELECT a2.weight_after
  1883. // FROM xt_assessment_after_dislysis a2
  1884. // JOIN xt_assessment_before_dislysis b2
  1885. // ON a2.patient_id = b2.patient_id
  1886. // AND a2.assessment_date = b2.assessment_date
  1887. // WHERE b2.patient_id = b.patient_id
  1888. // AND b2.assessment_date < b.assessment_date
  1889. // ORDER BY b2.assessment_date DESC LIMIT 1
  1890. // )
  1891. // ELSE NULL
  1892. // END
  1893. // ) <= 0.03
  1894. // `)
  1895. // case 2:
  1896. // query = query.Where(`
  1897. // (
  1898. // CASE
  1899. // WHEN (
  1900. // SELECT a2.weight_after
  1901. // FROM xt_assessment_after_dislysis a2
  1902. // JOIN xt_assessment_before_dislysis b2
  1903. // ON a2.patient_id = b2.patient_id
  1904. // AND a2.assessment_date = b2.assessment_date
  1905. // WHERE b2.patient_id = b.patient_id
  1906. // AND b2.assessment_date < b.assessment_date
  1907. // ORDER BY b2.assessment_date DESC LIMIT 1
  1908. // ) IS NOT NULL
  1909. // THEN (b.weight_before - (
  1910. // SELECT a2.weight_after
  1911. // FROM xt_assessment_after_dislysis a2
  1912. // JOIN xt_assessment_before_dislysis b2
  1913. // ON a2.patient_id = b2.patient_id
  1914. // AND a2.assessment_date = b2.assessment_date
  1915. // WHERE b2.patient_id = b.patient_id
  1916. // AND b2.assessment_date < b.assessment_date
  1917. // ORDER BY b2.assessment_date DESC LIMIT 1
  1918. // )) / (
  1919. // SELECT a2.weight_after
  1920. // FROM xt_assessment_after_dislysis a2
  1921. // JOIN xt_assessment_before_dislysis b2
  1922. // ON a2.patient_id = b2.patient_id
  1923. // AND a2.assessment_date = b2.assessment_date
  1924. // WHERE b2.patient_id = b.patient_id
  1925. // AND b2.assessment_date < b.assessment_date
  1926. // ORDER BY b2.assessment_date DESC LIMIT 1
  1927. // )
  1928. // ELSE NULL
  1929. // END
  1930. // ) > 0.03 AND (
  1931. // CASE
  1932. // WHEN (
  1933. // SELECT a2.weight_after
  1934. // FROM xt_assessment_after_dislysis a2
  1935. // JOIN xt_assessment_before_dislysis b2
  1936. // ON a2.patient_id = b2.patient_id
  1937. // AND a2.assessment_date = b2.assessment_date
  1938. // WHERE b2.patient_id = b.patient_id
  1939. // AND b2.assessment_date < b.assessment_date
  1940. // ORDER BY b2.assessment_date DESC LIMIT 1
  1941. // ) IS NOT NULL
  1942. // THEN (b.weight_before - (
  1943. // SELECT a2.weight_after
  1944. // FROM xt_assessment_after_dislysis a2
  1945. // JOIN xt_assessment_before_dislysis b2
  1946. // ON a2.patient_id = b2.patient_id
  1947. // AND a2.assessment_date = b2.assessment_date
  1948. // WHERE b2.patient_id = b.patient_id
  1949. // AND b2.assessment_date < b.assessment_date
  1950. // ORDER BY b2.assessment_date DESC LIMIT 1
  1951. // )) / (
  1952. // SELECT a2.weight_after
  1953. // FROM xt_assessment_after_dislysis a2
  1954. // JOIN xt_assessment_before_dislysis b2
  1955. // ON a2.patient_id = b2.patient_id
  1956. // AND a2.assessment_date = b2.assessment_date
  1957. // WHERE b2.patient_id = b.patient_id
  1958. // AND b2.assessment_date < b.assessment_date
  1959. // ORDER BY b2.assessment_date DESC LIMIT 1
  1960. // )
  1961. // ELSE NULL
  1962. // END
  1963. // ) < 0.05
  1964. // `)
  1965. // case 3:
  1966. // query = query.Where(`
  1967. // (
  1968. // CASE
  1969. // WHEN (
  1970. // SELECT a2.weight_after
  1971. // FROM xt_assessment_after_dislysis a2
  1972. // JOIN xt_assessment_before_dislysis b2
  1973. // ON a2.patient_id = b2.patient_id
  1974. // AND a2.assessment_date = b2.assessment_date
  1975. // WHERE b2.patient_id = b.patient_id
  1976. // AND b2.assessment_date < b.assessment_date
  1977. // ORDER BY b2.assessment_date DESC LIMIT 1
  1978. // ) IS NOT NULL
  1979. // THEN (b.weight_before - (
  1980. // SELECT a2.weight_after
  1981. // FROM xt_assessment_after_dislysis a2
  1982. // JOIN xt_assessment_before_dislysis b2
  1983. // ON a2.patient_id = b2.patient_id
  1984. // AND a2.assessment_date = b2.assessment_date
  1985. // WHERE b2.patient_id = b.patient_id
  1986. // AND b2.assessment_date < b.assessment_date
  1987. // ORDER BY b2.assessment_date DESC LIMIT 1
  1988. // )) / (
  1989. // SELECT a2.weight_after
  1990. // FROM xt_assessment_after_dislysis a2
  1991. // JOIN xt_assessment_before_dislysis b2
  1992. // ON a2.patient_id = b2.patient_id
  1993. // AND a2.assessment_date = b2.assessment_date
  1994. // WHERE b2.patient_id = b.patient_id
  1995. // AND b2.assessment_date < b.assessment_date
  1996. // ORDER BY b2.assessment_date DESC LIMIT 1
  1997. // )
  1998. // ELSE NULL
  1999. // END
  2000. // ) >= 0.05
  2001. // `)
  2002. // case 4:
  2003. // query = query.Where(`
  2004. // (
  2005. // SELECT a2.weight_after
  2006. // FROM xt_assessment_after_dislysis a2
  2007. // JOIN xt_assessment_before_dislysis b2
  2008. // ON a2.patient_id = b2.patient_id
  2009. // AND a2.assessment_date = b2.assessment_date
  2010. // WHERE b2.patient_id = b.patient_id
  2011. // AND b2.assessment_date < b.assessment_date
  2012. // ORDER BY b2.assessment_date DESC LIMIT 1
  2013. // ) IS NULL
  2014. // `)
  2015. // }
  2016. //
  2017. // switch afterType {
  2018. // case 1:
  2019. // query = query.Where(`
  2020. // CASE
  2021. // WHEN a.weight_after IS NULL THEN '其他'
  2022. // WHEN b.dry_weight IS NOT NULL THEN
  2023. // CASE
  2024. // WHEN ABS(a.weight_after - b.dry_weight) <= 0.3 THEN '达标'
  2025. // ELSE '不达标'
  2026. // END
  2027. // ELSE '其他'
  2028. // END = ?`, "达标")
  2029. // case 2:
  2030. // query = query.Where(`
  2031. // CASE
  2032. // WHEN a.weight_after IS NULL THEN '其他'
  2033. // WHEN b.dry_weight IS NOT NULL THEN
  2034. // CASE
  2035. // WHEN ABS(a.weight_after - b.dry_weight) <= 0.3 THEN '达标'
  2036. // ELSE '不达标'
  2037. // END
  2038. // ELSE '其他'
  2039. // END = ?`, "不达标")
  2040. // case 3:
  2041. // query = query.Where(`
  2042. // CASE
  2043. // WHEN a.weight_after IS NULL THEN '其他'
  2044. // WHEN b.dry_weight IS NOT NULL THEN
  2045. // CASE
  2046. // WHEN ABS(a.weight_after - b.dry_weight) <= 0.3 THEN '达标'
  2047. // ELSE '不达标'
  2048. // END
  2049. // ELSE '其他'
  2050. // END = ?`, "其他")
  2051. // }
  2052. //
  2053. // //// 获取总记录数
  2054. // if err := query.Count(&total).Error; err != nil {
  2055. // return nil, 0, fmt.Errorf("count query failed: %v", err)
  2056. // }
  2057. //
  2058. // var data []DialysisDataTwo
  2059. //
  2060. // query.Order("p.id, b.assessment_date").Offset(int(offset)).Limit(int(limit)).Find(&data)
  2061. //
  2062. // return data, total, nil
  2063. // }
  2064. //func GetNewDialysisWeightDetailTableTenTenOne(
  2065. // user_org_id int64,
  2066. // start_time int64,
  2067. // end_time int64,
  2068. //
  2069. //) ([]DialysisDataTwo, int64, error) {
  2070. //
  2071. // // Build base query
  2072. // query := readDb.Table("xt_assessment_before_dislysis b").
  2073. // Select(`
  2074. // p.dialysis_no AS dialysis_no,
  2075. // p.name AS patient_name,
  2076. // p.gender AS gender,
  2077. // FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
  2078. // b.assessment_date AS assessment_date,
  2079. // b.dry_weight AS dry_weight,
  2080. // b.weight_before AS weight_before,
  2081. // a.weight_after AS weight_after,
  2082. // (
  2083. // SELECT a2.weight_after
  2084. // FROM xt_assessment_after_dislysis a2
  2085. // WHERE a2.patient_id = b.patient_id
  2086. // AND a2.assessment_date < b.assessment_date
  2087. // ORDER BY a2.assessment_date DESC LIMIT 1
  2088. // ) AS last_weight_after,
  2089. // CASE
  2090. // WHEN (
  2091. // SELECT a2.weight_after
  2092. // FROM xt_assessment_after_dislysis a2
  2093. // WHERE a2.patient_id = b.patient_id
  2094. // AND a2.assessment_date < b.assessment_date
  2095. // ORDER BY a2.assessment_date DESC LIMIT 1
  2096. // ) IS NOT NULL
  2097. // THEN (b.weight_before - (
  2098. // SELECT a2.weight_after
  2099. // FROM xt_assessment_after_dislysis a2
  2100. // WHERE a2.patient_id = b.patient_id
  2101. // AND a2.assessment_date < b.assessment_date
  2102. // ORDER BY a2.assessment_date DESC LIMIT 1
  2103. // )) / (
  2104. // SELECT a2.weight_after
  2105. // FROM xt_assessment_after_dislysis a2
  2106. // WHERE a2.patient_id = b.patient_id
  2107. // AND a2.assessment_date < b.assessment_date
  2108. // ORDER BY a2.assessment_date DESC LIMIT 1
  2109. // )
  2110. // ELSE NULL
  2111. // END AS weight_add,
  2112. // CASE
  2113. // WHEN a.weight_after IS NULL OR a.weight_after = 0 THEN '其他'
  2114. // WHEN latest_dry_weight.dry_weight IS NOT NULL THEN
  2115. // CASE
  2116. // WHEN ABS(a.weight_after - latest_dry_weight.dry_weight) <= 0.3 THEN '达标'
  2117. // ELSE '不达标'
  2118. // END
  2119. // ELSE '其他'
  2120. // END AS weight_status
  2121. // `).
  2122. // Joins("JOIN xt_patients p ON p.id = b.patient_id ").
  2123. // Joins("LEFT JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date AND a.status = 1").
  2124. // Where("b.user_org_id = ?", user_org_id).
  2125. // Where("b.assessment_date BETWEEN ? AND ?", start_time, end_time).
  2126. // Where("b.assessment_date = latest_dry_weight.max_assessment_date").
  2127. // Where("b.status = ?", 1)
  2128. //
  2129. // // Apply filters for dryType
  2130. //
  2131. // // Apply filters for addType
  2132. //
  2133. // // Apply filters for afterType
  2134. // var total int64
  2135. //
  2136. // // Count total records
  2137. // if err := query.Count(&total).Error; err != nil {
  2138. // return nil, 0, fmt.Errorf("count query failed: %v", err)
  2139. // }
  2140. //
  2141. // // Fetch data with pagination
  2142. // var data []DialysisDataTwo
  2143. // if err := query.Order("p.id, b.assessment_date").Find(&data).Error; err != nil {
  2144. // return nil, 0, fmt.Errorf("data query failed: %v", err)
  2145. // }
  2146. //
  2147. // return data, total, nil
  2148. //}
  2149. // func GetNewDialysisWeightDetailTableTenTenOne(
  2150. //
  2151. // user_org_id int64,
  2152. // start_time int64,
  2153. // end_time int64,
  2154. // addType, dryType, afterType, page, limit int64, keyword string,
  2155. //
  2156. // ) ([]DialysisDataTwo, int64, error) {
  2157. // offset := (page - 1) * limit
  2158. // var total int64
  2159. // keyword = "%" + keyword + "%"
  2160. //
  2161. // // Build base query
  2162. // query := readDb.Table("xt_assessment_before_dislysis b").
  2163. // Select(`
  2164. // p.dialysis_no AS dialysis_no,
  2165. // p.name AS patient_name,
  2166. // p.gender AS gender,
  2167. // FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
  2168. // b.assessment_date AS assessment_date,
  2169. // latest_dry_weight.dry_weight AS dry_weight,
  2170. // b.weight_before AS weight_before,
  2171. // a.weight_after AS weight_after,
  2172. // (
  2173. // SELECT a2.weight_after
  2174. // FROM xt_assessment_after_dislysis a2
  2175. // WHERE a2.patient_id = b.patient_id
  2176. // AND a2.assessment_date < b.assessment_date
  2177. // ORDER BY a2.assessment_date DESC LIMIT 1
  2178. // ) AS last_weight_after,
  2179. // CASE
  2180. // WHEN (
  2181. // SELECT a2.weight_after
  2182. // FROM xt_assessment_after_dislysis a2
  2183. // WHERE a2.patient_id = b.patient_id
  2184. // AND a2.assessment_date < b.assessment_date
  2185. // ORDER BY a2.assessment_date DESC LIMIT 1
  2186. // ) IS NOT NULL
  2187. // THEN (b.weight_before - (
  2188. // SELECT a2.weight_after
  2189. // FROM xt_assessment_after_dislysis a2
  2190. // WHERE a2.patient_id = b.patient_id
  2191. // AND a2.assessment_date < b.assessment_date
  2192. // ORDER BY a2.assessment_date DESC LIMIT 1
  2193. // )) / (
  2194. // SELECT a2.weight_after
  2195. // FROM xt_assessment_after_dislysis a2
  2196. // WHERE a2.patient_id = b.patient_id
  2197. // AND a2.assessment_date < b.assessment_date
  2198. // ORDER BY a2.assessment_date DESC LIMIT 1
  2199. // )
  2200. // ELSE NULL
  2201. // END AS weight_add,
  2202. // CASE
  2203. // WHEN a.weight_after IS NULL THEN '其他'
  2204. // WHEN latest_dry_weight.dry_weight IS NOT NULL THEN
  2205. // CASE
  2206. // WHEN ABS(a.weight_after - latest_dry_weight.dry_weight) <= 0.3 THEN '达标'
  2207. // ELSE '不达标'
  2208. // END
  2209. // ELSE '其他'
  2210. // END AS weight_status
  2211. // `).
  2212. // Joins("JOIN xt_patients p ON p.id = b.patient_id AND p.name LIKE ?", keyword).
  2213. // Joins("LEFT JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date AND a.status = 1").
  2214. // Joins("JOIN (SELECT b2.patient_id, MAX(b2.assessment_date) AS max_assessment_date, MAX(b2.dry_weight) AS dry_weight FROM xt_assessment_before_dislysis b2 WHERE b2.user_org_id = ? AND b2.assessment_date BETWEEN ? AND ? GROUP BY b2.patient_id) latest_dry_weight ON latest_dry_weight.patient_id = b.patient_id", user_org_id, start_time, end_time).
  2215. // Where("b.user_org_id = ?", user_org_id).
  2216. // Where("b.assessment_date BETWEEN ? AND ?", start_time, end_time).
  2217. // Where("b.assessment_date = latest_dry_weight.max_assessment_date").
  2218. // Where("b.status = ?", 1)
  2219. //
  2220. // // Apply filters for dryType
  2221. // switch dryType {
  2222. // case 5:
  2223. // query = query.Where("b.dry_weight < ?", 40)
  2224. // case 1:
  2225. // query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 40, 50)
  2226. // case 2:
  2227. // query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 50, 60)
  2228. // case 3:
  2229. // query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 60, 70)
  2230. // case 4:
  2231. // query = query.Where("b.dry_weight >= ?", 70)
  2232. // case 6:
  2233. // query = query.Where("b.dry_weight IS NULL OR b.dry_weight = ''")
  2234. // }
  2235. //
  2236. // // Apply filters for addType
  2237. // switch addType {
  2238. // case 1:
  2239. // query = query.Where("(b.weight_before - last_weight_after) / last_weight_after <= 0.03")
  2240. // case 2:
  2241. // query = query.Where("(b.weight_before - last_weight_after) / last_weight_after > 0.03 AND (b.weight_before - last_weight_after) / last_weight_after < 0.05")
  2242. // case 3:
  2243. // query = query.Where("(b.weight_before - last_weight_after) / last_weight_after >= 0.05")
  2244. // case 4:
  2245. // query = query.Where("last_weight_after IS NULL")
  2246. // }
  2247. //
  2248. // // Apply filters for afterType
  2249. // switch afterType {
  2250. // case 1:
  2251. // query = query.Where("CASE WHEN a.weight_after IS NULL THEN '其他' WHEN latest_dry_weight.dry_weight IS NOT NULL AND ABS(a.weight_after - latest_dry_weight.dry_weight) <= 0.3 THEN '达标' ELSE '不达标' END = ?", "达标")
  2252. // case 2:
  2253. // query = query.Where("CASE WHEN a.weight_after IS NULL THEN '其他' WHEN latest_dry_weight.dry_weight IS NOT NULL AND ABS(a.weight_after - latest_dry_weight.dry_weight) <= 0.3 THEN '达标' ELSE '不达标' END = ?", "不达标")
  2254. // case 3:
  2255. // query = query.Where("CASE WHEN a.weight_after IS NULL THEN '其他' WHEN latest_dry_weight.dry_weight IS NOT NULL AND ABS(a.weight_after - latest_dry_weight.dry_weight) <= 0.3 THEN '达标' ELSE '不达标' END = ?", "其他")
  2256. // }
  2257. //
  2258. // // Count total records
  2259. // if err := query.Count(&total).Error; err != nil {
  2260. // return nil, 0, fmt.Errorf("count query failed: %v", err)
  2261. // }
  2262. //
  2263. // // Fetch data with pagination
  2264. // var data []DialysisDataTwo
  2265. // if err := query.Order("p.id, b.assessment_date").Offset(int(offset)).Limit(int(limit)).Find(&data).Error; err != nil {
  2266. // return nil, 0, fmt.Errorf("data query failed: %v", err)
  2267. // }
  2268. //
  2269. // return data, total, nil
  2270. // }
  2271. func GetNewDialysisWeightDetailTableTen(
  2272. user_org_id int64,
  2273. start_time int64,
  2274. end_time int64,
  2275. addType, dryType, afterType, page, limit int64, keyword string,
  2276. ) ([]DialysisDataTwo, int64, error) {
  2277. offset := (page - 1) * limit
  2278. var total int64
  2279. keyword = "%" + keyword + "%"
  2280. // Build base query
  2281. query := readDb.Table("xt_assessment_before_dislysis b").
  2282. Select(`
  2283. p.dialysis_no AS dialysis_no,
  2284. p.name AS patient_name,
  2285. p.gender AS gender,
  2286. FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
  2287. b.assessment_date AS assessment_date,
  2288. latest_dry_weight.dry_weight AS dry_weight,
  2289. b.weight_before AS weight_before,
  2290. a.weight_after AS weight_after,
  2291. (
  2292. SELECT a2.weight_after
  2293. FROM xt_assessment_after_dislysis a2
  2294. WHERE a2.patient_id = b.patient_id
  2295. AND a2.assessment_date < b.assessment_date
  2296. ORDER BY a2.assessment_date DESC LIMIT 1
  2297. ) AS last_weight_after
  2298. `).
  2299. Joins("JOIN xt_patients p ON p.id = b.patient_id AND p.name LIKE ?", keyword).
  2300. Joins("LEFT JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date AND a.status = 1").
  2301. Joins("JOIN (SELECT b2.patient_id, MAX(b2.assessment_date) AS max_assessment_date, MAX(b2.dry_weight) AS dry_weight FROM xt_assessment_before_dislysis b2 WHERE b2.user_org_id = ? AND b2.assessment_date BETWEEN ? AND ? GROUP BY b2.patient_id) latest_dry_weight ON latest_dry_weight.patient_id = b.patient_id", user_org_id, start_time, end_time).
  2302. Where("b.user_org_id = ?", user_org_id).
  2303. Where("b.assessment_date BETWEEN ? AND ?", start_time, end_time).
  2304. Where("b.assessment_date = latest_dry_weight.max_assessment_date").
  2305. Where("b.status = ?", 1)
  2306. // Apply filters for dryType
  2307. switch dryType {
  2308. case 5:
  2309. query = query.Where("b.dry_weight < ?", 40)
  2310. case 1:
  2311. query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 40, 50)
  2312. case 2:
  2313. query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 50, 60)
  2314. case 3:
  2315. query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 60, 70)
  2316. case 4:
  2317. query = query.Where("b.dry_weight >= ?", 70)
  2318. case 6:
  2319. query = query.Where("b.dry_weight IS NULL OR b.dry_weight = ''")
  2320. }
  2321. // Calculate last_weight_after as a subquery in the HAVING clause
  2322. query = query.Having("last_weight_after IS NOT NULL")
  2323. // Apply filters for addType
  2324. switch addType {
  2325. case 1:
  2326. query = query.Having("(b.weight_before - last_weight_after) / last_weight_after <= 0.03")
  2327. case 2:
  2328. query = query.Having("(b.weight_before - last_weight_after) / last_weight_after > 0.03 AND (b.weight_before - last_weight_after) / last_weight_after < 0.05")
  2329. case 3:
  2330. query = query.Having("(b.weight_before - last_weight_after) / last_weight_after >= 0.05")
  2331. case 4:
  2332. query = query.Having("last_weight_after IS NULL")
  2333. }
  2334. // Apply filters for afterType
  2335. switch afterType {
  2336. case 1:
  2337. query = query.Where("CASE WHEN a.weight_after IS NULL or a.weight_after = 0 THEN '其他' WHEN latest_dry_weight.dry_weight IS NOT NULL AND ABS(a.weight_after - latest_dry_weight.dry_weight) <= 0.3 THEN '达标' ELSE '不达标' END = ?", "达标")
  2338. case 2:
  2339. query = query.Where("CASE WHEN a.weight_after IS NULL or a.weight_after = 0 THEN '其他' WHEN latest_dry_weight.dry_weight IS NOT NULL AND ABS(a.weight_after - latest_dry_weight.dry_weight) <= 0.3 THEN '达标' ELSE '不达标' END = ?", "不达标")
  2340. case 3:
  2341. query = query.Where("CASE WHEN a.weight_after IS NULL or a.weight_after = 0 THEN '其他' WHEN latest_dry_weight.dry_weight IS NOT NULL AND ABS(a.weight_after - latest_dry_weight.dry_weight) <= 0.3 THEN '达标' ELSE '不达标' END = ?", "其他")
  2342. }
  2343. var data2 []DialysisDataTwo
  2344. //// Count total records
  2345. if err := query.Find(&data2).Error; err != nil {
  2346. return nil, 0, fmt.Errorf("count query failed: %v", err)
  2347. }
  2348. total = int64(len(data2))
  2349. // Fetch data with pagination
  2350. var data []DialysisDataTwo
  2351. if err := query.Order("p.id, b.assessment_date").Offset(int(offset)).Limit(int(limit)).Find(&data).Error; err != nil {
  2352. return nil, 0, fmt.Errorf("data query failed: %v", err)
  2353. }
  2354. return data, total, nil
  2355. }
  2356. func GetNewDialysisWeightDetailTableTenTen(
  2357. user_org_id int64,
  2358. start_time int64,
  2359. end_time int64) ([]DialysisDataTwo, int64, error) {
  2360. // Build base query
  2361. query := readDb.Table("xt_assessment_before_dislysis b").
  2362. Select(`
  2363. p.dialysis_no AS dialysis_no,
  2364. p.name AS patient_name,
  2365. p.gender AS gender,
  2366. FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
  2367. b.assessment_date AS assessment_date,
  2368. latest_dry_weight.dry_weight AS dry_weight,
  2369. b.weight_before AS weight_before,
  2370. a.weight_after AS weight_after,
  2371. (
  2372. SELECT a2.weight_after
  2373. FROM xt_assessment_after_dislysis a2
  2374. WHERE a2.patient_id = b.patient_id
  2375. AND a2.assessment_date < b.assessment_date
  2376. ORDER BY a2.assessment_date DESC LIMIT 1
  2377. ) AS last_weight_after
  2378. `).
  2379. Joins("JOIN xt_patients p ON p.id = b.patient_id").
  2380. Joins("LEFT JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date AND a.status = 1").
  2381. Joins("JOIN (SELECT b2.patient_id, MAX(b2.assessment_date) AS max_assessment_date, MAX(b2.dry_weight) AS dry_weight FROM xt_assessment_before_dislysis b2 WHERE b2.user_org_id = ? AND b2.assessment_date BETWEEN ? AND ? GROUP BY b2.patient_id) latest_dry_weight ON latest_dry_weight.patient_id = b.patient_id", user_org_id, start_time, end_time).
  2382. Where("b.user_org_id = ?", user_org_id).
  2383. Where("b.assessment_date BETWEEN ? AND ?", start_time, end_time).
  2384. Where("b.assessment_date = latest_dry_weight.max_assessment_date").
  2385. Where("b.status = ?", 1)
  2386. // Calculate last_weight_after as a subquery in the HAVING clause
  2387. //query = query.Having("last_weight_after IS NOT NULL")
  2388. //var data2 []DialysisDataTwo
  2389. //Count total records
  2390. var total int64
  2391. if err := query.Count(&total).Error; err != nil {
  2392. return nil, 0, fmt.Errorf("count query failed: %v", err)
  2393. }
  2394. //total = int64(len(data2))
  2395. // Fetch data with pagination
  2396. var data []DialysisDataTwo
  2397. if err := query.Order("p.id, b.assessment_date").Find(&data).Error; err != nil {
  2398. return nil, 0, fmt.Errorf("data query failed: %v", err)
  2399. }
  2400. return data, total, nil
  2401. }
  2402. func GetNewDialysisWeightDetailTableTenThree(
  2403. user_org_id int64,
  2404. start_time int64,
  2405. end_time int64,
  2406. ) ([]DialysisDataTwo, int64, error) {
  2407. // Subquery to get the latest assessment date for each patient
  2408. subQuery := `
  2409. SELECT MAX(ctime) AS max_ctime, patient_id
  2410. FROM xt_patient_dryweight
  2411. WHERE user_org_id = ? AND ctime BETWEEN ? AND ? AND status = 1
  2412. GROUP BY patient_id
  2413. `
  2414. // Build main query to get the latest records for each patient
  2415. query := readDb.Table("xt_patient_dryweight b").
  2416. Select(`
  2417. p.dialysis_no AS dialysis_no,
  2418. p.name AS patient_name,
  2419. FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
  2420. b.dry_weight AS dry_weight
  2421. `).
  2422. Joins("JOIN xt_patients p ON p.id = b.patient_id").
  2423. Joins("JOIN ("+subQuery+") AS latest ON b.patient_id = latest.patient_id AND b.ctime = latest.max_ctime", user_org_id, start_time, end_time).
  2424. Where("b.user_org_id = ?", user_org_id).
  2425. Where("b.ctime BETWEEN ? AND ?", start_time, end_time)
  2426. // Fetch data with pagination
  2427. var data []DialysisDataTwo
  2428. if err := query.Order("p.id").Find(&data).Error; err != nil {
  2429. return nil, 0, fmt.Errorf("data query failed: %v", err)
  2430. }
  2431. return data, int64(len(data)), nil
  2432. }
  2433. func GetNewDialysisWeightDetailTableTenSix(
  2434. user_org_id int64,
  2435. start_time int64,
  2436. end_time int64, dryType int64, page, limit int64) ([]DialysisDataTwo, int64, error) {
  2437. offset := (page - 1) * limit
  2438. var total int64
  2439. // Subquery to get the latest assessment date for each patient
  2440. subQuery := `
  2441. SELECT MAX(ctime) AS max_ctime, patient_id
  2442. FROM xt_patient_dryweight
  2443. WHERE user_org_id = ? AND ctime BETWEEN ? AND ? AND status = 1
  2444. GROUP BY patient_id
  2445. `
  2446. // Build main query to get the latest records for each patient
  2447. query := readDb.Table("xt_patient_dryweight b").
  2448. Select(`
  2449. p.dialysis_no AS dialysis_no,
  2450. p.name AS patient_name,
  2451. FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
  2452. b.dry_weight AS dry_weight
  2453. `).
  2454. Joins("JOIN xt_patients p ON p.id = b.patient_id").
  2455. Joins("JOIN ("+subQuery+") AS latest ON b.patient_id = latest.patient_id AND b.ctime = latest.max_ctime", user_org_id, start_time, end_time).
  2456. Where("b.user_org_id = ?", user_org_id).
  2457. Where("b.ctime BETWEEN ? AND ?", start_time, end_time)
  2458. switch dryType {
  2459. case 5:
  2460. query = query.Where("b.dry_weight < ?", 40)
  2461. case 1:
  2462. query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 40, 50)
  2463. case 2:
  2464. query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 50, 60)
  2465. case 3:
  2466. query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 60, 70)
  2467. case 4:
  2468. query = query.Where("b.dry_weight >= ?", 70)
  2469. case 6:
  2470. query = query.Where("b.dry_weight IS NULL OR b.dry_weight = ''")
  2471. }
  2472. // Fetch data with pagination
  2473. var data []DialysisDataTwo
  2474. if err := query.Order("p.id").Offset(int(offset)).Limit(int(limit)).Find(&data).Error; err != nil {
  2475. return nil, 0, fmt.Errorf("data query failed: %v", err)
  2476. }
  2477. var data2 []DialysisDataTwo
  2478. if err := query.Find(&data2).Error; err != nil {
  2479. return nil, 0, fmt.Errorf("count query failed: %v", err)
  2480. }
  2481. total = int64(len(data2))
  2482. // Fetch data with pagination
  2483. return data, total, nil
  2484. }
  2485. //func GetNewDialysisWeightDetailTableTenThree(
  2486. // user_org_id int64,
  2487. // start_time int64,
  2488. // end_time int64,
  2489. //) ([]DialysisDataTwo, int64, error) {
  2490. // var total int64
  2491. // // Build base query
  2492. // query := readDb.Table("xt_patient_dryweight b").
  2493. // Select(`
  2494. // p.dialysis_no AS dialysis_no,
  2495. // p.name AS patient_name,
  2496. // FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
  2497. // b.dry_weight AS dry_weight,
  2498. // `).
  2499. // Joins("JOIN xt_patients p ON p.id = b.patient_id").
  2500. // Where("b.user_org_id = ?", user_org_id).
  2501. // Where("b.ctime BETWEEN ? AND ?", start_time, end_time).
  2502. // Where("b.status = ?", 1)
  2503. //
  2504. // // Count total records
  2505. // if err := query.Count(&total).Error; err != nil {
  2506. // return nil, 0, fmt.Errorf("count query failed: %v", err)
  2507. // }
  2508. //
  2509. // // Fetch data with pagination
  2510. // var data []DialysisDataTwo
  2511. // if err := query.Order("p.id, b.assessment_date").Find(&data).Error; err != nil {
  2512. // return nil, 0, fmt.Errorf("data query failed: %v", err)
  2513. // }
  2514. //
  2515. // return data, total, nil
  2516. //}
  2517. func GetNewDialysisWeightDetailTableTenOne(
  2518. user_org_id int64,
  2519. start_time int64,
  2520. end_time int64) ([]DialysisDataTwo, error) {
  2521. // 构建基本查询
  2522. query := readDb.Table("xt_assessment_before_dislysis b").
  2523. Select(`
  2524. CASE
  2525. WHEN (
  2526. SELECT a2.weight_after
  2527. FROM xt_assessment_after_dislysis a2
  2528. JOIN xt_assessment_before_dislysis b2
  2529. ON a2.patient_id = b2.patient_id
  2530. AND a2.assessment_date = b2.assessment_date
  2531. WHERE b2.patient_id = b.patient_id
  2532. AND b2.assessment_date < b.assessment_date
  2533. ORDER BY b2.assessment_date DESC LIMIT 1
  2534. ) IS NOT NULL
  2535. THEN (b.weight_before - (
  2536. SELECT a2.weight_after
  2537. FROM xt_assessment_after_dislysis a2
  2538. JOIN xt_assessment_before_dislysis b2
  2539. ON a2.patient_id = b2.patient_id
  2540. AND a2.assessment_date = b2.assessment_date
  2541. WHERE b2.patient_id = b.patient_id
  2542. AND b2.assessment_date < b.assessment_date
  2543. ORDER BY b2.assessment_date DESC LIMIT 1
  2544. )) / (
  2545. SELECT a2.weight_after
  2546. FROM xt_assessment_after_dislysis a2
  2547. JOIN xt_assessment_before_dislysis b2
  2548. ON a2.patient_id = b2.patient_id
  2549. AND a2.assessment_date = b2.assessment_date
  2550. WHERE b2.patient_id = b.patient_id
  2551. AND b2.assessment_date < b.assessment_date
  2552. ORDER BY b2.assessment_date DESC LIMIT 1
  2553. )
  2554. ELSE NULL
  2555. END AS weight_add
  2556. `).
  2557. Joins("left JOIN xt_patients p ON p.id = b.patient_id").
  2558. Joins("left JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date AND a.status = 1").
  2559. Where("b.user_org_id = ?", user_org_id).
  2560. Where("b.assessment_date >= ?", start_time).
  2561. Where("b.assessment_date <= ?", end_time).
  2562. Where("b.status = ?", 1)
  2563. var data []DialysisDataTwo
  2564. query.Order("p.id, b.assessment_date").Find(&data)
  2565. return data, nil
  2566. }
  2567. func GetNewDialysisWeightDetailTableTenTwo(
  2568. user_org_id int64,
  2569. start_time int64,
  2570. end_time int64) ([]DialysisDataTwo, error) {
  2571. // Construct the base query
  2572. query := readDb.Table("xt_assessment_before_dislysis b").
  2573. Select(`
  2574. (b.weight_before - b.dry_weight) / b.dry_weight * 100 AS weight_add
  2575. `).
  2576. Joins("left JOIN xt_patients p ON p.id = b.patient_id").
  2577. Joins("left JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date AND a.status = 1").
  2578. Where("b.user_org_id = ?", user_org_id).
  2579. Where("b.assessment_date >= ?", start_time).
  2580. Where("b.assessment_date <= ?", end_time).
  2581. Where("b.status = ?", 1)
  2582. var data []DialysisDataTwo
  2583. query.Order("p.id, b.assessment_date").Find(&data)
  2584. return data, nil
  2585. }
  2586. // addType 条件处理
  2587. func addTypeCondition(query *gorm.DB, addType int64) {
  2588. }
  2589. // dryType 条件处理
  2590. func dryTypeCondition(query *gorm.DB, dryType int64) {
  2591. }
  2592. // afterType 条件处理
  2593. func afterTypeCondition(query *gorm.DB, afterType int64) {
  2594. switch afterType {
  2595. case 1:
  2596. query = query.Where("weight_status = '达标'")
  2597. case 2:
  2598. query = query.Where("weight_status = '不达标'")
  2599. case 3:
  2600. query = query.Where("weight_status = '其他'")
  2601. }
  2602. }
  2603. type DialysisDataThree struct {
  2604. DialysisNo string `json:"dialysis_no"`
  2605. PatientName string `json:"patient_name"`
  2606. Gender string `json:"gender"`
  2607. Age int `json:"age"`
  2608. AssessmentDate string `json:"assessment_date"`
  2609. DryWeight float64 `json:"dry_weight"`
  2610. PreDialysisBP string `json:"pre_dialysis_bp"`
  2611. PostDialysisBP string `json:"post_dialysis_bp"`
  2612. MonitoringBP string `json:"monitoring_bp"`
  2613. UltrafiltrationRate float64 `json:"ultrafiltration_rate"`
  2614. UltrafiltrationVol float64 `json:"ultrafiltration_vol"`
  2615. BPStatus string `json:"bp_status"`
  2616. ActualUltrafiltration float64 `json:"actual_ultrafiltration"`
  2617. }
  2618. // func GetNewDialysisBPDetailTableTen(user_org_id int64, start_time int64, end_time int64, addType, page, limit int64) ([]DialysisDataThree, int64, error) {
  2619. // offset := (page - 1) * limit
  2620. // var total int64
  2621. // var results []DialysisDataThree
  2622. // // SQL 查询
  2623. // query := `
  2624. // SELECT
  2625. // p.dialysis_no AS 透析号,
  2626. // p.name AS 患者姓名,
  2627. // p.gender AS 性别,
  2628. // FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS 年龄,
  2629. // b.assessment_date AS 透析日期,
  2630. // b.dry_weight AS 干体重,
  2631. // CONCAT(b.systolic_blood_pressure, '/', b.diastolic_blood_pressure) AS 透前血压,
  2632. // CONCAT(a.systolic_blood_pressure, '/', a.diastolic_blood_pressure) AS 透后血压,
  2633. // GROUP_CONCAT(CONCAT(m.systolic_blood_pressure, '/', m.diastolic_blood_pressure) ORDER BY m.monitoring_date ASC SEPARATOR ',') AS 监测记录血压,
  2634. // (SELECT mr.ultrafiltration_rate
  2635. // FROM xt_monitoring_record mr
  2636. // WHERE mr.patient_id = p.id AND mr.monitoring_date = b.assessment_date
  2637. // ORDER BY mr.operate_time DESC
  2638. // LIMIT 1) AS 超滤率,
  2639. // (SELECT mr.ultrafiltration_volume
  2640. // FROM xt_monitoring_record mr
  2641. // WHERE mr.patient_id = p.id AND mr.monitoring_date = b.assessment_date
  2642. // ORDER BY mr.operate_time DESC
  2643. // LIMIT 1) AS 超滤总量,
  2644. // CASE
  2645. // WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
  2646. // CASE
  2647. // WHEN b.systolic_blood_pressure < 140 OR b.diastolic_blood_pressure < 90 THEN '达标'
  2648. // ELSE '不达标'
  2649. // END
  2650. // WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
  2651. // CASE
  2652. // WHEN b.systolic_blood_pressure < 160 OR b.diastolic_blood_pressure < 90 THEN '达标'
  2653. // ELSE '不达标'
  2654. // END
  2655. // END AS 血压达标状态
  2656. // FROM
  2657. // xt_patients p
  2658. // JOIN
  2659. // xt_assessment_before_dislysis b ON p.id = b.patient_id
  2660. // JOIN
  2661. // xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date
  2662. // JOIN
  2663. // xt_monitoring_record m ON p.id = m.patient_id AND m.monitoring_date = b.assessment_date
  2664. // WHERE
  2665. // b.user_org_id = ?
  2666. // AND b.assessment_date >= ?
  2667. // AND b.assessment_date <= ?
  2668. // AND (
  2669. // (? = 1 AND CASE
  2670. // WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
  2671. // CASE
  2672. // WHEN b.systolic_blood_pressure < 140 OR b.diastolic_blood_pressure < 90 THEN 1
  2673. // ELSE 0
  2674. // END
  2675. // WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
  2676. // CASE
  2677. // WHEN b.systolic_blood_pressure < 160 OR b.diastolic_blood_pressure < 90 THEN 1
  2678. // ELSE 0
  2679. // END
  2680. // END = 1)
  2681. // OR
  2682. // (? = 2 AND CASE
  2683. // WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
  2684. // CASE
  2685. // WHEN b.systolic_blood_pressure >= 140 AND b.diastolic_blood_pressure >= 90 THEN 1
  2686. // ELSE 0
  2687. // END
  2688. // WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
  2689. // CASE
  2690. // WHEN b.systolic_blood_pressure >= 160 AND b.diastolic_blood_pressure >= 90 THEN 1
  2691. // ELSE 0
  2692. // END
  2693. // END = 1)
  2694. // )
  2695. // GROUP BY
  2696. // p.id, b.assessment_date
  2697. // ORDER BY
  2698. // p.id, b.assessment_date
  2699. // LIMIT ? OFFSET ?;
  2700. // `
  2701. //
  2702. // // 计算总数
  2703. // countQuery := `
  2704. // SELECT COUNT(*)
  2705. // FROM
  2706. // xt_patients p
  2707. // JOIN
  2708. // xt_assessment_before_dislysis b ON p.id = b.patient_id
  2709. // WHERE
  2710. // b.user_org_id = ?
  2711. // AND b.assessment_date >= ?
  2712. // AND b.assessment_date <= ?
  2713. // `
  2714. //
  2715. // // 执行统计查询
  2716. // if err := readDb.Raw(countQuery, user_org_id, start_time, end_time).Scan(&total).Error; err != nil {
  2717. // return nil, 0, fmt.Errorf("error calculating total count: %v", err)
  2718. // }
  2719. //
  2720. // // 执行数据查询
  2721. // if err := readDb.Raw(query, user_org_id, start_time, end_time, addType, addType, limit, offset).Scan(&results).Error; err != nil {
  2722. // return nil, 0, fmt.Errorf("error executing query: %v", err)
  2723. // }
  2724. //
  2725. // return results, total, nil
  2726. // }
  2727. func GetNewDialysisBPDetailTableTenOne(
  2728. user_org_id int64,
  2729. start_time int64,
  2730. end_time int64,
  2731. addType, page, limit int64,
  2732. ) ([]DialysisDataThree, int64, error) {
  2733. offset := (page - 1) * limit
  2734. var results []DialysisDataThree
  2735. var total int64
  2736. // 构建基本查询
  2737. query := readDb.Table("xt_assessment_after_dislysis b").
  2738. Select(`
  2739. p.dialysis_no AS dialysis_no,
  2740. b.actual_ultrafiltration as actual_ultrafiltration,
  2741. p.name AS patient_name,
  2742. p.gender AS gender,
  2743. FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
  2744. b.assessment_date AS assessment_date,
  2745. b.dry_weight AS dry_weight,
  2746. CONCAT(a.systolic_blood_pressure, '/', a.diastolic_blood_pressure) AS pre_dialysis_bp,
  2747. CONCAT(b.systolic_blood_pressure, '/', b.diastolic_blood_pressure) AS post_dialysis_bp,
  2748. GROUP_CONCAT(CONCAT(m.systolic_blood_pressure, '/', m.diastolic_blood_pressure) ORDER BY m.monitoring_date ASC SEPARATOR ',') AS monitoring_bp,
  2749. (
  2750. SELECT mr.ultrafiltration_rate
  2751. FROM xt_monitoring_record mr
  2752. WHERE mr.patient_id = p.id
  2753. AND mr.monitoring_date = b.assessment_date
  2754. ORDER BY mr.operate_time DESC LIMIT 1
  2755. ) AS ultrafiltration_rate,
  2756. (
  2757. SELECT mr.ultrafiltration_volume
  2758. FROM xt_monitoring_record mr
  2759. WHERE mr.patient_id = p.id
  2760. AND mr.monitoring_date = b.assessment_date
  2761. ORDER BY mr.operate_time DESC LIMIT 1
  2762. ) AS ultrafiltration_vol,
  2763. CASE
  2764. WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
  2765. CASE
  2766. WHEN b.systolic_blood_pressure <> 0 and b.diastolic_blood_pressure <> 0 and b.systolic_blood_pressure < 140 and b.diastolic_blood_pressure < 90 THEN '达标'
  2767. ELSE '不达标'
  2768. END
  2769. WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
  2770. CASE
  2771. WHEN b.systolic_blood_pressure <> 0 and b.diastolic_blood_pressure <> 0 and b.systolic_blood_pressure < 160 AND b.diastolic_blood_pressure < 90 THEN '达标'
  2772. ELSE '不达标'
  2773. END
  2774. END AS bp_status
  2775. `).
  2776. Joins("JOIN xt_patients p ON p.id = b.patient_id").
  2777. Joins("JOIN xt_assessment_before_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date").
  2778. Joins("JOIN xt_monitoring_record m ON p.id = m.patient_id AND m.monitoring_date = b.assessment_date").
  2779. Where("b.user_org_id = ?", user_org_id).
  2780. Where("b.assessment_date >= ?", start_time).
  2781. Where("b.assessment_date <= ?", end_time).
  2782. Group("p.id, b.assessment_date")
  2783. // 根据传入的 addType 值动态构建条件
  2784. switch addType {
  2785. case 1:
  2786. query = query.Where(`
  2787. CASE
  2788. WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
  2789. b.systolic_blood_pressure <> 0 and b.diastolic_blood_pressure <> 0 and b.systolic_blood_pressure < 140 AND b.diastolic_blood_pressure < 90
  2790. WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
  2791. b.systolic_blood_pressure <> 0 and b.diastolic_blood_pressure <> 0 and b.systolic_blood_pressure < 160 AND b.diastolic_blood_pressure < 90
  2792. END`)
  2793. case 2:
  2794. query = query.Where(`
  2795. CASE
  2796. WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
  2797. b.systolic_blood_pressure = 0 or b.diastolic_blood_pressure = 0 or b.systolic_blood_pressure >= 140 OR b.diastolic_blood_pressure >= 90
  2798. WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
  2799. b.systolic_blood_pressure = 0 or b.diastolic_blood_pressure = 0 or b.systolic_blood_pressure >= 160 OR b.diastolic_blood_pressure >= 90
  2800. END`)
  2801. }
  2802. // 获取总记录数
  2803. if err := query.Count(&total).Error; err != nil {
  2804. return nil, 0, fmt.Errorf("count query failed: %v", err)
  2805. }
  2806. // 获取分页数据
  2807. if err := query.Order("p.id, b.assessment_date").
  2808. Offset(int(offset)).
  2809. Limit(int(limit)).
  2810. Scan(&results).Error; err != nil {
  2811. return nil, 0, fmt.Errorf("query execution failed: %v", err)
  2812. }
  2813. return results, total, nil
  2814. }
  2815. func GetNewDialysisBPDetailTableTen(
  2816. user_org_id int64,
  2817. start_time int64,
  2818. end_time int64,
  2819. addType, page, limit int64,
  2820. ) ([]DialysisDataThree, int64, error) {
  2821. offset := (page - 1) * limit
  2822. var results []DialysisDataThree
  2823. var total int64
  2824. // 构建基本查询
  2825. query := readDb.Table("xt_assessment_before_dislysis b").
  2826. Select(`
  2827. p.dialysis_no AS dialysis_no,
  2828. a.actual_ultrafiltration as actual_ultrafiltration,
  2829. p.name AS patient_name,
  2830. p.gender AS gender,
  2831. FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
  2832. b.assessment_date AS assessment_date,
  2833. b.dry_weight AS dry_weight,
  2834. CONCAT(b.systolic_blood_pressure, '/', b.diastolic_blood_pressure) AS pre_dialysis_bp,
  2835. CONCAT(a.systolic_blood_pressure, '/', a.diastolic_blood_pressure) AS post_dialysis_bp,
  2836. GROUP_CONCAT(CONCAT(m.systolic_blood_pressure, '/', m.diastolic_blood_pressure) ORDER BY m.monitoring_date ASC SEPARATOR ',') AS monitoring_bp,
  2837. (
  2838. SELECT mr.ultrafiltration_rate
  2839. FROM xt_monitoring_record mr
  2840. WHERE mr.patient_id = p.id
  2841. AND mr.monitoring_date = b.assessment_date
  2842. ORDER BY mr.operate_time DESC LIMIT 1
  2843. ) AS ultrafiltration_rate,
  2844. (
  2845. SELECT mr.ultrafiltration_volume
  2846. FROM xt_monitoring_record mr
  2847. WHERE mr.patient_id = p.id
  2848. AND mr.monitoring_date = b.assessment_date
  2849. ORDER BY mr.operate_time DESC LIMIT 1
  2850. ) AS ultrafiltration_vol,
  2851. CASE
  2852. WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
  2853. CASE
  2854. WHEN b.systolic_blood_pressure <> 0 and b.diastolic_blood_pressure <> 0 and b.systolic_blood_pressure < 140 and b.diastolic_blood_pressure < 90 THEN '达标'
  2855. ELSE '不达标'
  2856. END
  2857. WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
  2858. CASE
  2859. WHEN b.systolic_blood_pressure <> 0 and b.diastolic_blood_pressure <> 0 and b.systolic_blood_pressure < 160 AND b.diastolic_blood_pressure < 90 THEN '达标'
  2860. ELSE '不达标'
  2861. END
  2862. END AS bp_status
  2863. `).
  2864. Joins("JOIN xt_patients p ON p.id = b.patient_id").
  2865. Joins("JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date").
  2866. Joins("JOIN xt_monitoring_record m ON p.id = m.patient_id AND m.monitoring_date = b.assessment_date").
  2867. Where("b.user_org_id = ?", user_org_id).
  2868. Where("b.assessment_date >= ?", start_time).
  2869. Where("b.assessment_date <= ?", end_time).
  2870. Group("p.id, b.assessment_date")
  2871. // 根据传入的 addType 值动态构建条件
  2872. switch addType {
  2873. case 1:
  2874. query = query.Where(`
  2875. CASE
  2876. WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
  2877. b.systolic_blood_pressure <> 0 and b.diastolic_blood_pressure <> 0 and b.systolic_blood_pressure < 140 AND b.diastolic_blood_pressure < 90
  2878. WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
  2879. b.systolic_blood_pressure <> 0 and b.diastolic_blood_pressure <> 0 and b.systolic_blood_pressure < 160 AND b.diastolic_blood_pressure < 90
  2880. END`)
  2881. case 2:
  2882. query = query.Where(`
  2883. CASE
  2884. WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
  2885. b.systolic_blood_pressure = 0 or b.diastolic_blood_pressure = 0 or b.systolic_blood_pressure >= 140 OR b.diastolic_blood_pressure >= 90
  2886. WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
  2887. b.systolic_blood_pressure = 0 or b.diastolic_blood_pressure = 0 or b.systolic_blood_pressure >= 160 OR b.diastolic_blood_pressure >= 90
  2888. END`)
  2889. }
  2890. // 获取总记录数
  2891. if err := query.Count(&total).Error; err != nil {
  2892. return nil, 0, fmt.Errorf("count query failed: %v", err)
  2893. }
  2894. // 获取分页数据
  2895. if err := query.Order("p.id, b.assessment_date").
  2896. Offset(int(offset)).
  2897. Limit(int(limit)).
  2898. Scan(&results).Error; err != nil {
  2899. return nil, 0, fmt.Errorf("query execution failed: %v", err)
  2900. }
  2901. return results, total, nil
  2902. }
  2903. func GetNewDialysisBPDetailTableTenThree(
  2904. user_org_id int64,
  2905. start_time int64,
  2906. end_time int64,
  2907. ) (item []otherItemAmount, err error) {
  2908. var results []DialysisDataThree
  2909. var total int64
  2910. // 构建基本查询
  2911. query := readDb.Table("xt_assessment_before_dislysis b").
  2912. Select(`
  2913. CASE
  2914. WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
  2915. CASE
  2916. WHEN b.systolic_blood_pressure <> 0 and b.diastolic_blood_pressure <> 0 and b.systolic_blood_pressure < 140 AND b.diastolic_blood_pressure < 90 THEN '达标'
  2917. ELSE '不达标'
  2918. END
  2919. WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
  2920. CASE
  2921. WHEN b.systolic_blood_pressure <> 0 and b.diastolic_blood_pressure <> 0 and b.systolic_blood_pressure < 160 AND b.diastolic_blood_pressure < 90 THEN '达标'
  2922. ELSE '不达标'
  2923. END
  2924. END AS bp_status
  2925. `).
  2926. Joins("JOIN xt_patients p ON p.id = b.patient_id").
  2927. Joins("JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date").
  2928. Where("b.user_org_id = ?", user_org_id).
  2929. Where("b.assessment_date >= ?", start_time).
  2930. Where("b.assessment_date <= ?", end_time).
  2931. Group("p.id, b.assessment_date")
  2932. // 获取总记录数
  2933. query.Count(&total)
  2934. // 获取分页数据
  2935. query.Order("p.id, b.assessment_date").Scan(&results)
  2936. var metStandard, unmetStandard int
  2937. for _, result := range results {
  2938. if result.BPStatus == "达标" {
  2939. metStandard++
  2940. } else {
  2941. unmetStandard++
  2942. }
  2943. }
  2944. if total > 0 {
  2945. metPercentage := float64(metStandard) / float64(total) * 100
  2946. unmetPercentage := float64(unmetStandard) / float64(total) * 100
  2947. fmt.Printf("Total patients: %d\n", total)
  2948. fmt.Printf("Met standard: %d (%.2f%%)\n", metStandard, metPercentage)
  2949. fmt.Printf("Unmet standard: %d (%.2f%%)\n", unmetStandard, unmetPercentage)
  2950. var items otherItemAmount
  2951. items.Name = "达标患者"
  2952. items.Ratio = strconv.FormatFloat(metPercentage, 'f', 1, 32)
  2953. items.Total = int64(metStandard)
  2954. item = append(item, items)
  2955. var items2 otherItemAmount
  2956. items2.Name = "未达标患者"
  2957. items2.Ratio = strconv.FormatFloat(unmetPercentage, 'f', 1, 32)
  2958. items2.Total = int64(unmetStandard)
  2959. item = append(item, items2)
  2960. }
  2961. return
  2962. }
  2963. func GetNewDialysisBPDetailTableTenFour(
  2964. user_org_id int64,
  2965. start_time int64,
  2966. end_time int64,
  2967. ) (item []otherItemAmount, err error) {
  2968. var results []DialysisDataThree
  2969. var total int64
  2970. // 构建基本查询
  2971. query := readDb.Table("xt_assessment_after_dislysis b").
  2972. Select(`
  2973. CASE
  2974. WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
  2975. CASE
  2976. WHEN b.systolic_blood_pressure <> 0 and b.diastolic_blood_pressure <> 0 and b.systolic_blood_pressure < 140 AND b.diastolic_blood_pressure < 90 THEN '达标'
  2977. ELSE '不达标'
  2978. END
  2979. WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
  2980. CASE
  2981. WHEN b.systolic_blood_pressure <> 0 and b.diastolic_blood_pressure <> 0 and b.systolic_blood_pressure < 160 AND b.diastolic_blood_pressure < 90 THEN '达标'
  2982. ELSE '不达标'
  2983. END
  2984. END AS bp_status
  2985. `).
  2986. Joins("JOIN xt_patients p ON p.id = b.patient_id").
  2987. Joins("JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date").
  2988. Where("b.user_org_id = ?", user_org_id).
  2989. Where("b.assessment_date >= ?", start_time).
  2990. Where("b.assessment_date <= ?", end_time).
  2991. Group("p.id, b.assessment_date")
  2992. // 获取总记录数
  2993. query.Count(&total)
  2994. // 获取分页数据
  2995. query.Order("p.id, b.assessment_date").Scan(&results)
  2996. var metStandard, unmetStandard int
  2997. for _, result := range results {
  2998. if result.BPStatus == "达标" {
  2999. metStandard++
  3000. } else {
  3001. unmetStandard++
  3002. }
  3003. }
  3004. if total > 0 {
  3005. metPercentage := float64(metStandard) / float64(total) * 100
  3006. unmetPercentage := float64(unmetStandard) / float64(total) * 100
  3007. fmt.Printf("Total patients: %d\n", total)
  3008. fmt.Printf("Met standard: %d (%.2f%%)\n", metStandard, metPercentage)
  3009. fmt.Printf("Unmet standard: %d (%.2f%%)\n", unmetStandard, unmetPercentage)
  3010. var items otherItemAmount
  3011. items.Name = "达标患者"
  3012. items.Ratio = strconv.FormatFloat(metPercentage, 'f', 1, 32)
  3013. items.Total = int64(metStandard)
  3014. item = append(item, items)
  3015. var items2 otherItemAmount
  3016. items2.Name = "未达标患者"
  3017. items2.Ratio = strconv.FormatFloat(unmetPercentage, 'f', 1, 32)
  3018. items2.Total = int64(unmetStandard)
  3019. item = append(item, items2)
  3020. }
  3021. return
  3022. }
  3023. func GetNewDialysisWeightDetailTableTenT(
  3024. user_org_id int64,
  3025. start_time int64,
  3026. end_time int64,
  3027. addType, dryType, afterType, page, limit int64, keyword string, item_type int64) ([]DialysisDataTwo, int64, error) {
  3028. offset := (page - 1) * limit
  3029. //var dialysisData []DialysisDataTwo
  3030. var total int64
  3031. keyword = "%" + keyword + "%"
  3032. var query *gorm.DB
  3033. if item_type == 0 || item_type == 1 { //体重增加,以透后体重为分母
  3034. // 构建基本查询
  3035. query = readDb.Table("xt_assessment_before_dislysis b").
  3036. Select(`
  3037. p.dialysis_no AS dialysis_no,
  3038. p.name AS patient_name,
  3039. p.gender AS gender,
  3040. FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
  3041. b.assessment_date AS assessment_date,
  3042. b.dry_weight AS dry_weight,
  3043. b.weight_before AS weight_before,
  3044. a.weight_after AS weight_after,
  3045. (
  3046. SELECT a2.weight_after
  3047. FROM xt_assessment_after_dislysis a2
  3048. JOIN xt_assessment_before_dislysis b2
  3049. ON a2.patient_id = b2.patient_id
  3050. AND a2.assessment_date = b2.assessment_date
  3051. WHERE b2.patient_id = b.patient_id
  3052. AND b2.assessment_date < b.assessment_date
  3053. ORDER BY b2.assessment_date DESC LIMIT 1
  3054. ) AS last_weight_after,
  3055. CASE
  3056. WHEN
  3057. a.weight_after and b.dry_weight
  3058. IS NOT NULL
  3059. THEN a.weight_after - b.dry_weight
  3060. ELSE NULL
  3061. END AS weight_add,
  3062. CASE
  3063. WHEN a.weight_after IS NULL or a.weight_after = 0 THEN '其他'
  3064. WHEN b.dry_weight IS NOT NULL THEN
  3065. CASE
  3066. WHEN ABS(a.weight_after - b.dry_weight) <= 0.3 THEN '达标'
  3067. ELSE '不达标'
  3068. END
  3069. ELSE '其他'
  3070. END AS weight_status
  3071. `).
  3072. Joins("JOIN xt_patients p ON p.id = b.patient_id and p.name like ?", keyword).
  3073. Joins("left JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date AND a.status = 1").
  3074. Where("b.user_org_id = ?", user_org_id).
  3075. Where("b.assessment_date >= ?", start_time).
  3076. Where("b.assessment_date <= ?", end_time).
  3077. Where("b.status = ?", 1)
  3078. } else {
  3079. query = readDb.Table("xt_assessment_before_dislysis b").
  3080. Select(`
  3081. p.dialysis_no AS dialysis_no,
  3082. p.name AS patient_name,
  3083. p.gender AS gender,
  3084. FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
  3085. b.assessment_date AS assessment_date,
  3086. b.dry_weight AS dry_weight,
  3087. b.weight_before AS weight_before,
  3088. a.weight_after AS weight_after,
  3089. (
  3090. SELECT a2.weight_after
  3091. FROM xt_assessment_after_dislysis a2
  3092. JOIN xt_assessment_before_dislysis b2
  3093. ON a2.patient_id = b2.patient_id
  3094. AND a2.assessment_date = b2.assessment_date
  3095. WHERE b2.patient_id = b.patient_id
  3096. AND b2.assessment_date < b.assessment_date
  3097. ORDER BY b2.assessment_date DESC LIMIT 1
  3098. ) AS last_weight_after,
  3099. CASE
  3100. WHEN
  3101. a.weight_after and b.dry_weight
  3102. IS NOT NULL
  3103. THEN a.weight_after - b.dry_weight
  3104. ELSE NULL
  3105. END AS weight_add,
  3106. CASE
  3107. WHEN a.weight_after IS NULL or a.weight_after = 0 THEN '其他'
  3108. WHEN b.dry_weight IS NOT NULL THEN
  3109. CASE
  3110. WHEN ABS(a.weight_after - b.dry_weight) <= 0.3 THEN '达标'
  3111. ELSE '不达标'
  3112. END
  3113. ELSE '其他'
  3114. END AS weight_status
  3115. `).
  3116. Joins("JOIN xt_patients p ON p.id = b.patient_id and p.name like ?", keyword).
  3117. Joins("left JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date AND a.status = 1").
  3118. Where("b.user_org_id = ?", user_org_id).
  3119. Where("b.assessment_date >= ?", start_time).
  3120. Where("b.assessment_date <= ?", end_time).
  3121. Where("b.status = ?", 1)
  3122. }
  3123. switch dryType {
  3124. case 5:
  3125. query = query.Where("b.dry_weight < ?", 40)
  3126. case 1:
  3127. query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 40, 50)
  3128. case 2:
  3129. query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 50, 60)
  3130. case 3:
  3131. query = query.Where("b.dry_weight >= ? AND b.dry_weight < ?", 60, 70)
  3132. case 4:
  3133. query = query.Where("b.dry_weight >= ?", 70)
  3134. case 6:
  3135. query = query.Where("b.dry_weight IS NULL OR b.dry_weight = ''")
  3136. }
  3137. if item_type == 0 || item_type == 1 {
  3138. switch addType {
  3139. case 1:
  3140. query = query.Where(`
  3141. (
  3142. CASE
  3143. WHEN (
  3144. SELECT a2.weight_after
  3145. FROM xt_assessment_after_dislysis a2
  3146. JOIN xt_assessment_before_dislysis b2
  3147. ON a2.patient_id = b2.patient_id
  3148. AND a2.assessment_date = b2.assessment_date
  3149. WHERE b2.patient_id = b.patient_id
  3150. AND b2.assessment_date < b.assessment_date
  3151. ORDER BY b2.assessment_date DESC LIMIT 1
  3152. ) IS NOT NULL
  3153. THEN (b.weight_before - (
  3154. SELECT a2.weight_after
  3155. FROM xt_assessment_after_dislysis a2
  3156. JOIN xt_assessment_before_dislysis b2
  3157. ON a2.patient_id = b2.patient_id
  3158. AND a2.assessment_date = b2.assessment_date
  3159. WHERE b2.patient_id = b.patient_id
  3160. AND b2.assessment_date < b.assessment_date
  3161. ORDER BY b2.assessment_date DESC LIMIT 1
  3162. )) / (
  3163. SELECT a2.weight_after
  3164. FROM xt_assessment_after_dislysis a2
  3165. JOIN xt_assessment_before_dislysis b2
  3166. ON a2.patient_id = b2.patient_id
  3167. AND a2.assessment_date = b2.assessment_date
  3168. WHERE b2.patient_id = b.patient_id
  3169. AND b2.assessment_date < b.assessment_date
  3170. ORDER BY b2.assessment_date DESC LIMIT 1
  3171. )
  3172. ELSE NULL
  3173. END
  3174. ) <= 0.03
  3175. `)
  3176. case 2:
  3177. query = query.Where(`
  3178. (
  3179. CASE
  3180. WHEN (
  3181. SELECT a2.weight_after
  3182. FROM xt_assessment_after_dislysis a2
  3183. JOIN xt_assessment_before_dislysis b2
  3184. ON a2.patient_id = b2.patient_id
  3185. AND a2.assessment_date = b2.assessment_date
  3186. WHERE b2.patient_id = b.patient_id
  3187. AND b2.assessment_date < b.assessment_date
  3188. ORDER BY b2.assessment_date DESC LIMIT 1
  3189. ) IS NOT NULL
  3190. THEN (b.weight_before - (
  3191. SELECT a2.weight_after
  3192. FROM xt_assessment_after_dislysis a2
  3193. JOIN xt_assessment_before_dislysis b2
  3194. ON a2.patient_id = b2.patient_id
  3195. AND a2.assessment_date = b2.assessment_date
  3196. WHERE b2.patient_id = b.patient_id
  3197. AND b2.assessment_date < b.assessment_date
  3198. ORDER BY b2.assessment_date DESC LIMIT 1
  3199. )) / (
  3200. SELECT a2.weight_after
  3201. FROM xt_assessment_after_dislysis a2
  3202. JOIN xt_assessment_before_dislysis b2
  3203. ON a2.patient_id = b2.patient_id
  3204. AND a2.assessment_date = b2.assessment_date
  3205. WHERE b2.patient_id = b.patient_id
  3206. AND b2.assessment_date < b.assessment_date
  3207. ORDER BY b2.assessment_date DESC LIMIT 1
  3208. )
  3209. ELSE NULL
  3210. END
  3211. ) > 0.03 AND (
  3212. CASE
  3213. WHEN (
  3214. SELECT a2.weight_after
  3215. FROM xt_assessment_after_dislysis a2
  3216. JOIN xt_assessment_before_dislysis b2
  3217. ON a2.patient_id = b2.patient_id
  3218. AND a2.assessment_date = b2.assessment_date
  3219. WHERE b2.patient_id = b.patient_id
  3220. AND b2.assessment_date < b.assessment_date
  3221. ORDER BY b2.assessment_date DESC LIMIT 1
  3222. ) IS NOT NULL
  3223. THEN (b.weight_before - (
  3224. SELECT a2.weight_after
  3225. FROM xt_assessment_after_dislysis a2
  3226. JOIN xt_assessment_before_dislysis b2
  3227. ON a2.patient_id = b2.patient_id
  3228. AND a2.assessment_date = b2.assessment_date
  3229. WHERE b2.patient_id = b.patient_id
  3230. AND b2.assessment_date < b.assessment_date
  3231. ORDER BY b2.assessment_date DESC LIMIT 1
  3232. )) / (
  3233. SELECT a2.weight_after
  3234. FROM xt_assessment_after_dislysis a2
  3235. JOIN xt_assessment_before_dislysis b2
  3236. ON a2.patient_id = b2.patient_id
  3237. AND a2.assessment_date = b2.assessment_date
  3238. WHERE b2.patient_id = b.patient_id
  3239. AND b2.assessment_date < b.assessment_date
  3240. ORDER BY b2.assessment_date DESC LIMIT 1
  3241. )
  3242. ELSE NULL
  3243. END
  3244. ) < 0.05
  3245. `)
  3246. case 3:
  3247. query = query.Where(`
  3248. (
  3249. CASE
  3250. WHEN (
  3251. SELECT a2.weight_after
  3252. FROM xt_assessment_after_dislysis a2
  3253. JOIN xt_assessment_before_dislysis b2
  3254. ON a2.patient_id = b2.patient_id
  3255. AND a2.assessment_date = b2.assessment_date
  3256. WHERE b2.patient_id = b.patient_id
  3257. AND b2.assessment_date < b.assessment_date
  3258. ORDER BY b2.assessment_date DESC LIMIT 1
  3259. ) IS NOT NULL
  3260. THEN (b.weight_before - (
  3261. SELECT a2.weight_after
  3262. FROM xt_assessment_after_dislysis a2
  3263. JOIN xt_assessment_before_dislysis b2
  3264. ON a2.patient_id = b2.patient_id
  3265. AND a2.assessment_date = b2.assessment_date
  3266. WHERE b2.patient_id = b.patient_id
  3267. AND b2.assessment_date < b.assessment_date
  3268. ORDER BY b2.assessment_date DESC LIMIT 1
  3269. )) / (
  3270. SELECT a2.weight_after
  3271. FROM xt_assessment_after_dislysis a2
  3272. JOIN xt_assessment_before_dislysis b2
  3273. ON a2.patient_id = b2.patient_id
  3274. AND a2.assessment_date = b2.assessment_date
  3275. WHERE b2.patient_id = b.patient_id
  3276. AND b2.assessment_date < b.assessment_date
  3277. ORDER BY b2.assessment_date DESC LIMIT 1
  3278. )
  3279. ELSE NULL
  3280. END
  3281. ) >= 0.05
  3282. `)
  3283. case 4:
  3284. query = query.Where(`
  3285. (
  3286. SELECT a2.weight_after
  3287. FROM xt_assessment_after_dislysis a2
  3288. JOIN xt_assessment_before_dislysis b2
  3289. ON a2.patient_id = b2.patient_id
  3290. AND a2.assessment_date = b2.assessment_date
  3291. WHERE b2.patient_id = b.patient_id
  3292. AND b2.assessment_date < b.assessment_date
  3293. ORDER BY b2.assessment_date DESC LIMIT 1
  3294. ) IS NULL
  3295. `)
  3296. }
  3297. } else {
  3298. switch addType {
  3299. case 1:
  3300. query = query.Where(`
  3301. (
  3302. CASE
  3303. WHEN b.weight_before and b.dry_weight IS NOT NULL
  3304. THEN (b.weight_before - b.dry_weight) / (
  3305. b.dry_weight
  3306. )
  3307. ELSE NULL
  3308. END
  3309. ) <= 0.03
  3310. `)
  3311. case 2:
  3312. query = query.Where(`
  3313. (
  3314. CASE
  3315. WHEN b.weight_before and b.dry_weight IS NOT NULL
  3316. THEN (b.weight_before - b.dry_weight) / (
  3317. b.dry_weight
  3318. )
  3319. ELSE NULL
  3320. END
  3321. ) > 0.03 AND (
  3322. CASE
  3323. WHEN b.weight_before and b.dry_weight IS NOT NULL
  3324. THEN (b.weight_before - b.dry_weight) / (
  3325. b.dry_weight
  3326. )
  3327. ELSE NULL
  3328. END
  3329. ) < 0.05
  3330. `)
  3331. case 3:
  3332. query = query.Where(`
  3333. (
  3334. CASE
  3335. WHEN b.weight_before and b.dry_weight IS NOT NULL
  3336. THEN (b.weight_before - b.dry_weight) / (
  3337. b.dry_weight
  3338. )
  3339. ELSE NULL
  3340. END
  3341. ) >= 0.05
  3342. `)
  3343. case 4:
  3344. query = query.Where(`b.weight_before and b.dry_weight IS NULL`)
  3345. }
  3346. }
  3347. switch afterType {
  3348. case 1:
  3349. query = query.Where(`
  3350. CASE
  3351. WHEN a.weight_after IS NULL or a.weight_after = 0 THEN '其他'
  3352. WHEN b.dry_weight IS NOT NULL THEN
  3353. CASE
  3354. WHEN ABS(a.weight_after - b.dry_weight) <= 0.3 THEN '达标'
  3355. ELSE '不达标'
  3356. END
  3357. ELSE '其他'
  3358. END = ?`, "达标")
  3359. case 2:
  3360. query = query.Where(`
  3361. CASE
  3362. WHEN a.weight_after IS NULL or a.weight_after = 0 THEN '其他'
  3363. WHEN b.dry_weight IS NOT NULL THEN
  3364. CASE
  3365. WHEN ABS(a.weight_after - b.dry_weight) <= 0.3 THEN '达标'
  3366. ELSE '不达标'
  3367. END
  3368. ELSE '其他'
  3369. END = ?`, "不达标")
  3370. case 3:
  3371. query = query.Where(`
  3372. CASE
  3373. WHEN a.weight_after IS NULL or a.weight_after = 0 THEN '其他'
  3374. WHEN b.dry_weight IS NOT NULL THEN
  3375. CASE
  3376. WHEN ABS(a.weight_after - b.dry_weight) <= 0.3 THEN '达标'
  3377. ELSE '不达标'
  3378. END
  3379. ELSE '其他'
  3380. END = ?`, "其他")
  3381. }
  3382. //// 获取总记录数
  3383. if err := query.Count(&total).Error; err != nil {
  3384. return nil, 0, fmt.Errorf("count query failed: %v", err)
  3385. }
  3386. var data []DialysisDataTwo
  3387. query.Order("p.id, b.assessment_date").Offset(int(offset)).Limit(int(limit)).Find(&data)
  3388. return data, total, nil
  3389. }
  3390. func GetNewDialysisWeightDetailTableTenTenOne(
  3391. user_org_id int64,
  3392. start_time int64,
  3393. end_time int64, add_type int64) ([]DialysisDataTwo, int64, error) {
  3394. var total int64
  3395. // 构建基本查询
  3396. query := readDb.Table("xt_assessment_before_dislysis b").
  3397. Select(`
  3398. p.dialysis_no AS dialysis_no,
  3399. p.name AS patient_name,
  3400. p.gender AS gender,
  3401. FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
  3402. b.assessment_date AS assessment_date,
  3403. b.dry_weight AS dry_weight,
  3404. b.weight_before AS weight_before,
  3405. a.weight_after AS weight_after,
  3406. (
  3407. SELECT a2.weight_after
  3408. FROM xt_assessment_after_dislysis a2
  3409. JOIN xt_assessment_before_dislysis b2
  3410. ON a2.patient_id = b2.patient_id
  3411. AND a2.assessment_date = b2.assessment_date
  3412. WHERE b2.patient_id = b.patient_id
  3413. AND b2.assessment_date < b.assessment_date
  3414. ORDER BY b2.assessment_date DESC LIMIT 1
  3415. ) AS last_weight_after,
  3416. CASE
  3417. WHEN (
  3418. SELECT a2.weight_after
  3419. FROM xt_assessment_after_dislysis a2
  3420. JOIN xt_assessment_before_dislysis b2
  3421. ON a2.patient_id = b2.patient_id
  3422. AND a2.assessment_date = b2.assessment_date
  3423. WHERE b2.patient_id = b.patient_id
  3424. AND b2.assessment_date < b.assessment_date
  3425. ORDER BY b2.assessment_date DESC LIMIT 1
  3426. ) IS NOT NULL
  3427. THEN (b.weight_before - (
  3428. SELECT a2.weight_after
  3429. FROM xt_assessment_after_dislysis a2
  3430. JOIN xt_assessment_before_dislysis b2
  3431. ON a2.patient_id = b2.patient_id
  3432. AND a2.assessment_date = b2.assessment_date
  3433. WHERE b2.patient_id = b.patient_id
  3434. AND b2.assessment_date < b.assessment_date
  3435. ORDER BY b2.assessment_date DESC LIMIT 1
  3436. )) / (
  3437. SELECT a2.weight_after
  3438. FROM xt_assessment_after_dislysis a2
  3439. JOIN xt_assessment_before_dislysis b2
  3440. ON a2.patient_id = b2.patient_id
  3441. AND a2.assessment_date = b2.assessment_date
  3442. WHERE b2.patient_id = b.patient_id
  3443. AND b2.assessment_date < b.assessment_date
  3444. ORDER BY b2.assessment_date DESC LIMIT 1
  3445. )
  3446. ELSE NULL
  3447. END AS weight_add,
  3448. CASE
  3449. WHEN a.weight_after IS NULL or a.weight_after = 0 THEN '其他'
  3450. WHEN b.dry_weight IS NOT NULL THEN
  3451. CASE
  3452. WHEN ABS(a.weight_after - b.dry_weight) <= 0.3 THEN '达标'
  3453. ELSE '不达标'
  3454. END
  3455. ELSE '其他'
  3456. END AS weight_status
  3457. `).
  3458. Joins("JOIN xt_patients p ON p.id = b.patient_id ").
  3459. Joins("left JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date AND a.status = 1").
  3460. Where("b.user_org_id = ?", user_org_id).
  3461. Where("b.assessment_date >= ?", start_time).
  3462. Where("b.assessment_date <= ?", end_time).
  3463. Where("b.status = ?", 1)
  3464. switch add_type {
  3465. case 1:
  3466. query = query.Where(`
  3467. (
  3468. CASE
  3469. WHEN (
  3470. SELECT a2.weight_after
  3471. FROM xt_assessment_after_dislysis a2
  3472. JOIN xt_assessment_before_dislysis b2
  3473. ON a2.patient_id = b2.patient_id
  3474. AND a2.assessment_date = b2.assessment_date
  3475. WHERE b2.patient_id = b.patient_id
  3476. AND b2.assessment_date < b.assessment_date
  3477. ORDER BY b2.assessment_date DESC LIMIT 1
  3478. ) IS NOT NULL
  3479. THEN (b.weight_before - (
  3480. SELECT a2.weight_after
  3481. FROM xt_assessment_after_dislysis a2
  3482. JOIN xt_assessment_before_dislysis b2
  3483. ON a2.patient_id = b2.patient_id
  3484. AND a2.assessment_date = b2.assessment_date
  3485. WHERE b2.patient_id = b.patient_id
  3486. AND b2.assessment_date < b.assessment_date
  3487. ORDER BY b2.assessment_date DESC LIMIT 1
  3488. )) / (
  3489. SELECT a2.weight_after
  3490. FROM xt_assessment_after_dislysis a2
  3491. JOIN xt_assessment_before_dislysis b2
  3492. ON a2.patient_id = b2.patient_id
  3493. AND a2.assessment_date = b2.assessment_date
  3494. WHERE b2.patient_id = b.patient_id
  3495. AND b2.assessment_date < b.assessment_date
  3496. ORDER BY b2.assessment_date DESC LIMIT 1
  3497. )
  3498. ELSE NULL
  3499. END
  3500. ) <= 0.03
  3501. `)
  3502. case 2:
  3503. query = query.Where(`
  3504. (
  3505. CASE
  3506. WHEN (
  3507. SELECT a2.weight_after
  3508. FROM xt_assessment_after_dislysis a2
  3509. JOIN xt_assessment_before_dislysis b2
  3510. ON a2.patient_id = b2.patient_id
  3511. AND a2.assessment_date = b2.assessment_date
  3512. WHERE b2.patient_id = b.patient_id
  3513. AND b2.assessment_date < b.assessment_date
  3514. ORDER BY b2.assessment_date DESC LIMIT 1
  3515. ) IS NOT NULL
  3516. THEN (b.weight_before - (
  3517. SELECT a2.weight_after
  3518. FROM xt_assessment_after_dislysis a2
  3519. JOIN xt_assessment_before_dislysis b2
  3520. ON a2.patient_id = b2.patient_id
  3521. AND a2.assessment_date = b2.assessment_date
  3522. WHERE b2.patient_id = b.patient_id
  3523. AND b2.assessment_date < b.assessment_date
  3524. ORDER BY b2.assessment_date DESC LIMIT 1
  3525. )) / (
  3526. SELECT a2.weight_after
  3527. FROM xt_assessment_after_dislysis a2
  3528. JOIN xt_assessment_before_dislysis b2
  3529. ON a2.patient_id = b2.patient_id
  3530. AND a2.assessment_date = b2.assessment_date
  3531. WHERE b2.patient_id = b.patient_id
  3532. AND b2.assessment_date < b.assessment_date
  3533. ORDER BY b2.assessment_date DESC LIMIT 1
  3534. )
  3535. ELSE NULL
  3536. END
  3537. ) > 0.03 AND (
  3538. CASE
  3539. WHEN (
  3540. SELECT a2.weight_after
  3541. FROM xt_assessment_after_dislysis a2
  3542. JOIN xt_assessment_before_dislysis b2
  3543. ON a2.patient_id = b2.patient_id
  3544. AND a2.assessment_date = b2.assessment_date
  3545. WHERE b2.patient_id = b.patient_id
  3546. AND b2.assessment_date < b.assessment_date
  3547. ORDER BY b2.assessment_date DESC LIMIT 1
  3548. ) IS NOT NULL
  3549. THEN (b.weight_before - (
  3550. SELECT a2.weight_after
  3551. FROM xt_assessment_after_dislysis a2
  3552. JOIN xt_assessment_before_dislysis b2
  3553. ON a2.patient_id = b2.patient_id
  3554. AND a2.assessment_date = b2.assessment_date
  3555. WHERE b2.patient_id = b.patient_id
  3556. AND b2.assessment_date < b.assessment_date
  3557. ORDER BY b2.assessment_date DESC LIMIT 1
  3558. )) / (
  3559. SELECT a2.weight_after
  3560. FROM xt_assessment_after_dislysis a2
  3561. JOIN xt_assessment_before_dislysis b2
  3562. ON a2.patient_id = b2.patient_id
  3563. AND a2.assessment_date = b2.assessment_date
  3564. WHERE b2.patient_id = b.patient_id
  3565. AND b2.assessment_date < b.assessment_date
  3566. ORDER BY b2.assessment_date DESC LIMIT 1
  3567. )
  3568. ELSE NULL
  3569. END
  3570. ) < 0.05
  3571. `)
  3572. case 3:
  3573. query = query.Where(`
  3574. (
  3575. CASE
  3576. WHEN (
  3577. SELECT a2.weight_after
  3578. FROM xt_assessment_after_dislysis a2
  3579. JOIN xt_assessment_before_dislysis b2
  3580. ON a2.patient_id = b2.patient_id
  3581. AND a2.assessment_date = b2.assessment_date
  3582. WHERE b2.patient_id = b.patient_id
  3583. AND b2.assessment_date < b.assessment_date
  3584. ORDER BY b2.assessment_date DESC LIMIT 1
  3585. ) IS NOT NULL
  3586. THEN (b.weight_before - (
  3587. SELECT a2.weight_after
  3588. FROM xt_assessment_after_dislysis a2
  3589. JOIN xt_assessment_before_dislysis b2
  3590. ON a2.patient_id = b2.patient_id
  3591. AND a2.assessment_date = b2.assessment_date
  3592. WHERE b2.patient_id = b.patient_id
  3593. AND b2.assessment_date < b.assessment_date
  3594. ORDER BY b2.assessment_date DESC LIMIT 1
  3595. )) / (
  3596. SELECT a2.weight_after
  3597. FROM xt_assessment_after_dislysis a2
  3598. JOIN xt_assessment_before_dislysis b2
  3599. ON a2.patient_id = b2.patient_id
  3600. AND a2.assessment_date = b2.assessment_date
  3601. WHERE b2.patient_id = b.patient_id
  3602. AND b2.assessment_date < b.assessment_date
  3603. ORDER BY b2.assessment_date DESC LIMIT 1
  3604. )
  3605. ELSE NULL
  3606. END
  3607. ) >= 0.05
  3608. `)
  3609. case 4:
  3610. query = query.Where(`
  3611. (
  3612. SELECT a2.weight_after
  3613. FROM xt_assessment_after_dislysis a2
  3614. JOIN xt_assessment_before_dislysis b2
  3615. ON a2.patient_id = b2.patient_id
  3616. AND a2.assessment_date = b2.assessment_date
  3617. WHERE b2.patient_id = b.patient_id
  3618. AND b2.assessment_date < b.assessment_date
  3619. ORDER BY b2.assessment_date DESC LIMIT 1
  3620. ) IS NULL
  3621. `)
  3622. }
  3623. //// 获取总记录数
  3624. if err := query.Count(&total).Error; err != nil {
  3625. return nil, 0, fmt.Errorf("count query failed: %v", err)
  3626. }
  3627. var data []DialysisDataTwo
  3628. query.Order("p.id, b.assessment_date").Find(&data)
  3629. return data, total, nil
  3630. }
  3631. func GetNewDialysisWeightDetailTableTenTenTwo(
  3632. user_org_id int64,
  3633. start_time int64,
  3634. end_time int64, add_type int64) ([]DialysisDataTwo, int64, error) {
  3635. var total int64
  3636. // 构建基本查询
  3637. query := readDb.Table("xt_assessment_before_dislysis b").
  3638. Select(`
  3639. p.dialysis_no AS dialysis_no,
  3640. p.name AS patient_name,
  3641. p.gender AS gender,
  3642. FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
  3643. b.assessment_date AS assessment_date,
  3644. b.dry_weight AS dry_weight,
  3645. b.weight_before AS weight_before,
  3646. a.weight_after AS weight_after,
  3647. (
  3648. SELECT a2.weight_after
  3649. FROM xt_assessment_after_dislysis a2
  3650. JOIN xt_assessment_before_dislysis b2
  3651. ON a2.patient_id = b2.patient_id
  3652. AND a2.assessment_date = b2.assessment_date
  3653. WHERE b2.patient_id = b.patient_id
  3654. AND b2.assessment_date < b.assessment_date
  3655. ORDER BY b2.assessment_date DESC LIMIT 1
  3656. ) AS last_weight_after,
  3657. CASE
  3658. WHEN b.dry_weight and b.weight_before IS NOT NULL
  3659. THEN (b.weight_before - b.dry_weight) / b.dry_weight
  3660. ELSE NULL
  3661. END AS weight_add,
  3662. CASE
  3663. WHEN a.weight_after IS NULL or a.weight_after = 0 THEN '其他'
  3664. WHEN b.dry_weight IS NOT NULL THEN
  3665. CASE
  3666. WHEN ABS(a.weight_after - b.dry_weight) <= 0.3 THEN '达标'
  3667. ELSE '不达标'
  3668. END
  3669. ELSE '其他'
  3670. END AS weight_status
  3671. `).
  3672. Joins("JOIN xt_patients p ON p.id = b.patient_id ").
  3673. Joins("left JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date AND a.status = 1").
  3674. Where("b.user_org_id = ?", user_org_id).
  3675. Where("b.assessment_date >= ?", start_time).
  3676. Where("b.assessment_date <= ?", end_time).
  3677. Where("b.status = ?", 1)
  3678. switch add_type {
  3679. case 1:
  3680. query = query.Where(`
  3681. (
  3682. CASE
  3683. WHEN b.dry_weight and b.weight_before IS NOT NULL
  3684. THEN (b.weight_before -b.dry_weight) / b.dry_weight
  3685. ELSE NULL
  3686. END
  3687. ) <= 0.03
  3688. `)
  3689. case 2:
  3690. query = query.Where(`
  3691. (
  3692. CASE
  3693. WHEN b.dry_weight and b.weight_before IS NOT NULL
  3694. THEN (b.weight_before -b.dry_weight) / b.dry_weight
  3695. ELSE NULL
  3696. END
  3697. ) > 0.03 AND (
  3698. CASE
  3699. WHEN b.dry_weight and b.weight_before IS NOT NULL
  3700. THEN (b.weight_before -b.dry_weight) / b.dry_weight
  3701. ELSE NULL
  3702. END
  3703. ) < 0.05
  3704. `)
  3705. case 3:
  3706. query = query.Where(`
  3707. (
  3708. CASE
  3709. WHEN b.dry_weight and b.weight_before IS NOT NULL
  3710. THEN (b.weight_before -b.dry_weight) / b.dry_weight
  3711. ELSE NULL
  3712. END
  3713. ) >= 0.05
  3714. `)
  3715. case 4:
  3716. query = query.Where(`
  3717. (
  3718. b.dry_weight
  3719. ) IS NULL or (
  3720. b.weight_before
  3721. ) IS NULL
  3722. `)
  3723. }
  3724. //// 获取总记录数
  3725. if err := query.Count(&total).Error; err != nil {
  3726. return nil, 0, fmt.Errorf("count query failed: %v", err)
  3727. }
  3728. var data []DialysisDataTwo
  3729. query.Order("p.id, b.assessment_date").Find(&data)
  3730. return data, total, nil
  3731. }