UserBatchJob.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\jobs\user;
  12. use app\services\user\label\UserLabelRelationServices;
  13. use app\services\user\UserServices;
  14. use app\services\wechat\WechatNewsCategoryServices;
  15. use crmeb\basic\BaseJobs;
  16. use crmeb\traits\QueueTrait;
  17. use think\facade\Log;
  18. /**
  19. * 用户批量任务队列
  20. * Class UserBatchJob
  21. * @package app\jobs\user
  22. */
  23. class UserBatchJob extends BaseJobs
  24. {
  25. use QueueTrait;
  26. /**
  27. * @return mixed
  28. */
  29. public static function queueName()
  30. {
  31. $default = config('queue.default');
  32. return config('queue.connections.' . $default . '.batch_queue');
  33. }
  34. /**
  35. * 用户批量队列
  36. * @param $type
  37. * @param $uids
  38. * @param $data
  39. * @return bool
  40. */
  41. public function userBatch($type, $uids, $data)
  42. {
  43. if (!$type || !$uids || !$data) {
  44. return true;
  45. }
  46. //拆分大数组 分批加入二级队列
  47. $uidsArr = array_chunk($uids, 100);
  48. foreach ($uidsArr as $ids) {
  49. //加入分批队列
  50. self::dispatchDo('chunkUserBatch', [$type, $ids, $data]);
  51. }
  52. return true;
  53. }
  54. /**
  55. * 拆分分批队列
  56. * @param $type
  57. * @param $uids
  58. * @param $data
  59. * @return bool
  60. */
  61. public function chunkUserBatch($type, $uids, $data)
  62. {
  63. if (!$type || !$uids || !$data) {
  64. return true;
  65. }
  66. foreach ($uids as $id) {
  67. self::dispatchDo('runUserBatch', [$type, $id, $data]);
  68. }
  69. return true;
  70. }
  71. /**
  72. * 实际执行用户操作队列
  73. * @param $type
  74. * @param $uid
  75. * @param $data
  76. * @return bool
  77. */
  78. public function runUserBatch($type, $uid, $data)
  79. {
  80. $uid = (int)$uid;
  81. if (!$type || !$uid || !$data) {
  82. return true;
  83. }
  84. try {
  85. switch ($type) {
  86. case 1://分组
  87. $group_id = (int)($data['group_id'] ?? 0);
  88. if ($group_id) {
  89. /** @var UserServices $userServices */
  90. $userServices = app()->make(UserServices::class);
  91. $userServices->setUserGroup([$uid], $group_id);
  92. }
  93. break;
  94. case 2://标签
  95. $label_id = $data['label_id'] ?? [];
  96. if ($label_id) {
  97. /** @var UserLabelRelationServices $services */
  98. $services = app()->make(UserLabelRelationServices::class);
  99. $services->setUserLable($uid, $label_id);
  100. }
  101. break;
  102. case 3://等级
  103. $level_id = (int)($data['level_id'] ?? 0);
  104. if ($level_id) {
  105. /** @var UserServices $userServices */
  106. $userServices = app()->make(UserServices::class);
  107. $userServices->saveGiveLevel($uid, $level_id);
  108. }
  109. break;
  110. case 4://积分余额
  111. $data['money'] = (string)$data['money'];
  112. $data['integration'] = (string)$data['integration'];
  113. $data['is_other'] = true;
  114. /** @var UserServices $userServices */
  115. $userServices = app()->make(UserServices::class);
  116. $userServices->updateInfo($uid, $data);
  117. break;
  118. case 5://赠送会员
  119. $day = (int)($data['day'] ?? 0);
  120. if ($day) {
  121. $days_status = (int)($data['days_status'] ?? 1);
  122. /** @var UserServices $userServices */
  123. $userServices = app()->make(UserServices::class);
  124. $userServices->saveGiveLevelTime($uid, $day, $days_status);
  125. }
  126. break;
  127. case 6://上级推广人
  128. $spread_uid = (int)($data['spread_uid'] ?? 0);
  129. if ($spread_uid) {
  130. /** @var UserServices $userServices */
  131. $userServices = app()->make(UserServices::class);
  132. $userServices->saveUserSpreadUid($uid, $spread_uid);
  133. }
  134. break;
  135. case 99://发送图文消息
  136. if($data['wechat_news'] ?? []) {
  137. /** @var WechatNewsCategoryServices $wechatNewsCategoryServices */
  138. $wechatNewsCategoryServices = app()->make(WechatNewsCategoryServices::class);
  139. $wechatNewsCategoryServices->runPush($uid, $data['wechat_news'] ?? []);
  140. }
  141. break;
  142. default:
  143. break;
  144. }
  145. } catch (\Throwable $e) {
  146. response_log_write([
  147. 'message' => '批量操作用户,type:' . $type . ';状态失败,' . ';参数:' . json_encode(['uid' => $uid, 'data' => $data]) . ', 失败原因:' . $e->getMessage(),
  148. 'file' => $e->getFile(),
  149. 'line' => $e->getLine()
  150. ]);
  151. }
  152. return true;
  153. }
  154. }