浏览代码

Merge branch '20230223_xt_api_new_branch' of http://git.shengws.com/csx/XT_New into 20230223_xt_api_new_branch

28169 2 个月前
父节点
当前提交
1d1fc71e04
共有 1 个文件被更改,包括 78 次插入99 次删除
  1. 78 99
      service/statistis_qc_service.go

+ 78 - 99
service/statistis_qc_service.go 查看文件

@@ -2087,18 +2087,19 @@ func afterTypeCondition(query *gorm.DB, afterType int64) {
2087 2087
 }
2088 2088
 
2089 2089
 type DialysisDataThree struct {
2090
-	DialysisNo          string  `json:"透析号"`
2091
-	PatientName         string  `json:"患者姓名"`
2092
-	Gender              string  `json:"性别"`
2093
-	Age                 int     `json:"年龄"`
2094
-	AssessmentDate      string  `json:"透析日期"`
2095
-	DryWeight           float64 `json:"干体重"`
2096
-	PreDialysisBP       string  `json:"透前血压"`
2097
-	PostDialysisBP      string  `json:"透后血压"`
2098
-	MonitoringBP        string  `json:"监测记录血压"`
2099
-	UltrafiltrationRate float64 `json:"超滤率"`
2100
-	UltrafiltrationVol  float64 `json:"超滤总量"`
2101
-	BPStatus            string  `json:"血压达标状态"`
2090
+	DialysisNo            string  `json:"dialysis_no"`
2091
+	PatientName           string  `json:"patient_name"`
2092
+	Gender                string  `json:"gender"`
2093
+	Age                   int     `json:"age"`
2094
+	AssessmentDate        string  `json:"assessment_date"`
2095
+	DryWeight             float64 `json:"dry_weight"`
2096
+	PreDialysisBP         string  `json:"pre_dialysis_bp"`
2097
+	PostDialysisBP        string  `json:"post_dialysis_bp"`
2098
+	MonitoringBP          string  `json:"monitoring_bp"`
2099
+	UltrafiltrationRate   float64 `json:"ultrafiltration_rate"`
2100
+	UltrafiltrationVol    float64 `json:"ultrafiltration_vol"`
2101
+	BPStatus              string  `json:"bp_status"`
2102
+	ActualUltrafiltration float64 `json:"actual_ultrafiltration"`
2102 2103
 }
2103 2104
 
2104 2105
 //	func GetNewDialysisBPDetailTableTen(user_org_id int64, start_time int64, end_time int64, addType, page, limit int64) ([]DialysisDataThree, int64, error) {
