ActivityRewardService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\Role;
  7. use Exception;
  8. class ActivityRewardService extends BaseService
  9. {
  10. public static string $MODEL = ActivityReward::class;
  11. /**
  12. * @description: 查询单条数据
  13. * @param array $search
  14. * @return ActivityReward|null
  15. */
  16. public static function findOne(array $search): ActivityReward|null
  17. {
  18. return static::$MODEL::where(static::getWhere($search))->first();
  19. }
  20. public static function getWhere(array $search = []): array
  21. {
  22. $where = [];
  23. if (isset($search['id']) && !empty($search['id'])) {
  24. $where[] = ['id', '=', $search['id']];
  25. }
  26. if (isset($search['status']) && $search['status'] != '') {
  27. $where[] = ['status', '=', $search['status']];
  28. }
  29. if (isset($search['title']) && !empty($search['title'])) {
  30. $where[] = ['title', 'like', "%{$search['title']}%"];
  31. }
  32. if (isset($search['start_time']) && !empty($search['start_time'])) {
  33. if (is_array($search['start_time'])) {
  34. if (count($search['start_time']) == 2) {
  35. $where[] = ['start_time', $search['start_time'][0], $search['start_time'][1]];
  36. } else {
  37. $where[] = ['start_time', '=', $search['start_time'][0]];
  38. }
  39. } else {
  40. $where[] = ['start_time', '=', $search['start_time']];
  41. }
  42. }
  43. if (isset($search['end_time']) && !empty($search['end_time'])) {
  44. if (is_array($search['end_time'])) {
  45. if (count($search['end_time']) == 2) {
  46. $where[] = ['end_time', $search['end_time'][0], $search['end_time'][1]];
  47. } else {
  48. $where[] = ['end_time', '=', $search['end_time'][0]];
  49. }
  50. } else {
  51. $where[] = ['end_time', '=', $search['end_time']];
  52. }
  53. }
  54. return $where;
  55. }
  56. public static function deleteAll(array $search = []): bool
  57. {
  58. $count = static::$MODEL::where(static::getWhere($search))->delete();
  59. if ($count < 1) throw new Exception('删除失败', HttpStatus::CUSTOM_ERROR);
  60. return true;
  61. }
  62. /**
  63. * Update or create
  64. * @param array $params
  65. * @return bool
  66. * @throws Exception
  67. */
  68. public static function submit(array $params = []): bool
  69. {
  70. if (isset($params['start_time']))
  71. $params['start_time'] = strtotime($params['start_time'] . " 00:00:00");
  72. if (isset($params['end_time']))
  73. $params['end_time'] = strtotime($params['end_time'] . " 23:59:59");
  74. if (isset($params['detail_image']))
  75. $params['detail_image'] = Util::replacePartInUrl($params['detail_image']);
  76. if (!empty($params['id'])) {
  77. $info = static::findOne(['id' => $params['id']]);
  78. if (!$info) throw new Exception("操作失败", HttpStatus::CUSTOM_ERROR);
  79. $info->update($params);
  80. } else {
  81. static::$MODEL::create($params);
  82. }
  83. return true;
  84. }
  85. }