AgentModel.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * 描述.
  4. *
  5. * @author nj
  6. * @date 2021-05-18 20:46:19
  7. */
  8. namespace App\Models\Mysql;
  9. use App\Jobs\AgentUpdateChildUidsByAllParentUidsJob;
  10. use Carbon\Carbon;
  11. class AgentModel extends BaseMysql
  12. {
  13. protected $table = 'agent';
  14. /**
  15. * UUID 键名
  16. * @var string
  17. */
  18. protected $uuidKeyName = 'uid';
  19. /**
  20. * 隐藏字段.
  21. *
  22. * @var array
  23. */
  24. protected $hidden = ['password', 'salt', 'otp_secret', 'withdraw_pwd'];
  25. /**
  26. * The "booted" method of the model.
  27. *
  28. * @return void
  29. */
  30. protected static function boot()
  31. {
  32. parent::boot();
  33. self::creating(function ($model) {
  34. $model->{$model->uuidKeyName} = self::generateId($model->uuidKeyName);
  35. });
  36. self::created(function ($model) {
  37. $uuid = hashEncodeId($model->id);
  38. $model->{$model->uuidKeyName} = $uuid;
  39. $strParentUids = $model->parent_uid;
  40. if (!empty($model->parent_uid)) {
  41. $arrParentUids = (new AgentModel())->findAllParentUids($model->parent_uid);
  42. if (!empty($arrParentUids)) {
  43. $strParentUids .= ','. implode(',', $arrParentUids);
  44. }
  45. }
  46. $model->parent_uids = $strParentUids;
  47. $model->save();
  48. // 马上创建一个属性数据.
  49. $setarr2 = [
  50. 'agent_uid' => $uuid,
  51. 'td_date' => Carbon::now()->toDateString(),
  52. ];
  53. AgentAttrsModel::create($setarr2);
  54. // 异步job,把所有上级的对象的父级和下级字段自己更新一次.
  55. $jobParams = [
  56. 'uid' => $uuid,
  57. ];
  58. dispatch(new AgentUpdateChildUidsByAllParentUidsJob($jobParams));
  59. });
  60. }
  61. /**
  62. * 关联属性表.
  63. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  64. */
  65. public function attrs()
  66. {
  67. return $this->hasOne(AgentAttrsModel::class, 'agent_uid', $this->uuidKeyName);
  68. }
  69. public function attrsDay()
  70. {
  71. return $this->hasOne(AgentAttrsDayModel::class, 'agent_uid', $this->uuidKeyName);
  72. }
  73. public function everyDay()
  74. {
  75. return $this->hasOne(AgentAttrsDayModel::class, 'agent_uid', $this->uuidKeyName);
  76. }
  77. /**
  78. * 关联多个渠道.
  79. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  80. */
  81. public function channels()
  82. {
  83. return $this->hasMany(ChannelModel::class, 'agent_uid', $this->uuidKeyName);
  84. }
  85. /**
  86. * 找到所有上级UID的集合(不包含参数$parentUid).
  87. * @param string $parentUid
  88. * @param array $cacheUids
  89. * @return array|mixed
  90. */
  91. public function findAllParentUids(string $parentUid, $cacheUids = [])
  92. {
  93. if (!empty($parentUid)) {
  94. $tmpParentUid = '';
  95. $parentObj = $this->query()->where(['uid' => $parentUid])->first(['id', 'uid', 'parent_uid']);
  96. if (!empty($parentObj->parent_uid)) {
  97. $tmpParentUid = $parentObj->parent_uid;
  98. array_push($cacheUids, $parentObj->uid);
  99. }
  100. $cacheUids = self::findAllParentUids($tmpParentUid, $cacheUids);
  101. }
  102. return $cacheUids;
  103. }
  104. /**
  105. * 找到所有子级UID集合(不包含参数$currUid).
  106. * @param string $currUid
  107. * @param array $cacheUids
  108. * @return array|mixed
  109. */
  110. public function findAllChildUids(string $currUid, $cacheUids = [])
  111. {
  112. $childItems = $this->query()->where(['parent_uid' => $currUid])->orderBy('id', 'ASC')->get(['id', 'uid', 'parent_uid']);
  113. if ($childItems->count() > 0) {
  114. foreach ($childItems as $item) {
  115. array_push($cacheUids, $item->uid);
  116. $cacheUids = self::findAllChildUids($item->uid, $cacheUids);
  117. }
  118. }
  119. return $cacheUids;
  120. }
  121. }