table_manager_model.dart
9.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:one_key/constant/constant.dart';
import 'package:one_key/constant/error.dart';
import 'package:one_key/constant/state.dart';
import 'package:one_key/models/area.dart';
import 'package:one_key/models/notebook.dart';
import 'package:one_key/models/tableOuter.dart';
import 'package:one_key/net/base_resp.dart';
import 'package:one_key/net/dio_util.dart';
import 'package:one_key/net/http_url.dart';
import 'package:one_key/models/table.dart' as t;
import 'package:one_key/provider/user_model.dart';
import 'package:one_key/utils/navigator_util.dart';
import 'package:one_key/utils/utils.dart';
import 'package:provider/provider.dart';
import 'notebook_model.dart';
class TableManagerModel with ChangeNotifier{
List<Area> areas = []; //区域列表
List<t.Table> selectTables = []; //选择的桌位
List<TableOuter> tableOuterList = []; //区域-桌位列表
Map<String, List<t.Table>> areaTableMap = {}; //区域-桌位Map
List<t.Table> currentTables=[];//当前显示的区域-桌位
Map<String, Notebook> tableMap = {}; //记录已预订桌位对应的订单
String currentArea; //当前选中区域
DateTime dateTime=DateTime.now();
String dateText = ""; //当前显示日期时间
int currTimeIndex = 1; //当前选中时间段
String morning; //早市时间段
String noon; //午市时间段
String night; //晚市时间段
UserModel userModel;
PageState pageState = PageState.Loading;
//初始化时间
initTime(BuildContext context) async {
userModel = Provider.of<UserModel>(context, listen: false);
dateTime = DateTime.now();
dateText = Utils.addZeroNumber(dateTime.month) +
"月" +
Utils.addZeroNumber(dateTime.day) +
"日" +
" 今天";
morning =
userModel.user.morning.isEmpty ? "00:00-00:00" : userModel.user.morning;
noon = userModel.user.noon.isEmpty ? "00:00-00:00" : userModel.user.noon;
night = userModel.user.night.isEmpty ? "00:00-00:00" : userModel.user.night;
if (dateTime.isBefore(DateTime.parse(dateTime.year.toString() +
"-" +
Utils.addZeroNumber(dateTime.month) +
"-" +
Utils.addZeroNumber(dateTime.day) +
" " +
morning.split("-")[1] +
":00"))) {
//未过早市
currTimeIndex = 1;
} else if (dateTime.isAfter(DateTime.parse(dateTime.year.toString() +
"-" +
Utils.addZeroNumber(dateTime.month) +
"-" +
Utils.addZeroNumber(dateTime.day) +
" " +
morning.split("-")[1] +
":00")) &&
dateTime.isBefore(DateTime.parse(dateTime.year.toString() +
"-" +
Utils.addZeroNumber(dateTime.month) +
"-" +
Utils.addZeroNumber(dateTime.day) +
" " +
noon.split("-")[1] +
":00"))) {
//过了早市,未过午市
currTimeIndex = 2;
} else if (dateTime.isAfter(DateTime.parse(dateTime.year.toString() +
"-" +
Utils.addZeroNumber(dateTime.month) +
"-" +
Utils.addZeroNumber(dateTime.day) +
" " +
noon.split("-")[1] +
":00")) &&
dateTime.isBefore(DateTime.parse(dateTime.year.toString() +
"-" +
Utils.addZeroNumber(dateTime.month) +
"-" +
Utils.addZeroNumber(dateTime.day) +
" " +
night.split("-")[1] +
":00"))) {
//过了午市,未过晚市
currTimeIndex = 3;
} else {
//过了晚市
currTimeIndex = 3;
}
await getTableList(context);
getReserveList(context);
}
getCurrDdateStr(){
String currSelectTime="";
if(currTimeIndex==1){
currSelectTime=morning.split("-")[0];
}else if(currTimeIndex==2){
currSelectTime=noon.split("-")[0];
}else{
currSelectTime=night.split("-")[0];
}
String ddateStr=DateUtil.getDateMsByTimeStr(DateUtil.formatDate(dateTime).replaceRange(11,19, "$currSelectTime:00")).toString();
return ddateStr.substring(0,10);
}
//获取桌位数据
Future<List<TableOuter>> getTableList(BuildContext context) async {
pageState = PageState.Loading;
notifyListeners();
Map<String, String> params = {"sellerid": userModel.user.sellerid};
BaseResp<List> baseResp = await DioUtil()
.request<List>(context, Method.get, HttpUrl.table_list,
data: params,showLoading: false)
.catchError((error) {
print(error.toString());
if (error is MyError) {
switch (error.type) {
case 1:
pageState = PageState.ConnectivityError;
break;
case 2:
pageState = PageState.ConnectivityTimeOut;
break;
default:
pageState = PageState.ConnectivityError;
break;
}
}
notifyListeners();
});
tableOuterList=[];
if(baseResp.code==Constant.status_success){
if ((baseResp.data[0] as List).isEmpty) {
pageState = PageState.Empty;
notifyListeners();
}
tableOuterList=(baseResp.data[0] as List).map((value){
TableOuter tableOuter=TableOuter();
tableOuter.name=value["name"];
tableOuter.list=(value["list"] as List).map((table){
return t.Table.fromJson(table as Map<String,dynamic>);
}).toList();
return tableOuter;
}).toList();
}
return tableOuterList;
}
//获取已预订列表
getReserveList(BuildContext context) async {
tableMap.clear();
NoteBookModel model = new NoteBookModel();
Map<String, String> params = {
"sid": userModel.user.sellerid,
"rdate": _getRdate(),
"ddate_solt": currTimeIndex.toString(),
"y": "200",
"x": "1",
"keywords": "",
"sort": "1"
};
pageState = PageState.Loading;
notifyListeners();
List<Notebook> noteBooks = await model.getReserveList(context, params);
for (Notebook notebook in noteBooks) {
if (notebook.tid.isEmpty) continue;
if (notebook.status == "1" ||
notebook.status == "2" ||
notebook.status == "5") {
//已预订,用餐中,已安排
List<String> tids = notebook.tid.split(",");
for (String tid in tids) {
tableMap["$tid"] = notebook;
}
}
}
print(tableMap);
initData();
}
//获取一天开始-结束时间字符串
String _getRdate() {
//一天开始时间
double startDay = (DateTime.parse((dateTime.year.toString() +
"-" +
Utils.addZeroNumber(dateTime.month) +
"-" +
Utils.addZeroNumber(dateTime.day) +
" 00:00:00"))
.millisecondsSinceEpoch) /
1000;
//一天结束时间
double endDay = (DateTime.parse((dateTime.year.toString() +
"-" +
Utils.addZeroNumber(dateTime.month) +
"-" +
Utils.addZeroNumber(dateTime.day) +
" 23:59:59"))
.millisecondsSinceEpoch) /
1000;
return startDay.floor().toString() + "-" + endDay.floor().toString();
}
//初始桌位数据
initData() {
areas.clear();
areaTableMap.clear();
for (TableOuter outer in tableOuterList) {
int count = 0;
for (t.Table table in outer.list) {
table.sign = "";
table.notebookId = "";
table.reserver = "";
if (table.tablestatus == "0") {//没有用餐中
if (tableMap["${table.id}"] != null) {
Notebook notebook = tableMap["${table.id}"];
table.sign = notebook.status;
table.notebookId = notebook.id;
table.reserver = notebook.rnameStr;
} else {
table.sign = "";
table.notebookId = "";
if (table.enable == "1") {
count++;
}
}
} else {//用餐中
table.sign = "2";
table.notebookId = "";
}
}
areaTableMap["${outer.name}"] = outer.list;
Area area = Area();
area.name = outer.name;
area.count = count;
areas.add(area);
}
currentArea = areaTableMap.keys.first;
currentTables=areaTableMap[currentArea];
pageState = PageState.Success;
notifyListeners();
}
//生成日期文本
createDateText(BuildContext context,DateTime day) {
if (Utils.isOneDay(day, dateTime)) return; //日期没变
DateTime dateNow = DateTime.now();
dateText = Utils.addZeroNumber(day.month) +
"月" +
Utils.addZeroNumber(day.day) +
"日";
if (Utils.isOneDay(day, dateNow)) {
dateText += " 今天";
} else if (Utils.isOneDay(day, dateNow.add(Duration(days: 1)))) {
dateText += " 明天";
} else if (Utils.isOneDay(day, dateNow.add(Duration(days: 2)))) {
dateText += " 后天";
} else {
dateText += " 周${Utils.formatWeekDayNum(day.weekday)}";
}
dateTime = day;
getReserveList(context);
}
//获取桌位数据
Future<dynamic> setSwitchStatus(BuildContext context,Map<String,String> params) async {
BaseResp<dynamic> baseResp = await DioUtil()
.request<dynamic>(context, Method.get, HttpUrl.set_switch_status,
data: params,showLoading: true)
.catchError((error) {
print(error.toString());
});
if(baseResp==null)return null;
if(baseResp.code==Constant.status_success){
return baseResp.data;
}else{
Utils.showToast(baseResp.msg);
}
}
}