166 lines
4.3 KiB
Dart
166 lines
4.3 KiB
Dart
import 'package:EasyDartModule/EasyDartModule.dart';
|
|
import '../model/Area.dart';
|
|
import '../const/MessageConstants.dart';
|
|
|
|
class AreaRepository {
|
|
// 获取所有区域列表
|
|
Future<List> fetchAreaList(Area query,
|
|
{int page = 1, int pageSize = 10}) async {
|
|
var db = EasyDartModule.dataBase;
|
|
|
|
// 构建查询条件
|
|
SelectorBuilder select = SelectorBuilder();
|
|
|
|
// 添加查询条件
|
|
if (query.oid != null) {
|
|
select.eq('oid', query.oid);
|
|
}
|
|
if (query.id != null) {
|
|
select.eq('area_id', query.id);
|
|
}
|
|
if (query.tid != null) {
|
|
select.eq('oid', query.tid);
|
|
}
|
|
if (query.level != null) {
|
|
select.gte('level', query.level);
|
|
}
|
|
if (query.area_name != null) {
|
|
select.match('area_name', query.area_name!, caseInsensitive: true);
|
|
}
|
|
if (query.area_desc != null) {
|
|
select.eq('area_desc', query.area_desc);
|
|
}
|
|
if (query.created_by_id != null) {
|
|
select.eq('created_by_id', query.created_by_id);
|
|
}
|
|
if (query.updated_by_id != null) {
|
|
select.eq('updated_by_id', query.updated_by_id);
|
|
}
|
|
if (query.created_at != null) {
|
|
select.gte('created_at', query.created_at);
|
|
}
|
|
if (query.updated_at != null) {
|
|
select.lte('updated_at', query.updated_at);
|
|
}
|
|
select.eq('deleted', 0);
|
|
// 设置分页
|
|
select.skip((page - 1) * pageSize).limit(pageSize);
|
|
// 设置排序
|
|
select.sortBy("created_at"); // 按创建时间排序
|
|
|
|
// 执行查询
|
|
var result = await db.query(
|
|
'bus_area_info',
|
|
condition: select, // 查询条件 + 分页 + 排序
|
|
);
|
|
|
|
if (result.isEmpty) {
|
|
return [];
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
// 获取单个区域信息
|
|
Future<Area?> fetchAreaById(int areaId) async {
|
|
var db = EasyDartModule.dataBase;
|
|
|
|
// 构建查询条件
|
|
var condition = {'area_id': areaId};
|
|
|
|
// 执行查询
|
|
var result = await db.query(
|
|
'bus_area_info',
|
|
condition: condition,
|
|
);
|
|
|
|
if (result.isNotEmpty) {
|
|
return Area.fromJson(result.first);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
// 添加区域
|
|
Future<bool> insertArea(Area area) async {
|
|
var db = EasyDartModule.dataBase;
|
|
// 执行插入操作
|
|
var result = await db.insert('bus_area_info', area.toJson());
|
|
// 返回是否插入成功
|
|
return true;
|
|
}
|
|
|
|
// 更新区域信息
|
|
Future<String> updateArea(String areaId, Area updatedArea) async {
|
|
String message = "";
|
|
var db = EasyDartModule.dataBase;
|
|
var str = updatedArea.toJson();
|
|
str.remove("_id");
|
|
var updateCondition = {
|
|
'_id': ObjectId.fromHexString(areaId),
|
|
};
|
|
try {
|
|
var result = await db.update('bus_area_info', str, updateCondition);
|
|
return message; // 返回受影响的行数
|
|
} catch (e) {
|
|
message = MessageConstants.UPDATE_ERROR;
|
|
return message;
|
|
}
|
|
}
|
|
|
|
// 删除区域 (逻辑删除)
|
|
Future<String> deleteArea(String areaId) async {
|
|
var db = EasyDartModule.dataBase;
|
|
String message = "";
|
|
try {
|
|
var areaIdList = areaId.split(',');
|
|
var objectIdList =
|
|
areaIdList.map((id) => ObjectId.fromHexString(id)).toList();
|
|
var condition = {
|
|
'_id': {'\$in': objectIdList}
|
|
}; // 使用 \$in 操作符查询多个 ID
|
|
var update = {
|
|
'\$set': {'deleted': 1}
|
|
}; // 逻辑删除,只更新 deleted 字段
|
|
var result = await db.update('bus_area_info', update, condition);
|
|
return message; // 返回受影响的行数
|
|
} catch (e) {
|
|
print("Error during deleteArea: $e");
|
|
message = MessageConstants.DELETE_ERROR;
|
|
return message;
|
|
}
|
|
}
|
|
|
|
// 获取区域数量
|
|
Future<int> getAreaCount(Area query) async {
|
|
var db = EasyDartModule.dataBase;
|
|
|
|
// 构建查询条件
|
|
SelectorBuilder select = SelectorBuilder();
|
|
|
|
if (query.id != null) {
|
|
select.eq('_id', query.id);
|
|
}
|
|
if (query.tid != null) {
|
|
select.eq('oid', query.tid);
|
|
}
|
|
if (query.level != null) {
|
|
select.gte('level', query.level);
|
|
}
|
|
if (query.area_name != null) {
|
|
select.match('area_name', query.area_name!, caseInsensitive: true);
|
|
}
|
|
if (query.area_desc != null) {
|
|
select.eq('area_desc', query.area_desc);
|
|
}
|
|
select.eq('deleted', 0);
|
|
// 执行查询并返回计数
|
|
var result = await db.query(
|
|
'bus_area_info',
|
|
condition: select,
|
|
);
|
|
|
|
return result.length;
|
|
}
|
|
}
|