job_applied_controller.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import 'package:cpt_job/modules/applied_staff_detail/applied_staff_detail_page.dart';
  2. import 'package:cpt_job/modules/job_applied_edit/job_applied_edit_page.dart';
  3. import 'package:cpt_job/widget/applied_staff_reviews.dart';
  4. import 'package:domain/entity/response/job_list_applied_staff_list_entity.dart';
  5. import 'package:domain/repository/job_repository.dart';
  6. import 'package:get/get.dart';
  7. import 'package:plugin_basic/constants/app_constant.dart';
  8. import 'package:plugin_platform/engine/dialog/dialog_engine.dart';
  9. import 'package:plugin_platform/engine/notify/notify_engine.dart';
  10. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  11. import 'package:plugin_platform/http/dio/dio_cancelable_mixin.dart';
  12. import 'package:shared/utils/date_time_utils.dart';
  13. import 'package:shared/utils/event_bus.dart';
  14. import 'package:shared/utils/util.dart';
  15. import 'package:widgets/dialog/app_default_dialog.dart';
  16. import 'package:widgets/load_state_layout.dart';
  17. import 'package:widgets/widget_export.dart';
  18. import '../../widget/applied_add_staff.dart';
  19. import '../../widget/applied_butch_modify.dart';
  20. import '../applied_workflow/applied_workflow_page.dart';
  21. import 'job_applied_state.dart';
  22. class JobAppliedController extends GetxController with DioCancelableMixin {
  23. final JobRepository _jobRepository = Get.find();
  24. final JobAppliedState state = JobAppliedState();
  25. var _curPage = 1;
  26. var _needShowPlaceholder = true;
  27. //页面PlaceHolder的展示
  28. LoadState loadingState = LoadState.State_Success;
  29. String? errorMessage;
  30. //刷新页面状态
  31. void changeLoadingState(LoadState state) {
  32. loadingState = state;
  33. update();
  34. }
  35. // Refresh 控制器
  36. final EasyRefreshController refreshController = EasyRefreshController(
  37. controlFinishRefresh: true,
  38. controlFinishLoad: true,
  39. );
  40. // Refresh 刷新事件
  41. Future onRefresh() async {
  42. _curPage = 1;
  43. fetchAppliedStaffList();
  44. }
  45. // Refresh 加载事件
  46. Future loadMore() async {
  47. _curPage++;
  48. fetchAppliedStaffList();
  49. }
  50. // 重试请求
  51. Future retryRequest() async {
  52. _curPage = 1;
  53. _needShowPlaceholder = true;
  54. fetchAppliedStaffList();
  55. }
  56. /// 获取服务器数据,通知消息列表
  57. Future fetchAppliedStaffList() async {
  58. if (_needShowPlaceholder) {
  59. changeLoadingState(LoadState.State_Loading);
  60. }
  61. if (state.jobInfo == null) {
  62. //不是并发的,顺序执行接口
  63. var jobInfoResult = await _jobRepository.fetchJobAppliedInfo(
  64. state.jobId,
  65. cancelToken: cancelToken,
  66. );
  67. if (jobInfoResult.isSuccess) {
  68. state.jobInfo = jobInfoResult.data;
  69. update();
  70. } else {
  71. errorMessage = jobInfoResult.errorMsg;
  72. changeLoadingState(LoadState.State_Error);
  73. return;
  74. }
  75. }
  76. // 获取 Applied 列表
  77. var listResult = await _jobRepository.fetchJobAppliedStaffs(
  78. state.jobId,
  79. state.keyword,
  80. curPage: _curPage,
  81. cancelToken: cancelToken,
  82. );
  83. // 处理数据
  84. if (listResult.isSuccess) {
  85. handleList(listResult.data?.rows);
  86. } else {
  87. errorMessage = listResult.errorMsg;
  88. changeLoadingState(LoadState.State_Error);
  89. }
  90. // 最后赋值
  91. _needShowPlaceholder = false;
  92. }
  93. // 处理数据与展示的逻辑
  94. void handleList(List<JobListAppliedStaffListRows>? list) {
  95. if (list != null && list.isNotEmpty) {
  96. //有数据,判断是刷新还是加载更多的数据
  97. if (_curPage == 1) {
  98. //刷新的方式
  99. state.datas.clear();
  100. state.datas.addAll(list);
  101. refreshController.finishRefresh();
  102. //更新展示的状态
  103. changeLoadingState(LoadState.State_Success);
  104. } else {
  105. //加载更多
  106. state.datas.addAll(list);
  107. refreshController.finishLoad();
  108. update();
  109. }
  110. } else {
  111. if (_curPage == 1) {
  112. //展示无数据的布局
  113. state.datas.clear();
  114. changeLoadingState(LoadState.State_Empty);
  115. refreshController.finishRefresh();
  116. } else {
  117. //展示加载完成,没有更多数据了
  118. refreshController.finishLoad(IndicatorResult.noMore);
  119. }
  120. }
  121. }
  122. @override
  123. void onReady() {
  124. super.onReady();
  125. fetchAppliedStaffList();
  126. registerEventBus();
  127. }
  128. @override
  129. void onClose() {
  130. unregisterEventBus();
  131. state.datas.clear();
  132. super.onClose();
  133. }
  134. // EventBus 的事件接收
  135. Subscription? subscribe;
  136. void registerEventBus() {
  137. subscribe = bus.on(AppConstant.eventAppliedListRefresh, (arg) {
  138. var appliedId = arg as String;
  139. if (Utils.isNotEmpty(appliedId)) {
  140. fetchItemByIdAndRefreshItem(appliedId);
  141. } else {
  142. refreshController.callRefresh();
  143. }
  144. });
  145. }
  146. void unregisterEventBus() {
  147. bus.off(AppConstant.eventAppliedListRefresh, subscribe);
  148. }
  149. /// 搜索员工
  150. void doSearch(String keyword) {
  151. state.keyword = keyword;
  152. //赋值之后刷新
  153. refreshController.callRefresh();
  154. }
  155. /// 清空筛选条件
  156. void resetFiltering() {
  157. state.keyword = "";
  158. state.searchController.text = "";
  159. //赋值之后刷新
  160. refreshController.callRefresh();
  161. }
  162. /// 展示添加员工的弹窗
  163. void showAddStaffDialog() {
  164. DialogEngine.show(
  165. widget: AppliedAddStaff(
  166. jobId: state.jobId!,
  167. confirmAction: (selectedIds) {
  168. //调用接口添加员工
  169. _requestAddStaff2Applied(selectedIds);
  170. },
  171. ),
  172. );
  173. }
  174. //调用接口添加员工
  175. void _requestAddStaff2Applied(String selectedIds) async {
  176. var result = await _jobRepository.addStaff2Job(state.jobId, selectedIds, cancelToken: cancelToken);
  177. if (result.isSuccess) {
  178. NotifyEngine.showSuccess("Successful".tr);
  179. //添加成功之后刷新页面
  180. refreshController.callRefresh();
  181. } else {
  182. ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
  183. return;
  184. }
  185. }
  186. /// 去编辑员工信息页面
  187. void gotoAppliedEditPage(JobListAppliedStaffListRows data) {
  188. JobAppliedEditPage.startInstance(data.appliedId.toString());
  189. }
  190. /// 展示评论的弹窗
  191. void showRemarkDialog(JobListAppliedStaffListRows data) async {
  192. //请求接口获取到已评论的数据
  193. var result = await _jobRepository.fetchAppliedStaffReviewView(data.appliedId.toString());
  194. if (result.isSuccess) {
  195. //接口数据获取成功,展示弹窗
  196. DialogEngine.show(
  197. widget: AppliedStaffReviews(
  198. appliedReviews: result.data!,
  199. confirmAction: (attitudeRate, performanceRate, experienceRate, groomingRate, content) async {
  200. //请求接口,提交评论
  201. var submitResult = await _jobRepository.remarkAppliedSingleStaffSubmit(
  202. data.appliedId.toString(),
  203. attitudeRate,
  204. groomingRate,
  205. performanceRate,
  206. experienceRate,
  207. content,
  208. );
  209. if (submitResult.isSuccess) {
  210. NotifyEngine.showSuccess("Successful".tr);
  211. } else {
  212. ToastEngine.show(submitResult.errorMsg ?? "Network Load Error".tr);
  213. }
  214. },
  215. ),
  216. );
  217. } else {
  218. ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
  219. return;
  220. }
  221. }
  222. /// 去展示员工状态的审核流程页面
  223. void gotoAppliedWorkflowPage(JobListAppliedStaffListRows data) {
  224. AppliedWorkflowPage.startInstance(data.appliedId.toString());
  225. }
  226. /// 去查看员工详情页面
  227. void gotoStaffDetailPage(JobListAppliedStaffListRows data) {
  228. AppliedStaffDetailPage.startInstance(data.memberId.toString());
  229. }
  230. /// Item选中与未选中设置
  231. void doSelectedOrNot(JobListAppliedStaffListRows data) {
  232. //只有 Approve = 3 的状态才能选中
  233. if (data.status == 3) {
  234. data.isSelected = !data.isSelected;
  235. update();
  236. }
  237. }
  238. /// 批量修改的弹窗
  239. void showBatchModifyDialog() async {
  240. if (state.jobInfo == null) return;
  241. //找出已经选中的员工(只有状态为3 Approve的状态才能修改)
  242. var selectedList = state.datas.where((element) => element.isSelected && element.status == 3).toList(growable: false);
  243. if (selectedList.isNotEmpty) {
  244. var ids = selectedList.map((e) => e.appliedId.toString()).toList(growable: false);
  245. var separatedIds = ids.join(',');
  246. DialogEngine.show(
  247. widget: AppliedButchModify(
  248. selectedStartDate: DateTimeUtils.getDateTime(state.jobInfo?.startTime ?? ""),
  249. selectedEndDate: DateTimeUtils.getDateTime(state.jobInfo?.endTime ?? ""),
  250. confirmAction: (start, end) {
  251. _requestBatchModify(start, end, separatedIds);
  252. }));
  253. } else {
  254. ToastEngine.show("Please select the applied record".tr);
  255. }
  256. }
  257. /// 执行批量修改的请求
  258. void _requestBatchModify(DateTime start, DateTime end, String separatedIds) async {
  259. //执行请求
  260. var result = await _jobRepository.batchEditStaffCheckTime(
  261. state.jobId,
  262. separatedIds,
  263. DateTimeUtils.formatDate(start),
  264. DateTimeUtils.formatDate(end),
  265. cancelToken: cancelToken,
  266. );
  267. if (result.isSuccess) {
  268. NotifyEngine.showSuccess("Successful".tr);
  269. //调用接口刷新指定的Staff的信息
  270. fetchItemByIdAndRefreshItem(separatedIds);
  271. } else {
  272. ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
  273. return;
  274. }
  275. }
  276. /// 批准的操作
  277. void operationApprove() async {
  278. //找出已经选中的员工(只有状态为3 Approve的状态才能修改)
  279. var selectedList = state.datas.where((element) => element.isSelected && element.status == 3).toList(growable: false);
  280. if (selectedList.isNotEmpty) {
  281. var ids = selectedList.map((e) => e.appliedId.toString()).toList(growable: false);
  282. var separatedIds = ids.join(',');
  283. // Are you sure 的弹窗
  284. DialogEngine.show(
  285. widget: AppDefaultDialog(
  286. title: "Message".tr,
  287. message: "Are you sure you want to setting approved?".tr,
  288. confirmAction: () async {
  289. //执行请求
  290. var result = await _jobRepository.submitBatchStaffApprove(separatedIds, cancelToken: cancelToken);
  291. if (result.isSuccess) {
  292. NotifyEngine.showSuccess("Successful".tr);
  293. //调用接口刷新指定的Staff的信息
  294. fetchItemByIdAndRefreshItem(separatedIds);
  295. } else {
  296. ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
  297. return;
  298. }
  299. },
  300. ),
  301. );
  302. } else {
  303. ToastEngine.show("Please select the applied record".tr);
  304. }
  305. }
  306. /// 根据ID获取Item对象,用于刷新
  307. void fetchItemByIdAndRefreshItem(String appliedIds) async {
  308. var result = await _jobRepository.fetchItemByAppliedIds(
  309. state.jobId,
  310. appliedIds,
  311. cancelToken: cancelToken,
  312. );
  313. //处理数据
  314. if (result.isSuccess) {
  315. var data = result.data;
  316. if (data != null && data.rows != null && data.rows!.isNotEmpty) {
  317. List<JobListAppliedStaffListRows> newItem = data.rows!;
  318. // 创建一个 Map 来加速查找
  319. Map<int, JobListAppliedStaffListRows> newItemMap = {for (var item in newItem) item.appliedId: item};
  320. // 遍历 state.datas 进行替换
  321. for (int i = 0; i < state.datas.length; i++) {
  322. if (newItemMap.containsKey(state.datas[i].appliedId)) {
  323. state.datas[i] = newItemMap[state.datas[i].appliedId]!;
  324. }
  325. }
  326. //刷新
  327. update();
  328. }
  329. } else {
  330. ToastEngine.show(result.errorMsg ?? "Network Load Error".tr);
  331. }
  332. }
  333. }