Browse Source

提交代码

陈少旭 6 months ago
parent
commit
8212c0cca2

+ 142 - 2
controllers/statistics_api_controller.go View File

@@ -44,6 +44,148 @@ func StatisticsApiRegistRouters() {
44 44
 	beego.Router("/api/commonqc/dialysistreat/finish", &StatisticsApiController{}, "get:GetDialysisTreatFinsh")
45 45
 	beego.Router("/api/commonqc/dialysistreat/detail", &StatisticsApiController{}, "get:GetDialysisTreatDetail")
46 46
 
47
+	beego.Router("/api/commonqc/patinet/weight", &StatisticsApiController{}, "get:GetPatientWeight")
48
+	beego.Router("/api/commonqc/weight/detail", &StatisticsApiController{}, "get:GetPatientWeightDetail")
49
+	beego.Router("/api/commonqc/patinet/bp", &StatisticsApiController{}, "get:GetPatientBP")
50
+	beego.Router("/api/commonqc/bp/detail", &StatisticsApiController{}, "get:GetPatientBPDetail")
51
+
52
+}
53
+func (c *StatisticsApiController) GetPatientWeight() {
54
+	start_time := c.GetString("start_time")
55
+	end_time := c.GetString("end_time")
56
+	statistics_type, _ := c.GetInt("statistics_type")
57
+
58
+	timeLayout := "2006-01-02"
59
+	loc, _ := time.LoadLocation("Local")
60
+	var startTime int64
61
+	if len(start_time) > 0 {
62
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
63
+		fmt.Println("err-----------", err)
64
+		if err != nil {
65
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
66
+			return
67
+		}
68
+		startTime = theTime.Unix()
69
+	}
70
+	var endTime int64
71
+	if len(end_time) > 0 {
72
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
73
+		if err != nil {
74
+			utils.ErrorLog(err.Error())
75
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
76
+			return
77
+		}
78
+		endTime = theTime.Unix()
79
+	}
80
+	item, _ := service.GetNewDialysisWeightChartData(c.GetAdminUserInfo().CurrentOrgId, startTime, endTime, statistics_type)
81
+	c.ServeSuccessJSON(map[string]interface{}{
82
+		"list": item,
83
+	})
84
+}
85
+func (c *StatisticsApiController) GetPatientWeightDetail() {
86
+	start_time := c.GetString("start_time")
87
+	end_time := c.GetString("end_time")
88
+	add_type, _ := c.GetInt64("add_type")
89
+	dry_type, _ := c.GetInt64("dry_type")
90
+	after_type, _ := c.GetInt64("after_type")
91
+	page, _ := c.GetInt64("page")
92
+	limit, _ := c.GetInt64("limit")
93
+
94
+	timeLayout := "2006-01-02"
95
+	loc, _ := time.LoadLocation("Local")
96
+	var startTime int64
97
+	if len(start_time) > 0 {
98
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
99
+		fmt.Println("err-----------", err)
100
+		if err != nil {
101
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
102
+			return
103
+		}
104
+		startTime = theTime.Unix()
105
+	}
106
+	var endTime int64
107
+	if len(end_time) > 0 {
108
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
109
+		if err != nil {
110
+			utils.ErrorLog(err.Error())
111
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
112
+			return
113
+		}
114
+		endTime = theTime.Unix()
115
+	}
116
+	data, total, _ := service.GetNewDialysisWeightDetailTableTen(c.GetAdminUserInfo().CurrentOrgId, startTime, endTime, add_type, dry_type, after_type, page, limit)
117
+	c.ServeSuccessJSON(map[string]interface{}{
118
+		"list":  data,
119
+		"total": total,
120
+	})
121
+}
122
+func (c *StatisticsApiController) GetPatientBP() {
123
+	start_time := c.GetString("start_time")
124
+	end_time := c.GetString("end_time")
125
+	statistics_type, _ := c.GetInt("statistics_type")
126
+
127
+	timeLayout := "2006-01-02"
128
+	loc, _ := time.LoadLocation("Local")
129
+	var startTime int64
130
+	if len(start_time) > 0 {
131
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
132
+		fmt.Println("err-----------", err)
133
+		if err != nil {
134
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
135
+			return
136
+		}
137
+		startTime = theTime.Unix()
138
+	}
139
+	var endTime int64
140
+	if len(end_time) > 0 {
141
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
142
+		if err != nil {
143
+			utils.ErrorLog(err.Error())
144
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
145
+			return
146
+		}
147
+		endTime = theTime.Unix()
148
+	}
149
+	item, _ := service.GetNewDialysisBloodChartData(c.GetAdminUserInfo().CurrentOrgId, startTime, endTime, statistics_type)
150
+	c.ServeSuccessJSON(map[string]interface{}{
151
+		"list": item,
152
+	})
153
+}
154
+func (c *StatisticsApiController) GetPatientBPDetail() {
155
+	start_time := c.GetString("start_time")
156
+	end_time := c.GetString("end_time")
157
+	add_type, _ := c.GetInt64("status_type")
158
+	page, _ := c.GetInt64("page")
159
+	limit, _ := c.GetInt64("limit")
160
+
161
+	timeLayout := "2006-01-02"
162
+	loc, _ := time.LoadLocation("Local")
163
+	var startTime int64
164
+	if len(start_time) > 0 {
165
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", start_time+" 00:00:00", loc)
166
+		fmt.Println("err-----------", err)
167
+		if err != nil {
168
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
169
+			return
170
+		}
171
+		startTime = theTime.Unix()
172
+	}
173
+	var endTime int64
174
+	if len(end_time) > 0 {
175
+		theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", end_time+" 23:59:59", loc)
176
+		if err != nil {
177
+			utils.ErrorLog(err.Error())
178
+			c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
179
+			return
180
+		}
181
+		endTime = theTime.Unix()
182
+	}
183
+	data, total, _ := service.GetNewDialysisBPDetailTableTen(c.GetAdminUserInfo().CurrentOrgId, startTime, endTime, add_type, page, limit)
184
+	c.ServeSuccessJSON(map[string]interface{}{
185
+		"list":  data,
186
+		"total": total,
187
+	})
188
+
47 189
 }
