| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- <?php
- namespace addons\OperationCenter\common\services;
- use common\enums\UserWalletEnum;
- use services\common\UserWalletService;
- use addons\OperationCenter\common\models\Level;
- use addons\OperationCenter\common\models\MallUser;
- use addons\OperationCenter\common\models\MallOrder;
- use addons\OperationCenter\common\models\UserCenter;
- use addons\OperationCenter\common\models\CommissionLog;
- use addons\OperationCenter\common\models\MallCapitalLog;
- use addons\OperationCenter\common\models\MallOrderDetail;
- use addons\OperationCenter\common\models\UserAffiliation;
- use addons\OperationCenter\common\enums\OperationCenterEnum;
- use addons\OperationCenter\common\helpers\ArrayHelper;
- use addons\OperationCenter\common\models\ApplyList;
- use addons\OperationCenter\common\models\CommissionItemSetting;
- use addons\OperationCenter\common\models\CommissionItemSettingDetail;
- /**
- * 佣金服务类
- *
- * CommissionService
- * @package addons\OperationCenter\common\services
- */
- class CommissionService extends BaseService
- {
- /**
- * @var MallOrder
- */
- protected $order;
- /**
- * @var array
- */
- protected $center = [];
- /**
- * @var Level
- */
- protected $level;
- /**
- * @var array
- */
- protected $commissionAmounts;
- /**
- * 设置订单
- *
- * @param MallOrder $order
- * @return void
- */
- public function setOrder(MallOrder $order)
- {
- $this->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;
- }
- }
|