UserService.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace app\common\Service\User;
  3. use app\common\exception\BusinessException;
  4. use app\common\model\User as UserModel;
  5. class UserService
  6. {
  7. public static function getUserById($userId = 0)
  8. {
  9. return UserModel::where('id', $userId)->find();;
  10. }
  11. /**
  12. * @name 追加消费金额和总订单数
  13. * @param int|object $userId 会员ID
  14. * @param float $amount 变更金额
  15. * @param int $orderCount 订单数量增量,默认为1
  16. * @return boolean
  17. */
  18. public static function consume($userId = 0, $amount, $orderCount = 1)
  19. {
  20. // 判断金额
  21. if ($amount == 0) {
  22. return false;
  23. }
  24. $user = self::getUserById($userId);
  25. if (!$user) {
  26. throw new BusinessException('未找到用户');
  27. }
  28. // 更新会员消费金额和订单数量
  29. $user->setInc('total_consume', $amount);
  30. if ($orderCount > 0) {
  31. $user->setInc('order_count', $orderCount);
  32. }
  33. return true;
  34. }
  35. }