ShareHolderSlippedLog.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\models;
  3. use yii\behaviors\TimestampBehavior;
  4. /**
  5. * This is the model class for table "{{%share_holder_slipped_log}}".
  6. *
  7. * @property integer $id
  8. * @property integer $store_id
  9. * @property integer $user_id
  10. * @property integer $parent_id
  11. * @property integer $old_parent_id
  12. * @property integer $is_expired
  13. * @property integer $created_at
  14. * @property integer $updated_at
  15. */
  16. class ShareHolderSlippedLog extends \yii\db\ActiveRecord
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%share_holder_slipped_log}}';
  24. }
  25. /**
  26. * @inheritdoc
  27. */
  28. public function rules()
  29. {
  30. return [
  31. [['id', 'store_id', 'user_id', 'parent_id', 'old_parent_id', 'is_expired', 'created_at', 'updated_at'], 'integer'],
  32. ];
  33. }
  34. public function behaviors()
  35. {
  36. return [
  37. [
  38. 'class' => TimestampBehavior::class
  39. ]
  40. ];
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function attributeLabels()
  46. {
  47. return [
  48. 'id' => '',
  49. 'store_id' => '店铺id',
  50. 'user_id' => '滑落用户',
  51. 'parent_id' => '新上级user_id',
  52. 'old_parent_id' => '原上级user_id',
  53. 'is_expired' => '是否过期',
  54. 'created_at' => '',
  55. 'updated_at' => ''
  56. ];
  57. }
  58. /**
  59. * 增加滑落记录
  60. * @param $user_id
  61. * @param $old_parent_id
  62. * @param $store_id
  63. * @return array
  64. */
  65. public static function addSlippedLog($user_id, $old_parent_id, $store_id) {
  66. $t = \Yii::$app->db->beginTransaction();
  67. try {
  68. $user = User::findOne($user_id);
  69. if ($user) {
  70. self::updateAll(['is_expired' => 1, 'updated_at' => time()], ['user_id' => $user_id]);
  71. $model = new self();
  72. $model->store_id = $store_id;
  73. $model->user_id = $user_id;
  74. $model->parent_id = $user->parent_id;
  75. $model->old_parent_id = $old_parent_id;
  76. $model->is_expired = 0;
  77. if (!$model->save()) {
  78. throw new \Exception(json_encode($model->errors, JSON_UNESCAPED_UNICODE));
  79. }
  80. }
  81. $t->commit();
  82. return [
  83. 'code' => 0,
  84. 'msg' => '添加成功'
  85. ];
  86. } catch (\Exception $e) {
  87. debug_log([
  88. 'file' => $e->getFile(),
  89. 'line' => $e->getLine(),
  90. 'message' => $e->getMessage()
  91. ], 'slipped.log');
  92. $t->rollBack();
  93. return [
  94. 'code' => 1,
  95. 'msg' => $e->getMessage()
  96. ];
  97. }
  98. }
  99. /**
  100. * 获取滑落下级
  101. * @param $parent_user_id
  102. * @return array
  103. */
  104. public static function getSlippedUserLog($parent_user_id) {
  105. $slippedUserLog = self::find()->where(['parent_id' => $parent_user_id, 'is_expired' => 0])
  106. ->select('user_id')->column();
  107. $data = [];
  108. if (!empty($slippedUserLog)) {
  109. foreach ($slippedUserLog as $slipped_user_item) {
  110. $user = User::findOne(['id' => $slipped_user_item, 'parent_id' => $parent_user_id, 'is_delete' => 0]);
  111. if ($user) {
  112. $data[] = $slipped_user_item;
  113. }
  114. }
  115. }
  116. return $data;
  117. }
  118. }