251 lines
8.8 KiB
Dart
251 lines
8.8 KiB
Dart
import 'dart:async';
|
||
import 'dart:ui';
|
||
|
||
import 'package:EasyDartModule/EasyDartModule.dart';
|
||
import 'package:ef/ef.dart';
|
||
import 'package:flutter/src/widgets/framework.dart';
|
||
import 'package:flutterflow_ui/flutterflow_ui.dart';
|
||
import 'package:json_annotation/json_annotation.dart';
|
||
import 'package:vbvs_app/common/color/ServiceConstant.dart';
|
||
import 'package:vbvs_app/common/color/appConstants.dart';
|
||
import 'package:vbvs_app/common/color/app_uri_status.dart';
|
||
import 'package:vbvs_app/common/pojo/city.dart';
|
||
import 'package:vbvs_app/common/util/DailyLogUtils.dart';
|
||
import 'package:vbvs_app/common/util/EventBus.dart';
|
||
import 'package:vbvs_app/common/util/MyUtils.dart';
|
||
import 'package:vbvs_app/common/util/eventType.dart';
|
||
import 'package:vbvs_app/common/util/requestWithLog.dart';
|
||
import 'package:vbvs_app/component/tool/TopSlideNotification.dart';
|
||
import 'package:vbvs_app/enum/APPPackageType.dart';
|
||
import 'package:vbvs_app/model/api_response.dart';
|
||
|
||
part 'people_info_controller.g.dart'; // 由json_serializable自动生成的部分
|
||
|
||
@JsonSerializable()
|
||
class PeopleInfoModel {
|
||
String? name;
|
||
String sex = "男";
|
||
String? height;
|
||
String? weight;
|
||
DateTime? birthday;
|
||
String? phone;
|
||
String? contact;
|
||
List peopleList = [];
|
||
List<CityModel> cityModels = [];
|
||
List selectedDiseaseIds = [];
|
||
PeopleInfoModel();
|
||
|
||
factory PeopleInfoModel.fromJson(Map<String, dynamic> json) {
|
||
try {
|
||
return _$PeopleInfoModelFromJson(json);
|
||
} catch (e) {
|
||
// 在实际应用中,应该有更细致的异常处理策略和错误日志
|
||
return PeopleInfoModel(); // 或者返回一个带有错误信息的特定PeopleInfoModel实例
|
||
}
|
||
}
|
||
|
||
// 序列化为JSON时的异常处理
|
||
Map<String, dynamic> toJson() => _$PeopleInfoModelToJson(this);
|
||
}
|
||
|
||
class PeopleInfoController extends GetControllerEx<PeopleInfoModel> {
|
||
PeopleInfoController() {
|
||
attr = GetModel(PeopleInfoModel()).obs;
|
||
}
|
||
|
||
final CityModelController cityController = Get.find<CityModelController>();
|
||
RxList diseaseList = [].obs;
|
||
|
||
@override
|
||
Future<void> onInit() async {
|
||
super.onInit();
|
||
cityDataFuture = cityController.loadAndSetCityData().then((success) {
|
||
return cityController.cityList;
|
||
});
|
||
// 监听切换语言
|
||
subscription = EventBus().on<SwitchLanguageEvent>().listen((event) async {
|
||
final CityModelController cityController =
|
||
Get.find<CityModelController>();
|
||
ef.log("切换语言事件通知:${event.language}");
|
||
cityController.cityList = [];
|
||
await initializeCityData();
|
||
});
|
||
}
|
||
|
||
late Future<List<CityModel>> cityDataFuture;
|
||
|
||
late StreamSubscription<SwitchLanguageEvent> subscription;
|
||
|
||
getPeoples(String mac) async {
|
||
String serviceAddress = ServiceConstant.service_address;
|
||
String serviceName = ServiceConstant.server_service;
|
||
String serviceApi = ServiceConstant.person_info;
|
||
String queryUrl = "$serviceAddress$serviceName$serviceApi?mac=$mac";
|
||
|
||
await requestWithLog(
|
||
logTitle: "获取设备的人员信息列表",
|
||
method: MyHttpMethod.get,
|
||
queryUrl: queryUrl,
|
||
onSuccess: (res) {
|
||
if (res.data != null && res.data is List) {
|
||
model.peopleList = res.data;
|
||
model.cityModels = []; // 清空之前的数据
|
||
initializeCityData(); // 初始化城市数据
|
||
updateAll();
|
||
print("peopleList: ${model.peopleList}");
|
||
}
|
||
},
|
||
onFailure: (err) {
|
||
print("获取人员信息失败: $err");
|
||
},
|
||
);
|
||
}
|
||
|
||
savePeoples(Map<String, dynamic> data, BuildContext context) async {
|
||
String serviceAddress = ServiceConstant.service_address;
|
||
String serviceName = ServiceConstant.server_service;
|
||
String serviceApi = ServiceConstant.person_info;
|
||
String queryUrl = "$serviceAddress$serviceName$serviceApi";
|
||
String code = '';
|
||
data['birthday'] = data['birthday'] is DateTime
|
||
? DateFormat('yyyy-MM-dd').format(data['birthday'])
|
||
: data['birthday'];
|
||
await requestWithLog(
|
||
logTitle: "更新人员信息",
|
||
method: MyHttpMethod.put,
|
||
queryUrl: queryUrl,
|
||
data: data,
|
||
onSuccess: (res) {
|
||
TopSlideNotification.show(context,
|
||
text: res.msg!, textColor: Color(0XFF00C1AA));
|
||
},
|
||
onFailure: (err) {
|
||
TopSlideNotification.show(context,
|
||
text: err.msg!, textColor: Color(0xFFFF7159));
|
||
},
|
||
);
|
||
}
|
||
|
||
Future initializeCityData() async {
|
||
try {
|
||
// 确保城市数据已加载
|
||
if (!cityController.isDataLoaded) {
|
||
await cityController.loadAndSetCityData();
|
||
}
|
||
|
||
// 清空现有的城市模型列表
|
||
model.cityModels = [];
|
||
|
||
// 遍历peopleList中的每个人,查找对应的城市数据
|
||
for (var person in model.peopleList) {
|
||
if (person is Map<String, dynamic>) {
|
||
// 获取person中的city_id
|
||
final cityId = person['city_id']?.toString();
|
||
|
||
if (cityId != null && cityId.isNotEmpty) {
|
||
ef.log("开始查找city_id: $cityId");
|
||
|
||
// 使用cityController查找完整的城市数据
|
||
final CityModel? completeCityData =
|
||
cityController.findCityById(cityId);
|
||
|
||
if (completeCityData != null) {
|
||
// 将找到的城市数据添加到cityModels列表中
|
||
model.cityModels.add(completeCityData);
|
||
ef.log(
|
||
"成功添加城市数据: ${completeCityData.displayName} for person: ${person['name'] ?? 'Unknown'}");
|
||
} else {
|
||
// 如果找不到对应的城市数据,添加一个空的CityModel,但保留city_id
|
||
final CityModel emptyCityModel =
|
||
CityModel(id: int.tryParse(cityId));
|
||
model.cityModels.add(emptyCityModel);
|
||
ef.log("未找到对应城市数据,创建默认CityModel for city_id: $cityId");
|
||
}
|
||
} else {
|
||
// 如果person没有city_id,添加一个空的CityModel
|
||
model.cityModels.add(CityModel());
|
||
ef.log("person没有city_id,添加空CityModel");
|
||
}
|
||
}
|
||
}
|
||
|
||
// 确保cityModels列表长度与peopleList一致
|
||
while (model.cityModels.length < model.peopleList.length) {
|
||
model.cityModels.add(CityModel());
|
||
}
|
||
|
||
// 更新UI
|
||
updateAll();
|
||
|
||
ef.log(
|
||
"初始化城市数据完成,peopleList长度: ${model.peopleList.length}, cityModels长度: ${model.cityModels.length}");
|
||
} catch (e) {
|
||
ef.log("初始化城市数据失败: $e");
|
||
}
|
||
}
|
||
|
||
// 可选:添加一个方法来获取特定person的城市数据
|
||
CityModel? getCityForPerson(int personIndex) {
|
||
if (personIndex >= 0 && personIndex < model.cityModels.length) {
|
||
return model.cityModels[personIndex];
|
||
}
|
||
return null;
|
||
}
|
||
|
||
// 可选:添加一个方法来更新特定person的城市数据
|
||
void updateCityForPerson(int personIndex, CityModel cityModel) {
|
||
if (personIndex >= 0 && personIndex < model.cityModels.length) {
|
||
model.cityModels[personIndex] = cityModel;
|
||
updateAll();
|
||
}
|
||
}
|
||
|
||
Future<ApiResponse> getDiseaseData() async {
|
||
try {
|
||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "请求失败".tr);
|
||
String serviceAddress = ServiceConstant.service_address;
|
||
String serviceName = ServiceConstant.server_service;
|
||
String serviceApi = ServiceConstant.disease_list;
|
||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||
String? language = "";
|
||
if (AppConstants().ent_type == APPPackageType.MHT.code) {
|
||
if (mhLanguageController.selectLanguage != null) {
|
||
language = mhLanguageController.selectLanguage.value!.language_code;
|
||
}
|
||
} else {
|
||
if (languageController.selectLanguage != null) {
|
||
language = languageController.selectLanguage.value!.language_code;
|
||
}
|
||
}
|
||
if (language != null && language.isNotEmpty) {
|
||
if (queryUrl.contains("?")) {
|
||
queryUrl += "&lang=$language";
|
||
} else {
|
||
queryUrl += "?lang=$language";
|
||
}
|
||
}
|
||
var response = await EasyDartModule.dio.get(queryUrl);
|
||
if (response != null) {
|
||
var responseData =
|
||
response.data is String ? jsonDecode(response.data) : response.data;
|
||
ApiResponse res =
|
||
ApiResponse.fromJson(responseData, (object) => object);
|
||
MyUtils.formatResponse(res, "请求成功".tr, "请求失败".tr);
|
||
if (res.code == HttpStatusCodes.ok) {
|
||
// bindDeviceNum.value = res.total!;
|
||
diseaseList.value = res.data!;
|
||
updateAll();
|
||
return res;
|
||
}
|
||
} else {
|
||
return ApiResponse(code: -1, msg: "失败".tr);
|
||
}
|
||
return apiResponse;
|
||
} catch (e) {
|
||
EasyDartModule.logger.info("疾病请求列表: $e");
|
||
DailyLogUtils.writeLog("疾病请求列表: $e");
|
||
}
|
||
return ApiResponse(code: -1, msg: "未知错误".tr); // Default return statement
|
||
}
|
||
}
|