@@ -2217,31 +2218,36 @@ func GetNewDialysisBPDetailTableTen(
2217 2218
 	addType, page, limit int64,
2218 2219
 ) ([]DialysisDataThree, int64, error) {
2219 2220
 	offset := (page - 1) * limit
2220
-	var total int64
2221 2221
 	var results []DialysisDataThree
2222
+	var total int64
2222 2223
 
2223
-	// SQL 查询
2224
-	query := `
2225
-        SELECT
2226
-            p.dialysis_no AS DialysisNo,
2227
-            p.name AS PatientName,
2228
-            p.gender AS Gender,
2229
-            FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS Age,
2230
-            b.assessment_date AS AssessmentDate,
2231
-            b.dry_weight AS DryWeight,
2232
-            CONCAT(b.systolic_blood_pressure, '/', b.diastolic_blood_pressure) AS PreDialysisBP,
2233
-            CONCAT(a.systolic_blood_pressure, '/', a.diastolic_blood_pressure) AS PostDialysisBP,
2234
-            GROUP_CONCAT(CONCAT(m.systolic_blood_pressure, '/', m.diastolic_blood_pressure) ORDER BY m.monitoring_date ASC SEPARATOR ',') AS MonitoringBP,
2235
-            (SELECT mr.ultrafiltration_rate
2236
-             FROM xt_monitoring_record mr
2237
-             WHERE mr.patient_id = p.id AND mr.monitoring_date = b.assessment_date
2238
-             ORDER BY mr.operate_time DESC
2239
-             LIMIT 1) AS UltrafiltrationRate,
2240
-            (SELECT mr.ultrafiltration_volume
2241
-             FROM xt_monitoring_record mr
2242
-             WHERE mr.patient_id = p.id AND mr.monitoring_date = b.assessment_date
2243
-             ORDER BY mr.operate_time DESC
2244
-             LIMIT 1) AS UltrafiltrationVol,
2224
+	// 构建基本查询
2225
+	query := readDb.Table("xt_assessment_before_dislysis b").
2226
+		Select(`
2227
+            p.dialysis_no AS dialysis_no,
2228
+			a.actual_ultrafiltration as actual_ultrafiltration,
2229
+            p.name AS patient_name,
2230
+            p.gender AS gender,
2231
+            FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) AS age,
2232
+            b.assessment_date AS assessment_date,
2233
+            b.dry_weight AS dry_weight,
2234
+            CONCAT(b.systolic_blood_pressure, '/', b.diastolic_blood_pressure) AS pre_dialysis_bp,
2235
+            CONCAT(a.systolic_blood_pressure, '/', a.diastolic_blood_pressure) AS post_dialysis_bp,
2236
+            GROUP_CONCAT(CONCAT(m.systolic_blood_pressure, '/', m.diastolic_blood_pressure) ORDER BY m.monitoring_date ASC SEPARATOR ',') AS monitoring_bp,
2237
+            (
2238
+                SELECT mr.ultrafiltration_rate
2239
+                FROM xt_monitoring_record mr
2240
+                WHERE mr.patient_id = p.id 
2241
+                AND mr.monitoring_date = b.assessment_date
2242
+                ORDER BY mr.operate_time DESC LIMIT 1
2243
+            ) AS ultrafiltration_rate,
2244
+            (
2245
+                SELECT mr.ultrafiltration_volume
2246
+                FROM xt_monitoring_record mr
2247
+                WHERE mr.patient_id = p.id 
2248
+                AND mr.monitoring_date = b.assessment_date
2249
+                ORDER BY mr.operate_time DESC LIMIT 1
2250
+            ) AS ultrafiltration_vol,
2245 2251
             CASE 
2246 2252
                 WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
2247 2253
                     CASE 
@@ -2253,74 +2259,47 @@ func GetNewDialysisBPDetailTableTen(
2253 2259
                         WHEN b.systolic_blood_pressure < 160 OR b.diastolic_blood_pressure < 90 THEN '达标'
2254 2260
                         ELSE '不达标'
2255 2261
                     END
2256
-            END AS BPStatus
2257
-        FROM
2258
-            xt_patients p
2259
-        JOIN
2260
-            xt_assessment_before_dislysis b ON p.id = b.patient_id
2261
-        JOIN
2262
-            xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date
2263
-        JOIN
2264
-            xt_monitoring_record m ON p.id = m.patient_id AND m.monitoring_date = b.assessment_date
2265
-        WHERE
2266
-            b.user_org_id = ?
2267
-            AND b.assessment_date >= ?
2268
-            AND b.assessment_date <= ?
2269
-            AND (
2270
-                (? = 1 AND CASE 
2271
-                    WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
2272
-                        CASE 
2273
-                            WHEN b.systolic_blood_pressure < 140 OR b.diastolic_blood_pressure < 90 THEN 1
2274
-                            ELSE 0
2275
-                        END
2276
-                    WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
2277
-                        CASE 
2278
-                            WHEN b.systolic_blood_pressure < 160 OR b.diastolic_blood_pressure < 90 THEN 1
2279
-                            ELSE 0
2280
-                        END
2281
-                END = 1)
2282
-                OR
2283
-                (? = 2 AND CASE 
2284
-                    WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
2285
-                        CASE 
2286
-                            WHEN b.systolic_blood_pressure >= 140 AND b.diastolic_blood_pressure >= 90 THEN 1
2287
-                            ELSE 0
2288
-                        END
2289
-                    WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
2290
-                        CASE 
2291
-                            WHEN b.systolic_blood_pressure >= 160 AND b.diastolic_blood_pressure >= 90 THEN 1
2292
-                            ELSE 0
2293
-                        END
2294
-                END = 1)
2295
-            )
2296
-        GROUP BY
2297
-            p.id, b.assessment_date
2298
-        ORDER BY
2299
-            p.id, b.assessment_date
2300
-        LIMIT ? OFFSET ?;
2301
-    `
2262
+            END AS bp_status
2263
+        `).
2264
+		Joins("JOIN xt_patients p ON p.id = b.patient_id").
2265
+		Joins("JOIN xt_assessment_after_dislysis a ON p.id = a.patient_id AND b.assessment_date = a.assessment_date").
2266
+		Joins("JOIN xt_monitoring_record m ON p.id = m.patient_id AND m.monitoring_date = b.assessment_date").
2267
+		Where("b.user_org_id = ?", user_org_id).
2268
+		Where("b.assessment_date >= ?", start_time).
2269
+		Where("b.assessment_date <= ?", end_time).
2270
+		Group("p.id, b.assessment_date")
2302 2271
 
