陈少旭 9 miesięcy temu
rodzic
commit
71e0653a46
3 zmienionych plików z 1398 dodań i 21 usunięć
  1. 724 21
      controllers/statistics_api_controller.go
  2. 7 0
      models/qc.go
  3. 667 0
      service/statistis_qc_service.go

+ 724 - 21
controllers/statistics_api_controller.go Wyświetl plik

@@ -2,11 +2,13 @@ package controllers
2 2
 
3 3
 import (
4 4
 	"XT_New/enums"
5
+	"XT_New/models"
5 6
 	"XT_New/service"
6 7
 	"XT_New/utils"
7 8
 	"fmt"
8 9
 	"github.com/astaxie/beego"
9 10
 	"strconv"
11
+	"strings"
10 12
 	"time"
11 13
 )
12 14
 
@@ -26,6 +28,465 @@ func StatisticsApiRegistRouters() {
26 28
 	beego.Router("/api/commonqc/statistiscperson/get", &StatisticsApiController{}, "get:GetPersonCommonInspectionStatistisc")
27 29
 	beego.Router("/api/commonqc/patientstatistiscall/get", &StatisticsApiController{}, "get:GetPatientCommonInspectionStatistisc")
28 30
 
31
+	//透析总量统计
32
+	beego.Router("/api/commonqc/dialysis/total", &StatisticsApiController{}, "get:GetDialysisTotal")
33
+	beego.Router("/api/commonqc/dialysis/detail", &StatisticsApiController{}, "get:GetDialysisTotalDetail")
34
+	beego.Router("/api/commonqc/dialysis/details", &StatisticsApiController{}, "get:GetDialysisTotalDetailInfo")
35
+
36
+	beego.Router("/api/commonqc/anticoagulant", &StatisticsApiController{}, "get:GetAnticoagulant")
37
+	beego.Router("/api/commonqc/anticoagulant/detail", &StatisticsApiController{}, "get:GetAnticoagulantDetail")
38
+
39
+	beego.Router("/api/commonqc/dialyzer", &StatisticsApiController{}, "get:GetDialyzer")
40
+	beego.Router("/api/commonqc/dialyzer/config", &StatisticsApiController{}, "get:GetDialyzerConfig")
41
+	beego.Router("/api/commonqc/dialyzer/detail", &StatisticsApiController{}, "get:GetDialyzerDetail")
42
+
43
+	beego.Router("/api/commonqc/dialysistreat/finish", &StatisticsApiController{}, "get:GetDialysisTreatFinsh")
44
+	beego.Router("/api/commonqc/dialysistreat/detail", &StatisticsApiController{}, "get:GetDialysisTreatDetail")
45
+
46
+}
47
+
48
+func (c *StatisticsApiController) GetDialysisTreatFinsh() {
49
+	start_date := c.GetString("start_date")
50
+	end_date := c.GetString("end_date")
51
+	timeLayout := "2006-01-02"
52
+	loc, _ := time.LoadLocation("Local")
53
+	var startTime int64
54
+	if len(start_date) > 0 {
55
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
56
+		fmt.Println("err-----------", err)
57
+		if err != nil {
58
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
59
+			return
60
+		}
61
+		startTime = theTime.Unix()
62
+	}
63
+	var endTime int64
64
+	if len(end_date) > 0 {
65
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
66
+		if err != nil {
67
+			utils.ErrorLog(err.Error())
68
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
69
+			return
70
+		}
71
+		endTime = theTime.Unix()
72
+	}
73
+	data, _ := service.GetDialysisCompletionRate(c.GetAdminUserInfo().CurrentOrgId, startTime, endTime)
74
+	total, _ := service.GetDialysisCompletionTotal(c.GetAdminUserInfo().CurrentOrgId, startTime, endTime)
75
+	c.ServeSuccessJSON(map[string]interface{}{
76
+		"data":  data,
77
+		"total": total,
78
+	})
79
+}
80
+func (c *StatisticsApiController) GetDialysisTreatDetail() {
81
+	start_date := c.GetString("start_date")
82
+	end_date := c.GetString("end_date")
83
+	mode, _ := c.GetInt64("mode")
84
+	page, _ := c.GetInt64("page", 0)
85
+	limit, _ := c.GetInt64("limit", 0)
86
+	if page <= 0 {
87
+		page = 1
88
+	}
89
+	if limit <= 0 {
90
+		limit = 10
91
+	}
92
+	timeLayout := "2006-01-02"
93
+	loc, _ := time.LoadLocation("Local")
94
+	var startTime int64
95
+	if len(start_date) > 0 {
96
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
97
+		fmt.Println("err-----------", err)
98
+		if err != nil {
99
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
100
+			return
101
+		}
102
+		startTime = theTime.Unix()
103
+	}
104
+	var endTime int64
105
+	if len(end_date) > 0 {
106
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
107
+		if err != nil {
108
+			utils.ErrorLog(err.Error())
109
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
110
+			return
111
+		}
112
+		endTime = theTime.Unix()
113
+	}
114
+	data, total, _ := service.GetDialysisCompletionDetail(c.GetAdminUserInfo().CurrentOrgId, startTime, endTime, mode, limit, page)
115
+	c.ServeSuccessJSON(map[string]interface{}{
116
+		"list":  data,
117
+		"total": total,
118
+	})
119
+}
120
+
121
+func (c *StatisticsApiController) GetDialysisTotal() {
122
+	start_date := c.GetString("start_date")
123
+	end_date := c.GetString("end_date")
124
+	mode, _ := c.GetInt64("mode")
125
+	origin, _ := c.GetInt64("origin")
126
+	time_way, _ := c.GetInt64("time_way")
127
+
128
+	timeLayout := "2006-01-02"
129
+	loc, _ := time.LoadLocation("Local")
130
+	var startTime int64
131
+	if len(start_date) > 0 {
132
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
133
+		fmt.Println("err-----------", err)
134
+		if err != nil {
135
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
136
+			return
137
+		}
138
+		startTime = theTime.Unix()
139
+	}
140
+	var endTime int64
141
+	if len(end_date) > 0 {
142
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
143
+		if err != nil {
144
+			utils.ErrorLog(err.Error())
145
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
146
+			return
147
+		}
148
+		endTime = theTime.Unix()
149
+	}
150
+	if mode == 0 { //统计透析模式不限
151
+		//获取总人数
152
+		p_total, _ := service.GetNewDialysiTotal(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, origin)
153
+		count_struct, _ := service.GetNewDialysisCountMode(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, origin, 0)
154
+		c.ServeSuccessJSON(map[string]interface{}{
155
+			"patient_count": p_total,
156
+			"list":          count_struct,
157
+		})
158
+	} else { //固定某个模式
159
+		// 拆分日期范围
160
+		dates, err := splitDateRange(start_date, end_date)
161
+		if err != nil {
162
+			fmt.Println("Error:", err)
163
+			return
164
+		}
165
+
166
+		var cuss []models.CustomDialysisData
167
+		p_total, _ := service.GetNewDialysiTotal(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, origin)
168
+		switch time_way {
169
+		case 1:
170
+			// 按周统计
171
+			weeks := groupByWeek(dates)
172
+			for _, week := range weeks {
173
+				var cus models.CustomDialysisData
174
+				counts, _ := service.GetNewDialysisCountModeTwo(week[0].Unix(), week[len(week)-1].Unix(), c.GetAdminUserInfo().CurrentOrgId, origin, mode)
175
+				cus.Date = strings.Split(week[0].String(), " ")[0] + "~" + strings.Split(week[len(week)-1].String(), " ")[0]
176
+				cus.Count = counts.Count
177
+				cus.Total = p_total
178
+				cuss = append(cuss, cus)
179
+				c.ServeSuccessJSON(map[string]interface{}{
180
+					"list": cuss,
181
+				})
182
+			}
183
+
184
+			break
185
+		case 2:
186
+			startDate, err := time.Parse("2006-01-02", start_date)
187
+			if err != nil {
188
+				fmt.Println("Error parsing start date:", err)
189
+				return
190
+			}
191
+
192
+			endDate, err := time.Parse("2006-01-02", end_date)
193
+			if err != nil {
194
+				fmt.Println("Error parsing end date:", err)
195
+				return
196
+			}
197
+			dailyDates := splitByDay(startDate, endDate)
198
+			for _, date := range dailyDates {
199
+				//fmt.Println(date)
200
+				var cus models.CustomDialysisData
201
+				counts, _ := service.GetNewDialysisCountModeTwo(date.Unix(), date.Unix(), c.GetAdminUserInfo().CurrentOrgId, origin, mode)
202
+				cus.Date = date.Format("2006-01-02")
203
+				cus.Count = counts.Count
204
+				cus.Total = p_total
205
+				cuss = append(cuss, cus)
206
+				c.ServeSuccessJSON(map[string]interface{}{
207
+					"list": cuss,
208
+				})
209
+
210
+			}
211
+
212
+			break
213
+		case 3:
214
+			// 按月统计
215
+			months := groupByMonth(dates)
216
+			for _, month := range months {
217
+				var cus models.CustomDialysisData
218
+				counts, _ := service.GetNewDialysisCountModeTwo(month[0].Unix(), month[len(month)-1].Unix(), c.GetAdminUserInfo().CurrentOrgId, origin, mode)
219
+				cus.Date = strings.Split(month[0].String(), " ")[0] + "~" + strings.Split(month[len(month)-1].String(), " ")[0]
220
+				cus.Count = counts.Count
221
+				cus.Total = p_total
222
+				cuss = append(cuss, cus)
223
+				c.ServeSuccessJSON(map[string]interface{}{
224
+					"list": cuss,
225
+				})
226
+				//fmt.Println("Month:", month[0], "~", month[len(month)-1])
227
+			}
228
+			break
229
+		case 4:
230
+			// 按年统计
231
+			years := groupByYear(dates)
232
+			for _, year := range years {
233
+				var cus models.CustomDialysisData
234
+				counts, _ := service.GetNewDialysisCountModeTwo(year[0].Unix(), year[len(year)-1].Unix(), c.GetAdminUserInfo().CurrentOrgId, origin, mode)
235
+				cus.Date = strings.Split(year[0].String(), " ")[0] + "~" + strings.Split(year[len(year)-1].String(), " ")[0]
236
+				cus.Count = counts.Count
237
+				cus.Total = p_total
238
+				cuss = append(cuss, cus)
239
+				c.ServeSuccessJSON(map[string]interface{}{
240
+					"list": cuss,
241
+				})
242
+			}
243
+			break
244
+		}
245
+	}
246
+
247
+}
248
+func (c *StatisticsApiController) GetDialysisTotalDetail() {
249
+	start_date := c.GetString("start_date")
250
+	end_date := c.GetString("end_date")
251
+	mode, _ := c.GetInt64("mode")
252
+	origin, _ := c.GetInt64("origin")
253
+	time_way, _ := c.GetInt64("time_way")
254
+	timeLayout := "2006-01-02"
255
+	loc, _ := time.LoadLocation("Local")
256
+	var startTime int64
257
+	if len(start_date) > 0 {
258
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
259
+		fmt.Println("err-----------", err)
260
+		if err != nil {
261
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
262
+			return
263
+		}
264
+		startTime = theTime.Unix()
265
+	}
266
+	var endTime int64
267
+	if len(end_date) > 0 {
268
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
269
+		if err != nil {
270
+			utils.ErrorLog(err.Error())
271
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
272
+			return
273
+		}
274
+		endTime = theTime.Unix()
275
+	}
276
+
277
+	if origin == 1 {
278
+		data, _ := service.GetDialysisStats(startTime, endTime, time_way, mode)
279
+		c.ServeSuccessJSON(map[string]interface{}{
280
+			"data": data,
281
+		})
282
+	} else {
283
+		data, _ := service.GetScheduleStats(startTime, endTime, time_way, mode)
284
+		c.ServeSuccessJSON(map[string]interface{}{
285
+			"data": data,
286
+		})
287
+	}
288
+}
289
+func (c *StatisticsApiController) GetDialysisTotalDetailInfo() {
290
+
291
+}
292
+
293
+func (c *StatisticsApiController) GetAnticoagulant() {
294
+	start_date := c.GetString("start_date")
295
+	end_date := c.GetString("end_date")
296
+	timeLayout := "2006-01-02"
297
+	loc, _ := time.LoadLocation("Local")
298
+	var startTime int64
299
+	if len(start_date) > 0 {
300
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
301
+		fmt.Println("err-----------", err)
302
+		if err != nil {
303
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
304
+			return
305
+		}
306
+		startTime = theTime.Unix()
307
+	}
308
+	var endTime int64
309
+	if len(end_date) > 0 {
310
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
311
+		if err != nil {
312
+			utils.ErrorLog(err.Error())
313
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
314
+			return
315
+		}
316
+		endTime = theTime.Unix()
317
+	}
318
+
319
+	total, err2 := service.GetAnticoagulantTotal(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId)
320
+	if err2 != nil {
321
+		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
322
+		return
323
+	}
324
+	anticoagulantData, err := service.GetAnticoagulantData(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId)
325
+	if err != nil {
326
+		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
327
+		return
328
+	}
329
+	type respData struct {
330
+		Name       string  `json:"name"`
331
+		Count      int     `json:"count"`
332
+		Percentage float64 `json:"percentage"`
333
+	}
334
+	var respDatas []respData
335
+	for anticoagulant, count := range anticoagulantData {
336
+		var respData respData
337
+		respData.Name = anticoagulant
338
+		respData.Count = count
339
+		respData.Percentage = float64(count) / float64(total) * 100
340
+		respDatas = append(respDatas, respData)
341
+	}
342
+	c.ServeSuccessJSON(map[string]interface{}{
343
+		"data": respDatas,
344
+	})
345
+
346
+}
347
+func (c *StatisticsApiController) GetAnticoagulantDetail() {
348
+	anticoagulant, _ := c.GetInt64("anticoagulant")
349
+	start_date := c.GetString("start_date")
350
+	end_date := c.GetString("end_date")
351
+	timeLayout := "2006-01-02"
352
+	page, _ := c.GetInt64("page", 0)
353
+	limit, _ := c.GetInt64("limit", 0)
354
+	if page <= 0 {
355
+		page = 1
356
+	}
357
+	if limit <= 0 {
358
+		limit = 10
359
+	}
360
+	loc, _ := time.LoadLocation("Local")
361
+	var startTime int64
362
+	if len(start_date) > 0 {
363
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
364
+		fmt.Println("err-----------", err)
365
+		if err != nil {
366
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
367
+			return
368
+		}
369
+		startTime = theTime.Unix()
370
+	}
371
+	var endTime int64
372
+	if len(end_date) > 0 {
373
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
374
+		if err != nil {
375
+			utils.ErrorLog(err.Error())
376
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
377
+			return
378
+		}
379
+		endTime = theTime.Unix()
380
+	}
381
+	prescriptions, total, _ := service.GetPrescriptionByAnticoagulant(page, limit, c.GetAdminUserInfo().CurrentOrgId, anticoagulant, startTime, endTime)
382
+
383
+	c.ServeSuccessJSON(map[string]interface{}{
384
+		"prescriptions": prescriptions,
385
+		"total":         total,
386
+	})
387
+
388
+}
389
+
390
+func (c *StatisticsApiController) GetDialyzer() {
391
+	start_date := c.GetString("start_date")
392
+	end_date := c.GetString("end_date")
393
+	timeLayout := "2006-01-02"
394
+	loc, _ := time.LoadLocation("Local")
395
+	var startTime int64
396
+	if len(start_date) > 0 {
397
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
398
+		fmt.Println("err-----------", err)
399
+		if err != nil {
400
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
401
+			return
402
+		}
403
+		startTime = theTime.Unix()
404
+	}
405
+	var endTime int64
406
+	if len(end_date) > 0 {
407
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
408
+		if err != nil {
409
+			utils.ErrorLog(err.Error())
410
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
411
+			return
412
+		}
413
+		endTime = theTime.Unix()
414
+	}
415
+
416
+	total, err2 := service.GetDialyzerTotal(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId)
417
+	if err2 != nil {
418
+		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
419
+		return
420
+	}
421
+	dialyzers, err := service.GetDialyzerData(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId)
422
+	if err != nil {
423
+		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
424
+		return
425
+	}
426
+	type respData struct {
427
+		Name       string  `json:"name"`
428
+		Count      int     `json:"count"`
429
+		Percentage float64 `json:"percentage"`
430
+	}
431
+	var respDatas []respData
432
+	for _, item := range dialyzers {
433
+		var respData respData
434
+		respData.Name = item.Dialyzer
435
+		respData.Count = item.Count
436
+		respData.Percentage = float64(item.Count) / float64(total) * 100
437
+		respDatas = append(respDatas, respData)
438
+	}
439
+	c.ServeSuccessJSON(map[string]interface{}{
440
+		"data": respDatas,
441
+	})
442
+
443
+}
444
+func (c *StatisticsApiController) GetDialyzerDetail() {
445
+	dialyzer := c.GetString("Dialyzer")
446
+	start_date := c.GetString("start_date")
447
+	end_date := c.GetString("end_date")
448
+	timeLayout := "2006-01-02"
449
+	page, _ := c.GetInt64("page", 0)
450
+	limit, _ := c.GetInt64("limit", 0)
451
+	if page <= 0 {
452
+		page = 1
453
+	}
454
+	if limit <= 0 {
455
+		limit = 10
456
+	}
457
+	loc, _ := time.LoadLocation("Local")
458
+	var startTime int64
459
+	if len(start_date) > 0 {
460
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
461
+		fmt.Println("err-----------", err)
462
+		if err != nil {
463
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
464
+			return
465
+		}
466
+		startTime = theTime.Unix()
467
+	}
468
+	var endTime int64
469
+	if len(end_date) > 0 {
470
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
471
+		if err != nil {
472
+			utils.ErrorLog(err.Error())
473
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
474
+			return
475
+		}
476
+		endTime = theTime.Unix()
477
+	}
478
+	prescriptions, total, _ := service.GetPrescriptionByDialyzer(page, limit, c.GetAdminUserInfo().CurrentOrgId, dialyzer, startTime, endTime)
479
+	c.ServeSuccessJSON(map[string]interface{}{
480
+		"prescriptions": prescriptions,
481
+		"total":         total,
482
+	})
483
+
484
+}
485
+func (c *StatisticsApiController) GetDialyzerConfig() {
486
+	dialyzers, _ := service.GetDialyzerSummary(c.GetAdminUserInfo().CurrentOrgId)
487
+	c.ServeSuccessJSON(map[string]interface{}{
488
+		"dialyzers": dialyzers,
489
+	})
29 490
 }
