| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- namespace App\Repositories;
- use App\Events\ChannelCreatedEvent;
- use App\Events\ChannelDeletedEvent;
- use App\Events\ChannelUpdatedEvent;
- use App\Models\Mysql\ChannelModel;
- use KZ2\Laravel\Repositories\Eloquent\Repository;
- class ChannelRepository extends Repository
- {
- public function model()
- {
- return ChannelModel::class;
- }
- /**
- * 创建一个
- * @param $item
- * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
- */
- public function createOne($item)
- {
- $obj = ChannelModel::query()->create($item);
- event(new ChannelCreatedEvent($obj->uid));
- return $obj;
- }
- /**
- * 更新一个
- * @param $item
- * @param $uid
- * @return bool
- */
- public function updateOne($item, $uid)
- {
- $opResult = false;
- $obj = ChannelModel::query()->where(['id', 'uid' => $uid])->first();
- if ($obj->count()) {
- $opResult = $obj->save($item);
- event(new ChannelUpdatedEvent($uid));
- }
- return $opResult;
- }
- /**
- * 删除一个.
- * @param $uid
- * @return bool|mixed|null
- * @throws \Exception
- */
- public function deleteOne($uid)
- {
- $opResult = false;
- $obj = ChannelModel::query()->where(['uid' => $uid])->first();
- if ($obj->count()) {
- $opResult = $obj->delete();
- $eventParams = [
- 'uid' => $uid,
- 'agent_uid' => $obj->agent_uid,
- 'mom_app_uid' => $obj->mom_app_uid,
- ];
- event(new ChannelDeletedEvent($eventParams));
- }
- return $opResult;
- }
- /**
- * 获取代理的渠道列表
- *
- * @param $agentUid
- * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
- */
- public function agentChannel($agentUid, $appUids = [])
- {
- return ChannelModel::query()->where('agent_uid', $agentUid)->whereIn('mom_app_uid', $appUids)->get()->keyBy('mom_app_uid');
- }
- /**
- * 创建代理渠道
- *
- * @param $agentUid
- */
- public function createAgentChannel($agentUid)
- {
- /**
- * @var AppRepository $appRepo
- */
- $appRepo = app(AppRepository::class);
- $appData = $appRepo->all(['uid']);
- if ($appData->isNotEmpty()) {
- $setData = [];
- foreach ($appData->toArray() as $key => $item) {
- $setData['name'] = rand_letter(9);
- $setData['agent_uid'] = $agentUid;
- $setData['mom_app_uid'] = $item['uid'];
- $this->create($setData);
- }
- }
- }
- /**
- *
- * @param $uid
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function getAppChannel($uid)
- {
- return ChannelModel::query()
- ->where('uid',$uid)
- ->with(['app'=>function($query){
- $query->select('uid','name','android_url','ios_url','android_version','ios_version','app_id');
- }])->first();
- }
- }
|