Notices.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Modules\System\Models;
  3. class Notices extends BaseModel
  4. {
  5. protected $table = 'notices';
  6. protected $fillable = [
  7. 'id', 'title', 'content', 'notice_type_id', "sort", 'type', 'notice_obj', 'is_popup', 'is_deleted', 'created_at', 'updated_at', 'deleted_at',
  8. ];
  9. /**
  10. * @var array
  11. */
  12. protected array $fields = ['id', 'title', 'content', "sort", 'notice_type_id', 'type', 'notice_obj', 'is_popup', 'created_at', 'updated_at'];
  13. /**
  14. * @var array
  15. */
  16. protected array $form = ['title', 'content', 'notice_type_id',"sort", 'type', 'notice_obj', 'is_popup'];
  17. protected $casts = ['notice_obj' => 'array'];
  18. public array $searchable = [
  19. 'title' => 'like',
  20. 'notice_type_id' => '=',
  21. 'type' => '=',
  22. ];
  23. protected string $sortField = 'sort';
  24. /**
  25. * 添加通知
  26. * @param array $data
  27. * @return bool
  28. * @throws \ReflectionException
  29. */
  30. public function storeBy(array $data): mixed
  31. {
  32. $result = '';
  33. $this->beginTransaction();
  34. try {
  35. $result = $this->create($this->filterData($data));
  36. if (isset($data['user_ids']) && !empty($data['user_ids'])) {
  37. $this->addUserNotice($data['user_ids'], $result->id);
  38. }
  39. $this->commit();
  40. } catch (\Exception $exception) {
  41. $this->rollback();
  42. }
  43. return $result->id ?? 0;
  44. }
  45. private function addUserNotice(mixed $userIds, mixed $id)
  46. {
  47. $list = [];
  48. foreach ($userIds as $val) {
  49. $list[] = ['user_id' => $val, 'notice_id' => $id];
  50. }
  51. if (!empty($list)) {
  52. UserNotice::insert($list);
  53. }
  54. return true;
  55. }
  56. }