30 491
 
31 492
 // 配置检验数据查询
@@ -206,34 +667,148 @@ func (c *StatisticsApiController) GetPatientCommonInspectionStatistisc() {
206 667
 }
207 668
 
208 669
 func (c *StatisticsApiController) GetFivePatientInspectionStatistisc() {
209
-	start_date := c.GetString("start_date")
670
+	//start_date := c.GetString("start_date")
210 671
 	end_date := c.GetString("end_date")
211
-	keyword := c.GetString("keyword")
212
-	timeLayout := "2006-01-02"
213
-	loc, _ := time.LoadLocation("Local")
214
-	var startTime int64
215
-	if len(start_date) > 0 {
216
-		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_date+" 00:00:00", loc)
217
-		if err != nil {
218
-			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
219
-			return
672
+	//keyword := c.GetString("keyword")
673
+	//timeLayout := "2006-01-02"
674
+	//loc, _ := time.LoadLocation("Local")
675
+
676
+	dynamicFields := make([]map[string]interface{}, 0)
677
+	list, _ := service.GetLatestInspectionValues(c.GetAdminUserInfo().CurrentOrgId, strings.Split(end_date, "-")[0]+"-"+strings.Split(end_date, "-")[1])
678
+	fmt.Println(list)
679
+
680
+	for _, result := range list {
681
+		fmt.Println(result)
682
+		// 打印患者ID
683
+		nb := result["姓名"].([]byte)
684
+		name := string(nb)
685
+
686
+		//ynamicFields := make([]map[string]interface{})
687
+
688
+		//var dynamicFields DynamicData
689
+		ynamicField := make(map[string]interface{})
690
+		ynamicField["姓名"] = name
691
+		// 打印其他列的值
692
+		for columnName, columnValue := range result {
693
+			// 跳过患者ID列
694
+
695
+			if columnName == "姓名" {
696
+				continue
697
+			}
698
+			// 检查值是否为nil
699
+			if columnValue == nil {
700
+				fmt.Printf("Column: %s, Value: <nil>\n", columnName)
701
+				continue
702
+			}
703
+
704
+			// 将字节切片转换为字符串
705
+			byteValue, ok := columnValue.([]byte)
706
+			if !ok {
707
+				// 如果无法转换为[]byte,输出错误信息
708
+				fmt.Printf("Error: Unable to convert value for column %s to []byte\n", columnName)
709
+				continue
710
+			}
711
+
712
+			// 将字节切片转换为字符串
713
+			strValue := string(byteValue)
714
+
715
+			// 尝试将字符串转换为浮点数
716
+			floatValue, err := strconv.ParseFloat(strValue, 64)
717
+			if err != nil {
718
+				// 如果转换失败,输出错误信息
719
+				fmt.Printf("Error converting value for column %s: %v\n", columnName, err)
720
+			} else {
721
+				// 如果转换成功,输出列名和浮点数值
722
+				fmt.Printf("Column: %s, Value: %f\n", columnName, floatValue)
723
+			}
724
+			ynamicField["年月"] = strings.Split(end_date, "-")[0] + "-" + strings.Split(end_date, "-")[1]
725
+			ynamicField[columnName] = floatValue
726
+			dynamicFields = append(dynamicFields, ynamicField)
220 727
 		}
221
-		startTime = theTime.Unix()
222 728
 	}
223
-	var endTime int64
224
-	if len(end_date) > 0 {
225
-		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_date+" 23:59:59", loc)
226
-		if err != nil {
227
-			utils.ErrorLog(err.Error())
228
-			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
229
-			return
729
+
730
+	seen := make(map[interface{}]struct{})
731
+	var uniqueFields []map[string]interface{}
732
+
733
+	for _, field := range dynamicFields {
734
+		value := field["姓名"]
735
+		// 如果值未在map中出现过,则将其添加到新的切片中,并将其添加到map中
736
+		if _, ok := seen[value]; !ok {
737
+			seen[value] = struct{}{}
738
+			uniqueFields = append(uniqueFields, field)
230 739
 		}
231
-		endTime = theTime.Unix()
232 740
 	}
233
-	list, _ := service.GetPatientFiveInspectionByID(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, keyword)
741
+
234 742
 	c.ServeSuccessJSON(map[string]interface{}{
235
-		"list": list,
743
+		"list": uniqueFields,
236 744
 	})
745
+	//list, _ := service.GetPatientFiveInspectionByID(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId, keyword)
746
+	//
747
+	//var names []string
748
+	//
749
+	//for _, item := range list {
750
+	//	names = append(names, item.ItemName)
751
+	//}
752
+	//
753
+	//names = RemoveRepeatedNameElement(names)
754
+	//fmt.Println(names)
755
+	//
756
+	//var tempList []models.InspectionTen
757
+	//list_two := RemoveRepeatedInspectPatientElement(list)
758
+	//for _, item := range list_two {
759
+	//	var temp models.InspectionTen
760
+	//	var temptwos []models.InspectionValue
761
+	//	temp.PatientId = item.PatientId
762
+	//	temp.Name = item.Name
763
+	//	for _, subitem := range list {
764
+	//		if item.PatientId == subitem.PatientId {
765
+	//			var temptwo models.InspectionValue
766
+	//			temptwo.Name = subitem.ItemName
767
+	//			temptwo.Value = subitem.InspectValue
768
+	//			temptwos = append(temptwos, temptwo)
769
+	//		}
770
+	//	}
771
+	//	temp.Values = temptwos
772
+	//	tempList = append(tempList, temp)
773
+	//}
774
+	//
775
+	//var filtered []string
776
+	//for _, item := range names {
777
+	//	if item != "a" {
778
+	//		filtered = append(filtered, item)
779
+	//	}
780
+	//}
781
+	//
782
+	//for _, item := range tempList {
783
+	//	for _, subitem := range item.Values {
784
+	//		for _, name := range filtered {
785
+	//			if subitem.Name != name {
786
+	//				filtered = append(filtered, name)
787
+	//			}
788
+	//		}
789
+	//	}
790
+	//}
791
+	//
792
+	////// 将剩余字符串填充到 Inspection 数组中
793
+	////for _, name := range filtered {
794
+	////	var temptwo models.InspectionValue
795
+	////	temptwo.Name = subitem.ItemName
796
+	////	temptwo.Value = subitem.InspectValue
797
+	////	temptwos = append(temptwos, temptwo)
798
+	////}
799
+	//
800
+	//for _, item := range tempList {
801
+	//	//for _, subitem := range item.Values {
802
+	//	var temptwo models.InspectionValue
803
+	//	for _, name := range filtered {
804
+	//		temptwo.Name = name
805
+	//		temptwo.Value = ""
806
+	//	}
807
+	//	item.Values = append(item.Values, temptwo)
808
+	//	//}
809
+	//	fmt.Println(item.Values)
810
+	//}
811
+
237 812
 }
238 813
 func (c *StatisticsApiController) GetPatientInspectionStatistisc() {
239 814
 	start_date := c.GetString("start_date")
@@ -544,3 +1119,131 @@ func (c *StatisticsApiController) GetStatistics() {
544 1119
 	})
545 1120
 
546 1121
 }
1122
+
1123
+func RemoveRepeatedInspectPatientElement(arr []models.InspectionTenOne) (newArr []models.InspectionTenOne) {
1124
+	newArr = make([]models.InspectionTenOne, 0)
1125
+	for i := 0; i < len(arr); i++ {
1126
+		repeat := false
1127
+		for j := i + 1; j < len(arr); j++ {
1128
+			if arr[i].PatientId == arr[j].PatientId {
1129
+				repeat = true
1130
+				break
1131
+			}
1132
+		}
1133
+		if !repeat {
1134
+			newArr = append(newArr, arr[i])
1135
+		}
1136
+	}
1137
+	return
1138
+}
1139
+
1140
+func RemoveRepeatedNameElement(arr []string) (newArr []string) {
1141
+	newArr = make([]string, 0)
1142
+	for i := 0; i < len(arr); i++ {
1143
+		repeat := false
1144
+		for j := i + 1; j < len(arr); j++ {
1145
+			if arr[i] == arr[j] {
1146
+				repeat = true
1147
+				break
1148
+			}
1149
+		}
1150
+		if !repeat {
1151
+			newArr = append(newArr, arr[i])
1152
+		}
1153
+	}
1154
+	return
1155
+}
1156
+
1157
+// 根据指定的日期范围拆分成周、月、年
1158
+func splitDateRange(startDate, endDate string) ([]time.Time, error) {
1159
+	startTime, err := time.Parse("2006-01-02", startDate)
1160
+	if err != nil {
1161
+		return nil, err
1162
+	}
1163
+
1164
+	endTime, err := time.Parse("2006-01-02", endDate)
1165
+	if err != nil {
1166
+		return nil, err
1167
+	}
1168
+
1169
+	var result []time.Time
1170
+
1171
+	for date := startTime; date.Before(endTime) || date.Equal(endTime); date = date.AddDate(0, 0, 1) {
1172
+		result = append(result, date)
1173
+	}
1174
+
1175
+	return result, nil
1176
+}
1177
+
1178
+// 按周统计日期
1179
+func groupByWeek(dates []time.Time) [][]time.Time {
1180
+	var result [][]time.Time
1181
+	var currentWeek []time.Time
1182
+
1183
+	for _, date := range dates {
1184
+		currentWeek = append(currentWeek, date)
1185
+		if date.Weekday() == time.Sunday {
1186
+			result = append(result, currentWeek)
1187
+			currentWeek = nil
1188
+		}
1189
+	}
1190
+
1191
+	if len(currentWeek) > 0 {
1192
+		result = append(result, currentWeek)
1193
+	}
1194
+
1195
+	return result
1196
+}
1197
+
1198
+// 按月统计日期
1199
+func groupByMonth(dates []time.Time) [][]time.Time {
1200
+	var result [][]time.Time
1201
+	var currentMonth []time.Time
1202
+	lastMonth := dates[0].Month()
1203
+
1204
+	for _, date := range dates {
1205
+		if date.Month() != lastMonth {
1206
+			result = append(result, currentMonth)
1207
+			currentMonth = nil
1208
+			lastMonth = date.Month()
1209
+		}
1210
+		currentMonth = append(currentMonth, date)
1211
+	}
1212
+
1213
+	if len(currentMonth) > 0 {
1214
+		result = append(result, currentMonth)
1215
+	}
1216
+
1217
+	return result
1218
+}
1219
+
1220
+// 按年统计日期
1221
+func groupByYear(dates []time.Time) [][]time.Time {
1222
+	var result [][]time.Time
1223
+	var currentYear []time.Time
1224
+	lastYear := dates[0].Year()
1225
+
1226
+	for _, date := range dates {
1227
+		if date.Year() != lastYear {
1228
+			result = append(result, currentYear)
1229
+			currentYear = nil
1230
+			lastYear = date.Year()
1231
+		}
1232
+		currentYear = append(currentYear, date)
1233
+	}
1234
+
1235
+	if len(currentYear) > 0 {
1236
+		result = append(result, currentYear)
1237
+	}
1238
+
1239
+	return result
1240
+}
1241
+
1242
+// 将日期拆分为按天统计
1243
+func splitByDay(startDate, endDate time.Time) []time.Time {
1244
+	var dates []time.Time
1245
+	for d := startDate; !d.After(endDate); d = d.AddDate(0, 0, 1) {
1246
+		dates = append(dates, d)
1247
+	}
1248
+	return dates
1249
+}

+ 7 - 0
models/qc.go Wyświetl plik

@@ -0,0 +1,7 @@
1
+package models
2
+
3
+type CustomDialysisData struct {
4
+	Date  string `json:"date"`
5
+	Count int64  `json:"count"`
6
+	Total int64  `json:"total"`
7
+}

+ 667 - 0
service/statistis_qc_service.go Wyświetl plik

@@ -0,0 +1,667 @@
1
+package service
2
+
3
+import (
4
+	"XT_New/models"
5
+	"fmt"
6
+	"strings"
7
+)
8
+
9
+func GetDialysisTotalDatas() {
10
+
11
+}
12
+
13
+func GetNewDialysiTotal(startime int64, endtime int64, orgid int64, origin int64) (int64, error) {
14
+	counts := models.PatientPrescriptionCountStruct{}
15
+	var err error
16
+	if origin == 1 { //透析记录,上机为准
17
+		db := XTReadDB().Table("xt_dialysis_order as x")
18
+		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
19
+		return counts.Count, err
20
+
21
+	} else {
22
+		db := XTReadDB().Table("xt_schedule as sch")
23
+		//err = db.Select("COUNT(DISTINCT patient_id) AS total_employees").Where("x.dialysis_date >= ? and x.dialysis_date<=? and x.user_org_id = ? AND x.status = 1", startime, endtime, orgid).Scan(&counts).Error
24
+		var count int64
25
+		err := db.Model(&Schedule{}).
26
+			Select("patient_id, schedule_date").
27
+			Group("patient_id, schedule_date").
28
+			Where("schedule_date >= ? AND schedule_date  <= ?", startime, endtime).
29
+			Count(&count).
30
+			Error
31
+		return count, err
32
+
33
+	}
34
+
35
+}
36
+
37
+type TreatmentMode struct {
38
+	ModeID   int    `json:"mode_id"`
39
+	ModeName string `json:"mode_name"`
40
+}
41
+
42
+type DialysisStat struct {
43
+	Date   string         `json:"日期"`
44
+	Counts map[string]int `json:"counts"`
45
+	Total  int            `json:"合计"`
46
+}
47
+
48
+func getTreatmentModes() ([]TreatmentMode, error) {
49
+	var modes []TreatmentMode
50
+	if err := XTReadDB().Find(&modes).Error; err != nil {
51
+		return nil, err
52
+	}
53
+	return modes, nil
54
+}
55
+
56
+func GetDialysisStats(startDate int64, endDate int64, groupBy int64, mode int64) ([]DialysisStat, error) {
57
+	var stats []DialysisStat
58
+	var modes []TreatmentMode
59
+	if mode > 0 {
60
+		modes, _ = getTreatmentModes()
61
+	} else {
62
+		modes_two, _ := getTreatmentModes()
63
+		for _, item := range modes_two {
64
+			if item.ModeID == int(mode) {
65
+				modes = append(modes, item)
66
+			}
67
+		}
68
+	}
69
+	// 获取透析模式
70
+	if err != nil {
71
+		return nil, err
72
+	}
73
+
74
+	// 构建日期格式
75
+	dateFormat := "%Y-%m-%d"
76
+	switch groupBy {
77
+	case 2:
78
+		dateFormat = "%Y-%m-%d"
79
+	case 1:
80
+		dateFormat = "%Y-%u"
81
+	case 3:
82
+		dateFormat = "%Y-%m"
83
+	case 4:
84
+		dateFormat = "%Y"
85
+	default:
86
+		return nil, fmt.Errorf("invalid group by option")
87
+	}
88
+	// 构建动态查询语句
89
+	selectClauses := []string{fmt.Sprintf("DATE_FORMAT(p.record_date, '%s') AS `日期`", dateFormat)}
90
+	for _, mode := range modes {
91
+		selectClauses = append(selectClauses, fmt.Sprintf("SUM(CASE WHEN t.mode_name = '%s' THEN 1 ELSE 0 END) AS `%s`", mode.ModeName, mode.ModeName))
92
+	}
93
+	selectClauses = append(selectClauses, "COUNT(*) AS `合计`")
94
+
95
+	query := fmt.Sprintf(`
96
+        SELECT
97
+            %s
98
+        FROM
99
+            xt_dialysis_prescription p
100
+        JOIN
101
+            xt_treatment_mode t ON p.mode_id = t.mode_id
102
+        WHERE
103
+            p.record_date >= ? AND p.record_date <= ?
104
+        GROUP BY
105
+            DATE_FORMAT(p.record_date, '%s')
106
+    `, dateFormat, dateFormat, selectClauses)
107
+
108
+	rows, err := readDb.Raw(query, startDate, endDate).Rows()
109
+	if err != nil {
110
+		return nil, err
111
+	}
112
+	defer rows.Close()
113
+
114
+	// 解析查询结果
115
+	for rows.Next() {
116
+		var stat DialysisStat
117
+		stat.Counts = make(map[string]int)
118
+		cols := []interface{}{&stat.Date}
119
+		for _, mode := range modes {
120
+			var count int
121
+			cols = append(cols, &count)
122
+			stat.Counts[mode.ModeName] = count
123
+		}
124
+		var total int
125
+		cols = append(cols, &total)
126
+		stat.Total = total
127
+		if err := rows.Scan(cols...); err != nil {
128
+			return nil, err
129
+		}
130
+		stats = append(stats, stat)
131
+	}
132
+
133
+	return stats, nil
134
+}
135
+
136
+func GetScheduleStats(startDate int64, endDate int64, groupBy int64, mode int64) ([]DialysisStat, error) {
137
+	var stats []DialysisStat
138
+	var modes []TreatmentMode
139
+	if mode > 0 {
140
+		modes, _ = getTreatmentModes()
141
+	} else {
142
+		modes_two, _ := getTreatmentModes()
143
+		for _, item := range modes_two {
144
+			if item.ModeID == int(mode) {
145
+				modes = append(modes, item)
146
+			}
147
+		}
148
+	}
149
+	// 获取透析模式
150
+	if err != nil {
151
+		return nil, err
152
+	}
153
+
154
+	// 构建日期格式
155
+	dateFormat := "%Y-%m-%d"
156
+	switch groupBy {
157
+	case 2:
158
+		dateFormat = "%Y-%m-%d"
159
+	case 1:
160
+		dateFormat = "%Y-%u"
161
+	case 3:
162
+		dateFormat = "%Y-%m"
163
+	case 4:
164
+		dateFormat = "%Y"
165
+	default:
166
+		return nil, fmt.Errorf("invalid group by option")
167
+	}
168
+	// 构建动态查询语句
169
+	selectClauses := []string{fmt.Sprintf("DATE_FORMAT(p.schudle_date, '%s') AS `日期`", dateFormat)}
170
+	for _, mode := range modes {
171
+		selectClauses = append(selectClauses, fmt.Sprintf("SUM(CASE WHEN t.mode_name = '%s' THEN 1 ELSE 0 END) AS `%s`", mode.ModeName, mode.ModeName))
172
+	}
173
+	selectClauses = append(selectClauses, "COUNT(*) AS `合计`")
174
+
175
+	query := fmt.Sprintf(`
176
+        SELECT
177
+            %s
178
+        FROM
179
+            xt_schedule p
180
+        JOIN
181
+            xt_treatment_mode t ON p.mode_id = t.mode_id
182
+        WHERE
183
+            p.schedule_date >= ? AND p.schedule_date <= ?
184
+        GROUP BY
185
+            DATE_FORMAT(p.schedule_date, '%s')
186
+    `, dateFormat, dateFormat, selectClauses)
187
+
188
+	rows, err := readDb.Raw(query, startDate, endDate).Rows()
189
+	if err != nil {
190
+		return nil, err
191
+	}
192
+	defer rows.Close()
193
+
194
+	// 解析查询结果
195
+	for rows.Next() {
196
+		var stat DialysisStat
197
+		stat.Counts = make(map[string]int)
198
+		cols := []interface{}{&stat.Date}
199
+		for _, mode := range modes {
200
+			var count int
201
+			cols = append(cols, &count)
202
+			stat.Counts[mode.ModeName] = count
203
+		}
204
+		var total int
205
+		cols = append(cols, &total)
206
+		stat.Total = total
207
+		if err := rows.Scan(cols...); err != nil {
208
+			return nil, err
209
+		}
210
+		stats = append(stats, stat)
211
+	}
212
+
213
+	return stats, nil
214
+}
215
+
216
+func GetNewDialysisCountMode(starttime int64, endtime int64, orgid int64, origin int64, mode_id int64) (counts []*models.PatientPrescriptionCountStruct, err error) {
217
+	if origin == 1 {
218
+
219
+		db := readDb.Table("xt_dialysis_order as o").Where("o.status = 1")
220
+		if starttime > 0 {
221
+			db = db.Where("o.dialysis_date >=?", starttime)
222
+		}
223
+		if endtime > 0 {
224
+			db = db.Where("o.dialysis_date<=?", endtime)
225
+		}
226
+		if orgid > 0 {
227
+			db = db.Where("o.user_org_id = ?", orgid)
228
+		}
229
+		if mode_id == 0 {
230
+			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
231
+
232
+		} else {
233
+
234
+			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
235
+
236
+		}
237
+		return counts, err
238
+
239
+	} else {
240
+		db := readDb.Table("xt_schedule as o").Where("o.status = 1").Select("patient_id, schedule_date").Group("patient_id, schedule_date,mode_id")
241
+		if starttime > 0 {
242
+			db = db.Where("o.schedule_date >= ?", starttime)
243
+		}
244
+		if endtime > 0 {
245
+			db = db.Where("o.schedule_date <= ?", endtime)
246
+		}
247
+		if orgid > 0 {
248
+			db = db.Where("o.user_org_id = ?", orgid)
249
+		}
250
+		err = db.Select("o.mode_id,count(o.mode_id) as count").Scan(&counts).Error
251
+		return counts, err
252
+
253
+	}
254
+
255
+}
256
+
257
+func GetNewDialysisCountModeTwo(starttime int64, endtime int64, orgid int64, origin int64, mode_id int64) (counts models.CustomDialysisData, err error) {
258
+	if origin == 1 {
259
+		db := readDb.Table("xt_dialysis_order as o").Where("o.status = 1")
260
+		if starttime > 0 {
261
+			db = db.Where("o.dialysis_date >=?", starttime)
262
+		}
263
+		if endtime > 0 {
264
+			db = db.Where("o.dialysis_date<=?", endtime)
265
+		}
266
+		if orgid > 0 {
267
+			db = db.Where("o.user_org_id = ?", orgid)
268
+		}
269
+
270
+		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
271
+		return counts, err
272
+
273
+	} else {
274
+		db := readDb.Table("xt_schedule as o").Where("o.status = 1").Select("patient_id, schedule_date").Group("patient_id, schedule_date,mode_id")
275
+		if starttime > 0 {
276
+			db = db.Where("o.schedule_date >= ?", starttime)
277
+		}
278
+		if endtime > 0 {
279
+			db = db.Where("o.schedule_date <= ?", endtime)
280
+		}
281
+		if orgid > 0 {
282
+			db = db.Where("o.user_org_id = ?", orgid)
283
+		}
284
+		if mode_id > 0 {
285
+			db = db.Where("o.mode_id = ?", mode_id)
286
+		}
287
+
288
+		err = db.Select("o.mode_id,count(o.mode_id) as count").Scan(&counts).Error
289
+		return counts, err
290
+
291
+	}
292
+
293
+}
294
+
295
+var anticoagulantMap = map[int]string{
296
+	1:  "无肝素",
297
+	2:  "普通肝素",
298
+	3:  "低分子肝素",
299
+	4:  "阿加曲班",
300
+	5:  "枸橼酸钠",
301
+	6:  "低分子肝素钙",
302
+	7:  "低分子肝素钠",
303
+	8:  "依诺肝素",
304
+	9:  "达肝素",
305
+	10: "体外抗凝",
306
+	11: "那屈肝素",
307
+	12: "无抗凝剂",
308
+	13: "那屈肝素钙",
309
+	14: "肝素钙注射液",
310
+	15: "甲磺酸萘莫司他",
311
+	16: "低分子量肝素钙",
312
+	17: "肝素钠",
313
+}
314
+
315
+func GetAnticoagulantData(start_time int64, end_time int64, org_id int64) (map[string]int, error) {
316
+	var results []struct {
317
+		Anticoagulant int
318
+		Count         int
319
+	}
320
+
321
+	err := XTReadDB().Model(&models.DialysisPrescription{}).
322
+		Select("anticoagulant, COUNT(*) as count").
323
+		Where("status = 1 and record_date >= ? and record_date <= ? and user_org_id = ?", start_time, end_time, org_id).
324
+		Group("anticoagulant").
325
+		Scan(&results).Error
326
+	if err != nil {
327
+		return nil, err
328
+	}
329
+	anticoagulantData := make(map[string]int)
330
+	for _, result := range results {
331
+		if name, ok := anticoagulantMap[result.Anticoagulant]; ok {
332
+			anticoagulantData[name] = result.Count
333
+		} else {
334
+			anticoagulantData[fmt.Sprintf("Unknown (%d)", result.Anticoagulant)] = result.Count
335
+		}
336
+	}
337
+
338
+	return anticoagulantData, nil
339
+}
340
+func GetAnticoagulantTotal(start_time int64, end_time int64, org_id int64) (total int64, err error) {
341
+	err = XTReadDB().Model(&models.DialysisPrescription{}).
342
+		Where("record_date >= ? and record_date <= ? and user_org_id = ? and status = 1", start_time, end_time, org_id).
343
+		Count(&total).Error
344
+
345
+	return
346
+}
347
+func GetDialyzerSummary(org_id int64) ([]string, error) {
348
+	var Dialyzer []string
349
+	err := XTReadDB().Model(&models.DialysisPrescription{}).
350
+		Select("dialysis_dialyszers").
351
+		Where(" user_org_id = ? and status = 1", org_id).
352
+		Group("dialysis_dialyszers").
353
+		Scan(&Dialyzer).Error
354
+	if err != nil {
355
+		return nil, err
356
+	}
357
+	return Dialyzer, nil
358
+}
359
+
360
+type DialyzerResult struct {
361
+	Dialyzer string
362
+	Count    int
363
+}
364
+
365
+func GetDialyzerData(start_time int64, end_time int64, org_id int64) (dr []DialyzerResult, err error) {
366
+	err = XTReadDB().Model(&models.DialysisPrescription{}).
367
+		Select("dialysis_dialyszers as dialyzer, COUNT(*) as count").
368
+		Where("record_date >= ? and record_date <= ? and user_org_id = ? and status = 1", start_time, end_time, org_id).
369
+		Group("dialysis_dialyszers").
370
+		Scan(&dr).Error
371
+	return
372
+}
373
+
374
+func GetDialyzerTotal(start_time int64, end_time int64, org_id int64) (total int64, err error) {
375
+	err = XTReadDB().Model(&models.DialysisPrescription{}).
376
+		Where("status = 1 and record_date >= ? and record_date <= ? and user_org_id = ? and dialysis_dialyszers <> ''", start_time, end_time, org_id).
377
+		Count(&total).Error
378
+	return
379
+}
380
+
381
+func GetPrescriptionByAnticoagulant(page int64, limit int64, orgid int64, anticoagulant int64, start_time int64, end_time int64) (solution []*models.DialysisPrescription, total int64, err error) {
382
+
383
+	db := XTReadDB().Model(&models.DialysisPrescription{}).Where("status = 1")
384
+	if anticoagulant > 0 {
385
+		db = db.Where("anticoagulant = ?", anticoagulant)
386
+	}
387
+	if orgid > 0 {
388
+		db = db.Where("user_org_id = ?", orgid)
389
+	}
390
+	db = db.Where("record_date >= ? and record_date <= ?", start_time, end_time)
391
+
392
+	offset := (page - 1) * limit
393
+
394
+	err = db.Count(&total).Offset(offset).Limit(limit).Find(&solution).Error
395
+	return solution, total, err
396
+}
397
+
398
+func GetPrescriptionByDialyzer(page int64, limit int64, orgid int64, dialyzer string, start_time int64, end_time int64) (solution []*models.DialysisPrescription, total int64, err error) {
399
+
400
+	db := XTReadDB().Model(&models.DialysisPrescription{}).Where("status = 1")
401
+	if len(dialyzer) > 0 {
402
+		db = db.Where("dialysis_dialyszers = ?", dialyzer)
403
+	}
404
+	if orgid > 0 {
405
+		db = db.Where("user_org_id = ?", orgid)
406
+	}
407
+	db = db.Where("record_date >= ? and record_date <= ?", start_time, end_time)
408
+
409
+	offset := (page - 1) * limit
410
+
411
+	err = db.Count(&total).Offset(offset).Limit(limit).Find(&solution).Error
412
+	return solution, total, err
413
+}
414
+
415
+type DialysisData struct {
416
+	DialysisStatus string `json:"dialysis_status"`
417
+	Count          int    `json:"count"`
418
+}
419
+
420
+type CustomData struct {
421
+	DialysisNo       string
422
+	PatientName      string
423
+	DialysisDate     string
424
+	DialysisDuration string
425
+	ActualDuration   string
426
+	Diff             string
427
+	Doctor           string
428
+	Nurse            string
429
+}
430
+
431
+func GetDialysisCompletionRate(org_id int64, start_time int64, end_time int64) (map[string]int, error) {
432
+	var results []DialysisData
433
+	query := `
434
+        SELECT
435
+            CASE
436
+                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 '达标'
437
+                WHEN TIMESTAMPDIFF(MINUTE, MAKETIME(p.dialysis_duration_hour, p.dialysis_duration_minute, 0), MAKETIME(a.actual_treatment_hour, a.actual_treatment_minute, 0)) < 0 THEN '未达标'
438
+                ELSE '超时'
439
+            END AS dialysis_status,
440
+            COUNT(*) AS count
441
+        FROM
442
+            xt_dialysis_order o
443
+        JOIN
444
+            xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
445
+        JOIN
446
+            xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
447
+        WHERE
448
+            o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ?
449
+        GROUP BY
450
+            dialysis_status;
451
+    `
452
+	if err := readDb.Raw(query, org_id, org_id, org_id, start_time, end_time).Scan(&results).Error; err != nil {
453
+		return nil, err
454
+	}
455
+	dialysisCompletionRate := make(map[string]int)
456
+	for _, result := range results {
457
+		dialysisCompletionRate[result.DialysisStatus] = result.Count
458
+	}
459
+	return dialysisCompletionRate, nil
460
+}
461
+func GetDialysisCompletionTotal(org_id int64, start_time int64, end_time int64) (int64, error) {
462
+	var Count int64
463
+	query := `
464
+        SELECT
465
+            COUNT(*) AS count
466
+        FROM
467
+            xt_dialysis_order o
468
+        JOIN
469
+            xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
470
+        JOIN
471
+            xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
472
+        WHERE
473
+            o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ?
474
+        GROUP BY
475
+            dialysis_status;
476
+    `
477
+	if err := readDb.Raw(query, org_id, org_id, org_id, start_time, end_time).Scan(&Count).Error; err != nil {
478
+		return 0, err
479
+	}
480
+
481
+	return Count, nil
482
+}
483
+func GetDialysisCompletionDetail(org_id int64, start_time int64, end_time int64, mode int64, limit int64, page int64) (date []*CustomData, total int64, err error) {
484
+	var query string
485
+	if mode == 0 {
486
+		query = `
487
+        SELECT
488
+           p.dialysis_no as dialysis_no,
489
+           p.name as patient_name,
490
+           FROM_UNIXTIME(o.dialysis_date) as dialysis_date,
491
+           CONCAT(p.dialysis_duration_hour, 'h', p.dialysis_duration_minute,'min') as dialysis_duration,
492
+           CONCAT(a.actual_treatment_hour, 'h', a.actual_treatment_minute,'min') as actual_duration,
493
+           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,
494
+           p.prescription_doctor as doctor,
495
+           p.finish_nurse as nurse
496
+        FROM
497
+            xt_dialysis_order o
498
+    	JOIN           
499
+            xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?
500
+        JOIN
501
+            xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
502
+        JOIN
503
+            xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
504
+        WHERE
505
+            o.stage = 2 AND o.user_org_id = ? AND o.dialysis_date >= ? AND o.dialysis_date <= ?
506
+    `
507
+
508
+	} else if mode == 1 {
509
+		query = `
510
+        SELECT
511
+           p.dialysis_no as dialysis_no,
512
+           p.name as patient_name,
513
+           FROM_UNIXTIME(o.dialysis_date) as dialysis_date,
514
+           CONCAT(p.dialysis_duration_hour, 'h', p.dialysis_duration_minute,'min') as dialysis_duration,
515
+           CONCAT(a.actual_treatment_hour, 'h', a.actual_treatment_minute,'min') as actual_duration,
516
+           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,
517
+           p.prescription_doctor as doctor,
518
+           p.finish_nurse as nurse
519
+        FROM
520
+            xt_dialysis_order o
521
+    	JOIN           
522
+            xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?
523
+        JOIN
524
+            xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
525
+        JOIN
526
+            xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
527
+        WHERE
528
+            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
529
+    `
530
+
531
+	} else if mode == 2 {
532
+		query = `
533
+        SELECT
534
+           p.dialysis_no as dialysis_no,
535
+           p.name as patient_name,
536
+           FROM_UNIXTIME(o.dialysis_date) as dialysis_date,
537
+           CONCAT(p.dialysis_duration_hour, 'h', p.dialysis_duration_minute,'min') as dialysis_duration,
538
+           CONCAT(a.actual_treatment_hour, 'h', a.actual_treatment_minute,'min') as actual_duration,
539
+           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,
540
+           p.prescription_doctor as doctor,
541
+           p.finish_nurse as nurse
542
+        FROM
543
+            xt_dialysis_order o
544
+    	JOIN           
545
+            xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?
546
+        JOIN
547
+            xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
548
+        JOIN
549
+            xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
550
+        WHERE
551
+            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
552
+    `
553
+
554
+	} else if mode == 3 {
555
+		query = `
556
+        SELECT
557
+           p.dialysis_no as dialysis_no,
558
+           p.name as patient_name,
559
+           FROM_UNIXTIME(o.dialysis_date) as dialysis_date,
560
+           CONCAT(p.dialysis_duration_hour, 'h', p.dialysis_duration_minute,'min') as dialysis_duration,
561
+           CONCAT(a.actual_treatment_hour, 'h', a.actual_treatment_minute,'min') as actual_duration,
562
+           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,
563
+           p.prescription_doctor as doctor,
564
+           p.finish_nurse as nurse
565
+        FROM
566
+            xt_dialysis_order o
567
+    	JOIN           
568
+            xt_patients pp ON o.patient_id = pp.id AND pp.user_org_id = ?
569
+        JOIN
570
+            xt_dialysis_prescription p ON o.dialysis_date = p.record_date AND o.patient_id = p.patient_id AND p.user_org_id = ?
571
+        JOIN
572
+            xt_assessment_after_dislysis a ON o.dialysis_date = a.assessment_date AND o.patient_id = a.patient_id AND a.user_org_id = ?
573
+        WHERE
574
+            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
575
+    `
576
+	}
577
+	offset := (page - 1) * limit
578
+
579
+	if err := readDb.Raw(query, org_id, org_id, org_id, org_id, start_time, end_time).Count(&total).Offset(offset).Limit(limit).Scan(&date).Error; err != nil {
580
+		return nil, 0, err
581
+	}
582
+
583
+	return
584
+}
585
+
586
+type QualityControlStandard struct {
587
+	ItemName        string `json:"item_name"`
588
+	ProjectMame     string `json:"project_name"`
589
+	InspectionMajor int64  `json:"inspection_major"`
590
+	InspectionMinor int64  `json:"inspection_minor"`
591
+}
592
+
593
+func getQualityControlStandards(org_id int64) ([]QualityControlStandard, error) {
594
+	var standards []QualityControlStandard
595
+	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 {
596
+		return nil, err
597
+	}
598
+	return standards, nil
599
+}
600
+
601
+type DialysisQualityStat struct {
602
+	Month  string             `json:"月"`
603
+	Name   string             `json:"姓名"`
604
+	Counts map[string]float64 `json:"counts"`
605
+}
606
+
607
+func GetLatestInspectionValues(org_id int64, yearMonth string) ([]map[string]interface{}, error) {
608
+	standards, err := getQualityControlStandards(org_id)
609
+	if err != nil {
610
+		return nil, err
611
+	}
612
+
613
+	var selectFields []string
614
+	for _, standard := range standards {
615
+		field := fmt.Sprintf("MAX(CASE WHEN i.item_name = %s THEN i.inspect_value END) AS `%s`",
616
+			"'"+standard.ItemName+"'", standard.ItemName)
617
+		selectFields = append(selectFields, field)
618
+	}
619
+
620
+	query := fmt.Sprintf(`
621
+		SELECT
622
+		    p.name as '姓名',
623
+			%s
624
+		FROM xt_inspection i
625
+		JOIN xt_patients p On p.id = i.patient_id
626
+		WHERE DATE_FORMAT(FROM_UNIXTIME(i.inspect_date), '%%Y-%%m') = ? and org_id = ?
627
+		
628
+		GROUP BY i.patient_id
629
+	`, strings.Join(selectFields, ", "))
630
+
631
+	var results []map[string]interface{}
632
+	rows, err := readDb.Raw(query, yearMonth, org_id).Rows()
633
+	if err != nil {
634
+		return nil, err
635
+	}
636
+	defer rows.Close()
637
+
638
+	columns, err := rows.Columns()
639
+	if err != nil {
640
+		return nil, err
641
+	}
642
+
643
+	for rows.Next() {
644
+		// 创建一个长度为列数的切片,用于存储每一行的值
645
+		columnValues := make([]interface{}, len(columns))
646
+		columnPointers := make([]interface{}, len(columns))
647
+		for i := range columnValues {
648
+			columnPointers[i] = &columnValues[i]
649
+		}
650
+
651
+		// 扫描当前行的值
652
+		if err := rows.Scan(columnPointers...); err != nil {
653
+			return nil, err
654
+		}
655
+
656
+		// 将扫描到的值放入结果 map 中
657
+		result := make(map[string]interface{})
658
+		for i, colName := range columns {
659
+			val := columnPointers[i].(*interface{})
660
+			result[colName] = *val
661
+		}
662
+
663
+		results = append(results, result)
664
+	}
665
+
666
+	return results, nil
667
+}