ActivityRewardService.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. if (isset($search['start_time']) && !empty($search['start_time'])) {
  26. if (is_array($search['start_time'])) {
  27. if (count($search['start_time']) == 2) {
  28. $where[] = ['start_time', $search['start_time'][0], $search['start_time'][1]];
  29. } else {
  30. $where[] = ['start_time', '=', $search['start_time'][0]];
  31. }
  32. } else {
  33. $where[] = ['start_time', '=', $search['start_time']];
  34. }
  35. }
  36. if (isset($search['end_time']) && !empty($search['end_time'])) {
  37. if (is_array($search['end_time'])) {
  38. if (count($search['end_time']) == 2) {
  39. $where[] = ['end_time', $search['end_time'][0], $search['end_time'][1]];
  40. } else {
  41. $where[] = ['end_time', '=', $search['end_time'][0]];
  42. }
  43. } else {
  44. $where[] = ['end_time', '=', $search['end_time']];
  45. }
  46. }
  47. return $where;
  48. }
  49. public static function deleteAll(array $search = []): bool
  50. {
  51. $count = static::$MODEL::where(static::getWhere($search))->delete();
  52. if ($count < 1) throw new Exception('删除失败', HttpStatus::CUSTOM_ERROR);
  53. return true;
  54. }
  55. /**
  56. * Update or create
  57. * @param array $params
  58. * @return bool
  59. * @throws Exception
  60. */
  61. public static function submit(array $params = []): bool
  62. {
  63. if (isset($params['start_time']))
  64. $params['start_time'] = strtotime($params['start_time'] . " 00:00:00");
  65. if (isset($params['end_time']))
  66. $params['end_time'] = strtotime($params['end_time'] . " 23:59:59");
  67. if (!empty($params['id'])) {
  68. $info = static::findOne(['id' => $params['id']]);
  69. if (!$info) throw new Exception("操作失败", HttpStatus::CUSTOM_ERROR);
  70. $info->update($params);
  71. } else {
  72. static::$MODEL::create($params);
  73. }
  74. return true;
  75. }
  76. }