ActivityRewardService.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\HttpStatus;
  4. use App\Models\ActivityReward;
  5. use App\Models\Role;
  6. use Exception;
  7. class ActivityRewardService extends BaseService
  8. {
  9. public static string $MODEL = ActivityReward::class;
  10. /**
  11. * @description: 查询单条数据
  12. * @param array $search
  13. * @return ActivityReward|null
  14. */
  15. public static function findOne(array $search): ActivityReward|null
  16. {
  17. return static::$MODEL::where(static::getWhere($search))->first();
  18. }
  19. public static function getWhere(array $search = []): array
  20. {
  21. $where = [];
  22. if (isset($search['title']) && !empty($search['title'])) {
  23. $where[] = ['title', 'like', "%{$search['title']}%"];
  24. }
  25. return $where;
  26. }
  27. public static function deleteAll(array $search = []): bool
  28. {
  29. $count = static::$MODEL::where(static::getWhere($search))->delete();
  30. if ($count < 1) throw new Exception('删除失败',HttpStatus::CUSTOM_ERROR);
  31. return true;
  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 (!empty($params['id'])) {
  46. $info = static::findOne(['id' => $params['id']]);
  47. if (!$info) throw new Exception("操作失败",HttpStatus::CUSTOM_ERROR);
  48. $info->update($params);
  49. } else {
  50. static::$MODEL::create($params);
  51. }
  52. return true;
  53. }
  54. }