2303
-	// 计算总数
2304
-	countQuery := `
2305
-        SELECT COUNT(*)
2306
-        FROM
2307
-            xt_patients p
2308
-        JOIN
2309
-            xt_assessment_before_dislysis b ON p.id = b.patient_id
2310
-        WHERE
2311
-            b.user_org_id = ?
2312
-            AND b.assessment_date >= ?
2313
-            AND b.assessment_date <= ?
2314
-    `
2272
+	// 根据传入的 addType 值动态构建条件
2273
+	switch addType {
2274
+	case 1:
2275
+		query = query.Where(`
2276
+			CASE 
2277
+				WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
2278
+					b.systolic_blood_pressure < 140 OR b.diastolic_blood_pressure < 90
2279
+				WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
2280
+					b.systolic_blood_pressure < 160 OR b.diastolic_blood_pressure < 90
2281
+			END`)
2282
+	case 2:
2283
+		query = query.Where(`
2284
+			CASE 
2285
+				WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) < 60 THEN
2286
+					b.systolic_blood_pressure >= 140 AND b.diastolic_blood_pressure >= 90
2287
+				WHEN FLOOR((YEAR(CURDATE()) - SUBSTRING(p.id_card_no, 7, 4))) >= 60 THEN
2288
+					b.systolic_blood_pressure >= 160 AND b.diastolic_blood_pressure >= 90
2289
+			END`)
2290
+	}
2315 2291
 
2316
-	// 执行统计查询
2317
-	if err := readDb.Raw(countQuery, user_org_id, start_time, end_time).Scan(&total).Error; err != nil {
2318
-		return nil, 0, fmt.Errorf("error calculating total count: %v", err)
2292
+	// 获取总记录数
2293
+	if err := query.Count(&total).Error; err != nil {
2294
+		return nil, 0, fmt.Errorf("count query failed: %v", err)
2319 2295
 	}
2320 2296
 
2321
-	// 执行数据查询
2322
-	if err := readDb.Raw(query, user_org_id, start_time, end_time, addType, addType, limit, offset).Scan(&results).Error; err != nil {
2323
-		return nil, 0, fmt.Errorf("error executing query: %v", err)
2297
+	// 获取分页数据
2298
+	if err := query.Order("p.id, b.assessment_date").
2299
+		Offset(int(offset)).
2300
+		Limit(int(limit)).
2301
+		Scan(&results).Error; err != nil {
2302
+		return nil, 0, fmt.Errorf("query execution failed: %v", err)
2324 2303
 	}
2325 2304
 
2326 2305
 	return results, total, nil