notebook_detail_model.dart 16.2 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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
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/manager.dart';
import 'package:one_key/models/notebook_detail.dart';
import 'package:one_key/models/user_history_entity.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/provider/customer_provider.dart';
import 'package:one_key/provider/user_model.dart';
import 'package:one_key/route/navigator_util.dart';
import 'package:one_key/utils/utils.dart';
import 'package:one_key/widgets/loading.dart';
import 'package:provider/provider.dart';

import '../models/get_reserve_type_field_model.dart';
import '../models/notebook.dart';
import '../models/reserveA_action_log_modol.dart';
import '../models/reserve_deposit_model.dart';
import '../models/reserve_recheck_model.dart';
import '../models/user.dart';

class NotebookDetailModel extends ChangeNotifier {
  PageState pageState = PageState.Loading;
  String selectNum;
  int selectIndex;
  TextEditingController numController = TextEditingController();
  String strTF;
  String prestrTF;
  TextEditingController phoneController = TextEditingController();
  TextEditingController nameController = TextEditingController();
  List sexList;
  List<UserHistoryEntity> userHistorys = []; //历史录入

  List<ReserveDepositSunModel> reserveDepositList = []; //订单订金数据
  List<ReserveRecheckSunModel> reserveRecheckList = []; //核餐记录数据

  List<GetReserveTypeFieldsSunModel> reserveTypes = []; //用餐类型

  List<ReserveActionLogSunModol> reserveActionLogData = []; // 获取预订操作记录

  NotebookDetailModel() {
    initSexList();
  }

  ///称谓
  initSexList() {
    String sexStr = '1';
    sexList = [
      {
        'title': '先生',
        'select': sexStr == '1' ? true : false,
        'code': '1',
        'hasLeftBorder': true
      },
      {
        'title': '女士',
        'select': sexStr == '2' ? true : false,
        'code': '2',
        'hasLeftBorder': false
      },
      {
        'title': '公司',
        'select': sexStr == '3' ? true : false,
        'code': '3',
        'hasLeftBorder': false
      }
    ];
  }

  //获取历史录入
  getUserHistorys(BuildContext context, Map<String, String> params) async {
    BaseResp<List> baseResp = await DioUtil()
        .request<List>(context, Method.get, HttpUrl.get_reserve_user,
            data: params, showLoading: false)
        .catchError((error) {
      print(error.toString());
    });
    if (baseResp != null && baseResp.code == Constant.status_success) {
      userHistorys = baseResp.data.map<UserHistoryEntity>((value) {
        return UserHistoryEntity.fromJson(value);
      }).toList();
      notifyListeners();
    }
  }

  /// 获取预订操作记录
  getReserveActionLog(BuildContext context, Map<String, String> params,
      {bool showCircle = false}) async {
    BaseResp<List> baseResp = await DioUtil()
        .request<List>(context, Method.get, HttpUrl.get_reserve_action_log,
            data: params, showLoading: showCircle)
        .catchError((error) {
      print(error.toString());
    });
    if (baseResp.code == Constant.status_success) {
      reserveActionLogData =
          ReserveActionLogModol.fromJson(baseResp.responseData).data;
    } else {
      if (baseResp != null) {
        Utils.showToast(baseResp.msg ?? "");
      }
    }
    notifyListeners();
  }

  /// 通过预订ID查收定金详情
  getReserveDepositByRid(
      BuildContext context, Map<String, String> params) async {
    BaseResp<dynamic> baseResp = await DioUtil()
        .request<dynamic>(
            context, Method.get, HttpUrl.get_reserve_deposit_by_rid,
            data: params, showLoading: false)
        .catchError((error) {
      print(error.toString());
    });
    if (baseResp.code == Constant.status_success) {
      reserveDepositList =
          ReserveDepositModel.fromJson(baseResp.responseData).data;
    } else {
      if (baseResp != null) {
        Utils.showToast(baseResp.msg ?? "");
      }
    }
    notifyListeners();
  }

  /// 获取核餐记录
  getReserveRecheckByRid(
      BuildContext context, Map<String, String> params) async {
    BaseResp<dynamic> baseResp = await DioUtil()
        .request<dynamic>(
            context, Method.get, HttpUrl.get_reserve_recheck_by_rid,
            data: params, showLoading: false)
        .catchError((error) {
      print(error.toString());
    });
    if (baseResp.code == Constant.status_success) {
      reserveRecheckList =
          ReserveRecheckModel.fromJson(baseResp.responseData).data;
    } else {
      if (baseResp != null) {
        Utils.showToast(baseResp.msg ?? "");
      }
    }
    notifyListeners();
  }

