ActivityUserService.php 2.0 KB

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