ActivityUserService.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. return $where;
  25. }
  26. /**
  27. * Update or create
  28. * @param array $params
  29. * @return bool
  30. * @throws Exception
  31. */
  32. public static function submit(array $params = []): bool
  33. {
  34. if (isset($params['start_time']))
  35. $params['start_time'] = strtotime($params['start_time'] . " 00:00:00");
  36. if (isset($params['end_time']))
  37. $params['end_time'] = strtotime($params['end_time'] . " 23:59:59");
  38. if (isset($params['detail_image']))
  39. $params['detail_image'] = Util::replacePartInUrl($params['detail_image']);
  40. if (!empty($params['id'])) {
  41. $info = static::findOne(['id' => $params['id']]);
  42. if (!$info) throw new Exception("操作失败", HttpStatus::CUSTOM_ERROR);
  43. $info->update($params);
  44. } else {
  45. $info = static::$MODEL::create($params);
  46. }
  47. return true;
  48. }
  49. }