| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770 |
- <?php
- namespace app\models;
- use yii\behaviors\TimestampBehavior;
- /**
- * This is the model class for table "{{%share}}".
- *
- * @property integer $id
- * @property integer $store_id
- * @property integer $user_id
- * @property integer $order_id
- * @property string $order_detail_id
- * @property string $goods_name
- * @property integer $parent_id
- * @property integer $parent_user_id
- * @property integer $child_level
- * @property integer $batch_id
- * @property integer $points
- * @property float $direct_price
- * @property float $support_price
- * @property float $group_price
- * @property integer $is_repeat
- * @property integer $is_auto_repeat
- * @property integer $created_at
- * @property integer $updated_at
- */
- class ShareGroupPurchaseParentLog extends \yii\db\ActiveRecord
- {
- //基本流程:购买指定商品后 判断是否是37拼购设置下的商品 判断上级是否存在拼购数据 如果上级存在数据且上级拼购数据信息中下级人数带当前购买人大于三人
- // 则第三人算做扶持区人员 前两人算是拼购区人员 以下结构展示(每个人的下级最多三个 且前两位是拼购区 第三位是扶持区)
- // /////////////////////
- // // A
- // // / | \
- // // B1 B2 B3
- // /////////////////////
- // 此时 B1 B2算作拼购区 B3以及下级算作扶持区
- // 拼购:如果B1 B2再发展下级那么带上A一共7人 算是已经拼购完成
- // 扶持区规则:A如果满三人(B1 B2 B3)再推荐人员的话 需要滑落给B3(B3也可以自己推荐人员)。
- // 佣金计算:
- // 直推佣金:只要是上级推荐的都需要先给上级发放直推佣金再进行滑落或者其他等操作。
- // 拼购佣金(成团奖):如果拼购完成后 需要给A发放 拼购区人数6 * 成团奖比例。
- // 帮扶佣金:扶持区每多一个人员都需要给A增加帮扶佣金。
- // /////////////////////
- // // |----------------|
- // // | 成团区 A |------------|
- // // | / | | \ 扶持区|
- // // | B1 B2 | B3 |
- // // | / \ / \ | / \ |
- // // | C1 C2 C3 C4 | C5 C6 |
- // // |----------------|------------|
- //成团区的下级是看订单的 比如B1不推荐下级进行下单 那么可以让B2或者B2下级推荐
- // /////////////////////
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%share_group_purchase_parent_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', 'is_repeat', 'is_auto_repeat'], 'integer'],
- [['direct_price', 'support_price', 'group_price'], 'number'],
- [['order_detail_id', 'goods_name'], 'string']
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => '',
- 'user_id' => '用户ID',
- 'order_id' => '入位订单ID',
- 'order_detail_id' => '',
- 'goods_name' => '商品名称 方便查询',
- 'parent_id' => '上级ID',
- 'parent_user_id' => '用户上级ID',
- 'child_level' => '当前层级(一个关系树内的层级)',
- 'batch_id' => '同一树标识ID',
- 'points' => '同一上级下的入位顺序',
- 'direct_price' => '当前点位直推佣金',
- 'support_price' => '当前点位扶持佣金',
- 'group_price' => '成团佣金',
- 'created_at' => '',
- 'updated_at' => '',
- ];
- }
- //
- public function afterSave($insert, $changedAttributes)
- {
- try {
- //添加成团佣金
- if ($insert) {
- //兼容一下之前的逻辑
- $quotient = ceil($this->points / 4) - 1;
- $array = range($quotient * 4 + 1, $this->points);
- foreach ($array as $item) {
- $quo_purchase_log = self::findOne(['batch_id' => $this->batch_id, 'points' => $item, 'child_level' => $this->child_level]);
- $prev_quo_purchase_log = self::findOne(['child_level' => $quo_purchase_log->child_level - 1, 'points' => ceil($item / 2), 'batch_id' => $this->batch_id]);
- if ($prev_quo_purchase_log) {
- //检查是否存在成团佣金构成
- $prev_quo_group_money = ShareGroupMoneySub::findOne(['order_id' => $quo_purchase_log->order_id]);
- if (!$prev_quo_group_money) {
- $result = self::addData($prev_quo_purchase_log->id);
- }
- }
- //检查是否存在成团佣金构成
- $quo_group_money = ShareGroupMoneySub::findOne(['order_id' => $quo_purchase_log->order_id]);
- if (!$quo_group_money) {
- $result = self::addData($quo_purchase_log->id);
- }
- }
- $parent_purchase_log = self::findOne($this->parent_id);
- if ($parent_purchase_log) {
- $group_parent_purchase_log = self::findOne($parent_purchase_log->parent_id);
- if ($group_parent_purchase_log) {
- if (self::checkUnfreeze($group_parent_purchase_log->user_id, $group_parent_purchase_log->batch_id ?: $group_parent_purchase_log->id)) {
- //释放之前冻结的 并且订单已经过售后期
- $freeze_amount_list = ShareGroupMoney::find()->where([
- 'is_freeze' => 1,
- 'is_send' => 1,
- 'is_send_freeze' => 0,
- 'user_id' => $group_parent_purchase_log->user_id
- ])->select('id')->column();
- foreach ($freeze_amount_list as $freeze_amount_item) {
- $freeze_amount_item_ = ShareGroupMoney::findOne($freeze_amount_item);
- $freeze_amount_item_->is_send_freeze = 1;
- $freeze_amount_item_->freeze_send_time = time();
- $freeze_amount_item_->save();
- $shareGroupPurchaseUser = ShareGroupPurchaseUser::findOne($freeze_amount_item_->user_id);
- if (!$shareGroupPurchaseUser) {
- continue;
- }
- $shareGroupPurchaseUser->support_price += $freeze_amount_item_->freeze_amount;
- $shareGroupPurchaseUser->save();
- //判断用户是否存在 存在则开始发放
- $user = User::findOne(['id' => $freeze_amount_item_->user_id, 'is_delete' => 0]);
- if ($user) {
- $user->price = bcadd($user->price, $freeze_amount_item_->freeze_amount);
- $user->total_price = bcadd($user->total_price, $freeze_amount_item_->freeze_amount);
- if (!$user->save()) {
- throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE));
- }
- $result = UserShareMoney::set($freeze_amount_item_->freeze_amount, $freeze_amount_item_->user_id, 0, 0, 12, $freeze_amount_item_->store_id, 0, '37拼购冻结佣金发放');
- if (!$result) {
- throw new \Exception('保存失败');
- }
- }
- }
- }
- }
- }
- $parent_user_id = $this->parent_user_id;
- $shareGroupSupportLog = ShareGroupSupportLog::findOne(['user_id' => $this->user_id]);
- if ($shareGroupSupportLog && $shareGroupSupportLog->parent_id) {
- $parentShareGroupSupportLog = ShareGroupSupportLog::findOne(['id' => $shareGroupSupportLog->parent_id]);
- $parent_user_id = $parentShareGroupSupportLog->user_id;
- }
- $shareGroupPurchaseUser = new ShareGroupPurchaseUser();
- $shareGroupPurchaseUser->user_id = $this->user_id;
- $shareGroupPurchaseUser->store_id = $this->store_id;
- $shareGroupPurchaseUser->direct_price = '0.00';
- $shareGroupPurchaseUser->support_price = '0.00';
- $shareGroupPurchaseUser->group_price = '0.00';
- $shareGroupPurchaseUser->parent_user_id = $parent_user_id;
- $shareGroupPurchaseUser->save();
- }
- parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
- } catch (\Exception $e) {
- return false;
- }
- }
- public static function addData($id) {
- try {
- $self_purchase_log = self::findOne($id);
- $share_group_setting = Option::get('share_group_setting', $self_purchase_log->store_id, 'share_group')['value'];
- $share_group_setting = json_decode($share_group_setting ?? '', true);
- if (empty($share_group_setting)) {
- throw new \Exception('未配置佣金设置');
- }
- $share_group_commission_profit = $share_group_setting['share_group_commission_profit'];
- $share_group_auto_switch = $share_group_setting['share_group_auto_switch'];
- $share_group_balance_profit = $share_group_setting['share_group_balance_profit'];
- $goods_ids = explode(',', $share_group_setting['goods_ids'] ?? '');
- $order = Order::findOne($self_purchase_log->order_id);
- $order_detail = OrderDetail::find()->where(['order_id' => $order->id])->select('goods_id, total_price, goods_name')
- ->asArray()->all();
- $item_radix = 0;
- $goods_name = '';
- foreach ($order_detail as $goods_item) {
- if (in_array($goods_item['goods_id'], $goods_ids)) {
- $item_radix = bcadd($goods_item['total_price'], $item_radix, 2);
- $goods_name .= $goods_item['goods_name'] . ';';
- }
- }
- $item_amount = bcdiv(bcmul($share_group_commission_profit, $item_radix, 2), 100, 2);
- $item_balance_amount = bcdiv(bcmul($item_radix, $share_group_balance_profit, 2), 100, 2);
- // $balance_amount = 0;
- //如果存在一层上级就添加一条成团记录
- //如果存在两层上级就添加两条成团记录
- // if ($this->points % 4 == 0) {
- $parent_purchase_log = self::findOne($self_purchase_log->parent_id);
- if ($parent_purchase_log) {
- $moneyForm = ShareGroupMoney::findOne([
- 'type' => ShareGroupMoney::TYPE_GROUP,
- 'type_id' => $parent_purchase_log->id,
- 'profit_type' => ShareGroupMoney::PROFIT_TYPE_COMMON
- ]);
- if (!$moneyForm) {
- $moneyForm = new ShareGroupMoney();
- $moneyForm->store_id = $self_purchase_log->store_id;
- $moneyForm->user_id = $parent_purchase_log->user_id;
- $moneyForm->type = ShareGroupMoney::TYPE_GROUP;
- $moneyForm->type_id = $parent_purchase_log->id;
- $moneyForm->amount = 0;
- $moneyForm->radix = 0;
- $moneyForm->profit = $share_group_commission_profit;
- $moneyForm->profit_type = ShareGroupMoney::PROFIT_TYPE_COMMON;
- $moneyForm->group_data = '';
- $moneyForm->save();
- }
- $money_sub_form = ShareGroupMoneySub::findOne(['order_id' => $self_purchase_log->order_id, 'share_money_id' => $moneyForm->id]);
- if (!$money_sub_form) {
- $money_sub_form = new ShareGroupMoneySub();
- $money_sub_form->order_id = $self_purchase_log->order_id;
- $money_sub_form->amount = $item_amount;
- $money_sub_form->radix = $item_radix;
- $money_sub_form->share_money_id = $moneyForm->id;
- $money_sub_form->goods_name = $goods_name;
- $money_sub_form->save();
- $group_data = json_decode($moneyForm->group_data, true) ?: [];
- array_push($group_data, [
- 'id' => $self_purchase_log->id,
- 'order_id' => $self_purchase_log->order_id,
- 'amount' => $item_amount,
- 'balance_amount' => $item_balance_amount,
- 'radix' => $item_radix,
- 'order_no' => $order->order_no,
- 'goods_name' => $goods_name
- ]);
- $moneyForm->group_data = json_encode($group_data, JSON_UNESCAPED_UNICODE);
- $moneyForm->radix = bcadd($moneyForm->radix, $item_radix, 2);
- $moneyForm->amount = bcadd($moneyForm->amount, $item_amount, 2);
- $moneyForm->save();
- }
- if (!intval($share_group_auto_switch)) {
- $moneyBalanceForm = ShareGroupMoney::findOne([
- 'type' => ShareGroupMoney::TYPE_GROUP,
- 'type_id' => $parent_purchase_log->id,
- 'profit_type' => ShareGroupMoney::PROFIT_TYPE_BALANCE
- ]);
- if (!$moneyBalanceForm) {
- $moneyBalanceForm = new ShareGroupMoney();
- $moneyBalanceForm->store_id = $self_purchase_log->store_id;
- $moneyBalanceForm->user_id = $parent_purchase_log->user_id;
- $moneyBalanceForm->type = ShareGroupMoney::TYPE_GROUP;
- $moneyBalanceForm->type_id = $parent_purchase_log->id;
- $moneyBalanceForm->amount = 0;
- $moneyBalanceForm->radix = 0;
- $moneyBalanceForm->profit = $share_group_commission_profit;
- $moneyBalanceForm->profit_type = ShareGroupMoney::PROFIT_TYPE_BALANCE;
- $moneyBalanceForm->group_data = '';
- $moneyBalanceForm->save();
- }
- $money_balance_sub_form = ShareGroupMoneySub::findOne(['order_id' => $self_purchase_log->order_id, 'share_money_id' => $moneyBalanceForm->id]);
- if (!$money_balance_sub_form) {
- $money_balance_sub_form = new ShareGroupMoneySub();
- $money_balance_sub_form->order_id = $self_purchase_log->order_id;
- $money_balance_sub_form->amount = $item_amount;
- $money_balance_sub_form->radix = $item_radix;
- $money_balance_sub_form->share_money_id = $moneyBalanceForm->id;
- $money_balance_sub_form->goods_name = $goods_name;
- $money_balance_sub_form->save();
- $group_data = json_decode($moneyBalanceForm->group_data, true) ?: [];
- array_push($group_data, [
- 'id' => $self_purchase_log->id,
- 'order_id' => $self_purchase_log->order_id,
- 'amount' => $item_amount,
- 'balance_amount' => $item_balance_amount,
- 'radix' => $item_radix,
- 'order_no' => $order->order_no,
- 'goods_name' => $goods_name
- ]);
- $moneyBalanceForm->group_data = json_encode($group_data, JSON_UNESCAPED_UNICODE);
- $moneyBalanceForm->radix = bcadd($moneyBalanceForm->radix, $item_radix, 2);
- $moneyBalanceForm->amount = bcadd($moneyBalanceForm->amount, $item_balance_amount, 2);
- $moneyBalanceForm->save();
- }
- }
- $group_parent_purchase_log = self::findOne($parent_purchase_log->parent_id);
- if ($group_parent_purchase_log) {
- $moneyParentForm = ShareGroupMoney::findOne([
- 'type' => ShareGroupMoney::TYPE_GROUP,
- 'type_id' => $group_parent_purchase_log->id,
- 'profit_type' => ShareGroupMoney::PROFIT_TYPE_COMMON
- ]);
- if (!$moneyParentForm) {
- $moneyParentForm = new ShareGroupMoney();
- $moneyParentForm->store_id = $self_purchase_log->store_id;
- $moneyParentForm->user_id = $group_parent_purchase_log->user_id;
- $moneyParentForm->type = ShareGroupMoney::TYPE_GROUP;
- $moneyParentForm->type_id = $group_parent_purchase_log->id;
- $moneyParentForm->amount = 0;
- $moneyParentForm->radix = 0;
- $moneyParentForm->profit = $share_group_commission_profit;
- $moneyParentForm->profit_type = ShareGroupMoney::PROFIT_TYPE_COMMON;
- $moneyParentForm->group_data = '';
- $moneyParentForm->save();
- }
- $money_parent_sub_form = ShareGroupMoneySub::findOne(['order_id' => $self_purchase_log->order_id, 'share_money_id' => $moneyParentForm->id]);
- if (!$money_parent_sub_form) {
- $money_parent_sub_form = new ShareGroupMoneySub();
- $money_parent_sub_form->order_id = $self_purchase_log->order_id;
- $money_parent_sub_form->amount = $item_amount;
- $money_parent_sub_form->radix = $item_radix;
- $money_parent_sub_form->share_money_id = $moneyParentForm->id;
- $money_parent_sub_form->goods_name = $goods_name;
- $money_parent_sub_form->save();
- $group_data = json_decode($moneyParentForm->group_data, true) ?: [];
- array_push($group_data, [
- 'id' => $self_purchase_log->id,
- 'order_id' => $self_purchase_log->order_id,
- 'amount' => $item_amount,
- 'balance_amount' => $item_balance_amount,
- 'radix' => $item_radix,
- 'order_no' => $order->order_no,
- 'goods_name' => $goods_name
- ]);
- $moneyParentForm->group_data = json_encode($group_data, JSON_UNESCAPED_UNICODE);
- $moneyParentForm->radix = bcadd($moneyParentForm->radix, $item_radix, 2);
- $moneyParentForm->amount = bcadd($moneyParentForm->amount, $item_amount, 2);
- $moneyParentForm->save();
- }
- if (!intval($share_group_auto_switch)) {
- $moneyParentBalanceForm = ShareGroupMoney::findOne([
- 'type' => ShareGroupMoney::TYPE_GROUP,
- 'type_id' => $group_parent_purchase_log->id,
- 'profit_type' => ShareGroupMoney::PROFIT_TYPE_BALANCE
- ]);
- if (!$moneyParentBalanceForm) {
- $moneyParentBalanceForm = new ShareGroupMoney();
- $moneyParentBalanceForm->store_id = $self_purchase_log->store_id;
- $moneyParentBalanceForm->user_id = $group_parent_purchase_log->user_id;
- $moneyParentBalanceForm->type = ShareGroupMoney::TYPE_GROUP;
- $moneyParentBalanceForm->type_id = $group_parent_purchase_log->id;
- $moneyParentBalanceForm->amount = 0;
- $moneyParentBalanceForm->radix = 0;
- $moneyParentBalanceForm->profit = $share_group_commission_profit;
- $moneyParentBalanceForm->profit_type = ShareGroupMoney::PROFIT_TYPE_BALANCE;
- $moneyParentBalanceForm->group_data = '';
- $moneyParentBalanceForm->save();
- }
- $money_parent_sub_balance_form = ShareGroupMoneySub::findOne(['order_id' => $self_purchase_log->order_id, 'share_money_id' => $moneyParentBalanceForm->id]);
- if (!$money_parent_sub_balance_form) {
- $money_parent_sub_balance_form = new ShareGroupMoneySub();
- $money_parent_sub_balance_form->order_id = $self_purchase_log->order_id;
- $money_parent_sub_balance_form->amount = $item_amount;
- $money_parent_sub_balance_form->radix = $item_radix;
- $money_parent_sub_balance_form->share_money_id = $moneyParentBalanceForm->id;
- $money_parent_sub_balance_form->goods_name = $goods_name;
- $money_parent_sub_balance_form->save();
- $group_data = json_decode($moneyParentBalanceForm->group_data, true) ?: [];
- array_push($group_data, [
- 'id' => $self_purchase_log->id,
- 'order_id' => $self_purchase_log->order_id,
- 'amount' => $item_amount,
- 'balance_amount' => $item_balance_amount,
- 'radix' => $item_radix,
- 'order_no' => $order->order_no,
- 'goods_name' => $goods_name
- ]);
- $moneyParentBalanceForm->group_data = json_encode($group_data, JSON_UNESCAPED_UNICODE);
- $moneyParentBalanceForm->radix = bcadd($moneyParentBalanceForm->radix, $item_radix, 2);
- $moneyParentBalanceForm->amount = bcadd($moneyParentBalanceForm->amount, $item_balance_amount, 2);
- $moneyParentBalanceForm->save();
- }
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 添加拼购区数据
- */
- //如果是一层(最高层):查询不到上级以及自己位置--应该要先查询自己(查询最新位置)防止下级先入队 上级后入队 造成入队混乱问题
- //查询自己或者上级时候需要判断一下当前队列的位置 查看当前行是否满员:points == 2 ** child_level
- //如果满员则查询下一层未满员的
- //新增订单下级
- public static function addOrder($user_id, $order_id, $is_auto_repeat = 0) {
- try {
- $child_user = User::findOne(['id' => $user_id, 'is_delete' => 0]);
- if (empty($child_user)) {
- throw new \Exception('用户不存在');
- }
- $purchaseLog = self::findOne(['order_id' => $order_id]);
- if ($purchaseLog) {
- throw new \Exception('订单已经添加过 请勿重复添加');
- }
- $order_detail = OrderDetail::find()->where(['order_id' => $order_id])->select('id, goods_id, total_price, goods_name')
- ->asArray()->all();
- $share_group_setting = Option::get('share_group_setting', $child_user->store_id, 'share_group')['value'];
- $share_group_setting = json_decode($share_group_setting ?? '', true);
- if (empty($share_group_setting)) {
- throw new \Exception('未配置佣金设置');
- }
- $goods_ids = explode(',', $share_group_setting['goods_ids'] ?? '');
- $open = false;
- $order_detail_id = [];
- $goods_name = '';
- foreach ($order_detail as $goods_item) {
- if (in_array($goods_item['goods_id'], $goods_ids)) {
- $open = true;
- array_push($order_detail_id, $goods_item['id']);
- $goods_name .= $goods_item['goods_name'] . ';';
- }
- }
- $order_detail_ids = implode(',', $order_detail_id);
- if (!$open) {
- throw new \Exception('下单商品未在拼购商品列表内');
- }
- $order = Order::find()->where(['id' => $order_id, 'is_delete' => 0, 'user_id' => $user_id])->andWhere(['OR', ['is_pay' => 1], [
- 'pay_type' => Order::PAY_TYPE_COD
- ]])->asArray()->one();
- if (empty($order)) {
- throw new \Exception('订单不存在或未支付');
- }
- $purchaseParentLog = self::find()->where(['user_id' => $child_user->id])->orderBy('child_level DESC ,points DESC')->asArray()->one();
- if (!$purchaseParentLog) {
- $is_user_support = 0;
- $old_parent_id = $child_user->old_parent_id;
- //查询扶持区 如果扶持区有当前下单人员 就查询扶持区的上级是否在拼购区数据表中然后按照剩余逻辑继续添加成拼购区
- $userSupportLog = ShareGroupSupportLog::findOne(['user_id' => $child_user->id]);
- if ($userSupportLog) {
- $shareGroupPurchaseUser = ShareGroupPurchaseUser::findOne(['user_id' => $child_user->id]);
- if ($shareGroupPurchaseUser) {
- $old_parent_id = $shareGroupPurchaseUser->parent_user_id;
- }
- $is_user_support = 1;
- }
- $parentPurchaseParentLog = self::find()->where(['user_id' => $old_parent_id])->orderBy('child_level DESC ,points DESC')->asArray()->one();
- if (!$parentPurchaseParentLog) {
- //第0层 数量1
- $addPurchaseLog = new self();
- $addPurchaseLog->points = 1;
- $addPurchaseLog->child_level = 0;
- $addPurchaseLog->user_id = $child_user->id;
- $addPurchaseLog->parent_user_id = $old_parent_id;
- $addPurchaseLog->store_id = $child_user->store_id;
- $addPurchaseLog->parent_id = 0;
- $addPurchaseLog->order_id = $order_id;
- $addPurchaseLog->order_detail_id = $order_detail_ids;
- $addPurchaseLog->is_auto_repeat = $is_auto_repeat;
- $addPurchaseLog->goods_name = $goods_name;
- $addPurchaseLog->save();
- } else {
- $purchaseParentLog['batch_id'] = $parentPurchaseParentLog['batch_id'] ?: $parentPurchaseParentLog['id'];
- //查询上级是否已经有两个下级 也就是当前人员的同级参加拼购活动 已经有两个同级参加活动需要给当前人员判定为扶持区人员
- $isSupportUser = 0;
- if (!$is_user_support) {
- // $isSupportUser = self::find()->where(['parent_user_id' => $old_parent_id, 'batch_id' => $purchaseParentLog])
- // ->groupBy('user_id')
- // ->orderBy('child_level DESC ,points DESC')->count();
- //
- // //判断当前上级是否是扶持区的 查询扶持区是不是也有下级 如果有则和拼购区下级加起来 判断是否超过两个人
- // $shareGroupSupportParentLog = ShareGroupSupportLog::findOne(['user_id' => $old_parent_id]);
- // if ($shareGroupSupportParentLog) {
- // $shareGroupSupportChildLog = ShareGroupSupportLog::find()->where(['parent_id' => $shareGroupSupportParentLog->id])
- // ->select('id')->count();
- //
- // $isSupportUser += $shareGroupSupportChildLog;
- // }
- $isSupportUser = ShareGroupPurchaseUser::find()->where(['parent_user_id' => $old_parent_id])->count();
- }
- if ($isSupportUser >= 2) {
- //寻找上级首次出现的位置 然后获取到上级的batch_id
- $parentPurchaseParentFirstLog = self::find()->where(['user_id' => $old_parent_id])
- ->orderBy('child_level ASC ,points ASC')->asArray()->one();
- $batch_id = $parentPurchaseParentFirstLog['id'];
- //进扶持区
- $result = ShareGroupSupportLog::addHuman($child_user->id, $order_id, $batch_id);
- if (!$result['code']) {
- $supportLog = $result['data'];
- if (!intval($supportLog->parent_id)) {
- //如果扶持区没有上级 也就是自己是最高级 就在拼购区增加一个最高级的树
- $addPurchaseLog = new self();
- $addPurchaseLog->points = 1;
- $addPurchaseLog->child_level = 0;
- $addPurchaseLog->user_id = $child_user->id;
- $addPurchaseLog->parent_user_id = $old_parent_id;
- $addPurchaseLog->store_id = $child_user->store_id;
- $addPurchaseLog->parent_id = 0;
- $addPurchaseLog->order_id = $order_id;
- $addPurchaseLog->order_detail_id = $order_detail_ids;
- $addPurchaseLog->goods_name = $goods_name;
- $addPurchaseLog->save();
- $result = ShareGroupMoney::addMoneyLog(ShareGroupMoney::TYPE_DIRECT, $addPurchaseLog->id, $addPurchaseLog->store_id, $addPurchaseLog->order_id);
- if ($result['code']) {
- // echo json_encode($result);
- debug_log(['进入扶持区添加记录' => $result], 'share_group_purchase_log.log');
- }
- debug_log(['进入扶持区添加记录-com' => $result], 'share_group_purchase_log.log');
- } else {
- //获取扶持区最高级的用户 查询后拿到最高用户处于哪条batch_id上的 然后再将该用户添加到对应batch_id的拼购数据表 再修改当前用户的上级用户
- $topSupportLog = ShareGroupSupportLog::findOne(['purchase_batch_id' => $batch_id]);
- $supportUserParent = self::findOne(['user_id' => $topSupportLog->user_id]);
- $purchaseParentLog['batch_id'] = $supportUserParent->batch_id ?: $supportUserParent->id;
- goto add_support_user_to_purchase;
- }
- }
- return $result;
- }
- }
- }
- add_support_user_to_purchase:
- //如果存在上级下单或者当前人员下单
- if (empty($addPurchaseLog)) {
- //查询到当前下单人是这个树的
- $batch_id = $purchaseParentLog['batch_id'] ?: $purchaseParentLog['id'];
- //查询未满的层
- $child_level_data = self::find()->where([
- 'batch_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'] ?? 1;
- }
- $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();
- }
- if ($purchaseParentLog['id']) {
- $addPurchaseLog->is_repeat = 1;
- }
- $addPurchaseLog->is_auto_repeat = $is_auto_repeat;
- $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_data['id'] ?? 0;
- $addPurchaseLog->batch_id = $batch_id;
- $addPurchaseLog->order_id = $order_id;
- $addPurchaseLog->order_detail_id = $order_detail_ids;
- $addPurchaseLog->goods_name = $goods_name;
- if (!$addPurchaseLog->save()) {
- throw new \Exception('添加记录失败');
- };
- //添加完拼购应该会有团长用户记录
- $userSupportLog = ShareGroupSupportLog::findOne(['user_id' => $child_user->id]);
- if ($userSupportLog && intval($userSupportLog->parent_id) === -1) {
- $userSupportLog->parent_id = -2;
- $userSupportLog->save();
- //修改帮扶区的当前用户的上级用户ID
- $newParentLog = ShareGroupPurchaseParentLog::findOne($parent_data['id']);
- $shareGroupPurchaseUser = ShareGroupPurchaseUser::findOne(['user_id' => $child_user->id]);
- $shareGroupPurchaseUser->parent_user_id = $newParentLog->user_id;
- $shareGroupPurchaseUser->save();
- }
- $result = ShareGroupMoney::addMoneyLog(ShareGroupMoney::TYPE_DIRECT, $addPurchaseLog->id, $addPurchaseLog->store_id, $addPurchaseLog->order_id);
- if ($result['code']) {
- debug_log(['进入拼购区添加记录' => $result], 'share_group_purchase_log.log');
- // echo json_encode($result);
- }
- debug_log(['开始帮扶区' => $child_user->id], 'share_group_purchase_log.log');
- //如果存在帮扶区数据就添加帮扶区记录
- if ($userSupportLog) {
- debug_log(['id' => $userSupportLog->id, 'store_id' => $userSupportLog->store_id, 'order_id' => $addPurchaseLog->order_id], 'share_group_purchase_log.log');
- $result = ShareGroupMoney::addMoneyLog(ShareGroupMoney::TYPE_SUPPORT, $userSupportLog->id, $userSupportLog->store_id, $addPurchaseLog->order_id);
- debug_log(['帮扶区结果' => $result], 'share_group_purchase_log.log');
- if ($result['code']) {
- return $result;
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => '添加成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //检测是否满足解冻条件
- public static function checkUnfreeze($user_id, $batch_id)
- {
- $direct_group_purchase_child = self::find()->where(['parent_user_id' => $user_id, 'batch_id' => $batch_id])->groupBy('user_id')
- ->select('user_id')->column();
- if ($direct_group_purchase_child && count($direct_group_purchase_child) >= 2) {
- $group_purchase_child = self::find()->where(['parent_user_id' => $direct_group_purchase_child, 'batch_id' => $batch_id])->groupBy('user_id')
- ->select('user_id')->column();
- if ($group_purchase_child && count($group_purchase_child) >= 4) {
- return true;
- }
- }
- return false;
- }
- }
|