AutoArchivedTask.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Tasks;
  3. @error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
  4. use App\Module\Base;
  5. use App\Module\Chat;
  6. use DB;
  7. use Hhxsv5\LaravelS\Swoole\Task\Task;
  8. /**
  9. * 完成的任务自动归档
  10. * Class AutoArchivedTask
  11. * @package App\Tasks
  12. */
  13. class AutoArchivedTask extends Task
  14. {
  15. public function __construct()
  16. {
  17. //
  18. }
  19. public function handle()
  20. {
  21. $setting = Base::setting('system');
  22. if ($setting['autoArchived'] === 'open') {
  23. $archivedDay = intval($setting['archivedDay']);
  24. if ($archivedDay > 0) {
  25. $time = time();
  26. $archivedDay = min(100, $archivedDay);
  27. $archivedTime = $time - ($archivedDay * 86400);
  28. //获取已完成未归档的任务
  29. DB::transaction(function () use ($time, $archivedTime) {
  30. $taskLists = Base::DBC2A(DB::table('project_task')->where([
  31. ['delete', '=', 0],
  32. ['archiveddate', '=', 0],
  33. ['complete', '=', 1],
  34. ['completedate', '<=', $archivedTime],
  35. ])->take(100)->get());
  36. if ($taskLists) {
  37. $idArray = [];
  38. $logArray = [];
  39. $pushLists = [];
  40. $upArray = [
  41. 'archived' => 1,
  42. 'archiveddate' => $time,
  43. ];
  44. foreach ($taskLists AS $taskDetail) {
  45. $idArray[] = $taskDetail['id'];
  46. $logArray[] = [
  47. 'type' => '日志',
  48. 'projectid' => $taskDetail['projectid'],
  49. 'taskid' => $taskDetail['id'],
  50. 'username' => $taskDetail['username'],
  51. 'detail' => '任务归档【自动】',
  52. 'indate' => $time,
  53. 'other' => Base::array2string([
  54. 'type' => 'task',
  55. 'id' => $taskDetail['id'],
  56. 'title' => $taskDetail['title'],
  57. ])
  58. ];
  59. $userLists = Chat::getTaskUsers($taskDetail['id']);
  60. if ($userLists) {
  61. foreach ($userLists as $user) {
  62. $pushLists[] = [
  63. 'fd' => $user['fd'],
  64. 'msg' => [
  65. 'messageType' => 'user',
  66. 'username' => '::system',
  67. 'target' => $user['username'],
  68. 'time' => $time,
  69. 'body' => [
  70. 'act' => 'archived',
  71. 'type' => 'taskA',
  72. 'taskDetail' => array_merge($taskDetail, $upArray),
  73. ]
  74. ]
  75. ];
  76. }
  77. }
  78. }
  79. if ($idArray) {
  80. DB::table('project_task')->whereIn('id', $idArray)->where([
  81. ['archiveddate', '=', 0],
  82. ['complete', '=', 1],
  83. ])->update($upArray);
  84. }
  85. if ($logArray) {
  86. DB::table('project_log')->insert($logArray);
  87. }
  88. if ($pushLists) {
  89. $pushTask = new PushTask($pushLists);
  90. Task::deliver($pushTask);
  91. }
  92. }
  93. });
  94. }
  95. }
  96. }
  97. }