将这段代码改为按照收缩压和舒张压的分级范围查询,要有是否需要同时满足舒张压和收缩压的选项,不再按照value值查询 - (NSMutableArray *)getBloodPresureDataField:(NSString *)field value:(NSInteger)value startTime:(NSTimeInterval)startTime endTime:(NSTimeInterval)endTime { __block NSMutableArray *array = [NSMutableArray array]; [[HWFitnessMgr sharedInstance] excuteInTransaction:^(FMDatabase *db ,BOOL * rollBack) { NSMutableString *rs = [NSMutableString string]; [rs appendString:@" where"]; //此处逻辑:若搜索字段不为COLUMN_SYNC_CLOUD_STATUS去掉本地已删除未上云的数据 if ([field isEqualToString:COLUMN_SYNC_CLOUD_STATUS]) { [rs appendFormat:@" %@ >= %.0lf and %@ <= %.0lf and %@ = %ld", Column_TimeStamp, startTime, Column_TimeStamp, endTime, field, (long)value]; } else { [rs appendFormat:@" %@ >= %.0lf and %@ <= %.0lf and %@ = %ld and %@ != %ld", Column_TimeStamp, startTime, Column_TimeStamp, endTime, field, (long)value, COLUMN_SYNC_CLOUD_STATUS, (long)HWHealthDataSyncCloudStatusDeleteNotSync]; } [rs appendFormat:@" order by %@ desc", Column_TimeStamp]; NSString *strSql = [@"select * from '%@'" stringByAppendingString:rs]; NSString *bloodPressureTable = [self getTableName:FitnessSamplePointBloodPressureTable]; FMResultSet * detailRs = [[HWFitnessMgr sharedInstance] selectBy:strSql andTable:bloodPressureTable inDatabase:db]; if (detailRs) { while ([detailRs next]) { [array addObject:[self fetchBloodPressureDataWithDetailRs:detailRs]]; } [detailRs close]; } }]; return array; }
时间: 2023-06-19 17:09:14 浏览: 94
收缩压越高越危险?3个因素才能决定
以下是修改后的代码:
- (NSMutableArray *)getBloodPresureDataField:(NSString *)field systolicValue:(NSInteger)systolicValue diastolicValue:(NSInteger)diastolicValue startTime:(NSTimeInterval)startTime endTime:(NSTimeInterval)endTime requireBoth:(BOOL)requireBoth {
__block NSMutableArray *array = [NSMutableArray array];
[[HWFitnessMgr sharedInstance] excuteInTransaction:^(FMDatabase *db ,BOOL * rollBack) {
NSMutableString *rs = [NSMutableString string];
[rs appendString:@" where"];
//此处逻辑:若搜索字段不为COLUMN_SYNC_CLOUD_STATUS去掉本地已删除未上云的数据
if ([field isEqualToString:COLUMN_SYNC_CLOUD_STATUS]) {
if (requireBoth) {
[rs appendFormat:@" %@ >= %.0lf and %@ <= %.0lf and %@ >= %ld and %@ <= %ld", Column_TimeStamp, startTime, Column_TimeStamp, endTime, COLUMN_SYSTOLIC, (long)systolicValue, COLUMN_DIASTOLIC, (long)diastolicValue];
} else {
[rs appendFormat:@" %@ >= %.0lf and %@ <= %.0lf and ((%@ >= %ld and %@ <= %ld) or (%@ >= %ld and %@ <= %ld))", Column_TimeStamp, startTime, Column_TimeStamp, endTime, COLUMN_SYSTOLIC, (long)systolicValue, COLUMN_SYSTOLIC, (long)diastolicValue, COLUMN_DIASTOLIC, (long)systolicValue, COLUMN_DIASTOLIC, (long)diastolicValue];
}
} else {
if (requireBoth) {
[rs appendFormat:@" %@ >= %.0lf and %@ <= %.0lf and %@ >= %ld and %@ <= %ld and %@ != %ld", Column_TimeStamp, startTime, Column_TimeStamp, endTime, field, (long)systolicValue, field, (long)diastolicValue, COLUMN_SYNC_CLOUD_STATUS, (long)HWHealthDataSyncCloudStatusDeleteNotSync];
} else {
[rs appendFormat:@" %@ >= %.0lf and %@ <= %.0lf and ((%@ >= %ld and %@ <= %ld) or (%@ >= %ld and %@ <= %ld)) and %@ != %ld", Column_TimeStamp, startTime, Column_TimeStamp, endTime, COLUMN_SYSTOLIC, (long)systolicValue, COLUMN_SYSTOLIC, (long)diastolicValue, COLUMN_DIASTOLIC, (long)systolicValue, COLUMN_DIASTOLIC, (long)diastolicValue, COLUMN_SYNC_CLOUD_STATUS, (long)HWHealthDataSyncCloudStatusDeleteNotSync];
}
}
[rs appendFormat:@" order by %@ desc", Column_TimeStamp];
NSString *strSql = [@"select * from '%@'" stringByAppendingString:rs];
NSString *bloodPressureTable = [self getTableName:FitnessSamplePointBloodPressureTable];
FMResultSet * detailRs = [[HWFitnessMgr sharedInstance] selectBy:strSql andTable:bloodPressureTable inDatabase:db];
if (detailRs) {
while ([detailRs next]) {
[array addObject:[self fetchBloodPressureDataWithDetailRs:detailRs]];
}
[detailRs close];
}
}];
return array;
}
修改后的代码根据收缩压和舒张压的分级范围查询数据,可选是否需要同时满足收缩压和舒张压。当 requireBoth 为 YES 时,只返回同时满足收缩压和舒张压的数据;当 requireBoth 为 NO 时,返回满足收缩压或舒张压的数据。同时,修改了变量名和注释,增强了代码的可读性。
阅读全文