| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- /**
- * 描述.
- *
- * @author nj
- * @date 2021-05-18 20:46:19
- */
- namespace App\Models\Mysql;
- use App\Jobs\AgentUpdateChildUidsByAllParentUidsJob;
- use Carbon\Carbon;
- class AgentModel extends BaseMysql
- {
- protected $table = 'agent';
- /**
- * UUID 键名
- * @var string
- */
- protected $uuidKeyName = 'uid';
- /**
- * 隐藏字段.
- *
- * @var array
- */
- protected $hidden = ['password', 'salt', 'otp_secret', 'withdraw_pwd'];
- /**
- * The "booted" method of the model.
- *
- * @return void
- */
- protected static function boot()
- {
- parent::boot();
- self::creating(function ($model) {
- $model->{$model->uuidKeyName} = self::generateId($model->uuidKeyName);
- });
- self::created(function ($model) {
- $uuid = hashEncodeId($model->id);
- $model->{$model->uuidKeyName} = $uuid;
- $strParentUids = $model->parent_uid;
- if (!empty($model->parent_uid)) {
- $arrParentUids = (new AgentModel())->findAllParentUids($model->parent_uid);
- if (!empty($arrParentUids)) {
- $strParentUids .= ','. implode(',', $arrParentUids);
- }
- }
- $model->parent_uids = $strParentUids;
- $model->save();
- // 马上创建一个属性数据.
- $setarr2 = [
- 'agent_uid' => $uuid,
- 'td_date' => Carbon::now()->toDateString(),
- ];
- AgentAttrsModel::create($setarr2);
- // 异步job,把所有上级的对象的父级和下级字段自己更新一次.
- $jobParams = [
- 'uid' => $uuid,
- ];
- dispatch(new AgentUpdateChildUidsByAllParentUidsJob($jobParams));
- });
- }
- /**
- * 关联属性表.
- * @return \Illuminate\Database\Eloquent\Relations\HasOne
- */
- public function attrs()
- {
- return $this->hasOne(AgentAttrsModel::class, 'agent_uid', $this->uuidKeyName);
- }
- public function attrsDay()
- {
- return $this->hasOne(AgentAttrsDayModel::class, 'agent_uid', $this->uuidKeyName);
- }
- public function everyDay()
- {
- return $this->hasOne(AgentAttrsDayModel::class, 'agent_uid', $this->uuidKeyName);
- }
- /**
- * 关联多个渠道.
- * @return \Illuminate\Database\Eloquent\Relations\HasMany
- */
- public function channels()
- {
- return $this->hasMany(ChannelModel::class, 'agent_uid', $this->uuidKeyName);
- }
- /**
- * 找到所有上级UID的集合(不包含参数$parentUid).
- * @param string $parentUid
- * @param array $cacheUids
- * @return array|mixed
- */
- public function findAllParentUids(string $parentUid, $cacheUids = [])
- {
- if (!empty($parentUid)) {
- $tmpParentUid = '';
- $parentObj = $this->query()->where(['uid' => $parentUid])->first(['id', 'uid', 'parent_uid']);
- if (!empty($parentObj->parent_uid)) {
- $tmpParentUid = $parentObj->parent_uid;
- array_push($cacheUids, $parentObj->uid);
- }
- $cacheUids = self::findAllParentUids($tmpParentUid, $cacheUids);
- }
- return $cacheUids;
- }
- /**
- * 找到所有子级UID集合(不包含参数$currUid).
- * @param string $currUid
- * @param array $cacheUids
- * @return array|mixed
- */
- public function findAllChildUids(string $currUid, $cacheUids = [])
- {
- $childItems = $this->query()->where(['parent_uid' => $currUid])->orderBy('id', 'ASC')->get(['id', 'uid', 'parent_uid']);
- if ($childItems->count() > 0) {
- foreach ($childItems as $item) {
- array_push($cacheUids, $item->uid);
- $cacheUids = self::findAllChildUids($item->uid, $cacheUids);
- }
- }
- return $cacheUids;
- }
- }
|