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; } }