105 lines
3.5 KiB
Dart
105 lines
3.5 KiB
Dart
import 'package:ef/ef.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:json_annotation/json_annotation.dart';
|
|
import 'package:vbvs_app/common/color/ServiceConstant.dart';
|
|
import 'package:vbvs_app/common/util/MyUtils.dart';
|
|
import 'package:vbvs_app/common/util/requestWithLog.dart';
|
|
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
|
|
import 'package:vbvs_app/controller/sleep/sleep_time_convert.dart';
|
|
import 'package:vbvs_app/pages/main_bottom/component/main_page_b_bottom_change.dart';
|
|
|
|
part 'sleep_report_controller.g.dart'; // 由json_serializable自动生成的部分
|
|
|
|
@JsonSerializable()
|
|
class SleepReportModel {
|
|
int? type = 1; //报告类型 1:日报 2.周报 3.月报
|
|
|
|
SleepReportModel();
|
|
|
|
// 从JSON反序列化时的异常处理
|
|
|
|
factory SleepReportModel.fromJson(Map<String, dynamic> json) {
|
|
try {
|
|
return _$SleepReportModelFromJson(json);
|
|
} catch (e) {
|
|
// 在实际应用中,应该有更细致的异常处理策略和错误日志
|
|
return SleepReportModel(); // 或者返回一个带有错误信息的特定DeviceInfoModel实例
|
|
}
|
|
}
|
|
|
|
// 序列化为JSON时的异常处理
|
|
Map<String, dynamic> toJson() => _$SleepReportModelToJson(this);
|
|
}
|
|
|
|
class SleepReportController extends GetControllerEx<SleepReportModel> {
|
|
Rx<DateTime?> selectedDate = Rx<DateTime?>(null);
|
|
RxMap<String, dynamic> sleepReport = <String, dynamic>{}.obs;
|
|
RxBool isLoading = false.obs;
|
|
// 下拉框的设备 mac
|
|
RxString mac = "".obs;
|
|
|
|
// 日历选择的日期
|
|
|
|
// 初始化方法(外部跳转时调用)
|
|
void initParams({String? macValue, int? dateValue}) {
|
|
if (macValue != null) mac.value = macValue;
|
|
if (dateValue != null) {
|
|
selectedDate.value = DateTime.fromMillisecondsSinceEpoch(dateValue);
|
|
}
|
|
}
|
|
|
|
// 每种类型对应一份数据
|
|
|
|
SleepReportController() {
|
|
attr = GetModel(SleepReportModel()).obs;
|
|
}
|
|
|
|
void loadSleepReport(String data, String mac, BuildContext context) async {
|
|
isLoading.value = true;
|
|
|
|
String serviceAddress = ServiceConstant.service_address;
|
|
String serviceName = ServiceConstant.server_service;
|
|
String serviceApi = ServiceConstant.sleep_report;
|
|
String queryUrl =
|
|
"$serviceAddress$serviceName$serviceApi?mac=${mac}&time=${data}&type=1&sleepType=2";
|
|
await requestWithLog(
|
|
logTitle: "查询睡眠报告",
|
|
method: MyHttpMethod.get,
|
|
queryUrl: queryUrl,
|
|
// onSuccess: (res) {
|
|
// isLoading.value = false;
|
|
// print(res);
|
|
// final Map<String, dynamic> data = res.data;
|
|
// sleepReport.value = res.data;
|
|
// updateAll();
|
|
// },
|
|
onSuccess: (res) {
|
|
isLoading.value = false;
|
|
print(res);
|
|
|
|
// 获取原始数据
|
|
final sleepData = Map<String, dynamic>.from(res.data);
|
|
// 处理时区转换
|
|
applyTimezoneConversion(sleepData);
|
|
|
|
sleepReport.value = sleepData;
|
|
updateAll();
|
|
},
|
|
onFailure: (res) {
|
|
isLoading.value = false;
|
|
if (MainPageBBottomChange.getCurrentIndex() != null) {
|
|
if (MainPageBBottomChange.getCurrentIndex() == 1) {
|
|
TopSlideNotification.show(context,
|
|
text: res.msg!, textColor: themeController.currentColor.sc9);
|
|
}
|
|
} else {
|
|
TopSlideNotification.show(context,
|
|
text: res.msg!, textColor: themeController.currentColor.sc9);
|
|
}
|
|
sleepReport.value = {};
|
|
updateAll();
|
|
print(res);
|
|
});
|
|
}
|
|
}
|