AppRepository.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Repositories;
  3. use App\Events\AppCreatedEvent;
  4. use App\Events\AppDeletedEvent;
  5. use App\Events\AppUpdatedEvent;
  6. use App\Models\Mysql\AppModel;
  7. use KZ2\Laravel\Repositories\Eloquent\Repository;
  8. class AppRepository extends Repository
  9. {
  10. public function model()
  11. {
  12. return AppModel::class;
  13. }
  14. /**
  15. * 查询数据返回UID
  16. * @param string $key
  17. * @param array $uids
  18. * @return array
  19. */
  20. public function findArray(string $key, array $uids)
  21. {
  22. return AppModel::query()->whereIn($key, $uids)->pluck($key)->toArray();
  23. }
  24. /**
  25. * 批量删除
  26. * @param string $key
  27. * @param array $uids
  28. * @return bool
  29. */
  30. public function delectUids(string $key, array $uids)
  31. {
  32. return AppModel::query()->whereIn($key, $uids)->delete();
  33. }
  34. /**
  35. * 创建一个.
  36. * @param $item
  37. * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
  38. */
  39. public function createOne($item)
  40. {
  41. $obj = AppModel::query()->create($item);
  42. // 事件触发.
  43. event(new AppCreatedEvent($obj->uid));
  44. return $obj;
  45. }
  46. public function updateOne($item, $uid)
  47. {
  48. $saveResult = false;
  49. $obj = AppModel::query()->where(['uid' => $uid])->first();
  50. if ($obj->count()) {
  51. event(new AppUpdatedEvent($uid));
  52. }
  53. return $saveResult;
  54. }
  55. /**
  56. * 删除一个.
  57. * @param $uid
  58. * @return bool|mixed|null
  59. * @throws \Exception
  60. */
  61. public function deleteOne($uid)
  62. {
  63. $opResult = false;
  64. $obj = AppModel::query()->where(['uid' => $uid])->first();
  65. if ($obj->count()) {
  66. $opResult = $obj->delete();
  67. $eventParams = [
  68. 'uid' => $obj->uid,
  69. 'icon_url' => $obj->icon_url,
  70. ];
  71. event(new AppDeletedEvent($eventParams));
  72. }
  73. return $opResult;
  74. }
  75. /**
  76. * 查询应用列表(uid为key)
  77. * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
  78. */
  79. public function findListKeyBy()
  80. {
  81. return AppModel::query()->get()->keyBy('uid');
  82. }
  83. public function getAgentApp($appUid = [])
  84. {
  85. return AppModel::query()->whereIn('uid', $appUid)->where('status', 1)->get(['uid', 'name']);
  86. }
  87. }