  ///获取用餐类型
  Future<dynamic> getReserveTypeField(BuildContext context,
      {isShowLoading = true, bool showCircle = false}) async {
    BaseResp<dynamic> baseResp = await DioUtil()
        .request<dynamic>(context, Method.get, HttpUrl.get_reserve_type_field,
            showLoading: isShowLoading ? false : showCircle)
        .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();
    });
    if (baseResp.code == Constant.status_success) {
      reserveTypes =
          GetReserveTypeFieldModel.fromJson(baseResp.responseData).data;
    } else {
      pageState = PageState.ConnectivityError;
      if (baseResp != null) {
        Utils.showToast(baseResp.msg ?? "");
      }
    }
  }

  ///获取已预订订单
  Future<NotebookDetail> getReserveDetail(
      BuildContext context, Map<String, String> params,
      {isShowLoading = true, bool showCircle = false}) async {
    if (isShowLoading) {
      pageState = PageState.Loading;
      notifyListeners();
    }
    BaseResp<Map<String, dynamic>> baseResp = await DioUtil()
        .request<Map<String, dynamic>>(
            context, Method.get, HttpUrl.reserve_detail,
            data: params, showLoading: isShowLoading ? false : showCircle)
        .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();
    });
    if (baseResp.code == Constant.status_success) {
      if (baseResp.data.isEmpty) {
        pageState = PageState.Empty;
      } else {
        pageState = PageState.Success;
      }
      notifyListeners();
      return NotebookDetail.fromJson(baseResp.data);
    } else {
      pageState = PageState.ConnectivityError;
      if (baseResp != null) {
        Utils.showToast(baseResp.msg ?? "");
      }
    }
  }

  ///获取商家信息
  Future<List<Manager>> getManagers(BuildContext context) async {
    UserModel userModel = Provider.of<UserModel>(context, listen: false);
    Map<String, String> params = {
      "seller_id": userModel.user.sellerid,
      "seller_openid": userModel.user.openid
    };
    BaseResp<Map<String, dynamic>> baseResp = await DioUtil()
        .request<Map<String, dynamic>>(context, Method.get, HttpUrl.sellerinfo,
            data: params, showLoading: false)
        .catchError((error) {
      print(error.toString());
    });
    if (baseResp?.code == Constant.status_success) {
      return (baseResp.data["manager"] as List)
          .map((value) => Manager.fromJson(value as Map<String, dynamic>))
          .toList();
    }
  }

  ///设置客户经理
  setReserveCusmnan(BuildContext context, Map<String, String> params) async {
    BaseResp<String> baseResp = await DioUtil()
        .request<String>(context, Method.get, HttpUrl.set_reserve_cusman,
            data: params)
        .catchError((error) {
      print(error.toString());
    });
    if (baseResp.code == Constant.status_success) {
    } else {
      Utils.showToast(baseResp.msg ?? "");
    }
  }

  ///创建收定金
  createReserveDeposit(BuildContext context, Map<String, String> params) async {
    BaseResp<dynamic> baseResp = await DioUtil()
        .request<dynamic>(context, Method.get, HttpUrl.create_reserve_deposit,
            data: params)
        .catchError((error) {
      print(error.toString());
      return false;
    });
    if (baseResp.code == Constant.status_success) {
      return true;
    } else {
      Utils.showToast(baseResp.msg ?? "");
      return false;
    }
  }

  ///创建核餐
  createReserveRecheck(BuildContext context, Map<String, String> params) async {
    BaseResp<dynamic> baseResp = await DioUtil()
        .request<dynamic>(context, Method.get, HttpUrl.create_reserve_recheck,
            data: params)
        .catchError((error) {
      print(error.toString());
      return false;
    });
    if (baseResp.code == Constant.status_success) {
      return true;
    } else {
      Utils.showToast(baseResp.msg ?? "");
      return false;
    }
  }

  //删除预订订金

  delReserveDepositByid(
      BuildContext context, Map<String, String> params) async {
    BaseResp<dynamic> baseResp = await DioUtil()
        .request<dynamic>(
            context, Method.get, HttpUrl.del_reserve_deposit_by_id,
            data: params)
        .catchError((error) {
      print(error.toString());
      return false;
    });
    if (baseResp.code == Constant.status_success) {
      return true;
    } else {
      Utils.showToast(baseResp.msg ?? "");
      return false;
    }
  }

  //取消预订发送短信
  wxSendCancelSms(BuildContext context, Map<String, String> params) async {
    BaseResp<dynamic> baseResp = await DioUtil()
        .request<dynamic>(context, Method.get, HttpUrl.wx_send_cancel_sms,
            data: params, showLoading: false)
        .catchError((error) {
      print(error.toString());
    });
    if (baseResp.code == Constant.status_success) {
      Utils.showToast('取消预订短信已发送给客户');
    } else {
      Utils.showToast(baseResp.msg);
    }
  }

  //安排在线预订
  Future<bool> setReserveStatus(
      BuildContext context, Map<String, String> params,
      {ifPush = true, showLoading = false, Notebook notebook}) async {
    ////到店  2 离店 3
    ///
    ///
    bool can = true;
    if (params['status'] == '2') {
      //到店操作,限制在就餐前一小时之后
      //  订单就餐时间
      DateTime dateTime =
          DateTime.fromMillisecondsSinceEpoch(int.parse(notebook.rdate) * 1000);

      //     // 现在的时间
      //  DateTime
      DateTime now = DateTime.now();
      // DateTime now = DateTime.fromMillisecondsSinceEpoch(1703905139 * 1000);
      var difference = dateTime.difference(now);

      print(difference.inMinutes > 60);
      if (difference.inMinutes > 60) {
        Utils.showToast('当前预订不能点击到店,因为还没到临餐时间!');
        can = false;
        Loading.hideLoading(context);
      }
    } else if (params['status'] == '3') {
      //离店操作,需要订单已经是到店状态
      if (notebook.status != '2') {
        Utils.showToast('当前订单不能点击离店,需要先做到店确认!');
        can = false;
        Loading.hideLoading(context);
      }
    }
    if (can) {
      BaseResp<Map<String, dynamic>> baseResp = await DioUtil()
          .request<Map<String, dynamic>>(
              context, Method.get, HttpUrl.set_reserve_status,
              data: params, showLoading: showLoading)
          .catchError((error) {
        print(error.toString());
      });
      if (baseResp == null) return false;
      if (baseResp.code == Constant.status_success && baseResp.data != null) {
        if (ifPush) {
          UserModel userModel = Provider.of<UserModel>(context, listen: false);
          NavigatorUtil.goNotebookDetailPage(
              context, params["rid"], userModel.user.openid,
              replace: true);
        }
        return true;
      } else {
        Utils.showToast("订单已被处理");
        return false;
      }
    }
    return false;
  }

  // 取消订单设置定金走向
  setReserveDeposit(BuildContext context, Map<String, String> params) async {
    BaseResp<dynamic> baseResp = await DioUtil()
        .request<dynamic>(context, Method.get, HttpUrl.set_reserve_deposit,
            data: params, showLoading: false)
        .catchError((error) {
      print(error.toString());
    });
    if (baseResp.code == Constant.status_success) {
    } else {
      Utils.showToast(baseResp.msg);
    }
  }

  //设置消息状态
  setMessageStatus(BuildContext context, Map<String, String> params) async {
    BaseResp<dynamic> baseResp = await DioUtil()
        .request<dynamic>(context, Method.get, HttpUrl.set_message_status,
            data: params, showLoading: false)
        .catchError((error) {
      print(error.toString());
    });
    if (baseResp.code == Constant.status_success) {
    } else {
      Utils.showToast(baseResp.msg);
    }
  }

  // 编辑订单附加信息

  Future<bool> upReserveInfo(BuildContext context, String rid,
      {String actualPum,
      String conAmount,
      String dateStr,
      String cusdem,
      String sex,
      String name,
      String phone,
      String ymd,
      String reserveType,
      bool showLoading = false,
      bool needToast = true,
      String photoNote}) async {
    if (phone != null && phone != '') {
      //判断是否可以输入自己的手机号
      CustomerProvider customerProvider =
          Provider.of<CustomerProvider>(context, listen: false);
      await customerProvider.getSellerGuest(context);
      if (customerProvider.isselfphone) {
        //录入客户电话时不能用自己手机号(管店)

        User user = Provider.of<UserModel>(context, listen: false).user;
        if (user.userphone == phone || user.userphoneMyPage == phone) {
          Utils.showToast('录入客户电话时不能用自己手机号!');
          return false;
        }
      }
    }
    Map<String, dynamic> params = {
      "rid": rid,
      'note_name': Provider.of<UserModel>(context, listen: false).user.account
    };

    if (photoNote != null) {
      params['photo_note'] = photoNote;
    }
    if (actualPum != null) {
      params['actual_pum'] = actualPum;
    }
    if (conAmount != null) {
      params['con_amount'] = conAmount;
    }
    if (dateStr != null) {
      String timeStr = ymd + ' ' + dateStr + ':00';
      int ddate = DateUtil.getDateMsByTimeStr(timeStr);
      params['ddate'] = '${ddate}'.substring(0, 10); //到店时间
    }
    if (cusdem != null) {
      params['cusdem'] = cusdem;
    }
    if (sex != null) {
      params['sex'] = sex;
    }
    if (name != null) {
      params['rname'] = name;
    }
    if (phone != null && phone != '') {
      params['phone'] = phone;
    }
    if (reserveType != null && reserveType != '') {
      params['reserve_type'] = reserveType;
    }

    bool sucess = await _upReserveInfo(context, params,
        showLoading: showLoading, needToast: needToast);
    if (sucess) {
      if (!showLoading && needToast) {
        Utils.showToast("修改成功!");
      }

      return true;
    }
    return false;
  }

  Future<bool> _upReserveInfo(BuildContext context, Map<String, dynamic> params,
      {ifPush = true, showLoading = false, bool needToast = true}) async {
    BaseResp<dynamic> baseResp = await DioUtil()
        .request<dynamic>(context, Method.get, HttpUrl.up_reserve_info,
            data: params, showLoading: showLoading)
        .catchError((error) {
      print(error.toString());
    });
    if (baseResp == null) return false;
    if (baseResp.code == Constant.status_success && baseResp.data != null) {
      return true;
    } else {
      if (needToast) {
        Utils.showToast(baseResp.msg);
      }

      return false;
    }
  }
}