UserLogic.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\logic;
  20. use app\common\{enum\PayEnum, model\Order, model\User, model\UserLevel};
  21. /**
  22. * 用户逻辑类
  23. * Class UserLogic
  24. * @package app\common\logic
  25. */
  26. class UserLogic extends BaseLogic
  27. {
  28. /**
  29. * @notes 注册奖励
  30. * @param int $userId
  31. * @author cjhao
  32. * @date 2021/9/15 15:25
  33. */
  34. public static function registerAward(int $userId)
  35. {
  36. // 创建分销基础表
  37. DistributionLogic::add($userId);
  38. // 默认等级
  39. self::defaultUserLevel($userId);
  40. // 注册奖励
  41. RegisterAwardLogic::registerAward($userId);
  42. }
  43. /**
  44. * @notes 注册后调整默认等级
  45. * @param $userId
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. * @author cjhao
  50. * @date 2021/9/15 15:33
  51. */
  52. public static function defaultUserLevel($userId)
  53. {
  54. $level = UserLevel::where(['rank' => 1])->find();
  55. if ($level) {
  56. User::where(['id' => $userId])->update(['level' => $level->id]);
  57. }
  58. }
  59. /**
  60. * @notes 格式化会员等级(兼容旧数据)
  61. * @param $condition
  62. * @return mixed
  63. * @author cjhao
  64. * @date 2022/4/6 18:05
  65. */
  66. public static function formatLevelCondition($condition)
  67. {
  68. if(empty($condition)){
  69. return [];
  70. }
  71. $condition['condition_type'] = (int)$condition['condition_type'];
  72. if(!isset($condition['is_single_money'])){
  73. $condition['is_single_money'] = $condition['single_money'] > 0 ? 1 :0;
  74. }
  75. if(!isset($condition['is_total_money'])){
  76. $condition['is_total_money'] = $condition['total_money'] > 0 ? 1 :0;
  77. }
  78. if(!isset($condition['is_total_num'])){
  79. $condition['is_total_num'] = $condition['total_num'] > 0 ? 1 :0;
  80. }
  81. return $condition;
  82. }
  83. /**
  84. * @notes 更新会员等级 todo 该方法调在更新用户的累计金额和累计订单数后调用
  85. * @param int $userId
  86. * @return bool
  87. * @author cjhao
  88. * @date 2021/7/29 18:06
  89. */
  90. public static function updateLevel(int $userId)
  91. {
  92. $user = User::with('user_level')->find($userId);
  93. $levelList = UserLevel::where('rank', '>', $user->rank)
  94. ->order('rank desc')
  95. ->select();
  96. //没有比会员当前高的等级,直接断掉
  97. if (empty($levelList)) {
  98. return true;
  99. }
  100. $orderAmount = Order::where(['user_id' => $userId, 'pay_status' => PayEnum::ISPAID])
  101. ->order('order_amount desc')
  102. ->value('order_amount');
  103. //从最高等级开始遍历
  104. foreach ($levelList as $level) {
  105. $condition = self::formatLevelCondition($level['condition']);
  106. $conditionType = $condition['condition_type'];
  107. $singleMoney = $condition['single_money'];
  108. $isSingleMoney = $condition['is_single_money'];
  109. $totalMoney = $condition['total_money'];
  110. $isTotalMoney = $condition['is_total_money'];
  111. $totalNum = $condition['total_num'];
  112. $isTotalNum = $condition['is_total_num'];
  113. //数据异常不处理
  114. if( 0 == $isSingleMoney && 0 == $isTotalMoney && 0 == $isTotalNum){
  115. continue;
  116. }
  117. //满足其中任意条件
  118. if(0 == $conditionType){
  119. $singleMoneyBoole = false; //满足单笔消费条件
  120. $totalMoneyBoole = false; //满足累计消费金额条件
  121. $totalNumBoole = false; //累计消费次数条件
  122. //是否满足单笔消费条件
  123. if (1 == $isSingleMoney && $singleMoney > 0 && $orderAmount >= $singleMoney) {
  124. $singleMoneyBoole = true;
  125. }
  126. //是否满足累计消费金额
  127. if (1 == $isTotalMoney && $totalMoney > 0 && $user->total_order_amount >= $totalMoney) {
  128. $totalMoneyBoole = true;
  129. }
  130. //是否满足消费次数
  131. if (1 == $isTotalNum && $totalNum > 0 && $user->total_order_num >= $totalNum) {
  132. $totalNumBoole = true;
  133. }
  134. //满足其中任意条件
  135. if ($singleMoneyBoole || $totalMoneyBoole || $totalNumBoole) {
  136. $user->level = $level->id;
  137. $user->save();
  138. break;
  139. }
  140. }else{
  141. //
  142. $singleMoneyBoole = true; //满足单笔消费条件
  143. $totalMoneyBoole = true; //满足累计消费金额条件
  144. $totalNumBoole = true; //累计消费次数条件
  145. //判断不满单笔消费条件
  146. if (1 == $isSingleMoney && $orderAmount < $singleMoney) {
  147. $singleMoneyBoole = false;
  148. }
  149. //判断不满累计消费金额条件
  150. if (1 == $isTotalMoney && $user->total_order_amount < $totalMoney) {
  151. $totalMoneyBoole = false;
  152. }
  153. //判断不满足消费次数条件
  154. if (1 == $isTotalNum && $user->total_order_num < $totalNum) {
  155. $totalNumBoole = false;
  156. }
  157. //满足勾选的全部条件
  158. if ($singleMoneyBoole && $totalMoneyBoole && $totalNumBoole) {
  159. $user->level = $level->id;
  160. $user->save();
  161. break;
  162. }
  163. }
  164. }
  165. return true;
  166. }
  167. }