UserSpread.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Created by CRMEB.
  4. * Copyright (c) 2017~2019 http://www.crmeb.com All rights reserved.
  5. * Author: liaofei <136327134@qq.com>
  6. * Date: 2019/3/27 21:44
  7. */
  8. namespace app\models\user;
  9. use app\models\store\StoreOrder;
  10. use think\Collection;
  11. use think\db\exception\DataNotFoundException;
  12. use think\db\exception\DbException;
  13. use think\db\exception\ModelNotFoundException;
  14. use think\facade\Cache;
  15. use crmeb\traits\ModelTrait;
  16. use crmeb\basic\BaseModel;
  17. /**
  18. * TODO 用户消费新增金额明细 model
  19. * Class UserBill
  20. * @package app\models\user
  21. */
  22. class UserSpread extends BaseModel
  23. {
  24. /**
  25. * 数据表主键
  26. * @var string
  27. */
  28. protected $pk = 'id';
  29. /**
  30. * 模型名称
  31. * @var string
  32. */
  33. protected $name = 'user_spread';
  34. use ModelTrait;
  35. /**
  36. * @param $uid
  37. * @param $spread_uid
  38. * @return bool|UserSpread
  39. * @throws DataNotFoundException
  40. * @throws DbException
  41. * @throws ModelNotFoundException
  42. */
  43. public static function setSpread($uid, $spread_uid)
  44. {
  45. if (!$spread_uid) {
  46. return true;
  47. }
  48. $sp = $spread_uid;
  49. $userlist = User::column('uid,spread_uid', 'uid');
  50. while ($sp) {
  51. if ($sp == $uid) {
  52. $spread_uid = 0;
  53. break;
  54. }
  55. $sp = $userlist[$sp]['spread_uid'] ?? 0;
  56. }
  57. $user = User::where('uid', $uid)->find();
  58. $spread_user = User::get($spread_uid);
  59. if (!$user || !$spread_user || !$spread_user['is_promoter']) {
  60. return true;
  61. }
  62. //首先获取绑定方式
  63. $bind_type = sys_config('bind_type', 1);
  64. switch ($bind_type) {
  65. case 2:
  66. if (self::where('uid', $uid)->where('lock', 1)->find()) {
  67. return true;
  68. }
  69. return self::create([
  70. 'uid' => $uid,
  71. 'spread_uid' => $spread_uid,
  72. 'lock' => 0,
  73. 'spread_time' => time()
  74. ]);
  75. break;
  76. case 3:
  77. if (self::where('uid', $uid)->where('lock', 1)->find()) {
  78. return true;
  79. }
  80. self::create([
  81. 'uid' => $uid,
  82. 'spread_uid' => $spread_uid,
  83. 'lock' => 0,
  84. 'spread_time' => time()
  85. ]);
  86. $user['spread_uid'] = $spread_uid;
  87. $user['spread_time'] = time();
  88. return $user->save() && User::setUserSpreadCount($spread_uid);
  89. break;
  90. default:
  91. if (self::where('uid', $uid)->where('lock', 1)->find()) {
  92. return true;
  93. }
  94. self::create([
  95. 'uid' => $uid,
  96. 'spread_uid' => $spread_uid,
  97. 'lock' => 1,
  98. 'spread_time' => time()
  99. ]);
  100. $user['spread_uid'] = $spread_uid;
  101. $user['spread_time'] = time();
  102. return $user->save() && User::setUserSpreadCount($spread_uid);
  103. break;
  104. }
  105. }
  106. /**
  107. * @param $uid
  108. * @return bool
  109. * @throws DataNotFoundException
  110. * @throws DbException
  111. * @throws ModelNotFoundException
  112. */
  113. public static function setSpreadSure($uid)
  114. {
  115. //首先获取绑定方式
  116. $bind_type = sys_config('bind_type', 1);
  117. if ($bind_type != 2) {
  118. return true;
  119. }
  120. if (self::where('uid', $uid)->where('lock', 1)->find()) {
  121. return true;
  122. }
  123. $log = self::where('uid', $uid)->order('spread_time', 'desc')->find();
  124. if (!$log) {
  125. return true;
  126. }
  127. $sp = $log['spread_uid'];
  128. $userlist = User::column('uid,spread_uid', 'uid');
  129. while ($sp) {
  130. if ($sp == $uid) {
  131. $log['spread_uid'] = 0;
  132. break;
  133. }
  134. $sp = $userlist[$sp]['spread_uid'];
  135. }
  136. if (!$log['spread_uid']) {
  137. return true;
  138. }
  139. $log['lock'] = 1;
  140. return $log->save() && User::where('uid', $uid)->update(['spread_uid' => $log['spread_uid'], 'spread_time' => $log['spread_time']]) && User::setUserSpreadCount($log['spread_uid']);
  141. }
  142. }