| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- namespace app\models;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- /**
- * This is the model class for table "{{%share}}".
- *
- * @property integer $id
- * @property integer $store_id
- * @property integer $user_id
- * @property integer $order_id
- * @property integer $parent_id
- * @property integer $parent_user_id
- * @property integer $child_level
- * @property integer $batch_id
- * @property integer $purchase_batch_id
- * @property integer $points
- * @property integer $created_at
- * @property integer $updated_at
- */
- class ShareGroupSupportLog extends \yii\db\ActiveRecord
- {
- //扶持区
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%share_group_support_log}}';
- }
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class
- ]
- ];
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['id', 'user_id', 'order_id', 'parent_id', 'parent_user_id', 'child_level', 'points', 'created_at', 'updated_at', 'batch_id', 'purchase_batch_id', 'store_id'], 'integer']
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => '',
- 'user_id' => '用户ID',
- 'order_id' => '入位订单ID',
- 'parent_id' => '上级ID',
- 'parent_user_id' => '用户上级ID',
- 'child_level' => '当前层级(一个关系树内的层级)',
- 'batch_id' => '同一树标识ID',
- 'purchase_batch_id' => '',
- 'points' => '同一上级下的入位顺序',
- 'created_at' => '',
- 'updated_at' => '',
- ];
- }
- /**
- * 添加扶持区数据
- */
- //如果是一层(最高层):查询不到上级以及自己位置--应该要先查询自己(查询最新位置)防止下级先入队 上级后入队 造成入队混乱问题
- //查询自己或者上级时候需要判断一下当前队列的位置 查看当前行是否满员:points == 2 ** child_level
- //如果满员则查询下一层未满员的
- //新增订单下级
- public static function addHuman($user_id, $order_id, $purchase_batch_id) {
- try {
- $child_user = User::findOne(['id' => $user_id, 'is_delete' => 0]);
- $purchaseLog = self::findOne(['order_id' => $order_id]);
- if ($purchaseLog) {
- throw new \Exception('订单已经添加过 请勿重复添加1');
- }
- $purchaseUserLog = self::findOne(['user_id' => $user_id]);
- if ($purchaseUserLog) {
- throw new \Exception('当前用户已经进入扶持区');
- }
- $topSupportLog = self::findOne(['purchase_batch_id' => $purchase_batch_id]);
- if ($topSupportLog) {//已经存在其他首个帮扶了
- $batch_id = $topSupportLog->id;
- // $child_level_data = self::find()->where(['OR', [
- // 'batch_id' => $batch_id
- // ], [
- // 'id' => $batch_id
- // ]])->andWhere(['>=', 'points', new \yii\db\Expression('POWER(2, child_level)')])
- // ->orderBy('child_level DESC')->asArray()->one();
- $addPurchaseLog = new self();
- // if ($child_level_data) {
- // //如果存在已满的层 就查询正序排列的比当前最大层数大的层 拿到最大的点位
- // $child_level_data_ = self::find()->where([
- // 'batch_id' => $batch_id
- // ])->andWhere(['>', 'child_level', $child_level_data['child_level']])
- // ->orderBy('child_level ASC, points DESC')->asArray()->one();
- //
- //
- // $addPurchaseLog->points = ($child_level_data_['points'] + 1);
- // $addPurchaseLog->child_level = ($child_level_data['child_level'] + 1);
- // } else {
- // //如果不存在已满的层 就查询最大的点位 应该不会走到这里
- // $child_level_data_ = self::find()->where([
- // 'batch_id' => $batch_id
- // ])->orderBy('points DESC')->asArray()->one();
- // $addPurchaseLog->points = (($child_level_data_['points'] ?? 0) + 1);
- // $addPurchaseLog->child_level = $child_level_data_['child_level'] ?? 0;
- // }
- // $parent_data = [];
- // if ($addPurchaseLog->child_level - 1 >= 0) {
- //
- // $parent_data = self::find()->where([
- // 'points' => ceil($addPurchaseLog->points / 2),
- // 'child_level' => $addPurchaseLog->child_level - 1
- // ])->andWhere(['OR', ['batch_id' => $batch_id], ['id' => $batch_id]])->orderBy('points DESC')
- // ->asArray()->one();
- // }
- $parent_id = -1;//已经存在其他首个帮扶的上级了
- $addPurchaseLog->points = 0;
- $addPurchaseLog->child_level = 0;
- } else {
- $batch_id = 0;
- $addPurchaseLog = new self();
- $addPurchaseLog->points = 0;
- $addPurchaseLog->child_level = 0;
- $addPurchaseLog->purchase_batch_id = $purchase_batch_id;
- }
- $addPurchaseLog->store_id = $child_user->store_id;
- $addPurchaseLog->user_id = $child_user->id;
- $addPurchaseLog->parent_user_id = $child_user->old_parent_id;
- $addPurchaseLog->parent_id = $parent_id ?? 0;
- $addPurchaseLog->batch_id = $batch_id;
- $addPurchaseLog->order_id = $order_id;
- if (!$addPurchaseLog->save()) {
- throw new \Exception('添加扶持失败');
- };
- $result = ShareGroupMoney::addMoneyLog(ShareGroupMoney::TYPE_SUPPORT, $addPurchaseLog->id, $addPurchaseLog->store_id, $addPurchaseLog->order_id);
- if ($result['code']) {
- return $result;
- }
- return [
- 'code' => 0,
- 'msg' => '添加成功',
- 'data' => $addPurchaseLog
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|