123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\jobs\user;
- use app\services\user\label\UserLabelRelationServices;
- use app\services\user\UserServices;
- use app\services\wechat\WechatNewsCategoryServices;
- use crmeb\basic\BaseJobs;
- use crmeb\traits\QueueTrait;
- use think\facade\Log;
- /**
- * 用户批量任务队列
- * Class UserBatchJob
- * @package app\jobs\user
- */
- class UserBatchJob extends BaseJobs
- {
- use QueueTrait;
- /**
- * @return mixed
- */
- public static function queueName()
- {
- $default = config('queue.default');
- return config('queue.connections.' . $default . '.batch_queue');
- }
- /**
- * 用户批量队列
- * @param $type
- * @param $uids
- * @param $data
- * @return bool
- */
- public function userBatch($type, $uids, $data)
- {
- if (!$type || !$uids || !$data) {
- return true;
- }
- //拆分大数组 分批加入二级队列
- $uidsArr = array_chunk($uids, 100);
- foreach ($uidsArr as $ids) {
- //加入分批队列
- self::dispatchDo('chunkUserBatch', [$type, $ids, $data]);
- }
- return true;
- }
- /**
- * 拆分分批队列
- * @param $type
- * @param $uids
- * @param $data
- * @return bool
- */
- public function chunkUserBatch($type, $uids, $data)
- {
- if (!$type || !$uids || !$data) {
- return true;
- }
- foreach ($uids as $id) {
- self::dispatchDo('runUserBatch', [$type, $id, $data]);
- }
- return true;
- }
- /**
- * 实际执行用户操作队列
- * @param $type
- * @param $uid
- * @param $data
- * @return bool
- */
- public function runUserBatch($type, $uid, $data)
- {
- $uid = (int)$uid;
- if (!$type || !$uid || !$data) {
- return true;
- }
- try {
- switch ($type) {
- case 1://分组
- $group_id = (int)($data['group_id'] ?? 0);
- if ($group_id) {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $userServices->setUserGroup([$uid], $group_id);
- }
- break;
- case 2://标签
- $label_id = $data['label_id'] ?? [];
- if ($label_id) {
- /** @var UserLabelRelationServices $services */
- $services = app()->make(UserLabelRelationServices::class);
- $services->setUserLable($uid, $label_id);
- }
- break;
- case 3://等级
- $level_id = (int)($data['level_id'] ?? 0);
- if ($level_id) {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $userServices->saveGiveLevel($uid, $level_id);
- }
- break;
- case 4://积分余额
- $data['money'] = (string)$data['money'];
- $data['integration'] = (string)$data['integration'];
- $data['is_other'] = true;
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $userServices->updateInfo($uid, $data);
- break;
- case 5://赠送会员
- $day = (int)($data['day'] ?? 0);
- if ($day) {
- $days_status = (int)($data['days_status'] ?? 1);
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $userServices->saveGiveLevelTime($uid, $day, $days_status);
- }
- break;
- case 6://上级推广人
- $spread_uid = (int)($data['spread_uid'] ?? 0);
- if ($spread_uid) {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $userServices->saveUserSpreadUid($uid, $spread_uid);
- }
- break;
- case 99://发送图文消息
- if($data['wechat_news'] ?? []) {
- /** @var WechatNewsCategoryServices $wechatNewsCategoryServices */
- $wechatNewsCategoryServices = app()->make(WechatNewsCategoryServices::class);
- $wechatNewsCategoryServices->runPush($uid, $data['wechat_news'] ?? []);
- }
- break;
- default:
- break;
- }
- } catch (\Throwable $e) {
- response_log_write([
- 'message' => '批量操作用户,type:' . $type . ';状态失败,' . ';参数:' . json_encode(['uid' => $uid, 'data' => $data]) . ', 失败原因:' . $e->getMessage(),
- 'file' => $e->getFile(),
- 'line' => $e->getLine()
- ]);
- }
- return true;
- }
- }
|