ActivityUserService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\HttpStatus;
  4. use App\Constants\Util;
  5. use App\Models\ActivityReward;
  6. use App\Models\ActivityUser;
  7. use App\Models\Wallet as WalletModel;
  8. use Exception;
  9. use Illuminate\Database\Eloquent\Builder;
  10. class ActivityUserService extends BaseService
  11. {
  12. public static string $MODEL = ActivityUser::class;
  13. public static function getWhere(array $search = []): array
  14. {
  15. $where = [];
  16. if (isset($search['id']) && !empty($search['id'])) {
  17. $where[] = ['id', '=', $search['id']];
  18. }
  19. if (isset($search['title']) && !empty($search['title'])) {
  20. $where[] = ['title', 'like', "%{$search['title']}%"];
  21. }
  22. if (isset($search['status']) && $search['status'] != '') {
  23. $where[] = ['status', '=', $search['status']];
  24. }
  25. if (isset($search['member_id']) && !empty($search['member_id'])) {
  26. $where[] = ['member_id', '=', $search['member_id']];
  27. }
  28. return $where;
  29. }
  30. public static function findOne(array $search)
  31. {
  32. return static::$MODEL::where(static::getWhere($search))->first();
  33. }
  34. /**
  35. * Update or create
  36. * @param array $params
  37. * @return bool
  38. * @throws Exception
  39. */
  40. public static function submit(array $params = []): bool
  41. {
  42. if (isset($params['start_time']))
  43. $params['start_time'] = strtotime($params['start_time'] . " 00:00:00");
  44. if (isset($params['end_time']))
  45. $params['end_time'] = strtotime($params['end_time'] . " 23:59:59");
  46. if (isset($params['detail_image']))
  47. $params['detail_image'] = Util::replacePartInUrl($params['detail_image']);
  48. if (!empty($params['id'])) {
  49. $info = static::findOne(['id' => $params['id']]);
  50. if (!$info) throw new Exception("操作失败", HttpStatus::CUSTOM_ERROR);
  51. $info->update($params);
  52. } else {
  53. $info = static::$MODEL::create($params);
  54. }
  55. return true;
  56. }
  57. public static function updateBettingAmount($memberId, $amount)
  58. {
  59. $activityUser = static::findOne([
  60. 'member_id' => $memberId,
  61. 'status' => static::$MODEL::STATUS_IN_PROGRESS
  62. ]);
  63. if ($activityUser) {
  64. $activityUser->increment('effective_betting_amount', $amount);
  65. if ($activityUser->effective_betting_amount >= $activityUser->betting_amount) {
  66. $activityUser->status = static::$MODEL::STATUS_COMPLETE;
  67. }
  68. $activityUser->save();
  69. }
  70. }
  71. public static function finish($id)
  72. {
  73. $activityUser = static::findOne(['id' => $id]);
  74. $activityUser->status = static::$MODEL::STATUS_COMPLETE;
  75. $activityUser->save();
  76. }
  77. public static function gift($id, $amount, $bettingAmount)
  78. {
  79. $activityUser = static::findOne(['id' => $id]);
  80. if (!$activityUser) throw new Exception('活动不存在', HttpStatus::CUSTOM_ERROR);
  81. if ($activityUser->status == static::$MODEL::STATUS_IN_PROGRESS) throw new Exception('充值失败:活动进行中', HttpStatus::CUSTOM_ERROR);
  82. if ($activityUser->status == static::$MODEL::STATUS_COMPLETE) throw new Exception("充值失败:活动已完成", HttpStatus::CUSTOM_ERROR);
  83. if ($activityUser->status != static::$MODEL::STATUS_APPLY) throw new Exception('充值失败:状态异常', HttpStatus::CUSTOM_ERROR);
  84. $activityUser->status = static::$MODEL::STATUS_IN_PROGRESS;
  85. $activityUser->betting_amount = $bettingAmount;
  86. $activityUser->gift_amount = $amount;
  87. $activityUser->save();
  88. $memberId = $activityUser->member_id;
  89. $res = WalletService::updateBalance($memberId, $amount);
  90. BalanceLogService::addLog($memberId, $amount, $res['before_balance'], $res['after_balance'], "优惠活动", $activityUser->id, "$activityUser->title; 赠送:$activityUser->gift_amount; 打码量:$activityUser->betting_amount");
  91. $availableBalance = floatval($res['after_balance']);
  92. // 去除多余0后,再用 sprintf 补足两位
  93. $availableBalance = sprintf('%.2f', $availableBalance);
  94. TopUpService::notifyTransferSuccess($memberId, "余额变动:" . ($amount > 0 ? '+' : '') . "{$amount} \n总余额为:{$availableBalance}");
  95. }
  96. }