Ver código fonte

提交代码

陈少旭 9 meses atrás
pai
commit
54df1ae8d4
2 arquivos alterados com 246 adições e 41 exclusões
  1. 227 32
      controllers/statistics_api_controller.go
  2. 19 9
      service/statistis_qc_service.go

+ 227 - 32
controllers/statistics_api_controller.go Ver arquivo

@@ -7,6 +7,7 @@ import (
7 7
 	"XT_New/utils"
8 8
 	"fmt"
9 9
 	"github.com/astaxie/beego"
10
+	"sort"
10 11
 	"strconv"
11 12
 	"strings"
12 13
 	"time"
@@ -345,27 +346,23 @@ func (c *StatisticsApiController) GetDialysisTotalDetail() {
345 346
 				//fmt.Println(aa)
346 347
 				var dynamicFields []map[string]interface{}
347 348
 				//var uniqueFields []map[string]interface{}
348
-
349 349
 				for _, result := range list {
350
-					// 打印患者ID
351 350
 					nb := result["日期"].([]byte)
352 351
 					name := string(nb)
353 352
 
354
-					// 创建动态字段的map
355 353
 					dynamicField := make(map[string]interface{})
356
-					// 将日期放在首位
357 354
 					dynamicField["日期"] = name
358 355
 
359
-					// 打印其他列的值,并添加到动态字段中
356
+					numericColumns := make(map[string]interface{})
357
+					nonNumericColumns := make(map[string]interface{})
358
+
359
+					// 分类处理列
360 360
 					for columnName, columnValue := range result {
361
-						if columnName == "日期" {
362
-							continue
363
-						}
364
-						if columnName == "合计" {
361
+						if columnName == "日期" || columnName == "合计" {
365 362
 							continue
366 363
 						}
367 364
 						if columnValue == nil {
368
-							dynamicField[columnName] = ""
365
+							nonNumericColumns[columnName] = ""
369 366
 							continue
370 367
 						}
371 368
 
@@ -376,36 +373,49 @@ func (c *StatisticsApiController) GetDialysisTotalDetail() {
376 373
 						}
377 374
 
378 375
 						strValue := string(byteValue)
379
-
380 376
 						floatValue, err := strconv.ParseFloat(strValue, 64)
381 377
 						if err != nil {
382 378
 							fmt.Printf("Error converting value for column %s: %v\n", columnName, err)
379
+							nonNumericColumns[columnName] = strValue
383 380
 						} else {
384 381
 							if floatValue == 0 {
385
-								dynamicField[columnName] = ""
382
+								nonNumericColumns[columnName] = ""
386 383
 							} else {
387
-								dynamicField[columnName] = floatValue
384
+								numericColumns[columnName] = floatValue
388 385
 							}
389 386
 						}
390 387
 					}
391 388
 
392
-					// 将合计放在末尾
389
+					// 添加数值列
390
+					for columnName, columnValue := range numericColumns {
391
+						dynamicField[columnName] = columnValue
392
+					}
393
+
394
+					// 添加非数值列
395
+					for columnName, columnValue := range nonNumericColumns {
396
+						dynamicField[columnName] = columnValue
397
+					}
398
+
393 399
 					hj := result["合计"].(int64)
394 400
 					dynamicField["合计"] = hj
395 401
 
396 402
 					dynamicFields = append(dynamicFields, dynamicField)
397 403
 				}
398 404
 
399
-				// 将日期放在首位
400 405
 				seen := make(map[interface{}]struct{})
401 406
 				for _, field := range dynamicFields {
402 407
 					value := field["日期"]
403 408
 					if _, ok := seen[value]; !ok {
404 409
 						seen[value] = struct{}{}
405 410
 						uniqueFields = append(uniqueFields, field)
411
+
412
+						// 对字段进行排序,有值的排在前面
413
+						sortedField := sortFieldsByValue(field)
414
+						uniqueFields = append(uniqueFields, sortedField)
406 415
 					}
407 416
 				}
408 417
 			}
418
+
409 419
 			c.ServeSuccessJSON(map[string]interface{}{
410 420
 				"list": uniqueFields,
411 421
 			})
@@ -427,29 +437,25 @@ func (c *StatisticsApiController) GetDialysisTotalDetail() {
427 437
 			var uniqueFields []map[string]interface{}
428 438
 			for _, date := range dailyDates {
429 439
 				list, _ := service.GetDialysisStats(date.Unix(), date.Unix(), mode, c.GetAdminUserInfo().CurrentOrgId, time_way)
430
-				//fmt.Println(aa)
431 440
 				var dynamicFields []map[string]interface{}
432 441
 
433 442
 				for _, result := range list {
434
-					// 打印患者ID
435 443
 					nb := result["日期"].([]byte)
436 444
 					name := string(nb)
437 445
 
438
-					// 创建动态字段的map
439 446
 					dynamicField := make(map[string]interface{})
440
-					// 将日期放在首位
441 447
 					dynamicField["日期"] = name
442 448
 
443
-					// 打印其他列的值,并添加到动态字段中
449
+					numericColumns := make(map[string]interface{})
450
+					nonNumericColumns := make(map[string]interface{})
451
+
452
+					// 分类处理列
444 453
 					for columnName, columnValue := range result {
445
-						if columnName == "日期" {
446
-							continue
447
-						}
448
-						if columnName == "合计" {
454
+						if columnName == "日期" || columnName == "合计" {
449 455
 							continue
450 456
 						}
451 457
 						if columnValue == nil {
452
-							dynamicField[columnName] = ""
458
+							nonNumericColumns[columnName] = ""
453 459
 							continue
454 460
 						}
455 461
 
@@ -460,27 +466,35 @@ func (c *StatisticsApiController) GetDialysisTotalDetail() {
460 466
 						}
461 467
 
462 468
 						strValue := string(byteValue)
463
-
464 469
 						floatValue, err := strconv.ParseFloat(strValue, 64)
465 470
 						if err != nil {
466 471
 							fmt.Printf("Error converting value for column %s: %v\n", columnName, err)
472
+							nonNumericColumns[columnName] = strValue
467 473
 						} else {
468 474
 							if floatValue == 0 {
469
-								dynamicField[columnName] = ""
475
+								nonNumericColumns[columnName] = ""
470 476
 							} else {
471
-								dynamicField[columnName] = floatValue
477
+								numericColumns[columnName] = floatValue
472 478
 							}
473 479
 						}
474 480
 					}
475 481
 
476
-					// 将合计放在末尾
482
+					// 添加数值列
483
+					for columnName, columnValue := range numericColumns {
484
+						dynamicField[columnName] = columnValue
485
+					}
486
+
487
+					// 添加非数值列
488
+					for columnName, columnValue := range nonNumericColumns {
489
+						dynamicField[columnName] = columnValue
490
+					}
491
+
477 492
 					hj := result["合计"].(int64)
478 493
 					dynamicField["合计"] = hj
479 494
 
480 495
 					dynamicFields = append(dynamicFields, dynamicField)
481 496
 				}
482 497
 
483
-				// 将日期放在首位
484 498
 				seen := make(map[interface{}]struct{})
485 499
 				for _, field := range dynamicFields {
486 500
 					value := field["日期"]
@@ -490,6 +504,143 @@ func (c *StatisticsApiController) GetDialysisTotalDetail() {
490 504
 					}
491 505
 				}
492 506
 			}
507
+
508
+			//for _, date := range dailyDates {
509
+			//	list, _ := service.GetDialysisStats(date.Unix(), date.Unix(), mode, c.GetAdminUserInfo().CurrentOrgId, time_way)
510
+			//	//fmt.Println(aa)
511
+			//	var dynamicFields []map[string]interface{}
512
+			//
513
+			//	for _, result := range list {
514
+			//		// 打印患者ID
515
+			//		nb := result["日期"].([]byte)
516
+			//		name := string(nb)
517
+			//
518
+			//		// 创建动态字段的map
519
+			//		dynamicField := make(map[string]interface{})
520
+			//		// 将日期放在首位
521
+			//		dynamicField["日期"] = name
522
+			//
523
+			//		// 临时存储无数值的列
524
+			//		nonNumericColumns := make(map[string]interface{})
525
+			//
526
+			//		// 先处理包含数值的列
527
+			//		for columnName, columnValue := range result {
528
+			//			if columnName == "日期" || columnName == "合计" {
529
+			//				continue
530
+			//			}
531
+			//			if columnValue == nil {
532
+			//				nonNumericColumns[columnName] = ""
533
+			//				continue
534
+			//			}
535
+			//
536
+			//			byteValue, ok := columnValue.([]byte)
537
+			//			if !ok {
538
+			//				fmt.Printf("Error: Unable to convert value for column %s to []byte\n", columnName)
539
+			//				continue
540
+			//			}
541
+			//
542
+			//			strValue := string(byteValue)
543
+			//
544
+			//			floatValue, err := strconv.ParseFloat(strValue, 64)
545
+			//			if err != nil {
546
+			//				fmt.Printf("Error converting value for column %s: %v\n", columnName, err)
547
+			//				nonNumericColumns[columnName] = strValue
548
+			//			} else {
549
+			//				if floatValue == 0 {
550
+			//					nonNumericColumns[columnName] = ""
551
+			//				} else {
552
+			//					dynamicField[columnName] = floatValue
553
+			//				}
554
+			//			}
555
+			//		}
556
+			//
557
+			//		// 添加无数值的列
558
+			//		for columnName, columnValue := range nonNumericColumns {
559
+			//			dynamicField[columnName] = columnValue
560
+			//		}
561
+			//
562
+			//		// 将合计放在末尾
563
+			//		hj := result["合计"].(int64)
564
+			//		dynamicField["合计"] = hj
565
+			//
566
+			//		dynamicFields = append(dynamicFields, dynamicField)
567
+			//	}
568
+			//
569
+			//	// 将日期放在首位
570
+			//	seen := make(map[interface{}]struct{})
571
+			//	for _, field := range dynamicFields {
572
+			//		value := field["日期"]
573
+			//		if _, ok := seen[value]; !ok {
574
+			//			seen[value] = struct{}{}
575
+			//			uniqueFields = append(uniqueFields, field)
576
+			//		}
577
+			//	}
578
+			//}
579
+			//for _, date := range dailyDates {
580
+			//	list, _ := service.GetDialysisStats(date.Unix(), date.Unix(), mode, c.GetAdminUserInfo().CurrentOrgId, time_way)
581
+			//	//fmt.Println(aa)
582
+			//	var dynamicFields []map[string]interface{}
583
+			//
584
+			//	for _, result := range list {
585
+			//		// 打印患者ID
586
+			//		nb := result["日期"].([]byte)
587
+			//		name := string(nb)
588
+			//
589
+			//		// 创建动态字段的map
590
+			//		dynamicField := make(map[string]interface{})
591
+			//		// 将日期放在首位
592
+			//		dynamicField["日期"] = name
593
+			//
594
+			//		// 打印其他列的值,并添加到动态字段中
595
+			//		for columnName, columnValue := range result {
596
+			//			if columnName == "日期" {
597
+			//				continue
598
+			//			}
599
+			//			if columnName == "合计" {
600
+			//				continue
601
+			//			}
602
+			//			if columnValue == nil {
603
+			//				dynamicField[columnName] = ""
604
+			//				continue
605
+			//			}
606
+			//
607
+			//			byteValue, ok := columnValue.([]byte)
608
+			//			if !ok {
609
+			//				fmt.Printf("Error: Unable to convert value for column %s to []byte\n", columnName)
610
+			//				continue
611
+			//			}
612
+			//
613
+			//			strValue := string(byteValue)
614
+			//
615
+			//			floatValue, err := strconv.ParseFloat(strValue, 64)
616
+			//			if err != nil {
617
+			//				fmt.Printf("Error converting value for column %s: %v\n", columnName, err)
618
+			//			} else {
619
+			//				if floatValue == 0 {
620
+			//					dynamicField[columnName] = ""
621
+			//				} else {
622
+			//					dynamicField[columnName] = floatValue
623
+			//				}
624
+			//			}
625
+			//		}
626
+			//
627
+			//		// 将合计放在末尾
628
+			//		hj := result["合计"].(int64)
629
+			//		dynamicField["合计"] = hj
630
+			//
631
+			//		dynamicFields = append(dynamicFields, dynamicField)
632
+			//	}
633
+			//
634
+			//	// 将日期放在首位
635
+			//	seen := make(map[interface{}]struct{})
636
+			//	for _, field := range dynamicFields {
637
+			//		value := field["日期"]
638
+			//		if _, ok := seen[value]; !ok {
639
+			//			seen[value] = struct{}{}
640
+			//			uniqueFields = append(uniqueFields, field)
641
+			//		}
642
+			//	}
643
+			//}
493 644
 			c.ServeSuccessJSON(map[string]interface{}{
494 645
 				"list": uniqueFields,
495 646
 			})
@@ -1173,6 +1324,8 @@ func (c *StatisticsApiController) GetDialyzer() {
1173 1324
 		return
1174 1325
 	}
1175 1326
 	dialyzers, err := service.GetDialyzerData(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId)
1327
+	DialysisIrrigation, err := service.GetDialysisIrrigationData(startTime, endTime, c.GetAdminUserInfo().CurrentOrgId)
1328
+
1176 1329
 	if err != nil {
1177 1330
 		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
1178 1331
 		return
@@ -1190,6 +1343,13 @@ func (c *StatisticsApiController) GetDialyzer() {
1190 1343
 		respData.Percentage = float64(item.Count) / float64(total) * 100
1191 1344
 		respDatas = append(respDatas, respData)
1192 1345
 	}
1346
+	for _, item := range DialysisIrrigation {
1347
+		var respData respData
1348
+		respData.Name = item.Dialyzer
1349
+		respData.Count = item.Count
1350
+		respData.Percentage = float64(item.Count) / float64(total) * 100
1351
+		respDatas = append(respDatas, respData)
1352
+	}
1193 1353
 	c.ServeSuccessJSON(map[string]interface{}{
1194 1354
 		"data": respDatas,
1195 1355
 	})
@@ -1238,8 +1398,8 @@ func (c *StatisticsApiController) GetDialyzerDetail() {
1238 1398
 }
1239 1399
 func (c *StatisticsApiController) GetDialyzerConfig() {
1240 1400
 	dialyzers, _ := service.GetDialyzerSummary(c.GetAdminUserInfo().CurrentOrgId)
1241
-	//Irrigation, _ := service.GetIrrigationSummary(c.GetAdminUserInfo().CurrentOrgId)
1242
-	//dialyzers = append(dialyzers, Irrigation...)
1401
+	Irrigation, _ := service.GetIrrigationSummary(c.GetAdminUserInfo().CurrentOrgId)
1402
+	dialyzers = append(dialyzers, Irrigation...)
1243 1403
 	c.ServeSuccessJSON(map[string]interface{}{
1244 1404
 		"dialyzers": dialyzers,
1245 1405
 	})
@@ -2011,3 +2171,38 @@ func splitByDay(startDate, endDate time.Time) []time.Time {
2011 2171
 	}
2012 2172
 	return dates
2013 2173
 }
2174
+
2175
+// 排序函数
2176
+func sortFieldsByValue(field map[string]interface{}) map[string]interface{} {
2177
+	// 将键值对分成两个部分,一个有值,一个无值
2178
+	var withValue, withoutValue []string
2179
+
2180
+	for key, value := range field {
2181
+		if key == "日期" || key == "合计" {
2182
+			continue
2183
+		}
2184
+		if value == "" {
2185
+			withoutValue = append(withoutValue, key)
2186
+		} else {
2187
+			withValue = append(withValue, key)
2188
+		}
2189
+	}
2190
+
2191
+	// 对有值和无值部分分别排序
2192
+	sort.Strings(withValue)
2193
+	sort.Strings(withoutValue)
2194
+
2195
+	// 创建一个新的有序map
2196
+	sortedField := make(map[string]interface{})
2197
+	sortedField["日期"] = field["日期"]
2198
+
2199
+	for _, key := range withValue {
2200
+		sortedField[key] = field[key]
2201
+	}
2202
+	for _, key := range withoutValue {
2203
+		sortedField[key] = field[key]
2204
+	}
2205
+
2206
+	sortedField["合计"] = field["合计"]
2207
+	return sortedField
2208
+}

+ 19 - 9
service/statistis_qc_service.go Ver arquivo

@@ -94,7 +94,7 @@ func GetDialysisStats(start int64, end int64, mode int64, org_id int64, time_way
94 94
 	// 构建动态查询语句
95 95
 	if time_way == 1 {
96 96
 		//selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0])}
97
-		selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0]+"~"+strings.Split(endDate, " ")[0])}
97
+		selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0])}
98 98
 
99 99
 	} else if time_way == 3 {
100 100
 		selectClauses = []string{fmt.Sprintf("'%s' AS `日期`", strings.Split(startDate, " ")[0]+"~"+strings.Split(endDate, " ")[0])}
@@ -555,9 +555,10 @@ func GetAnticoagulantData(start_time int64, end_time int64, org_id int64) (map[s
555 555
 	for _, result := range results {
556 556
 		if name, ok := anticoagulantMap[result.Anticoagulant]; ok {
557 557
 			anticoagulantData[name] = result.Count
558
-		} else {
559
-			anticoagulantData[fmt.Sprintf("Unknown (%d)", result.Anticoagulant)] = result.Count
560 558
 		}
559
+		//else {
560
+		//	anticoagulantData[fmt.Sprintf("Unknown (%d)", result.Anticoagulant)] = result.Count
561
+		//}
561 562
 	}
562 563
 
563 564
 	return anticoagulantData, nil
@@ -601,7 +602,7 @@ type DialyzerResult struct {
601 602
 }
602 603
 
603 604
 func GetDialyzerData(start_time int64, end_time int64, org_id int64) (dr []DialyzerResult, err error) {
604
-	err = XTReadDB().Model(&models.DialysisPrescription{}).
605
+	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").
605 606
 		Select("dialysis_dialyszers as dialyzer, COUNT(*) as count").
606 607
 		Where("record_date >= ? and record_date <= ? and user_org_id = ? and status = 1", start_time, end_time, org_id).
607 608
 		Group("dialysis_dialyszers").
@@ -609,6 +610,15 @@ func GetDialyzerData(start_time int64, end_time int64, org_id int64) (dr []Dialy
609 610
 	return
610 611
 }
611 612
 
613
+func GetDialysisIrrigationData(start_time int64, end_time int64, org_id int64) (dr []DialyzerResult, err error) {
614
+	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").
615
+		Select("dialysis_irrigation as dialysis_irrigation, COUNT(*) as count").
616
+		Where("record_date >= ? and record_date <= ? and user_org_id = ? and status = 1", start_time, end_time, org_id).
617
+		Group("dialysis_irrigation").
618
+		Scan(&dr).Error
619
+	return
620
+}
621
+
612 622
 func GetIrrigationData(start_time int64, end_time int64, org_id int64) (dr []DialyzerResult, err error) {
613 623
 	err = XTReadDB().Model(&models.DialysisPrescription{}).
614 624
 		Select("dialysis_irrigation as dialyzer, COUNT(*) as count").
@@ -619,15 +629,15 @@ func GetIrrigationData(start_time int64, end_time int64, org_id int64) (dr []Dia
619 629
 }
620 630
 
621 631
 func GetDialyzerTotal(start_time int64, end_time int64, org_id int64) (total int64, err error) {
622
-	err = XTReadDB().Model(&models.DialysisPrescription{}).
623
-		Where("status = 1 and record_date >= ? and record_date <= ? and user_org_id = ? and dialysis_dialyszers <> ''", start_time, end_time, org_id).
632
+	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").
633
+		Where("status = 1 and record_date >= ? and record_date <= ? and user_org_id = ?", start_time, end_time, org_id).
624 634
 		Count(&total).Error
625 635
 	return
626 636
 }
627 637
 
628 638
 func GetPrescriptionByAnticoagulant(page int64, limit int64, orgid int64, anticoagulant int64, start_time int64, end_time int64) (solution []*models.DialysisPrescription, total int64, err error) {
629 639
 
630
-	db := XTReadDB().Model(&models.DialysisPrescription{}).Preload("QCPatients", "status = 1 and user_org_id = ?", orgid).Where("status = 1")
640
+	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", "status = 1 and user_org_id = ?", orgid).Where("status = 1")
631 641
 	if anticoagulant > 0 {
632 642
 		db = db.Where("anticoagulant = ?", anticoagulant)
633 643
 	}
@@ -644,9 +654,9 @@ func GetPrescriptionByAnticoagulant(page int64, limit int64, orgid int64, antico
644 654
 
645 655
 func GetPrescriptionByDialyzer(page int64, limit int64, orgid int64, dialyzer string, start_time int64, end_time int64) (solution []*models.DialysisPrescription, total int64, err error) {
646 656
 
647
-	db := XTReadDB().Model(&models.DialysisPrescription{}).Preload("QCPatients", "status = 1 and user_org_id = ?", orgid).Where("status = 1")
657
+	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", "status = 1 and user_org_id = ?", orgid).Where("status = 1")
648 658
 	if len(dialyzer) > 0 {
649
-		db = db.Where("dialysis_dialyszers = ?", dialyzer)
659
+		db = db.Where("dialysis_dialyszers = ? or dialysis_irrigation = ?", dialyzer, dialyzer)
650 660
 	}
651 661
 	if orgid > 0 {
652 662
 		db = db.Where("user_org_id = ?", orgid)