UserUpgradeService.php 6.9 KB

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