statistis_qc_service.go 150KB

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