48 190
 
49 191
 func (c *StatisticsApiController) GetDialysisTreatFinsh() {
@@ -118,7 +260,6 @@ func (c *StatisticsApiController) GetDialysisTreatDetail() {
118 260
 		"total": total,
119 261
 	})
120 262
 }
121
-
122 263
 func (c *StatisticsApiController) GetDialysisTotal() {
123 264
 	start_date := c.GetString("start_date")
124 265
 	end_date := c.GetString("end_date")
@@ -1204,7 +1345,6 @@ func (c *StatisticsApiController) GetDialysisTotalDetailInfo() {
1204 1345
 	}
1205 1346
 
1206 1347
 }
1207
-
1208 1348
 func (c *StatisticsApiController) GetAnticoagulant() {
1209 1349
 	start_date := c.GetString("start_date")
1210 1350
 	end_date := c.GetString("end_date")

+ 1 - 4
service/statistics_service/index_evaluation_service.go View File

@@ -365,16 +365,13 @@ func GetDialysisWeightChartData(user_org_id int64, start_time int64, end_time in
365 365
 			if item.Name == "40~50kg" {
366 366
 				isHasConditionTwo = true
367 367
 			}
368
-
369 368
 			if item.Name == "60~70kg" {
370 369
 				isHasConditionThree = true
371 370
 			}
372
-
373 371
 			if item.Name == "大于70kg" {
374 372
 				isHasConditionFour = true
375 373
 			}
376
-
377
-			if item.Name == "未知" {
374
+			if item.Name == "待定/卧床" {
378 375
 				isHasConditionFive = true
379 376
 			}
380 377
 		}

+ 807 - 0
service/statistis_qc_service.go View File

@@ -3,7 +3,10 @@ package service
3 3
 import (
4 4
 	"database/sql"
5 5
 	"fmt"
6
+	"log"
7
+	"math"
6 8
 	"reflect"
9
+	"strconv"
7 10
 	"strings"
8 11
 	"time"
9 12
 
@@ -1232,3 +1235,807 @@ func GetScheduleInfo(start_date string, end_date string, mode int64, org_id int6
1232 1235
 	}).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
1233 1236
 	return
1234 1237
 }
1238
+
1239
+type otherItemAmount struct {
1240
+	Total int64  `json:"total"`
1241
+	Name  string `json:"name"`
1242
+	Ratio string `json:"ratio"`
1243
+}
1244
+
1245
+// xt_patients model
1246
+type Patient struct {
1247
+	ID       uint   `gorm:"primaryKey"`
1248
+	IDCardNo string `gorm:"column:id_card_no"`
1249
+}
1250
+
1251
+// xt_assessment_before_dislysis model
1252
+type Assessment struct {
1253
+	ID          uint      `gorm:"id"`
1254
+	PatientID   uint      `gorm:"column:patent_id"`
1255
+	SystolicBP  int       `gorm:"column:systolic_blood_pressure"`
1256
+	DiastolicBP int       `gorm:"column:diastolic_blood_pressure"`
1257
+	RecordDate  time.Time `gorm:"column:record_date"`
1258
+	UserOrgID   int       `gorm:"column:user_org_id"`
1259
+}
1260
+type WeightDetail struct {
1261
+	DialysisNo      string
1262
+	PatientName     string
1263
+	Gender          int64
1264
+	IDCardNo        string
1265
+	DialysisDate    string
1266
+	DryWeight       float64
1267
+	LastWeightAfter float64
1268
+	WeightBefore    float64
1269
+}
1270
+
1271
+func GetNewDialysisWeightChartData(user_org_id int64, start_time int64, end_time int64, statistics_type int) (item []*otherItemAmount, err error) {
1272
+	db := XTReadDB()
1273
+	var items []*otherItemAmount
1274
+	var tempErr error
1275
+	var Total int64
1276
+	switch statistics_type {
1277
+	case 1:
1278
+		db.Table("xt_assessment_before_dislysis ").Where("user_org_id=? and status=1 and  created_time >= ? and created_time <= ?", user_org_id, start_time, end_time).Count(&Total)
1279
+		tempErr = db.Table("xt_assessment_before_dislysis ").Where("user_org_id=? and status=1 and  created_time >= ? and created_time <= ?", user_org_id, start_time, end_time).
1280
+			Select("CASE WHEN dry_weight < 40 THEN '小于40kg'" +
1281
+				" WHEN dry_weight >= 40 AND dry_weight < 50 THEN '40~50kg'" +
1282
+				" WHEN dry_weight >= 50 AND dry_weight < 60 THEN '50~60kg'" +
1283
+				" WHEN dry_weight >= 60 AND dry_weight < 70 THEN '60~70kg'" +
1284
+				" WHEN dry_weight >= 70 THEN '大于70kg'" +
1285
+				" ELSE '未知' END AS name, COUNT(*) AS total",
1286
+			).Group("name").Scan(&items).Error
1287
+
1288
+		var isHasConditionOne bool = false
1289
+		var isHasConditionTwo bool = false
1290
+		var isHasConditionThree bool = false
1291
+		var isHasConditionFour bool = false
1292
+		var isHasConditionFive bool = false
1293
+
1294
+		for _, item := range items {
1295
+			if item.Name == "小于40kg" {
1296
+				isHasConditionOne = true
1297
+			}
1298
+			if item.Name == "40~50kg" {
1299
+				isHasConditionTwo = true
1300
+			}
1301
+			if item.Name == "60~70kg" {
1302
+				isHasConditionThree = true
1303
+			}
1304
+			if item.Name == "大于70kg" {
1305
+				isHasConditionFour = true
1306
+			}
1307
+			if item.Name == "待定/卧床" {
1308
+				isHasConditionFive = true
1309
+			}
1310
+		}
1311
+		if !isHasConditionOne {
1312
+			newItem := &otherItemAmount{
1313
+				Total: 0,
1314
+				Name:  "小于40kg",
1315
+				Ratio: "0",
1316
+			}
1317
+			items = append(items, newItem)
1318
+		}
1319
+
1320
+		if !isHasConditionTwo {
1321
+			newItem := &otherItemAmount{
1322
+				Total: 0,
1323
+				Name:  "40~50kg",
1324
+				Ratio: "0",
1325
+			}
1326
+			items = append(items, newItem)
1327
+		}
1328
+		if !isHasConditionThree {
1329
+			newItem := &otherItemAmount{
1330
+				Total: 0,
1331
+				Name:  "60~70kg",
1332
+				Ratio: "0",
1333
+			}
1334
+			items = append(items, newItem)
1335
+
1336
+		}
1337
+
1338
+		if !isHasConditionFour {
1339
+			newItem := &otherItemAmount{
1340
+				Total: 0,
1341
+				Name:  "大于70kg",
1342
+				Ratio: "0",
1343
+			}
1344
+			items = append(items, newItem)
1345
+
1346
+		}
1347
+
1348
+		if !isHasConditionFive {
1349
+			newItem := &otherItemAmount{
1350
+				Total: 0,
1351
+				Name:  "未知",
1352
+				Ratio: "0",
1353
+			}
1354
+			items = append(items, newItem)
1355
+		}
1356
+
1357
+		for _, item := range items {
1358
+			if math.IsNaN(float64(item.Total) / float64(Total)) {
1359
+				item.Ratio = "0.0"
1360
+			} else {
1361
+				float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(Total))*100), 64)
1362
+				item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
1363
+			}
1364
+		}
1365
+
1366
+		break
1367
+	case 2:
1368
+
1369
+		//db.Table("xt_assessment_before_dislysis ").Where("user_org_id=? and status=1 and  created_time >= ? and created_time <= ?", user_org_id, start_time, end_time).Count(&Total)
1370
+
1371
+		//db.Raw("Select count(*) from `xt_assessment_before_dislysis`   LEFT JOIN  `xt_assessment_after_dislysis`  On xt_assessment_before_dislysis.`patient_id` = xt_assessment_after_dislysis.`patient_id` AND xt_assessment_before_dislysis.assessment_date = xt_assessment_after_dislysis.assessment_date AND xt_assessment_after_dislysis.weight_after != 0   Where  xt_assessment_before_dislysis.user_org_id = ? AND xt_assessment_before_dislysis.created_time >= ? AND  xt_assessment_before_dislysis.created_time <= ? Order by xt_assessment_before_dislysis.assessment_date desc", user_org_id, start_time, end_time).Count(&Total)
1372
+
1373
+		var newTotal int64
1374
+		type weight struct {
1375
+			WeightAfter  float64 `json:"weight_after"`
1376
+			WeightBefore float64 `json:"weight_before"`
1377
+		}
1378
+		var weights []*weight
1379
+		var weightAdd []float64
1380
+		db.Raw("Select xt_assessment_after_dislysis.weight_after as weight_after ,xt_assessment_before_dislysis.weight_before as weight_before from `xt_assessment_before_dislysis`   LEFT JOIN  `xt_assessment_after_dislysis`  On xt_assessment_before_dislysis.`patient_id` = xt_assessment_after_dislysis.`patient_id` AND xt_assessment_before_dislysis.assessment_date = xt_assessment_after_dislysis.assessment_date AND xt_assessment_after_dislysis.weight_after <> 0 AND xt_assessment_after_dislysis.user_org_id = ?  Where  xt_assessment_before_dislysis.user_org_id = ? AND xt_assessment_before_dislysis.created_time >= ? AND  xt_assessment_before_dislysis.created_time <= ? Order by xt_assessment_before_dislysis.assessment_date desc", user_org_id, user_org_id, start_time, end_time).Scan(&weights)
1381
+		for index, _ := range weights {
1382
+			if index+1 < len(weights) {
1383
+				if weights[index+1].WeightAfter == 0 || weights[index].WeightBefore == 0 {
1384
+					continue
1385
+				}
1386
+				weightAdd = append(weightAdd, ((weights[index].WeightBefore - weights[index+1].WeightAfter) / weights[index+1].WeightAfter))
1387
+			}
1388
+		}
1389
+
1390
+		var total_one int64
1391
+		var total_two int64
1392
+		var total_three int64
1393
+		for _, item := range weightAdd {
1394
+			if item <= 0.03 {
1395
+				total_one++
1396
+
1397
+			}
1398
+
1399
+			if item > 0.03 && item < 0.05 {
1400
+				total_two++
1401
+
1402
+			}
1403
+
1404
+			if item >= 0.05 {
1405
+				total_three++
1406
+
1407
+			}
1408
+
1409
+		}
1410
+
1411
+		newTotal = total_one + total_two + total_three
1412
+
1413
+		newItem := &otherItemAmount{
1414
+			Total: total_one,
1415
+			Name:  "体重增长<=3%",
1416
+			Ratio: "0",
1417
+		}
1418
+		newItem1 := &otherItemAmount{
1419
+			Total: total_two,
1420
+			Name:  "3%<体重增长<5%",
1421
+			Ratio: "0",
1422
+		}
1423
+		newItem2 := &otherItemAmount{
1424
+			Total: total_three,
1425
+			Name:  "体重增长>=5%",
1426
+			Ratio: "0",
1427
+		}
1428
+
1429
+		items = append(items, newItem)
1430
+		items = append(items, newItem1)
1431
+		items = append(items, newItem2)
1432
+		for _, item := range items {
1433
+			if math.IsNaN(float64(item.Total) / float64(newTotal)) {
1434
+				item.Ratio = "0.0"
1435
+			} else {
1436
+				float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(newTotal))*100), 64)
1437
+				item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
1438
+			}
1439
+		}
1440
+
1441
+		break
1442
+	case 3:
1443
+
1444
+		//db.Table("xt_assessment_before_dislysis ").Where("user_org_id=? and status=1 and  created_time >= ? and created_time <= ?", user_org_id, start_time, end_time).Count(&Total)
1445
+
1446
+		//db.Raw("Select count(*) from `xt_assessment_before_dislysis`   LEFT JOIN  `xt_assessment_after_dislysis`  On xt_assessment_before_dislysis.`patient_id` = xt_assessment_after_dislysis.`patient_id` AND xt_assessment_before_dislysis.assessment_date = xt_assessment_after_dislysis.assessment_date AND xt_assessment_after_dislysis.weight_after != 0   Where  xt_assessment_before_dislysis.user_org_id = ? AND xt_assessment_before_dislysis.created_time >= ? AND  xt_assessment_before_dislysis.created_time <= ? Order by xt_assessment_before_dislysis.assessment_date desc", user_org_id, start_time, end_time).Count(&Total)
1447
+
1448
+		var newTotal int64
1449
+		type weight struct {
1450
+			DryWeight    float64 `json:"dry_weight"`
1451
+			WeightBefore float64 `json:"weight_before"`
1452
+		}
1453
+		var weights []*weight
1454
+		var weightAdd []float64
1455
+		db.Raw("Select xt_assessment_before_dislysis.dry_weight as dry_weight ,xt_assessment_before_dislysis.weight_before as weight_before from `xt_assessment_before_dislysis`  Where  xt_assessment_before_dislysis.user_org_id = ? AND xt_assessment_before_dislysis.created_time >= ? AND  xt_assessment_before_dislysis.created_time <= ? Order by xt_assessment_before_dislysis.assessment_date desc", user_org_id, user_org_id, start_time, end_time).Scan(&weights)
1456
+		for index, _ := range weights {
1457
+			weightAdd = append(weightAdd, ((weights[index].WeightBefore - weights[index].DryWeight) / weights[index].DryWeight))
1458
+		}
1459
+
1460
+		var total_one int64
1461
+		var total_two int64
1462
+		var total_three int64
1463
+		for _, item := range weightAdd {
1464
+			if item <= 0.03 {
1465
+				total_one++
1466
+
1467
+			}
1468
+
1469
+			if item > 0.03 && item < 0.05 {
1470
+				total_two++
1471
+
1472
+			}
1473
+
1474
+			if item >= 0.05 {
1475
+				total_three++
1476
+
1477
+			}
1478
+
1479
+		}
1480
+
1481
+		newTotal = total_one + total_two + total_three
1482
+
1483
+		newItem := &otherItemAmount{
1484
+			Total: total_one,
1485
+			Name:  "体重增长<=3%",
1486
+			Ratio: "0",
1487
+		}
1488
+		newItem1 := &otherItemAmount{
1489
+			Total: total_two,
1490
+			Name:  "3%<体重增长<5%",
1491
+			Ratio: "0",
1492
+		}
1493
+		newItem2 := &otherItemAmount{
1494
+			Total: total_three,
1495
+			Name:  "体重增长>=5%",
1496
+			Ratio: "0",
1497
+		}
1498
+
1499
+		items = append(items, newItem)
1500
+		items = append(items, newItem1)
1501
+		items = append(items, newItem2)
1502
+		for _, item := range items {
1503
+			if math.IsNaN(float64(item.Total) / float64(newTotal)) {
1504
+				item.Ratio = "0.0"
1505
+			} else {
1506
+				float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(newTotal))*100), 64)
1507
+				item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
1508
+			}
1509
+		}
1510
+
1511
+		break
1512
+	case 4:
1513
+		var newTotal int64
1514
+		type weight struct {
1515
+			WeightAfter float64 `json:"weight_after"`
1516
+			DryWeight   float64 `json:"dry_weight"`
1517
+		}
1518
+		var weights []*weight
1519
+		//var weightAdd []float64
1520
+		db.Raw("Select xt_assessment_after_dislysis.weight_after as weight_after ,xt_assessment_before_dislysis.dry_weight as dry_weight from `xt_assessment_before_dislysis`   LEFT JOIN  `xt_assessment_after_dislysis`  On xt_assessment_before_dislysis.`patient_id` = xt_assessment_after_dislysis.`patient_id` AND xt_assessment_before_dislysis.assessment_date = xt_assessment_after_dislysis.assessment_date AND xt_assessment_after_dislysis.weight_after <> 0 AND xt_assessment_after_dislysis.user_org_id = ?  Where  xt_assessment_before_dislysis.user_org_id = ? AND xt_assessment_before_dislysis.created_time >= ? AND  xt_assessment_before_dislysis.created_time <= ? Order by xt_assessment_before_dislysis.assessment_date desc", user_org_id, user_org_id, start_time, end_time).Scan(&weights)
1521
+
1522
+		var total_one int64
1523
+		var total_two int64
1524
+		var total_three int64
1525
+		for _, item := range weights {
1526
+			if item.WeightAfter != 0 && item.DryWeight != 0 {
1527
+				if item.WeightAfter-item.DryWeight == 0.3 || item.WeightAfter-item.DryWeight == -0.3 {
1528
+					total_one++
1529
+
1530
+				}
1531
+
1532
+				if item.WeightAfter-item.DryWeight > 0.3 && item.WeightAfter-item.DryWeight < -0.3 {
1533
+					total_two++
1534
+
1535
+				}
1536
+			}
1537
+
1538
+			if item.WeightAfter == 0 {
1539
+				total_three++
1540
+			}
1541
+
1542
+		}
1543
+
1544
+		newTotal = total_one + total_two + total_three
1545
+
1546
+		newItem := &otherItemAmount{
1547
+			Total: total_one,
1548
+			Name:  "达到干体重",
1549
+			Ratio: "0",
1550
+		}
1551
+		newItem1 := &otherItemAmount{
1552
+			Total: total_two,
1553
+			Name:  "未达到干体重",
1554
+			Ratio: "0",
1555
+		}
1556
+		newItem2 := &otherItemAmount{
1557
+			Total: total_three,
1558
+			Name:  "其他或卧床",
1559
+			Ratio: "0",
1560
+		}
1561
+
1562
+		items = append(items, newItem)
1563
+		items = append(items, newItem1)
1564
+		items = append(items, newItem2)
1565
+		for _, item := range items {
1566
+			if math.IsNaN(float64(item.Total) / float64(newTotal)) {
1567
+				item.Ratio = "0.0"
1568
+			} else {
1569
+				float_value, _ := strconv.ParseFloat(fmt.Sprintf("%.1f", (float64(item.Total)/float64(newTotal))*100), 64)
1570
+				item.Ratio = strconv.FormatFloat(float_value, 'f', 1, 32)
1571
+			}
1572
+		}
1573
+
1574
+		break
1575
+	}
1576
+	if tempErr != nil {
1577
+		return nil, err
1578
+	}
1579
+
1580
+	fmt.Println(items)
1581
+	return items, nil
1582
+}
1583
+func GetNewDialysisBloodChartData(user_org_id int64, start_time int64, end_time int64, statistics_type int) (item []otherItemAmount, err error) {
1584
+	db := XTReadDB()
1585
+	switch statistics_type {
1586
+	case 1:
1587
+		var assessments []Assessment
1588
+		err = db.Joins("JOIN xt_patients ON xt_patients.id = xt_assessment_before_dislysis.patent_id").
1589
+			Where("record_date >= ? AND record_date <= ? AND user_org_id = ?", start_time, end_time, user_org_id).
1590
+			Find(&assessments).Error
1591
+		if err != nil {
1592
+			log.Fatal("Query execution error:", err)
1593
+		}
1594
+
1595
+		var total, metStandard, unmetStandard int
1596
+
1597
+		// Iterate through the result set
1598
+		for _, assessment := range assessments {
1599
+			// Fetch patient information
1600
+			var patient Patient
1601
+			err = db.First(&patient, assessment.PatientID).Error
1602
+			if err != nil {
1603
+				log.Printf("Skipping record due to missing patient data for ID: %v", assessment.PatientID)
1604
+				continue
1605
+			}
1606
+
1607
+			// Calculate the patient's age
1608
+			age, err := calculateAge(patient.IDCardNo)
1609
+			if err != nil {
1610
+				log.Printf("Skipping record due to invalid ID card number: %v", patient.IDCardNo)
1611
+				continue
1612
+			}
1613
+
1614
+			// Update the total count of patients
1615
+			total++
1616
+
1617
+			// Determine if the patient meets the standard based on their age and blood pressure
1618
+			if (age < 60 && assessment.SystolicBP < 140 && assessment.DiastolicBP < 90) || (age >= 60 && assessment.SystolicBP < 160 && assessment.DiastolicBP < 90) {
1619
+				metStandard++
1620
+			} else {
1621
+				unmetStandard++
1622
+			}
1623
+		}
1624
+
1625
+		// Calculate and print the results
1626
+		if total > 0 {
1627
+			metPercentage := float64(metStandard) / float64(total) * 100
1628
+			unmetPercentage := float64(unmetStandard) / float64(total) * 100
1629
+			fmt.Printf("Total patients: %d\n", total)
1630
+			fmt.Printf("Met standard: %d (%.2f%%)\n", metStandard, metPercentage)
1631
+			fmt.Printf("Unmet standard: %d (%.2f%%)\n", unmetStandard, unmetPercentage)
1632
+
1633
+			var items otherItemAmount
1634
+			items.Name = "达标患者"
1635
+			items.Ratio = strconv.FormatFloat(metPercentage, 'f', 1, 32)
1636
+			items.Total = int64(metStandard)
1637
+			item = append(item, items)
1638
+
1639
+			var items2 otherItemAmount
1640
+			items2.Name = "未达标患者"
1641
+			items2.Ratio = strconv.FormatFloat(unmetPercentage, 'f', 1, 32)
1642
+			items2.Total = int64(unmetStandard)
1643
+			item = append(item, items2)
1644
+
1645
+		}
1646
+	case 2:
1647
+		var assessments []Assessment
1648
+		err = db.Joins("JOIN xt_patients ON xt_patients.id = xt_assessment_after_dislysis.patent_id").
1649
+			Where("record_date >= ? AND record_date <= ? AND user_org_id = ?", start_time, end_time, user_org_id).
1650
+			Find(&assessments).Error
1651
+		if err != nil {
1652
+			log.Fatal("Query execution error:", err)
1653
+		}
1654
+
1655
+		var total, metStandard, unmetStandard int
1656
+
1657
+		// Iterate through the result set
1658
+		for _, assessment := range assessments {
1659
+			// Fetch patient information
1660
+			var patient Patient
1661
+			err = db.First(&patient, assessment.PatientID).Error
1662
+			if err != nil {
1663
+				log.Printf("Skipping record due to missing patient data for ID: %v", assessment.PatientID)
1664
+				continue
1665
+			}
1666
+
1667
+			// Calculate the patient's age
1668
+			age, err := calculateAge(patient.IDCardNo)
1669
+			if err != nil {
1670
+				log.Printf("Skipping record due to invalid ID card number: %v", patient.IDCardNo)
1671
+				continue
1672
+			}
1673
+
1674
+			// Update the total count of patients
1675
+			total++
1676
+
1677
+			// Determine if the patient meets the standard based on their age and blood pressure
1678
+			if (age < 60 && assessment.SystolicBP < 140 && assessment.DiastolicBP < 90) || (age >= 60 && assessment.SystolicBP < 160 && assessment.DiastolicBP < 90) {
1679
+				metStandard++
1680
+			} else {
1681
+				unmetStandard++
1682
+			}
1683
+		}
1684
+
1685
+		// Calculate and print the results
1686
+		if total > 0 {
1687
+			metPercentage := float64(metStandard) / float64(total) * 100
1688
+			unmetPercentage := float64(unmetStandard) / float64(total) * 100
1689
+			fmt.Printf("Total patients: %d\n", total)
1690
+			fmt.Printf("Met standard: %d (%.2f%%)\n", metStandard, metPercentage)
1691
+			fmt.Printf("Unmet standard: %d (%.2f%%)\n", unmetStandard, unmetPercentage)
1692
+
1693
+			var items otherItemAmount
1694
+			items.Name = "达标患者"
1695
+			items.Ratio = strconv.FormatFloat(metPercentage, 'f', 1, 32)
1696
+			items.Total = int64(metStandard)
1697
+			item = append(item, items)
1698
+
1699
+			var items2 otherItemAmount
1700
+			items2.Name = "未达标患者"
1701
+			items2.Ratio = strconv.FormatFloat(unmetPercentage, 'f', 1, 32)
1702
+			items2.Total = int64(unmetStandard)
1703
+			item = append(item, items2)
1704
+		}
1705
+
1706
+		break
1707
+	}
1708
+	return
1709
+}
1710
+func calculateAge(idCardNo string) (int, error) {
1711
+	if len(idCardNo) < 14 {
1712
+		return 0, fmt.Errorf("invalid ID card number")
1713
+	}
1714
+
1715
+	// Extract birth year, month, and day from ID card number
1716
+	year := idCardNo[6:10]
1717
+	month := idCardNo[10:12]
1718
+	day := idCardNo[12:14]
1719
+
1720
+	// Parse the birth date
1721
+	birthDate, err := time.Parse("20060102", year+month+day)
1722
+	if err != nil {
1723
+		return 0, err
1724
+	}
1725
+
1726
+	// Calculate age
1727
+	now := time.Now()
1728
+	age := now.Year() - birthDate.Year()
1729
+	if now.YearDay() < birthDate.YearDay() {
1730
+		age--
1731
+	}
1732
+
1733
+	return age, nil
1734
+}
1735
+
1736
+// 定义结果结构体
1737
+type DialysisDataTwo struct {
1738
+	DialysisNo        string   `json:"dialysis_no"`
1739
+	PatientName       string   `json:"patient_name"`
1740
+	Gender            string   `json:"gender"`
1741
+	Age               int      `json:"age"`
1742
+	AssessmentDate    string   `json:"assessment_date"` // 使用 string 存储日期,或使用 time.Time
1743
+	DryWeight         float64  `json:"dry_weight"`
1744
+	WeightBefore      float64  `json:"weight_before"`
1745
+	WeightAfter       float64  `json:"weight_after"`
1746
+	LastWeightAfter   *float64 `json:"last_weight_after"` // 使用指针处理可能为空的值
1747
+	WeightAdd         *float64 `json:"weight_add"`        // 使用指针处理可能为空的值
1748
+	DryWeightCategory string   `json:"dry_weight_category"`
1749
+	WeightStatus      string   `json:"weight_status"`
1750
+}
1751
+
1752
+func GetNewDialysisWeightDetailTableTen(user_org_id int64, start_time int64, end_time int64, addType, dryType, afterType, page, limit int64) ([]DialysisDataTwo, int64, error) {
1753
+	offset := (page - 1) * limit
1754
+	var dialysisData []DialysisDataTwo
1755
+	var total int64
1756
+	// 构建基本查询
1757
+	query := readDb.Table("xt_assessment_before_dislysis b").
1758
+		Select(`
1759
+			p.dialysis_no AS dialysis_no, 
1760
+			p.name AS patient_name, 
1761
+			p.gender AS gender, 
1762
+			FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
1763
+			b.assessment_date AS assessment_date, 
1764
+			b.dry_weight AS dry_weight, 
1765
+			b.weight_before AS weight_before, 
1766
+			a.weight_after AS weight_after,
1767
+			(
1768
+				SELECT a2.weight_after 
1769
+				FROM xt_assessment_after_dislysis a2
1770
+				JOIN xt_assessment_before_dislysis b2 
1771
+				ON a2.patient_id = b2.patient_id 
1772
+				AND a2.assessment_date = b2.assessment_date
1773
+				WHERE b2.patient_id = b.patient_id 
1774
+				AND b2.assessment_date < b.assessment_date
1775
+				ORDER BY b2.assessment_date DESC LIMIT 1
1776
+			) AS last_weight_after,
1777
+			CASE 
1778
+				WHEN (
1779
+					SELECT a2.weight_after 
1780
+					FROM xt_assessment_after_dislysis a2
1781
+					JOIN xt_assessment_before_dislysis b2 
1782
+					ON a2.patient_id = b2.patient_id 
1783
+					AND a2.assessment_date = b2.assessment_date
1784
+					WHERE b2.patient_id = b.patient_id 
1785
+					AND b2.assessment_date < b.assessment_date
1786
+					ORDER BY b2.assessment_date DESC LIMIT 1
1787
+				) IS NOT NULL
1788
+				THEN (b.weight_before - (
1789
+					SELECT a2.weight_after 
1790
+					FROM xt_assessment_after_dislysis a2
1791
+					JOIN xt_assessment_before_dislysis b2 
1792
+					ON a2.patient_id = b2.patient_id 
1793
+					AND a2.assessment_date = b2.assessment_date
1794
+					WHERE b2.patient_id = b.patient_id 
1795
+					AND b2.assessment_date < b.assessment_date
1796
+					ORDER BY b2.assessment_date DESC LIMIT 1
1797
+				)) / (
1798
+					SELECT a2.weight_after 
1799
+					FROM xt_assessment_after_dislysis a2
1800
+					JOIN xt_assessment_before_dislysis b2 
1801
+					ON a2.patient_id = b2.patient_id 
1802
+					AND a2.assessment_date = b2.assessment_date
1803
+					WHERE b2.patient_id = b.patient_id 
1804
+					AND b2.assessment_date < b.assessment_date
1805
+					ORDER BY b2.assessment_date DESC LIMIT 1
1806
+				)
1807
+				ELSE NULL
1808
+			END AS weight_add,
1809
+			CASE 
1810
+				WHEN b.dry_weight IS NOT NULL THEN 
1811
+					CASE 
1812
+						WHEN b.dry_weight < 40 THEN '小于40'
1813
+						WHEN b.dry_weight BETWEEN 40 AND 50 THEN '40~50'
1814
+						WHEN b.dry_weight BETWEEN 50 AND 60 THEN '50~60'
1815
+						WHEN b.dry_weight BETWEEN 60 AND 70 THEN '60~70'
1816
+						WHEN b.dry_weight > 70 THEN '大于70'
1817
+					END
1818
+				ELSE '其他'
1819
+			END AS dry_weight_category,
1820
+			CASE 
1821
+				WHEN b.dry_weight IS NOT NULL THEN 
1822
+					CASE 
1823
+						WHEN ABS(a.weight_after - b.dry_weight) <= 0.3 THEN '达标'
1824
+						ELSE '不达标'
1825
+					END
1826
+				ELSE '其他'
1827
+			END AS weight_status
1828
+		`).
1829
+		Joins("JOIN xt_patients p ON p.id = b.patient_id").
1830
+		Joins("JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date AND a.status = 1").
1831
+		Where("b.user_org_id = ?", user_org_id).
1832
+		Where("b.assessment_date >= ?", start_time).
1833
+		Where("b.assessment_date <= ?", end_time).
1834
+		Where("b.status = ?", 1)
1835
+	// 根据传入的 add_type 值动态构建条件
1836
+	addTypeCondition(query, addType)
1837
+	// 根据传入的 dry_type 值动态构建条件
1838
+	dryTypeCondition(query, dryType)
1839
+	// 根据传入的 after_type 值动态构建条件
1840
+	afterTypeCondition(query, afterType)
1841
+	// 获取总记录数
1842
+	if err := query.Count(&total).Error; err != nil {
1843
+		return nil, 0, fmt.Errorf("count query failed: %v", err)
1844
+	}
1845
+	// 获取分页数据
1846
+	rows, err := query.Order("p.id, b.assessment_date").Offset(int(offset)).Limit(int(limit)).Rows()
1847
+	if err != nil {
1848
+		return nil, 0, fmt.Errorf("pagination query failed: %v", err)
1849
+	}
1850
+	defer rows.Close()
1851
+	// 处理结果集
1852
+	for rows.Next() {
1853
+		var data DialysisDataTwo
1854
+		if err := rows.Scan(
1855
+			&data.DialysisNo,
1856
+			&data.PatientName,
1857
+			&data.Gender,
1858
+			&data.Age,
1859
+			&data.AssessmentDate,
1860
+			&data.DryWeight,
1861
+			&data.WeightBefore,
1862
+			&data.WeightAfter,
1863
+			&data.LastWeightAfter,
1864
+			&data.WeightAdd,
1865
+			&data.DryWeightCategory,
1866
+			&data.WeightStatus,
1867
+		); err != nil {
1868
+			return nil, 0, fmt.Errorf("row scan failed: %v", err)
1869
+		}
1870
+		// 将每一行结果追加到切片中
1871
+		dialysisData = append(dialysisData, data)
1872
+	}
1873
+	return dialysisData, total, nil
1874
+}
1875
+
1876
+// addType 条件处理
1877
+func addTypeCondition(query *gorm.DB, addType int64) {
1878
+	switch addType {
1879
+	case 1:
1880
+		query.Where("weight_add <= 0.03")
1881
+	case 2:
1882
+		query.Where("weight_add > 0.03 AND weight_add < 0.05")
1883
+	case 3:
1884
+		query.Where("weight_add >= 0.05")
1885
+	case 4:
1886
+		query.Where("weight_add IS NULL")
1887
+	}
1888
+}
1889
+
1890
+// dryType 条件处理
1891
+func dryTypeCondition(query *gorm.DB, dryType int64) {
1892
+	switch dryType {
1893
+	case 1:
1894
+		query.Where("dry_weight_category = '小于40'")
1895
+	case 2:
1896
+		query.Where("dry_weight_category = '40~50'")
1897
+	case 3:
1898
+		query.Where("dry_weight_category = '50~60'")
1899
+	case 4:
1900
+		query.Where("dry_weight_category = '60~70'")
1901
+	case 5:
1902
+		query.Where("dry_weight_category = '大于70'")
1903
+	}
1904
+}
1905
+
1906
+// afterType 条件处理
1907
+func afterTypeCondition(query *gorm.DB, afterType int64) {
1908
+	switch afterType {
1909
+	case 1:
1910
+		query.Where("weight_status = '达标'")
1911
+	case 2:
1912
+		query.Where("weight_status = '不达标'")
1913
+	case 3:
1914
+		query.Where("weight_status = '其他'")
1915
+	}
1916
+}
1917
+
1918
+type DialysisDataThree struct {
1919
+	DialysisNo          string  `json:"透析号"`
1920
+	PatientName         string  `json:"患者姓名"`
1921
+	Gender              string  `json:"性别"`
1922
+	Age                 int     `json:"年龄"`
1923
+	AssessmentDate      string  `json:"透析日期"`
1924
+	DryWeight           float64 `json:"干体重"`
1925
+	PreDialysisBP       string  `json:"透前血压"`
1926
+	PostDialysisBP      string  `json:"透后血压"`
1927
+	MonitoringBP        string  `json:"监测记录血压"`
1928
+	UltrafiltrationRate float64 `json:"超滤率"`
1929
+	UltrafiltrationVol  float64 `json:"超滤总量"`
1930
+	BPStatus            string  `json:"血压达标状态"`
1931
+}
1932
+
1933
+func GetNewDialysisBPDetailTableTen(user_org_id int64, start_time int64, end_time int64, addType, page, limit int64) ([]DialysisDataThree, int64, error) {
1934
+	offset := (page - 1) * limit
1935
+	var total int64
1936
+	var results []DialysisDataThree
1937
+	// SQL 查询
1938
+	query := `
1939
+		SELECT
1940
+			p.dialysis_no AS 透析号,
1941
+			p.name AS 患者姓名,
1942
+			p.gender AS 性别,
1943
+			FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS 年龄,
1944
+			b.assessment_date AS 透析日期,
1945
+			b.dry_weight AS 干体重,
1946
+			CONCAT(b.systolic_blood_pressure, '/', b.diastolic_blood_pressure) AS 透前血压,
1947
+			CONCAT(a.systolic_blood_pressure, '/', a.diastolic_blood_pressure) AS 透后血压,
1948
+			GROUP_CONCAT(CONCAT(m.systolic_blood_pressure, '/', m.diastolic_blood_pressure) ORDER BY m.monitoring_date ASC SEPARATOR ',') AS 监测记录血压,
1949
+			(SELECT mr.ultrafiltration_rate
1950
+			 FROM xt_monitoring_record mr
1951
+			 WHERE mr.patient_id = p.id AND mr.monitoring_date = b.assessment_date
1952
+			 ORDER BY mr.operate_time DESC
1953
+			 LIMIT 1) AS 超滤率,
1954
+			(SELECT mr.ultrafiltration_volume
1955
+			 FROM xt_monitoring_record mr
1956
+			 WHERE mr.patient_id = p.id AND mr.monitoring_date = b.assessment_date
1957
+			 ORDER BY mr.operate_time DESC
1958
+			 LIMIT 1) AS 超滤总量,
1959
+			CASE 
1960
+				WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
1961
+					CASE 
1962
+						WHEN b.systolic_blood_pressure < 140 OR b.diastolic_blood_pressure < 90 THEN '达标'
1963
+						ELSE '不达标'
1964
+					END
1965
+				WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
1966
+					CASE 
1967
+						WHEN b.systolic_blood_pressure < 160 OR b.diastolic_blood_pressure < 90 THEN '达标'
1968
+						ELSE '不达标'
1969
+					END
1970
+			END AS 血压达标状态
1971
+		FROM
1972
+			xt_patients p
1973
+		JOIN
1974
+			xt_assessment_before_dislysis b ON p.id = b.patient_id
1975
+		JOIN
1976
+			xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date
1977
+		JOIN
1978
+			xt_monitoring_record m ON p.id = m.patient_id AND m.monitoring_date = b.assessment_date
1979
+		WHERE
1980
+			b.user_org_id = ?
1981
+			AND b.assessment_date >= ?
1982
+			AND b.assessment_date <= ?
1983
+			AND (
1984
+				(? = 1 AND CASE 
1985
+					WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
1986
+						CASE 
1987
+							WHEN b.systolic_blood_pressure < 140 OR b.diastolic_blood_pressure < 90 THEN 1
1988
+							ELSE 0
1989
+						END
1990
+					WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
1991
+						CASE 
1992
+							WHEN b.systolic_blood_pressure < 160 OR b.diastolic_blood_pressure < 90 THEN 1
1993
+							ELSE 0
1994
+						END
1995
+				END = 1)
1996
+				OR
1997
+				(? = 2 AND CASE 
1998
+					WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
1999
+						CASE 
2000
+							WHEN b.systolic_blood_pressure >= 140 AND b.diastolic_blood_pressure >= 90 THEN 1
2001
+							ELSE 0
2002
+						END
2003
+					WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
2004
+						CASE 
2005
+							WHEN b.systolic_blood_pressure >= 160 AND b.diastolic_blood_pressure >= 90 THEN 1
2006
+							ELSE 0
2007
+						END
2008
+				END = 1)
2009
+			)
2010
+		GROUP BY
2011
+			p.id, b.assessment_date
2012
+		ORDER BY
2013
+			p.id, b.assessment_date
2014
+		LIMIT ? OFFSET ?;
2015
+	`
2016
+
2017
+	// 计算总数
2018
+	countQuery := `
2019
+		SELECT COUNT(*)
2020
+		FROM
2021
+			xt_patients p
2022
+		JOIN
2023
+			xt_assessment_before_dislysis b ON p.id = b.patient_id
2024
+		WHERE
2025
+			b.user_org_id = ?
2026
+			AND b.assessment_date >= ?
2027
+			AND b.assessment_date <= ?
2028
+	`
2029
+
2030
+	// 执行统计查询
2031
+	if err := readDb.Raw(countQuery, user_org_id, start_time, end_time).Scan(&total).Error; err != nil {
2032
+		return nil, 0, fmt.Errorf("error calculating total count: %v", err)
2033
+	}
2034
+
2035
+	// 执行数据查询
2036
+	if err := readDb.Raw(query, user_org_id, start_time, end_time, addType, addType, limit, offset).Scan(&results).Error; err != nil {
2037
+		return nil, 0, fmt.Errorf("error executing query: %v", err)
2038
+	}
2039
+
2040
+	return results, total, nil
2041
+}