/*
 * @Author: WuWei
 * @Date: 2020-06-19 17:12:09
 * @LastEditors: wuwei
 * @LastEditTime: 2023-12-14 09:58:24
 * @Description:
 */

import 'package:app_package/app_package.dart';
import 'package:task_package/src/models/department.dart';
import 'package:task_package/src/models/task_multi_list.dart';
import '../../src/models/my_time_line.dart';
import '../../src/models/task_assign_model.dart';
import '../../src/models/task_detail.dart';
import '../../src/models/task_emp_infos.dart';
import '../../src/models/task_inventory_model.dart';
import '../../src/models/task_inventory_stat_model.dart';
import '../../src/models/task_item_model.dart';
import '../../src/models/task_manage_stat_model.dart';
import '../../src/models/task_mycc_v2_model.dart';
import '../../src/models/task_myorgan_model.dart';
import '../../src/models/task_organ_stat.dart';
import '../../src/models/task_plan_detail.dart';
import '../../src/models/task_state.dart';
import '../../src/models/task_statistic_model.dart';
import '../models/labels.dart';
import '../service/task_service.dart';
import '../models/task_child.dart';
import '../../src/models/record_abnormal_model.dart';
import '../../src/models/sort_list.dart';
import '../models/task_inventory_tree_model.dart';
// import '../models/task_mycc.dart';

class TaskServiceImpl implements TaskService {
  static TaskServiceImpl? get instance => _getInstance();
  static TaskServiceImpl? _instance;
  TaskServiceImpl._internal() {
    // 初始化
  }
  static TaskServiceImpl? _getInstance() {
    if (_instance == null) {
      _instance = new TaskServiceImpl._internal();
    }
    return _instance;
  }

  static String prefix = '/app/admin/excutive';

  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/15197
  /// 子任务列表-v2
  Future<ResModel<List<TaskChildModel>?>> getTaskChildList(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v2/child',
      queryParameters: data,
    );

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

  /// 任务转派
  @override
  Future<bool> putTransfer(data) async {
    var res = await HttpUtil.instance.put(
      '$prefix/v1/transfer/${data['id']}',
      queryParameters: data,
    );
    return res.data['data'] == 1;
  }

