challenge_model.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import 'package:flutter/foundation.dart';
  2. import 'package:intl/intl.dart';
  3. // 待办
  4. @immutable
  5. class ChallengeModel {
  6. final int? id;
  7. final int actionType; // 1-愿景 2-战略目标 3-挑战目标 4-待办目标
  8. final String title;
  9. final String description;
  10. final String cover;
  11. final int sort;
  12. final int status; // 1-正常 2-进行中 3-已完成 4-失败放弃
  13. final int difficulty; // 1-简单 2-正常 3-困难 4-地狱
  14. final String remark;
  15. final DateTime? startDate;
  16. final DateTime? endDate;
  17. final DateTime? finishDate;
  18. final DateTime? planFinishDate;
  19. final int parentId;
  20. const ChallengeModel({
  21. required this.id,
  22. required this.actionType,
  23. required this.title,
  24. required this.description,
  25. required this.cover,
  26. required this.sort,
  27. required this.status,
  28. required this.difficulty,
  29. required this.remark,
  30. required this.startDate,
  31. required this.endDate,
  32. required this.finishDate,
  33. required this.parentId,
  34. required this.planFinishDate,
  35. });
  36. factory ChallengeModel.fromJson(Map<String, dynamic> json) {
  37. return ChallengeModel(
  38. id: json['id'] as int?,
  39. title: json['title'] as String,
  40. description: json['description'] as String,
  41. startDate: DateTime.parse(json['start_date'] as String),
  42. endDate: DateTime.parse(json['end_date'] as String),
  43. cover: json['cover'] as String,
  44. sort: json['sort'] as int,
  45. status: json['status'] as int,
  46. remark: json['remark'] as String,
  47. finishDate: DateTime.parse(json['finish_date'] as String),
  48. parentId: json['parent_id'] as int,
  49. actionType: 0,
  50. difficulty: 0,
  51. planFinishDate: DateTime.parse(json['end_date'] as String),
  52. );
  53. }
  54. /// 转换为JSON
  55. Map<String, dynamic> toJson() => {
  56. if (id != null) 'id': id,
  57. 'title': title,
  58. 'description': description,
  59. };
  60. ChallengeModel copyWith({
  61. int? id,
  62. int? actionType,
  63. String? title,
  64. String? description,
  65. int? status,
  66. int? sort,
  67. String? cover,
  68. String? remark,
  69. int? difficulty,
  70. DateTime? startDate,
  71. DateTime? endDate,
  72. DateTime? finishDate,
  73. DateTime? planFinishDate,
  74. int? parentId,
  75. }) {
  76. return ChallengeModel(
  77. id: id ?? this.id,
  78. actionType: actionType ?? this.actionType,
  79. title: title ?? this.title,
  80. description: description ?? this.description,
  81. startDate: startDate ?? this.startDate,
  82. endDate: endDate ?? this.endDate,
  83. cover: cover ?? this.cover,
  84. sort: sort ?? this.sort,
  85. status: status ?? this.status,
  86. remark: remark ?? this.remark,
  87. finishDate: finishDate ?? this.finishDate,
  88. planFinishDate: planFinishDate ?? this.planFinishDate,
  89. parentId: parentId ?? this.parentId,
  90. difficulty: difficulty ?? this.difficulty,
  91. );
  92. }
  93. }
  94. // 挑战类的 form 表单,用于绑定等
  95. class ChallengeFormModel {
  96. int? id;
  97. int actionType; // 1-愿景 2-战略目标 3-挑战目标 4-待办目标
  98. String title;
  99. String description;
  100. String cover;
  101. int sort;
  102. int status; // 1-正常 2-进行中 3-已完成 4-失败放弃
  103. int difficulty; // 1-简单 2-正常 3-困难 4-地狱
  104. String remark;
  105. DateTime? startDate;
  106. DateTime? endDate;
  107. DateTime? finishDate;
  108. DateTime? planFinishDate;
  109. int parentId;
  110. ChallengeFormModel({
  111. this.id,
  112. this.actionType = 0,
  113. this.title = '',
  114. this.description = '',
  115. this.cover = '',
  116. this.sort = 100,
  117. this.status = 0,
  118. this.difficulty = 0,
  119. this.remark = '',
  120. this.startDate,
  121. this.endDate,
  122. this.finishDate,
  123. this.planFinishDate,
  124. this.parentId = 0
  125. });
  126. // 转换为Map方便打印
  127. Map<String, dynamic> toMap() {
  128. return {
  129. 'id': id,
  130. 'actionType': actionType ,
  131. 'title': title,
  132. 'description': description,
  133. 'cover': cover,
  134. 'sort': sort,
  135. 'status': status,
  136. 'difficulty': difficulty,
  137. 'remark': remark,
  138. 'startDate': startDate != null ? DateFormat('yyyy-MM-dd').format(startDate!) : null,
  139. 'endDate': endDate != null ? DateFormat('yyyy-MM-dd').format(endDate!) : null,
  140. 'finishDate': finishDate != null ? DateFormat('yyyy-MM-dd').format(finishDate!) : null,
  141. 'planFinishDate': planFinishDate != null ? DateFormat('yyyy-MM-dd').format(planFinishDate!) : null,
  142. 'parentId': parentId,
  143. };
  144. }
  145. // 添加转换方法
  146. factory ChallengeFormModel.fromChallenge(ChallengeModel challenge) {
  147. return ChallengeFormModel(
  148. id: challenge.id,
  149. actionType: challenge.actionType,
  150. title: challenge.title,
  151. description: challenge.description,
  152. cover: challenge.cover,
  153. sort: challenge.sort,
  154. status: challenge.status,
  155. difficulty: challenge.difficulty,
  156. remark: challenge.remark,
  157. startDate: challenge.startDate,
  158. endDate: challenge.endDate,
  159. finishDate: challenge.finishDate,
  160. planFinishDate: challenge.planFinishDate,
  161. parentId: challenge.parentId,
  162. );
  163. }
  164. ChallengeModel toChallenge() {
  165. return ChallengeModel(
  166. id: id,
  167. actionType: actionType,
  168. title: title,
  169. description: description,
  170. cover: cover,
  171. sort: sort,
  172. status: status,
  173. difficulty: difficulty,
  174. remark: remark,
  175. startDate: startDate,
  176. endDate: endDate,
  177. finishDate: finishDate,
  178. planFinishDate: planFinishDate,
  179. parentId: parentId,
  180. );
  181. }
  182. }