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