  @override
  Future<int?> postChildTask(data) async {
    var res = await HttpUtil.instance.post(
      '$prefix/v1/create/child',
      data: data,
    );

    if (res.data['data'] != null && res.data['data'] is int) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  ///任务删除
  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/7775
  Future<ResModel<int?>> delete(id) async {
    var res = await HttpUtil.instance.delete('$prefix/v1/delete/$id');
    return ResModel<int?>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }

  ///任务完成
  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/7773
  Future<ResModel<int?>> putFinished(data) async {
    var res = await HttpUtil.instance.put(
      '$prefix/v2/finished/${data['id']}',
      queryParameters: data,
    );
    return ResModel<int?>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }

  ///任务重新打开
  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/7791
  Future<ResModel<int?>> putReopen(data) async {
    var res = await HttpUtil.instance.put(
      '$prefix/v1/reopen',
      data: data,
    );
    return ResModel<int?>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }

  ///任务动态查询
  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/7799
  Future<ResModel<List<TaskStateModel>?>> getState(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/comment',
      queryParameters: data,
    );

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

  ///任务详情
  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/7821
  Future<ResModel<TaskDetailModel>> getTaskDetail(id) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/excutive/v2/$id',
    );

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

  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/8369
  ///我的任务列表（已完成/未完成/当前未开始）- 版本2
  Future<ResModel<PagingModel<TaskItemModel>>> getMyTaskList(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v2/my',
      queryParameters: data,
    );
    return ResModel<PagingModel<TaskItemModel>>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => PagingModel(
        json: v,
        to: (v) => v.toJson(),
        from: (v) => TaskItemModel.fromJson(v),
      ),
    );
  }

  /// 检查是否有子任务未完成
  Future<bool?> getCheckChild(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/checkChild/${data['id']}',
      queryParameters: data,
    );

    if (res.data['data'] != null && res.data['data'] is bool) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  /// 任务用时
  Future<double?> getUseTime(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/v1/useTime/${data['id']}',
      queryParameters: data,
    );

    if (res.data['data'] != null && res.data['data'] is double) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  ///任务开始
  Future<bool> putStartTask(id) async {
    var res = await HttpUtil.instance.put('$prefix/v1/start/$id');
    return res.data['data'] == 1;
  }

  ///创建任务
  Future<bool> postCreate(Map data) async {
    var res = await HttpUtil.instance.post('$prefix/v2/create', data: data);
    return res.data['data'] == 1;
  }

  ///修改任务
  Future<bool> putUpdate(Map data) async {
    var res = await HttpUtil.instance.put('$prefix/v1/update', data: data);
    return res.data['data'] == 1;
  }

  /// 我的日程
  /// /app/admin/plan/Timeline
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/14612
  Future<ResModel<List<MyTimeLineModel>?>> getMyTimeLine(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/plan/Timeline',
      queryParameters: data,
    );

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

  /// /app/admin/plan/argsDetails
  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/14621
  ///
  Future<ResModel<TaskPlanDetailModel>> getPlanDetail(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/plan/argsDetails',
      queryParameters: data,
    );

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

  /// 任务新增-v4
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/18212
  Future<ResModel<num?>> postTaskAddV3(data) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/excutive/v4/create',
      data: data,
    );

    return ResModel<num?>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }

  /// 任务编辑-v3
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/18239
  Future<ResModel<num?>> putTaskUpdateV2(data) async {
    var res = await HttpUtil.instance.put(
      '/app/admin/excutive/v3/update',
      data: data,
    );

    return ResModel<num?>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }

  /// 子任务添加-v2
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/14666
  Future<ResModel<num?>> postTaskChildV2(data) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/excutive/v2/create/child',
      data: data,
    );

    return ResModel<num?>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }

  ///任务取消
  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/15215
  Future<ResModel<int?>> putTaskCancelV1(data) async {
    var res = await HttpUtil.instance.put(
      '/app/admin/excutive/v1/cancel/${data['id']}',
      data: data,
    );

    return ResModel<int?>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }

  /// 我指派的任务列表 -v2
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/14675
  Future<ResModel<PagingModel<TaskAssignModel>>> getAssignTaskList(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/excutive/v2/assign',
      queryParameters: data,
    );

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

  ///抄送我的任务列表-v2
  ////app/admin/excutive/v2/cc/my
  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/14684
  Future<ResModel<PagingModel<TaskMyccV2Model>>> getMyCcTaskList(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/excutive/v2/cc/my',
      queryParameters: data,
    );

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

  /// 本部门的任务列表
  /// /app/admin/excutive/v1/myOrgan
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/14693
  Future<ResModel<PagingModel<TaskMyorganModel>>> getMyOrganList(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/excutive/v1/myOrgan',
      queryParameters: data,
    );

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

  ///任务统计-（四期通用）
  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/14747
  Future<ResModel<TaskStatisticModel>> getTaskStatictis(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/excutive/v2/taskStatistic',
      queryParameters: Map<String, dynamic>.from(data),
    );

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

  /// 任务类型列表
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/14711
  Future<ResModel<List<TaskInventoryModel>?>> getInventoryList(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/inventory/v2',
      queryParameters: data,
    );

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

  /// 任务清单树（二级三级任务类型）
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/24388
   Future<ResModel<List<TaskInventoryTreeModel>?>> getTaskInventoryTree(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/inventory/tree',
      queryParameters: data,
    );

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

  ///  任务管理统计(我的，我派发的，抄送我的,部门））
  /// /app/admin/excutive/v1/taskManageStatistic
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/14756
  Future<ResModel<TaskManageStatModel>> getTaskManageStatistic(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/excutive/v1/taskManageStatistic',
      queryParameters: data,
    );

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

  /// 任务类型统计
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/14765
  Future<ResModel<List<TaskInventoryStatModel>?>> getTaskInventoryStat(
      data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/inventory/stat',
      queryParameters: data,
    );

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

  /// 本人计划任务数量
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/14774
  Future<ResModel<num?>> getMypalnCount(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/plan/count',
      queryParameters: data,
    );

    return ResModel<num?>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }

  /// 部门统计-登录人管理的部门（根据部门角色定）
  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/14801
  Future<ResModel<List<TaskOrganStat>?>> getEployeeOransStat(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/employee/organs',
      queryParameters: data,
    );

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

  /// 人员统计-登录人管理部门下的人员
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/14810
  Future<ResModel<List<TaskEmpInfos>?>> getEmployeeEmpInfos(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/employee/empInfos',
      queryParameters: data,
    );

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

  ///评论动态新增
  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/7793
  Future<ResModel<int?>> postCommentCreate(data) async {
    var res = await HttpUtil.instance.post(
      '$prefix/v1/comment/create',
      data: data,
    );

    return ResModel<int?>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }

  /// 审查任务
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/20616
  Future<ResModel<int>> putTaskCheck(data) async {
    var res = await HttpUtil.instance.put(
      '/app/admin/excutive/toExamine',
      data: data,
    );
    return ResModel<int>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }

  /// 任务重启
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/7791
  Future<ResModel<int>> putTaskReopen(data) async {
    var res = await HttpUtil.instance.put(
      '/app/admin/excutive/v1/reopen',
      data: data,
    );
    return ResModel<int>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }

  ////////////////////////////////
  /// 领取任务
  ///
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/16565
  Future<ResModel<num?>> getTaskReceive(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/excutivePlatform/${data['id']}/receive',
      queryParameters: data,
    );
    return ResModel<num?>(
      json: res.data,
      to: (v) => v,
      from: (v) => v,
    );
  }

  /// 任务多环节列表
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/18392
  Future<ResModel<List<TaskMultiList>?>> getMultiTaskList(exPlatformId) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/excutivePlatformNode/getTaskMultiSteps/$exPlatformId',
    );

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

  /// 催办
  Future<int?> postTaskUrge(id) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/excutivePlatformNode/urge/$id',
    );

    if (res.data['data'] != null && res.data['data'] is int) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  /// 一键催办
  Future<int?> postTaskMultiUrge(id) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/excutivePlatformNode/oneClickUrge/$id',
    );

    if (res.data['data'] != null && res.data['data'] is int) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  /// 动态删除
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/20422
  Future<int?> deleteTaskState(id) async {
    var res = await HttpUtil.instance.delete(
      '/app/admin/excutive/v1/comment/delete/$id',
    );
    if (res.data['data'] != null && res.data['data'] is int) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/21523
  /// 作业统计视图
  Future<ResModel<TaskStatisticModel>> getWorkStatisticTotal(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/excutivePlatformStatisticsView',
      queryParameters: data,
    );

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

  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/21508
  /// 操作记录异常排名
  Future<ResModel<PagingModel<RecordAbnormalModel>>> getRecordAbnormalRank(
      data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/operationRecord/countList',
      queryParameters: data,
    );
    return ResModel<PagingModel<RecordAbnormalModel>>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => PagingModel(
        json: v,
        to: (v) => v.toJson(),
        from: (v) => RecordAbnormalModel.fromJson(v),
      ),
    );
  }

  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/22113
  /// 按月份查询每天的是否存在未处理的操作记录标识
  Future<ResModel<List<CalendarAbnormalList>>> getRecordIdsByMonth(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/operationRecord/notSolveByMonth',
      queryParameters: data,
    );

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

  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/21498
  /// 操作记录列表
  Future<ResModel<PagingModel<RecordAbnormalModel>>> getRecordList(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/operationRecord',
      queryParameters: data,
    );

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

  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/21503
  /// 排名列表分组类型枚举
  Future<ResModel<List<EnumModel>>> getRecordRankType() async {
    var res = await HttpUtil.instance
        .get('/app/admin/operationRecord/countList/groupType');

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

  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/21493
  /// 部门树
  Future<ResModel<DepartmentModel>> getDepartmentTree() async {
    var res = await HttpUtil.instance
        .get('/app/admin/organs/getOrganWebTreeByCurrentEmployee');

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

  Future<ResModel<List<LabelModel>>> getLabelList() async {
    var res = await HttpUtil.instance.get(
      '/app/admin/excutive/labelsEnum',
    );

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