UserBatchProcessServices.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. //declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\jobs\user\UserBatchJob;
  14. use app\services\BaseServices;
  15. use app\dao\user\UserDao;
  16. use app\services\user\group\UserGroupServices;
  17. use app\services\user\label\UserLabelServices;
  18. use app\services\user\level\SystemUserLevelServices;
  19. use app\services\wechat\WechatNewsCategoryServices;
  20. use crmeb\exceptions\AdminException;
  21. use think\exception\ValidateException;
  22. /**
  23. * 用户批量操作
  24. * Class UserBatchProcessServices
  25. * @package app\services\user
  26. * @mixin UserDao
  27. */
  28. class UserBatchProcessServices extends BaseServices
  29. {
  30. /**
  31. * 批量处理参数
  32. * @var array[]
  33. */
  34. protected $data = [
  35. 'group_id' => [],//分组ID
  36. 'label_id' => [],//标签ids
  37. 'level_id' => 0,//等级ID
  38. 'money_status' => 0,//余额变动状态
  39. 'money'=> 0,//余额变动金额
  40. 'integration_status'=> 0,//积分变动状态
  41. 'integration' => 0,//积分变动数量
  42. 'days_status' => 0,//付费会员条数变动状态
  43. 'day' => 0,//变动天数
  44. 'spread_uid' => 0,//上级推广人uid
  45. ];
  46. /**
  47. * UserServices constructor.
  48. * @param UserDao $dao
  49. */
  50. public function __construct(UserDao $dao)
  51. {
  52. $this->dao = $dao;
  53. }
  54. /**
  55. * 根据搜索条件查询uids
  56. * @param $where
  57. * @return array
  58. */
  59. public function getUidByWhere($where)
  60. {
  61. if ($where) {
  62. if (is_string($where)) {
  63. $where = json_decode($where, true);
  64. } elseif (is_array($where)) {
  65. $where = $where;
  66. } else {
  67. $where = [];
  68. }
  69. }
  70. /** @var UserWechatuserServices $userWechatUser */
  71. $userWechatUser = app()->make(UserWechatuserServices::class);
  72. $fields = 'u.uid';
  73. [$list, $count] = $userWechatUser->getWhereUserList($where, $fields);
  74. $uids = array_unique(array_column($list, 'uid'));
  75. return $uids;
  76. }
  77. /**
  78. * 用户批量操作
  79. * @param int $type
  80. * @param array $uids
  81. * @param array $input_data
  82. * @param bool $is_all
  83. * @param array $where
  84. * @return bool
  85. */
  86. public function batchProcess(int $type, array $uids, array $input_data, bool $is_all = false, array $where = [])
  87. {
  88. //全选
  89. if ($is_all) {
  90. $uids = $this->getUidByWhere($where);
  91. }
  92. $data = [];
  93. switch ($type) {
  94. case 1://分组
  95. $group_id = $input_data['group_id'] ?? 0;
  96. if (!$group_id) throw new ValidateException('请选择分组');
  97. /** @var UserGroupServices $userGroup */
  98. $userGroup = app()->make(UserGroupServices::class);
  99. if (!$userGroup->getGroup($group_id)) {
  100. throw new ValidateException('该分组不存在');
  101. }
  102. $data['group_id'] = $group_id;
  103. break;
  104. case 2://标签
  105. $label_id = $input_data['label_id'] ?? [];
  106. if (!$label_id) throw new ValidateException ('请选择标签');
  107. /** @var UserLabelServices $userLabelServices */
  108. $userLabelServices = app()->make(UserLabelServices::class);
  109. $count = $userLabelServices->getCount([['id', 'IN', $label_id]]);
  110. if ($count != count($label_id)) {
  111. throw new ValidateException('用户标签不存在或被删除');
  112. }
  113. $data['label_id'] = $label_id;
  114. break;
  115. case 3://等级
  116. $level_id = $input_data['level_id'] ?? 0;
  117. if (!$level_id) throw new ValidateException('请选择用户等级');
  118. /** @var SystemUserLevelServices $systemLevelServices */
  119. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  120. //查询当前选择的会员等级
  121. $systemLevel = $systemLevelServices->getLevel($level_id);
  122. if (!$systemLevel) throw new AdminException('您选择赠送的用户等级不存在!');
  123. $data['level_id'] = $level_id;
  124. break;
  125. case 4://积分余额
  126. $data['money_status'] = $input_data['money_status'] ?? 0;
  127. $data['money'] = $input_data['money'] ?? 0;
  128. $data['integration_status'] = $input_data['integration_status'] ?? 0;
  129. $data['integration'] = $input_data['integration'] ?? 0;
  130. if (!$data['money_status'] && !$data['integration_status']) {
  131. throw new AdminException('请选择操作积分或余额');
  132. }
  133. if ($data['money_status'] && !$data['money']) {
  134. throw new AdminException('请填写变动余额数量');
  135. }
  136. if ($data['integration_status'] && !$data['integration']) {
  137. throw new AdminException('请填写变动积分数量');
  138. }
  139. break;
  140. case 5://赠送会员
  141. $data['days_status'] = $input_data['days_status'] ?? 0;
  142. $data['day'] = $input_data['day'] ?? 0;
  143. if (!$data['days_status']) {
  144. throw new AdminException('请选择增加或者减少会员天数');
  145. }
  146. if (!$data['day']) {
  147. throw new AdminException('请填写变动会员天数');
  148. }
  149. break;
  150. case 6://上级推广人
  151. $spread_uid = $input_data['spread_uid'] ?? 0;
  152. if (!$spread_uid) throw new ValidateException('请选择上级推广人');
  153. if (!$this->dao->count(['uid' => $spread_uid])) {
  154. throw new ValidateException('上级用户不存在');
  155. }
  156. $data['spread_uid'] = $spread_uid;
  157. break;
  158. case 99://发送图文消息
  159. $new_id = $input_data['id'] ?? 0;
  160. if (!$new_id) throw new ValidateException('请选择要发送的图文消息');
  161. $wechatNewsCategoryServices = app()->make(WechatNewsCategoryServices::class);
  162. $list = $wechatNewsCategoryServices->getWechatNewsItem((int)$new_id);
  163. $wechatNews = [];
  164. if ($list) {
  165. if (is_array($list['new']) && count($list['new'])) {
  166. $wechatNews['title'] = $list['new'][0]['title'];
  167. $wechatNews['image_input'] = $list['new'][0]['image_input'];
  168. $wechatNews['date'] = date('m月d日', time());
  169. $wechatNews['description'] = $list['new'][0]['synopsis'];
  170. $wechatNews['id'] = $list['new'][0]['id'];
  171. }
  172. $wechatNews = $wechatNewsCategoryServices->wechatPush($wechatNews);
  173. }
  174. $data['wechat_news'] = $wechatNews;
  175. break;
  176. default:
  177. throw new AdminException('暂不支持该类型批操作');
  178. }
  179. //加入批量队列
  180. UserBatchJob::dispatchDo('userBatch', [$type, $uids, $data]);
  181. return true;
  182. }
  183. }