ActivityRewardService.php 11 KB

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