order = $order; } /** * 获取佣金分页列表 * * @param array $params * @return array */ public function getPageList(array $params = []) { $name = $params['name'] ?? ''; $status = $params['status'] ?? ''; $mobile = $params['mobile'] ?? ''; $user_id = $params['user_id'] ?? ''; $user_keyword = $params['user_keyword'] ?? ''; return CommissionLog::getPageList( $this->getPage($params), $this->getLimit($params), function ($query) use ($name, $status, $mobile, $user_id, $user_keyword) { $query->andWhere(['mall_id' => $this->mall_id]); // 会员ID if (!empty($user_id)) { $query->andWhere(['user_id' => $user_id]); } // 会员名称 if (!empty($name)) { $query->andWhere(['user_id' => ApplyList::getUserIdByName( $this->mall_id, $name )]); } // 联系电话 if (!empty($mobile)) { $query->andWhere(['user_id' => ApplyList::getUserIdByMobile( $this->mall_id, $mobile )]); } // 会员信息查询 if (!empty($user_keyword)) { $query->andWhere(['user_id' => MallUser::getUserIdByUserKeyWord( $this->mall_id, $user_keyword )]); } // 状态 if ($status !== '') { $query->andWhere(['status' => $status]); } $query->with([ 'user' => function ($query) { $query->select(['id', 'nickname', 'username', 'mobile', 'avatar_url']); }, 'levelInfo' => function ($query) { $query->select(['id', 'mall_id', 'level', 'name']); }, 'order' => function ($query) { $query->select(['id', 'order_no']); }, 'apply' => function ($query) { $query->select(['id', 'user_id', 'name', 'center_name']); }, ]); } ); } /** * 检测佣金 * * @param MallOrder $order * @return void */ public function checkCommission(MallOrder $order) { $this->setOrder($order); if (!$this->getSettingService()->isOpen()) { return false; } // 直推 if ( // $this->getSettingService()->hasCommission(1) // && $user_id = $this->getSelectCenterUser() ) { // 添加佣金 $this->addCommission($user_id, 1); } // 间推 if ( // $this->getSettingService()->hasCommission(2) // && $user_id && ($parant_id = $this->getUserParentId($user_id)) ) { $this->addCommission($parant_id, 2); } } /** * 发放佣金 * * @param integer $order_id * @return void */ public function paymentCommission(int $order_id, int $is_finish) { // 是否结算 if ($this->getSettingService()->isSettlement($is_finish)) { $commission_logs = CommissionLog::getLogByOrderId($order_id); $ids = array_column($commission_logs, 'id'); // 查询 MallCapitalLog::$capitalType = 'commission_frozen'; $list = MallCapitalLog::getListByIds($ids); // 分组 $array = ArrayHelper::arrayGroupByField($list, 'user_id'); foreach ($array as $user_id => $val) { // 增加已结算 $sum = array_sum(array_column($val, 'change_num')); $sum && UserCenter::addSettlementAmount($user_id, $sum); } // 结算 foreach ($list as $item) { UserWalletService::unfrozen($item['id'], 'commission'); } // 修改状态 CommissionLog::changeStatusByIds($ids); } } /** * 废弃佣金 * * @param integer $order_detail_id * @return void */ public function discardCommission(int $order_detail_id) { $commission_logs = CommissionLog::getLogByOrderDetailId($order_detail_id); $ids = array_column($commission_logs, 'id'); // 分组 $array = ArrayHelper::arrayGroupByField($commission_logs, 'user_id'); foreach ($array as $user_id => $val) { // 增加已结算 $sum = array_sum(array_column($val, 'commission_amount')); $sum && UserCenter::addDiscardAmount($user_id, $sum); } // 修改状态 CommissionLog::changeStatusByIds($ids, 2); } /** * 添加佣金 * * @param integer $user_id * @param integer $level 佣金等级 直推 间推 * @return void */ protected function addCommission(int $user_id, int $level) { // 获取设置记录ID $setting_id = $this->getSettingService()->getLastSettingId(); $wallet_log = []; foreach ($this->order->detail as $detail) { // 是否已经创建佣金 $has_commission = CommissionLog::hasCommissonByDetailId($detail->id, $level); if (!$has_commission) { $this->commissionAmounts[$level] = $commission_amount = $this->getCommissionAmount($level, $detail); if ( $commission_amount && $this->getSettingService()->hasCommission($level) // 查看设置是否拥有佣金 ) { // 添加分佣 $id = CommissionLog::addCommissionLog( $this->mall_id, $user_id, $this->order->user_id, $detail->goods_name, $detail->price, $this->order->order_no, $this->level->level, $level, $this->order->id, $detail->id, $setting_id, $commission_amount ); $id && $wallet_log[] = [ 'mall_id' => $this->mall_id, 'user_id' => $user_id, 'is_frozen' => true, 'wallet_type' => 'commission', 'money' => $commission_amount, 'desc' => sprintf('运营中心%s级佣金', $level), 'source_table' => CommissionLog::tableName(), 'source_table_id' => $id, 'from_type' => OperationCenterEnum::ADDONS_NAME_OPERATION_CENTER, 'capital_type' => 'operation_center_' . $level, ]; } } } // 待结算金额 $sum = array_sum(array_column($wallet_log, 'money')); $sum && UserCenter::addSettlementAmountTotal($user_id, $sum); $wallet_log && UserWalletService::batchHandleByQueue($wallet_log); } /** * 获取佣金数量 * * @param int $level * @return float|false */ protected function getCommissionAmount($level, MallOrderDetail $order_detail) { /** * @var Level */ $level_info = $this->center[$level]->levelInfo; if (empty($level_info)) return false; // level $this->level = $level_info; // 商品独立分佣 $item_setting_detail = $this->hasGoodsAloneCommissionByDetail($order_detail, $level); // 不参与分佣 if ($item_setting_detail && ($item_setting_detail->itemSetting->is_partake == 0)) { return false; } // 独立分佣 if ($item_setting_detail && $item_setting_detail->itemSetting->is_enable) { // 分佣类型 $type = $item_setting_detail->getCommissionTypeByLevel($level); // 分佣数量 $amount = $item_setting_detail->getCommissionValueByLevel($level); } else { // 分佣类型 $type = $level_info->getCommissionTypeByLevel($level); // 分佣数量 $amount = $level_info->getCommissionValueByLevel($level); } // 固定金额 if ($type == Level::FIXED) { return bcmul($amount, $order_detail->num); } // 获取订单金额 $order_amount = $this->getOrderCommissionAmount($order_detail, $level); return bcdiv(bcmul($order_amount, $amount), 100, 2); } /** * 是否商品独立分佣 * * @param MallOrderDetail $detail * @param int $level 分佣级别 * @return false|CommissionItemSettingDetail */ protected function hasGoodsAloneCommissionByDetail(MallOrderDetail $detail) { /** * @var CommissionItemSetting|null */ $item_setting = $detail->goods->itemSetting; if (empty($item_setting)) return false; // 未开启独立分佣 // if (!($item_setting->is_partake && $item_setting->is_enable)) return false; return $item_setting->getCommissionDetailByLevel($this->level->level); } /** * 获取订单金额进行分佣金额 * * @return float */ protected function getOrderCommissionAmount($order_detail, $level) { // 间推佣金使用1级佣金的分佣金额进行计算 if ($level == 2) return $this->commissionAmounts[1]; $computing_method = $this->getSettingService()->getComputingMethod(); // 支付金额 if ($computing_method == OperationCenterEnum::COMPUTING_METHOD_PAY_PRICE) { return $order_detail->goods_price; } // 商品售价 if ($computing_method == OperationCenterEnum::COMPUTING_METHOD_GOODS_PRICE) { return $order_detail->original_goods_price; } // 商品利润 if ($computing_method == OperationCenterEnum::COMPUTING_METHOD_GOODS_PROFIT) { return bcadd( bcsub($order_detail->goods_price, $order_detail->cost_price, 2), $order_detail->adjust_money, 2 ); } return 0; } /** * 获取直推 * * @return int|false */ protected function getSelectCenterUser() { if (empty($this->order)) return false; $buy_user_id = $this->order->user_id; // 获取选择 $user_affiliation = UserAffiliation::getUserAffiliationByUserId($buy_user_id); // 为选择运营中心 if (empty($user_affiliation->center)) { return false; } // 设置直推 $this->center[1] = $user_affiliation->center; return $user_affiliation->center->user_id; } /** * 获取间推 * * @param int $user_id * @return int|false */ protected function getUserParentId(int $user_id) { $user_id = MallUser::getParentId($user_id); if (empty($user_id)) return false; $center = UserCenter::getCenterByUserId($user_id); if (empty($center)) return false; $this->center[2] = $center; return $user_id; } }