ActivityRewardService.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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\Config;
  7. use App\Models\Role;
  8. use App\Models\UserSession;
  9. use App\Models\User;
  10. use Illuminate\Support\Facades\Validator;
  11. use Illuminate\Validation\Rule;
  12. use Exception;
  13. class ActivityRewardService extends BaseService
  14. {
  15. public static string $MODEL = ActivityReward::class;
  16. public static function configurationDefaults(): array
  17. {
  18. return [
  19. 'publish_platforms' => [], 'budget' => null, 'reward_quota' => null, 'sort' => 0,
  20. 'register_time_limited' => false, 'register_start_time' => null, 'register_end_time' => null,
  21. 'register_platforms' => [], 'deposit_limited' => false, 'deposit_min' => null,
  22. 'deposit_max' => null, 'show_ineligible' => true,
  23. ];
  24. }
  25. private static function configuration(array $params, ?ActivityReward $existing): array
  26. {
  27. $defaults = static::configurationDefaults();
  28. if ((int)($params['type'] ?? $existing?->type) === 2) {
  29. return $defaults;
  30. }
  31. $quotaMode = !$existing || $existing->quota_enabled;
  32. Validator::make($params, [$quotaMode ? 'budget' : 'reward_quota' => ['prohibited']])->validate();
  33. $previous = $existing ? $existing->only(array_keys($defaults)) : [];
  34. foreach (['publish_platforms', 'register_platforms'] as $field) {
  35. if (($previous[$field] ?? null) === null) $previous[$field] = [];
  36. }
  37. $config = array_replace($defaults, $previous, array_intersect_key($params, $defaults));
  38. if ($quotaMode) $config['budget'] = null;
  39. else $config['reward_quota'] = null;
  40. if (empty($config['register_time_limited'])) {
  41. $config['register_start_time'] = $config['register_end_time'] = null;
  42. }
  43. if (empty($config['deposit_limited'])) {
  44. $config['deposit_min'] = $config['deposit_max'] = null;
  45. }
  46. $money = ['nullable', 'numeric', 'min:0', 'regex:/^\d{1,12}(\.\d{1,2})?$/'];
  47. Validator::make($config, [
  48. 'publish_platforms' => ['present', 'array'],
  49. 'publish_platforms.*' => ['required', 'string', 'distinct', Rule::in(['PC', 'H5', 'APP'])],
  50. 'reward_quota' => ['nullable', 'integer', 'min:0', 'max:2147483647'],
  51. 'budget' => $money,
  52. 'sort' => ['required', 'integer', 'min:0', 'max:4294967295'],
  53. 'register_time_limited' => ['required', 'boolean'],
  54. 'register_start_time' => ['nullable', 'required_if:register_time_limited,1', 'date'],
  55. 'register_end_time' => ['nullable', 'required_if:register_time_limited,1', 'date', 'after_or_equal:register_start_time'],
  56. 'register_platforms' => ['present', 'array'],
  57. 'register_platforms.*' => ['required', 'string', 'distinct', Rule::in(['PC', 'H5', 'APP'])],
  58. 'deposit_limited' => ['required', 'boolean'],
  59. 'deposit_min' => array_merge($money, ['required_if:deposit_limited,1']),
  60. 'deposit_max' => array_merge($money, ['required_if:deposit_limited,1', 'gte:deposit_min']),
  61. 'show_ineligible' => ['required', 'boolean'],
  62. ])->validate();
  63. foreach (['budget', 'deposit_min', 'deposit_max'] as $field) {
  64. if ($config[$field] !== null) {
  65. $config[$field] = bcadd((string)$config[$field], '0', 2);
  66. }
  67. }
  68. if ($config['reward_quota'] !== null) $config['reward_quota'] = (int)$config['reward_quota'];
  69. foreach (['register_start_time', 'register_end_time'] as $field) {
  70. if ($config[$field] !== null) {
  71. $config[$field] = date('Y-m-d H:i:s', strtotime($config[$field]));
  72. }
  73. }
  74. return $config;
  75. }
  76. /**
  77. * @description: 查询单条数据
  78. * @param array $search
  79. * @return ActivityReward|null
  80. */
  81. public static function findOne(array $search): ActivityReward|null
  82. {
  83. return static::$MODEL::where(static::getWhere($search))->first();
  84. }
  85. public static function activeForMember(int $type, $memberId): ?ActivityReward
  86. {
  87. $context = ActivityEligibilityService::memberContext($memberId);
  88. $activities = ActivityReward::where('lang', 'zh')->where('type', $type)->where('status', 1)
  89. ->where('start_time', '<=', time())->where('end_time', '>=', time())
  90. ->orderByDesc('sort')->orderByDesc('id')->get();
  91. foreach ($activities as $activity) {
  92. if (!ActivityEligibilityService::evaluate($activity, $context)['is_eligible']) continue;
  93. if (!ActivityQuotaService::canAward($activity, $memberId)) continue;
  94. return $activity;
  95. }
  96. return null;
  97. }
  98. public static function getWhere(array $search = []): array
  99. {
  100. $where = [];
  101. if (isset($search['id']) && !empty($search['id'])) {
  102. $where[] = ['id', '=', $search['id']];
  103. }
  104. if (isset($search['status']) && $search['status'] != '') {
  105. $where[] = ['status', '=', $search['status']];
  106. }
  107. if (isset($search['type']) && $search['type'] != '') {
  108. $where[] = ['type', '=', $search['type']];
  109. }
  110. if (isset($search['title']) && !empty($search['title'])) {
  111. $where[] = ['title', 'like', "%{$search['title']}%"];
  112. }
  113. if (isset($search['start_time']) && !empty($search['start_time'])) {
  114. if (is_array($search['start_time'])) {
  115. if (count($search['start_time']) == 2) {
  116. $where[] = ['start_time', $search['start_time'][0], $search['start_time'][1]];
  117. } else {
  118. $where[] = ['start_time', '=', $search['start_time'][0]];
  119. }
  120. } else {
  121. $where[] = ['start_time', '=', $search['start_time']];
  122. }
  123. }
  124. if (isset($search['end_time']) && !empty($search['end_time'])) {
  125. if (is_array($search['end_time'])) {
  126. if (count($search['end_time']) == 2) {
  127. $where[] = ['end_time', $search['end_time'][0], $search['end_time'][1]];
  128. } else {
  129. $where[] = ['end_time', '=', $search['end_time'][0]];
  130. }
  131. } else {
  132. $where[] = ['end_time', '=', $search['end_time']];
  133. }
  134. }
  135. return $where;
  136. }
  137. public static function deleteAll(array $search = []): bool
  138. {
  139. $count = static::$MODEL::where(static::getWhere($search))->delete();
  140. if ($count < 1) throw new Exception('删除失败', HttpStatus::CUSTOM_ERROR);
  141. return true;
  142. }
  143. /**
  144. * Update or create
  145. * @param array $params
  146. * @return bool
  147. * @throws Exception
  148. */
  149. public static function submit(array $params = []): bool
  150. {
  151. $info = !empty($params['id']) ? static::findOne(['id' => $params['id']]) : null;
  152. if (!empty($params['id']) && !$info) throw new Exception('操作失败', HttpStatus::CUSTOM_ERROR);
  153. $params = array_merge($params, static::configuration($params, $info));
  154. unset($params['rewarded_count'], $params['quota_enabled'], $params['budget_used']); // Server-owned fields.
  155. if (isset($params['start_time']))
  156. $params['start_time'] = strtotime($params['start_time']);
  157. if (isset($params['end_time']))
  158. $params['end_time'] = strtotime($params['end_time']);
  159. if (isset($params['detail_image']))
  160. $params['detail_image'] = Util::replacePartInUrl($params['detail_image']);
  161. if (isset($params['pc_image']))
  162. $params['pc_image'] = Util::replacePartInUrl($params['pc_image']);
  163. if (isset($params['detail_banner']))
  164. $params['detail_banner'] = Util::replacePartInUrl($params['detail_banner']);
  165. if (isset($params['pc_banner']))
  166. $params['pc_banner'] = Util::replacePartInUrl($params['pc_banner']);
  167. if (!empty($params['id'])) {
  168. $info->update($params);
  169. } else {
  170. $info = new ActivityReward($params);
  171. $info->quota_enabled = true; // Existing rows stay false; clients cannot change the mode.
  172. $info->save();
  173. }
  174. return true;
  175. }
  176. public static function promotionalActivity($chatId): array
  177. {
  178. $keyboard = [];
  179. $url = Config::where('field','activity_home_url')->first()->val;
  180. $url.="?member_id=".$chatId;
  181. $keyboard[] = [
  182. ['text'=>'活动首页','url'=>$url]
  183. ];
  184. $msg = [];
  185. $msg['chat_id'] = $chatId;
  186. $msg['text'] = "点击下方按钮进入活动首页";
  187. $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  188. return $msg;
  189. }
  190. //F1bet娱乐更多玩法(足球首页)
  191. public static function footballUrl($chatId): array
  192. {
  193. $token = UserSession::setToken($chatId);
  194. $is_bind = User::where('member_id', $chatId)->value('account') ? 1 : 0;
  195. $keyboard = [];
  196. $url = Config::where('field','football_home_url ')->first()->val;
  197. $url.="?token=".$token."&is_bind=".$is_bind;
  198. $keyboard[] = [
  199. ['text'=>'F1bet娱乐首页','url'=>$url]
  200. ];
  201. $msg = [];
  202. $msg['chat_id'] = $chatId;
  203. $msg['text'] = "点击下方按钮进入F1bet娱乐首页";
  204. $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  205. return $msg;
  206. }
  207. }