ActivityUserService.php 4.6 KB

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