ActivityRewardService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\Config;
  7. use App\Models\Role;
  8. use Exception;
  9. class ActivityRewardService extends BaseService
  10. {
  11. public static string $MODEL = ActivityReward::class;
  12. /**
  13. * @description: 查询单条数据
  14. * @param array $search
  15. * @return ActivityReward|null
  16. */
  17. public static function findOne(array $search): ActivityReward|null
  18. {
  19. return static::$MODEL::where(static::getWhere($search))->first();
  20. }
  21. public static function getWhere(array $search = []): array
  22. {
  23. $where = [];
  24. if (isset($search['id']) && !empty($search['id'])) {
  25. $where[] = ['id', '=', $search['id']];
  26. }
  27. if (isset($search['status']) && $search['status'] != '') {
  28. $where[] = ['status', '=', $search['status']];
  29. }
  30. if (isset($search['type']) && $search['type'] != '') {
  31. $where[] = ['type', '=', $search['type']];
  32. }
  33. if (isset($search['title']) && !empty($search['title'])) {
  34. $where[] = ['title', 'like', "%{$search['title']}%"];
  35. }
  36. if (isset($search['start_time']) && !empty($search['start_time'])) {
  37. if (is_array($search['start_time'])) {
  38. if (count($search['start_time']) == 2) {
  39. $where[] = ['start_time', $search['start_time'][0], $search['start_time'][1]];
  40. } else {
  41. $where[] = ['start_time', '=', $search['start_time'][0]];
  42. }
  43. } else {
  44. $where[] = ['start_time', '=', $search['start_time']];
  45. }
  46. }
  47. if (isset($search['end_time']) && !empty($search['end_time'])) {
  48. if (is_array($search['end_time'])) {
  49. if (count($search['end_time']) == 2) {
  50. $where[] = ['end_time', $search['end_time'][0], $search['end_time'][1]];
  51. } else {
  52. $where[] = ['end_time', '=', $search['end_time'][0]];
  53. }
  54. } else {
  55. $where[] = ['end_time', '=', $search['end_time']];
  56. }
  57. }
  58. return $where;
  59. }
  60. public static function deleteAll(array $search = []): bool
  61. {
  62. $count = static::$MODEL::where(static::getWhere($search))->delete();
  63. if ($count < 1) throw new Exception('删除失败', HttpStatus::CUSTOM_ERROR);
  64. return true;
  65. }
  66. /**
  67. * Update or create
  68. * @param array $params
  69. * @return bool
  70. * @throws Exception
  71. */
  72. public static function submit(array $params = []): bool
  73. {
  74. if (isset($params['start_time']))
  75. $params['start_time'] = strtotime($params['start_time'] . " 00:00:00");
  76. if (isset($params['end_time']))
  77. $params['end_time'] = strtotime($params['end_time'] . " 23:59:59");
  78. if (isset($params['detail_image']))
  79. $params['detail_image'] = Util::replacePartInUrl($params['detail_image']);
  80. if (!empty($params['id'])) {
  81. $info = static::findOne(['id' => $params['id']]);
  82. if (!$info) throw new Exception("操作失败", HttpStatus::CUSTOM_ERROR);
  83. $info->update($params);
  84. } else {
  85. static::$MODEL::create($params);
  86. }
  87. return true;
  88. }
  89. public static function promotionalActivity($chatId): array
  90. {
  91. $keyboard = [];
  92. $url = Config::where('field','activity_home_url ')->first()->val;
  93. $url.="?member_id=".$chatId;
  94. $keyboard[] = [
  95. ['text'=>'活动首页','url'=>$url]
  96. ];
  97. $msg = [];
  98. $msg['chat_id'] = $chatId;
  99. $msg['text'] = "点击下方按钮进入活动首页";
  100. $msg['reply_markup'] = json_encode(['inline_keyboard' => $keyboard]);
  101. return $msg;
  102. }
  103. }