/*
 * @Author: WuXiangNan
 * @Date: 2021-05-27 14:44:35
 * @LastEditors: WuXiangNan
 * @LastEditTime: 2021-08-16 11:19:25
 * @Description: 实现绘经营数据接口
 */

import 'package:app_package/app_package.dart';
import 'package:business_package/src/models/community/community_data.dart';
import 'package:business_package/src/models/serve_data/serve_data.dart';
import 'package:business_package/src/models/platform_data_line_chart.dart';
import 'package:business_package/src/models/platform_data.dart';
import 'package:business_package/src/models/hardware_data/hardware_data.dart';
import 'package:business_package/src/models/community_statistics.dart';
import 'package:business_package/src/models/bill_data_line_chart.dart';
import 'package:business_package/src/models/bill_data.dart';
import 'package:business_package/src/service/business_service.dart';

class BusinessServiceImpl implements BusinessService {
  static String prefix = '/app/admin/bi';
  @override
  Future<ResModel<BillData>> getBillData(params) async {
    var res = await HttpUtil.instance.get(
      '$prefix/bill-data',
      queryParameters: params,
    );

    return ResModel<BillData>(
        json: res.data,
        to: (v) => v.toJson(),
        from: (v) => BillData.fromJson(v));
  }

  @override
  Future<ResModel<BillDataLineChart>> getBillDataLineChart(params) async {
    var res = await HttpUtil.instance.get(
      '$prefix/bill-data/line-chart',
      queryParameters: params,
    );

    return ResModel<BillDataLineChart>(
        json: res.data,
        to: (v) => v.toJson(),
        from: (v) => BillDataLineChart.fromJson(v));
  }

  @override
  Future<ResModel<CommunityStatistics>> getCommunityStatistics(params) async {
    var res = await HttpUtil.instance.get(
      '$prefix/community-statistics',
      queryParameters: params,
    );

    return ResModel<CommunityStatistics>(
        json: res.data,
        to: (v) => v.toJson(),
        from: (v) => CommunityStatistics.fromJson(v));
  }

  @override
  Future<ResModel<HardwareData>> getHardwareData(params) async {
    var res = await HttpUtil.instance.get(
      '$prefix/hardware-data',
      queryParameters: params,
    );

    return ResModel<HardwareData>(
        json: res.data,
        to: (v) => v.toJson(),
        from: (v) => HardwareData.fromJson(v));
  }

  @override
  Future<ResModel<PlatformData>> getPlatformData(params) async {
    var res = await HttpUtil.instance.get(
      '$prefix/platform-data',
      queryParameters: params,
    );

    return ResModel<PlatformData>(
        json: res.data,
        to: (v) => v.toJson(),
        from: (v) => PlatformData.fromJson(v));
  }

  @override
  Future<ResModel<PlatformDataLineChart>> getPlatformDataLineChart(
      params) async {
    var res = await HttpUtil.instance.get(
      '$prefix/platform-data/line-chart',
      queryParameters: params,
    );

    return ResModel<PlatformDataLineChart>(
        json: res.data,
        to: (v) => v.toJson(),
        from: (v) => PlatformDataLineChart.fromJson(v));
  }

  @override
  Future<ResModel<ServeData>> getServeData(params) async {
    var res = await HttpUtil.instance.get(
      '$prefix/serve-data',
      queryParameters: params,
    );

    return ResModel<ServeData>(
        json: res.data,
        to: (v) => v.toJson(),
        from: (v) => ServeData.fromJson(v));
  }

  @override
  Future<ResModel<List<CommunityData>?>> getCommunities(params) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/statistics/sift',
      queryParameters: params,
    );

    return ResModel<List<CommunityData>?>(
      json: res.data,
      to: (v) => (v ?? []).map((e) => e.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<CommunityData>(
            (f) => CommunityData.fromJson(f),
          )
          .toList(),
    );
  }
}
