ChannelRepository.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Repositories;
  3. use App\Events\ChannelCreatedEvent;
  4. use App\Events\ChannelDeletedEvent;
  5. use App\Events\ChannelUpdatedEvent;
  6. use App\Models\Mysql\ChannelModel;
  7. use KZ2\Laravel\Repositories\Eloquent\Repository;
  8. class ChannelRepository extends Repository
  9. {
  10. public function model()
  11. {
  12. return ChannelModel::class;
  13. }
  14. /**
  15. * 创建一个
  16. * @param $item
  17. * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
  18. */
  19. public function createOne($item)
  20. {
  21. $obj = ChannelModel::query()->create($item);
  22. event(new ChannelCreatedEvent($obj->uid));
  23. return $obj;
  24. }
  25. /**
  26. * 更新一个
  27. * @param $item
  28. * @param $uid
  29. * @return bool
  30. */
  31. public function updateOne($item, $uid)
  32. {
  33. $opResult = false;
  34. $obj = ChannelModel::query()->where(['id', 'uid' => $uid])->first();
  35. if ($obj->count()) {
  36. $opResult = $obj->save($item);
  37. event(new ChannelUpdatedEvent($uid));
  38. }
  39. return $opResult;
  40. }
  41. /**
  42. * 删除一个.
  43. * @param $uid
  44. * @return bool|mixed|null
  45. * @throws \Exception
  46. */
  47. public function deleteOne($uid)
  48. {
  49. $opResult = false;
  50. $obj = ChannelModel::query()->where(['uid' => $uid])->first();
  51. if ($obj->count()) {
  52. $opResult = $obj->delete();
  53. $eventParams = [
  54. 'uid' => $uid,
  55. 'agent_uid' => $obj->agent_uid,
  56. 'mom_app_uid' => $obj->mom_app_uid,
  57. ];
  58. event(new ChannelDeletedEvent($eventParams));
  59. }
  60. return $opResult;
  61. }
  62. /**
  63. * 获取代理的渠道列表
  64. *
  65. * @param $agentUid
  66. * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
  67. */
  68. public function agentChannel($agentUid, $appUids = [])
  69. {
  70. return ChannelModel::query()->where('agent_uid', $agentUid)->whereIn('mom_app_uid', $appUids)->get()->keyBy('mom_app_uid');
  71. }
  72. /**
  73. * 创建代理渠道
  74. *
  75. * @param $agentUid
  76. */
  77. public function createAgentChannel($agentUid)
  78. {
  79. /**
  80. * @var AppRepository $appRepo
  81. */
  82. $appRepo = app(AppRepository::class);
  83. $appData = $appRepo->all(['uid']);
  84. if ($appData->isNotEmpty()) {
  85. $setData = [];
  86. foreach ($appData->toArray() as $key => $item) {
  87. $setData['name'] = rand_letter(9);
  88. $setData['agent_uid'] = $agentUid;
  89. $setData['mom_app_uid'] = $item['uid'];
  90. $this->create($setData);
  91. }
  92. }
  93. }
  94. /**
  95. *
  96. * @param $uid
  97. * @return \Illuminate\Database\Eloquent\Builder
  98. */
  99. public function getAppChannel($uid)
  100. {
  101. return ChannelModel::query()
  102. ->where('uid',$uid)
  103. ->with(['app'=>function($query){
  104. $query->select('uid','name','android_url','ios_url','android_version','ios_version','app_id');
  105. }])->first();
  106. }
  107. }