/*
 * @Author       : WuWei
 * @LastEditors: wuwei
 * @Date         : 2021-08-26 10:35:25
 * @LastEditTime: 2023-12-29 10:43:37
 * @Description  : 二维码任务二期-api接口
 */
import 'package:app_package/app_package.dart';
import 'package:task_package/src/models/sort_list.dart';
import 'package:task_package/src/models/tak_operation_instruction.dart';
import 'package:task_package/src/models/task_check.dart';
import 'package:task_package/src/models/task_equipment_repair.dart';
import 'package:task_package/src/models/task_excutive_receive.dart';
import 'package:task_package/src/models/task_operation_form.dart';
import 'package:task_package/src/models/task_operation_instruction_list.dart';
import 'package:task_package/src/models/task_operation_node_child_list.dart';
import 'package:task_package/src/models/task_operation_node_detail.dart';
import 'package:task_package/src/models/task_operation_node_list.dart';
import 'package:task_package/src/models/task_operation_standard.dart';
import 'package:task_package/src/models/type_list.dart';
import 'package:task_package/src/service/qrcode_service.dart';
import '../../src/models/task_item_model.dart';

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

  static String prefix = '/app/admin';

  /// 任务id和主体id列表获取任务节点详情
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/16439
  Future<ResModel<TaskOperationFormModel>> getTaskOperationDetail(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/excutivePlatformNode/detailByExPlatformIdAndRelationSubjectIds',
      queryParameters: data,
    );

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

  /// 任务节点操作记录新增
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/16448
  Future<int?> postOperationForm(data) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/excutivePlatformNode/operation',
      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/16457
  Future<ResModel<List<TaskOperationStandard>?>> getOperationStandardList(
      data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/workStandard/byExPlatformId',
      queryParameters: data,
    );

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

  /// 任务节点列表
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/16484
  Future<ResModel<TaskOperationNodeList>> getOperationNodeList(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/excutivePlatformNode/list/${data['excutivePlatformId']}',
    );

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

  /// 任务节点对应的子节点列表
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/16493
  Future<ResModel<List<TaskOperationNodeChildList>?>> getOperationNodeChildList(
      data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/excutivePlatformNode/child/${data['id']}',
    );

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

  /// 获取作业指导内容
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/16682
  Future<ResModel<TakOperationInstruction>> getOperationInstructionDetail(
      data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/workInstruction/${data['id']}',
      queryParameters: data,
    );

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

  /// 设备设备间报修新增
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/17114
  Future<int?> postEquipmentRepair(data) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/repairTaskRecord/${data['excutivePlatformLinkId']}',
      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/17114
  Future<ResModel<TaskEquipmentRepair>> getEquipmentRepairDetail(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/repairTaskRecord/${data['excutivePlatformLinkId']}',
    );

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

  /// 根据主体id获取作业指导列表
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/17123
  Future<ResModel<List<TaskOperationInstructionList>?>>
      getOperationInstructionList(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/workInstruction/byPlatformNodeId',
      queryParameters: data,
    );

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

  /// 设置作业标准已读状态
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/21603
  Future<ResModel<int?>> setPlatformNodeRead(data) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/excutivePlatformNode/${data['id']}/read',
    );

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

  /// 根据环节id获取操作记录结果
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/17132
  Future<ResModel<TaskOperationNodeDetail>> getOperationNodeDetail(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/operationRecord/${data['excutivePlatformLinkId']}',
    );

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

  /// 任务节点详情
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/17348
  Future<ResModel<TaskOperationFormModel>> getOperationDetail(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/excutivePlatformNode/${data['id']}',
      queryParameters: data,
    );

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

  /// 任务大厅/待领取列表
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/16574
  Future<ResModel<PagingModel<TaskExcutiveReceive>>> getTaskExcutiveReceive(
      data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/excutive/receive',
      queryParameters: data,
    );

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

  /// 领取任务
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/16565
  Future<int?> postTaskReceive(data) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/excutive/receive/${data['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/16574
  Future<ResModel<PagingModel<TaskItemModel>>> getTaskList(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/excutive',
      queryParameters: Map<String, dynamic>.from(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<ResModel<TaskCheck>> getCheckTask(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/excutivePlatformNode/checkExecutableNodeByExPlatformId',
      queryParameters: data,
    );

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

  /// 设备点/设备间报修
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/20696
  Future<String?> postTaskEquRepair(data) async {
    var res = await HttpUtil.instance.post(
      '/app/admin/repair/v2',
      data: data,
    );
    return res.data['data'];
  }

  /// 类别列表 http://yapi.wisdomcity.com.cn/project/16/interface/api/20670
  Future<ResModel<List<TypeListModel>>> getTypeList() async {
    var res = await HttpUtil.instance.get(
      '/app/admin/repairBusinessType',
    );

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

  /// 分类列表 http://yapi.wisdomcity.com.cn/project/16/interface/api/20670
  Future<ResModel<List<SortListModel>>> getSortList(data) async {
    var res = await HttpUtil.instance.get(
      '/app/admin/repairSort/name',
      queryParameters: data,
    );
    return ResModel<List<SortListModel>>(
      json: res.data,
      to: (v) => (v).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<SortListModel>((f) => SortListModel.fromJson(f))
          .toList(),
    );
  }
}
