notebook_model.dart 6.53 KB
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/notebook.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/user_model.dart';
import 'package:one_key/utils/utils.dart';
import 'package:provider/provider.dart';

import '../models/get_board_model.dart';
import '../models/note_book_only_model.dart';

class NoteBookModel extends ChangeNotifier {
  List<Notebook> notebooks = []; //订单列表

  String totalmorning = ""; //一天统计
  String totalnoon = ""; //一天统计
  String totalnight = ""; //一天统计
 String totalmeal1 = ""; //一天统计
  String totalmeal2 = ""; //一天统计
  PageState pageState = PageState.Loading;

  bool isSimple = true;
  List<NoteBookOnlySunModel> notebooksSort = [];

  //获取已预订订单
  Future<List<Notebook>> getReserveList(
      BuildContext context, Map<String, String> params,
      {bool needLoading = false, bool showLoading = true}) async {
    if (needLoading) {
      pageState = PageState.Loading;
      showLoading = false;
      notifyListeners();
    }
    // pageState = PageState.Loading;
    // notifyListeners();
    BaseResp<List> baseResp = await DioUtil()
        .request<List>(context, Method.get, HttpUrl.get_reserve_list,
            data: params, showLoading: needLoading ? false : showLoading)
        .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 == null) return null;
    notebooks = [];
    if (baseResp.code == Constant.status_success) {
      notebooks = baseResp.data.map<Notebook>((value) {
        return Notebook.fromJson(value);
      }).toList();
      // notebooks.removeWhere((element) => element.id == null);
      if (notebooks.isEmpty) {
        pageState = PageState.Empty;
      } else {
        if (notebooks.length > 0) {
          if (isSimple) {
            for (Notebook item in notebooks) {
              item.issimple = true;
            }
          } else {
            for (Notebook item in notebooks) {
              item.issimple = false;
            }
          }
        }
        pageState = PageState.Success;
      }
    } else {
      Utils.showToast(baseResp.msg ?? "");
    }
    notifyListeners();
    return notebooks;
  }

  //获取已预订订单
  Future<List<NoteBookOnlySunModel>> getReserveListSort(
      BuildContext context, Map<String, String> params,
      {bool needLoading = false, bool showLoading = true}) async {
    if (needLoading) {
      pageState = PageState.Loading;
      showLoading = false;
      notifyListeners();
    }
    // pageState = PageState.Loading;
    // notifyListeners();
    BaseResp<List> baseResp = await DioUtil()
        .request<List>(context, Method.get, HttpUrl.get_reserve_list_also_order,
            data: params, showLoading: needLoading ? false : showLoading)
        .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 == null) return null;
    notebooksSort = [];
    if (baseResp.code == Constant.status_success) {
      notebooksSort = NoteBookOnlyModel.fromJson(baseResp.responseData).data;

      if (notebooksSort.isEmpty) {
        pageState = PageState.Empty;
      } else {
        if (notebooksSort.length > 0) {
          if (isSimple) {
            for (NoteBookOnlySunModel item in notebooksSort) {
              item.issimple = true;
            }
          } else {
            for (NoteBookOnlySunModel item in notebooksSort) {
              item.issimple = false;
            }
          }
        }
        pageState = PageState.Success;
      }
    } else {
      Utils.showToast(baseResp.msg ?? "");
    }
    notifyListeners();
    return notebooksSort;
  }

  simpleStateOnly() {
    if (notebooksSort.length > 0) {
      if (isSimple) {
        for (NoteBookOnlySunModel item in notebooksSort) {
          item.issimple = true;
        }
      } else {
        for (NoteBookOnlySunModel item in notebooksSort) {
          item.issimple = false;
        }
      }
    }
  }

  simpleState() {
    if (notebooks.length > 0) {
      if (isSimple) {
        for (Notebook item in notebooks) {
          item.issimple = true;
        }
      } else {
        for (Notebook item in notebooks) {
          item.issimple = false;
        }
      }
    }
  }

  //获取订单统计数目
  getReserveCountDay(BuildContext context, Map<String, String> params) async {
    BaseResp<Map<String, dynamic>> baseResp = await DioUtil()
        .request<Map<String, dynamic>>(
            context, Method.get, HttpUrl.reserve_count_day,
            data: params, showLoading: false)
        .catchError((error) {
      print(error.toString());
    });
    if (baseResp != null && baseResp.code == Constant.status_success) {
      int morning = int.parse(baseResp.data["morning"].toString());
      int noon = int.parse(baseResp.data["noon"].toString());
      int night = int.parse(baseResp.data["night"].toString());
      // int meal1 = int.parse(baseResp.data["meal1"].toString());
      // int meal2 = int.parse(baseResp.data["meal2"].toString());
      // UserModel userModel = Provider.of<UserModel>(context, listen: false);
      // total = "已订" + (morning + noon + night).toString();
      // if (userModel.user.morning.isNotEmpty) {
        totalmorning = "($morning)";
      // }
      // if (userModel.user.noon.isNotEmpty) {
        totalnoon = "($noon)";
      // }
      // if (userModel.user.night.isNotEmpty) {
        totalnight = "($night)";
      // }
      // if (userModel.user.meal1.isNotEmpty) {
        // totalmeal1 = "($meal1)";
      // }
      // if (userModel.user.night.isNotEmpty) {
        // totalmeal2 = "($meal2)";
      // }
      notifyListeners();
    }
  }
}