UserUpgradeService.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace app\data\service;
  3. use app\data\model\BaseUserUpgrade;
  4. use app\data\model\DataUser;
  5. use app\data\model\DataUserBalance;
  6. use app\data\model\ShopOrder;
  7. use app\data\model\ShopOrderItem;
  8. use think\admin\Service;
  9. /**
  10. * 用户等级升级服务
  11. * Class UserUpgradeService
  12. * @package app\data\service
  13. */
  14. class UserUpgradeService extends Service
  15. {
  16. /**
  17. * 尝试绑定上级代理
  18. * @param integer $uuid 用户UID
  19. * @param integer $pid0 代理UID
  20. * @param integer $mode 操作类型(0临时绑定, 1永久绑定, 2强行绑定)
  21. * @return array
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\DbException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. */
  26. public function bindAgent(int $uuid, int $pid0 = 0, int $mode = 1): array
  27. {
  28. $user = DataUser::mk()->where(['id' => $uuid])->find();
  29. if (empty($user)) return [0, '查询用户资料失败'];
  30. if ($user['pids'] && in_array($mode, [0, 1])) return [1, '已经绑定代理'];
  31. // 检查代理用户
  32. if (empty($pid0)) $pid0 = $user['pid0'];
  33. if (empty($pid0)) return [0, '绑定的代理不存在'];
  34. if ($uuid == $pid0) return [0, '不能绑定自己为代理'];
  35. // 检查代理资格
  36. $agent = DataUser::mk()->where(['id' => $pid0])->find();
  37. if (empty($agent['vip_code'])) return [0, '代理无推荐资格'];
  38. if (strpos($agent['path'], "-{$uuid}-") !== false) return [0, '不能绑定下属'];
  39. try {
  40. $this->app->db->transaction(function () use ($user, $agent, $mode) {
  41. // 更新用户代理
  42. $path1 = rtrim($agent['path'] ?: '-', '-') . "-{$agent['id']}-";
  43. $user->save(['pid0' => $agent['id'], 'pid1' => $agent['id'], 'pid2' => $agent['pid1'], 'pids' => $mode > 0 ? 1 : 0, 'path' => $path1, 'layer' => substr_count($path1, '-')]);
  44. // 更新下级代理
  45. $path2 = "{$user['path']}{$user['id']}-";
  46. if (DataUser::mk()->whereLike('path', "{$path2}%")->count() > 0) {
  47. foreach (DataUser::mk()->whereLike('path', "{$path2}%")->order('layer desc')->select() as $item) {
  48. $attr = array_reverse(str2arr($path3 = preg_replace("#^{$path2}#", "{$path1}{$user['id']}-", $item['path']), '-'));
  49. $item->save(['pid0' => $attr[0] ?? 0, 'pid1' => $attr[0] ?? 0, 'pid2' => $attr[1] ?? 0, 'path' => $path3, 'layer' => substr_count($path3, '-')]);
  50. }
  51. }
  52. });
  53. $this->upgrade($user['id']);
  54. return [1, '绑定代理成功'];
  55. } catch (\Exception $exception) {
  56. return [0, "绑定代理失败, {$exception->getMessage()}"];
  57. }
  58. }
  59. /**
  60. * 同步计算用户等级
  61. * @param integer $uuid 指定用户UID
  62. * @param boolean $parent 同步计算上级
  63. * @param ?string $orderNo 升级触发订单
  64. * @return boolean
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. public function upgrade(int $uuid, bool $parent = true, ?string $orderNo = null): bool
  70. {
  71. $user = DataUser::mk()->where(['id' => $uuid])->find();
  72. if (empty($user)) return true;
  73. // 初始化等级参数
  74. $levels = BaseUserUpgrade::items();
  75. [$vipName, $vipCode, $vipTeam] = [$levels[0]['name'] ?? '普通用户', 0, []];
  76. // 统计用户数据
  77. foreach ($levels as $key => $level) if ($level['upgrade_team'] === 1) $vipTeam[] = $key;
  78. $orderAmount = ShopOrder::mk()->where("uuid={$uuid} and status>=4")->sum('amount_total');
  79. $teamsDirect = DataUser::mk()->where(['pid1' => $uuid])->whereIn('vip_code', $vipTeam)->count();
  80. $teamsIndirect = DataUser::mk()->where(['pid2' => $uuid])->whereIn('vip_code', $vipTeam)->count();
  81. $teamsUsers = $teamsDirect + $teamsIndirect;
  82. // 动态计算用户等级
  83. foreach ($levels as $item) {
  84. $l1 = empty($item['goods_vip_status']) || $user['buy_vip_entry'] > 0;
  85. $l2 = empty($item['teams_users_status']) || $item['teams_users_number'] <= $teamsUsers;
  86. $l3 = empty($item['order_amount_status']) || $item['order_amount_number'] <= $orderAmount;
  87. $l4 = empty($item['teams_direct_status']) || $item['teams_direct_number'] <= $teamsDirect;
  88. $l5 = empty($item['teams_indirect_status']) || $item['teams_indirect_number'] <= $teamsIndirect;
  89. if (
  90. ($item['upgrade_type'] == 0 && ($l1 || $l2 || $l3 || $l4 || $l5)) /* 满足任何条件可以等级 */
  91. ||
  92. ($item['upgrade_type'] == 1 && ($l1 && $l2 && $l3 && $l4 && $l5)) /* 满足所有条件可以等级 */
  93. ) {
  94. [$vipName, $vipCode] = [$item['name'], $item['number']];
  95. break;
  96. }
  97. }
  98. // 购买入会商品升级
  99. $query = ShopOrderItem::mk()->alias('b')->join('shop_order a', 'b.order_no=a.order_no');
  100. $tmpCode = $query->whereRaw("a.uuid={$uuid} and a.payment_status=1 and a.status>=4 and b.vip_entry=1")->max('b.vip_upgrade');
  101. if ($tmpCode > $vipCode && isset($levels[$tmpCode])) {
  102. [$vipName, $vipCode] = [$levels[$tmpCode]['name'], $levels[$tmpCode]['number']];
  103. } else {
  104. $orderNo = null;
  105. }
  106. // 后台余额充值升级
  107. $tmpCode = DataUserBalance::mk()->where(['uuid' => $uuid, 'deleted' => 0])->max('upgrade');
  108. if ($tmpCode > $vipCode && isset($levels[$tmpCode])) {
  109. [$vipName, $vipCode] = [$levels[$tmpCode]['name'], $levels[$tmpCode]['number']];
  110. }
  111. // 统计用户订单金额
  112. $orderAmountTotal = ShopOrder::mk()->whereRaw("uuid={$uuid} and status>=4")->sum('amount_goods');
  113. $teamsAmountDirect = ShopOrder::mk()->whereRaw("puid1={$uuid} and status>=4")->sum('amount_goods');
  114. $teamsAmountIndirect = ShopOrder::mk()->whereRaw("puid2={$uuid} and status>=4")->sum('amount_goods');
  115. // 更新用户团队数据
  116. $data = [
  117. 'vip_name' => $vipName,
  118. 'vip_code' => $vipCode,
  119. 'teams_users_total' => $teamsUsers,
  120. 'teams_users_direct' => $teamsDirect,
  121. 'teams_users_indirect' => $teamsIndirect,
  122. 'teams_amount_total' => $teamsAmountDirect + $teamsAmountIndirect,
  123. 'teams_amount_direct' => $teamsAmountDirect,
  124. 'teams_amount_indirect' => $teamsAmountIndirect,
  125. 'order_amount_total' => $orderAmountTotal,
  126. ];
  127. if (!empty($orderNo)) $data['vip_order'] = $orderNo;
  128. if ($data['vip_code'] !== $user['vip_code']) $data['vip_datetime'] = date('Y-m-d H:i:s');
  129. DataUser::mk()->where(['id' => $uuid])->update($data);
  130. // 用户升级事件
  131. if ($user['vip_code'] < $vipCode) $this->app->event->trigger('UserUpgradeLevel', [
  132. 'uuid' => $user['id'], 'order_no' => $orderNo, 'vip_code_old' => $user['vip_code'], 'vip_code_new' => $vipCode,
  133. ]);
  134. return !($parent && $user['pid1'] > 0) || $this->upgrade($user['pid1'], false);
  135. }
  136. }