ActivityUserService.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 Exception;
  8. use Illuminate\Database\Eloquent\Builder;
  9. class ActivityUserService extends BaseService
  10. {
  11. public static string $MODEL = ActivityUser::class;
  12. public static function getWhere(array $search = []): array
  13. {
  14. $where = [];
  15. if (isset($search['id']) && !empty($search['id'])) {
  16. $where[] = ['id', '=', $search['id']];
  17. }
  18. if (isset($search['title']) && !empty($search['title'])) {
  19. $where[] = ['title', 'like', "%{$search['title']}%"];
  20. }
  21. if (isset($search['status']) && !empty($search['status'])) {
  22. $where[] = ['status', '=', $search['status']];
  23. }
  24. if (isset($search['member_id']) && !empty($search['member_id'])) {
  25. $where[] = ['member_id', '=', $search['member_id']];
  26. }
  27. return $where;
  28. }
  29. /**
  30. * Update or create
  31. * @param array $params
  32. * @return bool
  33. * @throws Exception
  34. */
  35. public static function submit(array $params = []): bool
  36. {
  37. if (isset($params['start_time']))
  38. $params['start_time'] = strtotime($params['start_time'] . " 00:00:00");
  39. if (isset($params['end_time']))
  40. $params['end_time'] = strtotime($params['end_time'] . " 23:59:59");
  41. if (isset($params['detail_image']))
  42. $params['detail_image'] = Util::replacePartInUrl($params['detail_image']);
  43. if (!empty($params['id'])) {
  44. $info = static::findOne(['id' => $params['id']]);
  45. if (!$info) throw new Exception("操作失败", HttpStatus::CUSTOM_ERROR);
  46. $info->update($params);
  47. } else {
  48. $info = static::$MODEL::create($params);
  49. }
  50. return true;
  51. }
  52. }