statistis_qc_service.go 151KB

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