ActivityRewardService.php 9.7 KB

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