outing_log_model.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import 'outing_log_comment.dart';
  2. class OutingLogModel {
  3. final String id;
  4. final String visitNo;
  5. final String salespersonId;
  6. final String salespersonName;
  7. final String deptId;
  8. final String deptName;
  9. final String customerId;
  10. final String customerName;
  11. final String contactId;
  12. final String contactName;
  13. final String contactPhone;
  14. final double? checkInLongitude;
  15. final double? checkInLatitude;
  16. final double gpsAccuracy;
  17. final String checkInAddress;
  18. final String visitSummary;
  19. final String nextPlan;
  20. final List<String> visitPhotos;
  21. final String status;
  22. final List<OutingLogComment> comments;
  23. final DateTime? lastViewedTime;
  24. final DateTime createTime;
  25. final DateTime updateTime;
  26. const OutingLogModel({
  27. required this.id,
  28. required this.visitNo,
  29. this.salespersonId = '',
  30. this.salespersonName = '',
  31. this.deptId = '',
  32. this.deptName = '',
  33. this.customerId = '',
  34. required this.customerName,
  35. this.contactId = '',
  36. this.contactName = '',
  37. this.contactPhone = '',
  38. this.checkInLongitude,
  39. this.checkInLatitude,
  40. this.gpsAccuracy = 15.0,
  41. this.checkInAddress = '',
  42. required this.visitSummary,
  43. this.nextPlan = '',
  44. this.visitPhotos = const [],
  45. this.status = 'completed',
  46. this.comments = const [],
  47. this.lastViewedTime,
  48. required this.createTime,
  49. required this.updateTime,
  50. });
  51. bool get hasNewComment {
  52. if (comments.isEmpty) return false;
  53. if (lastViewedTime == null) return true;
  54. return comments.any((c) => c.createTime.isAfter(lastViewedTime!));
  55. }
  56. bool get isDraft => status == 'draft';
  57. bool get isCompleted => status == 'completed';
  58. String get statusLabel => isDraft ? '草稿' : '已完成';
  59. String get statusCode => status;
  60. factory OutingLogModel.fromJson(Map<String, dynamic> json) {
  61. return OutingLogModel(
  62. id: json['id'] as String,
  63. visitNo: json['visitNo'] as String? ?? '',
  64. salespersonId: json['salespersonId'] as String? ?? '',
  65. salespersonName: json['salespersonName'] as String? ?? '',
  66. deptId: json['deptId'] as String? ?? '',
  67. deptName: json['deptName'] as String? ?? '',
  68. customerId: json['customerId'] as String? ?? '',
  69. customerName: json['customerName'] as String? ?? '',
  70. contactId: json['contactId'] as String? ?? '',
  71. contactName: json['contactName'] as String? ?? '',
  72. contactPhone: json['contactPhone'] as String? ?? '',
  73. checkInLongitude: (json['checkInLongitude'] as num?)?.toDouble(),
  74. checkInLatitude: (json['checkInLatitude'] as num?)?.toDouble(),
  75. gpsAccuracy: (json['gpsAccuracy'] as num?)?.toDouble() ?? 15.0,
  76. checkInAddress: json['checkInAddress'] as String? ?? '',
  77. visitSummary: json['visitSummary'] as String? ?? '',
  78. nextPlan: json['nextPlan'] as String? ?? '',
  79. visitPhotos: (json['visitPhotos'] as List<dynamic>?)
  80. ?.map((e) => e as String)
  81. .toList() ??
  82. [],
  83. status: json['status'] as String? ?? 'completed',
  84. comments: (json['comments'] as List<dynamic>?)
  85. ?.map(
  86. (e) =>
  87. OutingLogComment.fromJson(e as Map<String, dynamic>),
  88. )
  89. .toList() ??
  90. [],
  91. lastViewedTime: json['lastViewedTime'] != null
  92. ? DateTime.parse(json['lastViewedTime'] as String)
  93. : null,
  94. createTime: DateTime.parse(json['createTime'] as String),
  95. updateTime: DateTime.parse(json['updateTime'] as String),
  96. );
  97. }
  98. Map<String, dynamic> toJson() => {
  99. 'id': id,
  100. 'visitNo': visitNo,
  101. 'salespersonId': salespersonId,
  102. 'salespersonName': salespersonName,
  103. 'deptId': deptId,
  104. 'deptName': deptName,
  105. 'customerId': customerId,
  106. 'customerName': customerName,
  107. 'contactId': contactId,
  108. 'contactName': contactName,
  109. 'contactPhone': contactPhone,
  110. 'checkInLongitude': checkInLongitude,
  111. 'checkInLatitude': checkInLatitude,
  112. 'gpsAccuracy': gpsAccuracy,
  113. 'checkInAddress': checkInAddress,
  114. 'visitSummary': visitSummary,
  115. 'nextPlan': nextPlan,
  116. 'visitPhotos': visitPhotos,
  117. 'status': status,
  118. 'comments': comments.map((c) => c.toJson()).toList(),
  119. 'lastViewedTime': lastViewedTime?.toIso8601String(),
  120. 'createTime': createTime.toIso8601String(),
  121. 'updateTime': updateTime.toIso8601String(),
  122. };
  123. }