statistis_qc_service.go 150KB

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