| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- namespace app\models;
- use yii\behaviors\TimestampBehavior;
- /**
- * This is the model class for table "{{%share_holder_slipped_log}}".
- *
- * @property integer $id
- * @property integer $store_id
- * @property integer $user_id
- * @property integer $parent_id
- * @property integer $old_parent_id
- * @property integer $is_expired
- * @property integer $created_at
- * @property integer $updated_at
- */
- class ShareHolderSlippedLog extends \yii\db\ActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%share_holder_slipped_log}}';
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['id', 'store_id', 'user_id', 'parent_id', 'old_parent_id', 'is_expired', 'created_at', 'updated_at'], 'integer'],
- ];
- }
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class
- ]
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => '',
- 'store_id' => '店铺id',
- 'user_id' => '滑落用户',
- 'parent_id' => '新上级user_id',
- 'old_parent_id' => '原上级user_id',
- 'is_expired' => '是否过期',
- 'created_at' => '',
- 'updated_at' => ''
- ];
- }
- /**
- * 增加滑落记录
- * @param $user_id
- * @param $old_parent_id
- * @param $store_id
- * @return array
- */
- public static function addSlippedLog($user_id, $old_parent_id, $store_id) {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $user = User::findOne($user_id);
- if ($user) {
- self::updateAll(['is_expired' => 1, 'updated_at' => time()], ['user_id' => $user_id]);
- $model = new self();
- $model->store_id = $store_id;
- $model->user_id = $user_id;
- $model->parent_id = $user->parent_id;
- $model->old_parent_id = $old_parent_id;
- $model->is_expired = 0;
- if (!$model->save()) {
- throw new \Exception(json_encode($model->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '添加成功'
- ];
- } catch (\Exception $e) {
- debug_log([
- 'file' => $e->getFile(),
- 'line' => $e->getLine(),
- 'message' => $e->getMessage()
- ], 'slipped.log');
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 获取滑落下级
- * @param $parent_user_id
- * @return array
- */
- public static function getSlippedUserLog($parent_user_id) {
- $slippedUserLog = self::find()->where(['parent_id' => $parent_user_id, 'is_expired' => 0])
- ->select('user_id')->column();
- $data = [];
- if (!empty($slippedUserLog)) {
- foreach ($slippedUserLog as $slipped_user_item) {
- $user = User::findOne(['id' => $slipped_user_item, 'parent_id' => $parent_user_id, 'is_delete' => 0]);
- if ($user) {
- $data[] = $slipped_user_item;
- }
- }
- }
- return $data;
- }
- }
|