setRequest(); \Yii::$app->setMall(Mall::findOne($this->mall->id)); $t = \Yii::$app->db->beginTransaction(); try { $this->search('first_parent_id', 'first_price'); $this->search('second_parent_id', 'second_price'); $this->search('third_parent_id', 'third_price'); $t->commit(); } catch (\Exception $exception) { $t->rollBack(); \Yii::error('分销记录:'); \Yii::error($exception); } } /** * @param $idKey * @param $priceKey * @throws \Exception */ public function search($idKey, $priceKey) { if ($this->order) { $shareOrderList = ShareOrder::find()->where([ 'mall_id' => $this->order->mall_id, 'order_id' => $this->order->id, 'user_id' => $this->order->user_id, 'is_delete' => 0, ])->all(); $beforeList = $this->beforeList; $this->allOrder = true; } else { $shareOrderList = [$this->shareOrder]; $beforeList = [$this->before]; $other = ShareOrder::find()->where([ 'mall_id' => $this->order->mall_id, 'order_id' => $this->order->id, 'user_id' => $this->order->user_id, 'is_delete' => 0, ])->andWhere(['!=', 'id', $this->shareOrder->id])->all(); if (empty($other)) { // 没有其他的分销订单,需要计算总订单 $this->allOrder = true; } else { $this->allOrder = false; } } foreach ($shareOrderList as $shareOrder) { if ($shareOrder[$idKey]) { if ($this->type === 'add') { $beforeUserId = 0; $beforeMoney = 0; if (!empty($beforeList)) { $before = null; foreach ($beforeList as $before) { if ($before['id'] == $shareOrder['id']) { $beforeUserId = $before[$idKey]; $beforeMoney = $before[$priceKey]; break; } } } $this->add($shareOrder[$idKey], $shareOrder[$priceKey], $beforeUserId, $beforeMoney); } elseif ($this->type === 'sub') { $this->sub($shareOrder[$idKey], $shareOrder[$priceKey]); } else { throw new \Exception('错误的状态'); } } } } /** * @param $afterUserId * @param $afterMoney * @param int $beforeUserId * @param int $beforeMoney * @throws \Exception * 增加分销商分销订单总数及总佣金 */ public function add($afterUserId, $afterMoney, $beforeUserId = 0, $beforeMoney = 0) { $after = Share::findOne(['user_id' => $afterUserId]); if ($beforeUserId) { if ($afterUserId == $beforeUserId) { $afterMoney = floatval($after->all_money) + $afterMoney - $beforeMoney; } else { $before = Share::findOne(['user_id' => $beforeUserId]); $beforeMoney = floatval($before->all_money) - min(floatval($before->all_money), $beforeMoney); $before->all_money = price_format($beforeMoney); if ($this->allOrder) { $before->all_order = intval($before->all_order - min($before->all_order, 1)); $after->all_order += 1; } if (!$before->save()) { throw new \Exception('分销保存出错'); } $afterMoney = floatval($after->all_money) + $afterMoney; } } else { if ($this->allOrder) { $after->all_order += 1; } $afterMoney = floatval($after->all_money) + $afterMoney; } $after->all_money = price_format($afterMoney); if (!$after->save()) { throw new \Exception('分销保存出错'); } $this->allOrder = false; } /** * @param $userId * @param $money * @throws \Exception * 减少分销商分销订单总数及总佣金 */ public function sub($userId, $money) { $before = Share::findOne(['user_id' => $userId]); $beforeMoney = floatval($before->all_money) - min(floatval($before->all_money), $money); $before->all_money = price_format($beforeMoney); if ($this->allOrder) { $before->all_order = intval($before->all_order - min($before->all_order, 1)); } if (!$before->save()) { throw new \Exception('分销保存出错'); } $this->allOrder = false; } }