将这段代码改为按照收缩压和舒张压的分级范围查询,分别取数据中的收缩压和舒张压和分级规则中的收缩压取值范围和舒张压取值范围比较,要有是否需要同时满足舒张压和收缩压范围的选项,不再按照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 14:08:08 浏览: 98
修改后的代码如下:
- (NSMutableArray *)getBloodPresureDataWithSystolicRange:(NSRange)systolicRange diastolicRange:(NSRange)diastolicRange requireBoth:(BOOL)requireBoth 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去掉本地已删除未上云的数据
[rs appendFormat:@" %@ >= %.0lf and %@ <= %.0lf and %@ != %ld", Column_TimeStamp, startTime, Column_TimeStamp, endTime, COLUMN_SYNC_CLOUD_STATUS, (long)HWHealthDataSyncCloudStatusDeleteNotSync];
if (requireBoth) {
[rs appendFormat:@" and %@ BETWEEN %ld AND %ld AND %@ BETWEEN %ld AND %ld", Column_BloodPressureSystolic, (long)systolicRange.location, (long)systolicRange.location + (long)systolicRange.length - 1, Column_BloodPressureDiastolic, (long)diastolicRange.location, (long)diastolicRange.location + (long)diastolicRange.length - 1];
} else {
if (systolicRange.length > 0) {
[rs appendFormat:@" and %@ BETWEEN %ld AND %ld", Column_BloodPressureSystolic, (long)systolicRange.location, (long)systolicRange.location + (long)systolicRange.length - 1];
}
if (diastolicRange.length > 0) {
[rs appendFormat:@" and %@ BETWEEN %ld AND %ld", Column_BloodPressureDiastolic, (long)diastolicRange.location, (long)diastolicRange.location + (long)diastolicRange.length - 1];
}
}
[rs appendFormat:@" order by %@ desc", Column_TimeStamp];
NSString *strSql = [NSString stringWithFormat:@"select * from '%@'%@", [self getTableName:FitnessSamplePointBloodPressureTable], rs];
FMResultSet *detailRs = [[HWFitnessMgr sharedInstance] selectBy:strSql andTable:[self getTableName:FitnessSamplePointBloodPressureTable] inDatabase:db];
if (detailRs) {
while ([detailRs next]) {
[array addObject:[self fetchBloodPressureDataWithDetailRs:detailRs]];
}
[detailRs close];
}
}];
return array;
}
这里修改了方法名,同时增加了两个参数,分别是收缩压和舒张压的分级范围,使用NSRange类型表示,另外增加了一个布尔值参数requireBoth,用于控制是否需要同时满足收缩压和舒张压范围的选项。如果requireBoth为YES,则需要同时满足两个范围;如果为NO,则只需要满足其中一个范围即可。
在方法的实现中,按照收缩压和舒张压范围分别构造查询条件。如果requireBoth为NO,则只需要满足其中一个条件,所以在构造查询条件时需要判断范围值是否大于0。最后拼接查询语句并执行查询,将查询结果转化为模型对象并添加到数组中返回。
阅读全文