UserSpread.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. use think\Model;
  18. /**
  19. * TODO 用户消费新增金额明细 model
  20. * Class UserBill
  21. * @package app\models\user
  22. */
  23. class UserSpread extends BaseModel
  24. {
  25. /**
  26. * 数据表主键
  27. * @var string
  28. */
  29. protected $pk = 'id';
  30. /**
  31. * 模型名称
  32. * @var string
  33. */
  34. protected $name = 'user_spread';
  35. use ModelTrait;
  36. /**
  37. * @param $uid
  38. * @param $spread_uid
  39. * @return bool|UserSpread
  40. * @throws DataNotFoundException
  41. * @throws DbException
  42. * @throws ModelNotFoundException
  43. */
  44. public static function setSpread($uid, $spread_uid)
  45. {
  46. if (!$spread_uid) {
  47. return true;
  48. }
  49. $sp = $spread_uid;
  50. $userlist = User::column('uid,spread_uid', 'uid');
  51. while ($sp) {
  52. if ($sp == $uid) {
  53. $spread_uid = 0;
  54. break;
  55. }
  56. $sp = $userlist[$sp]['spread_uid'];
  57. }
  58. $user = User::where('uid', $uid)->find();
  59. $spread_user = User::get($spread_uid);
  60. if (!$user || !$spread_user || !$spread_user['is_promoter']) {
  61. return true;
  62. }
  63. //首先获取绑定方式
  64. $bind_type = sys_config('bind_type', 1);
  65. switch ($bind_type) {
  66. case 2:
  67. if (self::where('uid', $uid)->where('lock', 1)->find()) {
  68. return true;
  69. }
  70. return self::create([
  71. 'uid' => $uid,
  72. 'spread_uid' => $spread_uid,
  73. 'lock' => 0,
  74. 'spread_time' => time()
  75. ]);
  76. break;
  77. case 3:
  78. if (self::where('uid', $uid)->where('lock', 1)->find()) {
  79. return true;
  80. }
  81. self::create([
  82. 'uid' => $uid,
  83. 'spread_uid' => $spread_uid,
  84. 'lock' => 0,
  85. 'spread_time' => time()
  86. ]);
  87. $user['spread_uid'] = $spread_uid;
  88. $user['spread_time'] = time();
  89. return $user->save() && User::setUserSpreadCount($spread_uid);
  90. break;
  91. default:
  92. if (self::where('uid', $uid)->where('lock', 1)->find()) {
  93. return true;
  94. }
  95. self::create([
  96. 'uid' => $uid,
  97. 'spread_uid' => $spread_uid,
  98. 'lock' => 1,
  99. 'spread_time' => time()
  100. ]);
  101. $user['spread_uid'] = $spread_uid;
  102. $user['spread_time'] = time();
  103. return $user->save() && User::setUserSpreadCount($spread_uid);
  104. break;
  105. }
  106. }
  107. /**
  108. * @param $uid
  109. * @return bool
  110. * @throws DataNotFoundException
  111. * @throws DbException
  112. * @throws ModelNotFoundException
  113. */
  114. public static function setSpreadSure($uid)
  115. {
  116. //首先获取绑定方式
  117. $bind_type = sys_config('bind_type', 1);
  118. if ($bind_type != 2) {
  119. return true;
  120. }
  121. if (self::where('uid', $uid)->where('lock', 1)->find()) {
  122. return true;
  123. }
  124. $log = self::where('uid', $uid)->order('spread_time', 'desc')->find();
  125. if (!$log) {
  126. return true;
  127. }
  128. $sp = $log['spread_uid'];
  129. $userlist = User::column('uid,spread_uid', 'uid');
  130. while ($sp) {
  131. if ($sp == $uid) {
  132. $log['spread_uid'] = 0;
  133. break;
  134. }
  135. $sp = $userlist[$sp]['spread_uid'];
  136. }
  137. if (!$log['spread_uid']) {
  138. return true;
  139. }
  140. $log['lock'] = 1;
  141. return $log->save() && User::where('uid', $uid)->update(['spread_uid' => $log['spread_uid'], 'spread_time' => $log['spread_time']]) && User::setUserSpreadCount($log['spread_uid']);
  142. }
  143. public static function checkValidSpread($uid)
  144. {
  145. $bind_type = sys_config('bind_type', 1);
  146. if ($bind_type != 2) {
  147. return true;
  148. }
  149. if (self::where('uid', $uid)->where('lock', 1)->find()) {
  150. return true;
  151. }
  152. $log = self::where('uid', $uid)->order('spread_time', 'desc')->find();
  153. if (!$log) {
  154. return false;
  155. }
  156. $sp = $log['spread_uid'];
  157. $userlist = User::column('uid,spread_uid', 'uid');
  158. while ($sp) {
  159. if ($sp == $uid) {
  160. $log['spread_uid'] = 0;
  161. break;
  162. }
  163. $sp = $userlist[$sp]['spread_uid'];
  164. }
  165. if (!$log['spread_uid']) {
  166. return false;
  167. }
  168. return true;
  169. }
  170. }