| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
- namespace addons\Distribution\common\traits;
- use addons\Distribution\common\enums\ComputingEnum;
- use addons\Distribution\common\enums\DistributionEnum;
- use addons\Distribution\common\models\Distribution;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\models\mall\MallAddons;
- use common\models\order\OrderDetail;
- use services\common\UserWalletService;
- use yii\helpers\Json;
- trait DistributionRewardTrait
- {
- /**
- * @param array $order
- * @return bool
- */
- public function repurchaseReward(array $order)
- {
- // 判断插件有没有安装
- try {
- $mall_id = $order['mall_id'];
- $order_id = $order['id'];
- $user_id = $order['user_id'];
- if (MallAddons::isInstall($mall_id, DistributionEnum::ADDONS_NAME) == 0) return false;
- $setting = \Yii::$app->services->setting->addons->get($mall_id, DistributionEnum::ADDONS_NAME, 'basic');
- if (empty($setting['level'])) throw new \Exception('没开启经销商');
- if (empty($setting['enable_repurchase_reward'])) return false;
- if (MallAddons::isInstall($mall_id, AddonsEnum::ADDONS_NAME_AGENT) == 0) return false;
- $agent_setting = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_AGENT, 'basic');
- if (empty($agent_setting['is_enable'])) throw new \Exception('没开启团队分红');
- // 判断自己是否为经销商,如果是经销商,那么自己买了后需要给自己奖励一个一级经销商的奖励金额
- $params['where'] = [['order_id' => $order_id], ['user_id' => $user_id], ['mall_id' => $mall_id]];
- $order_detail_list = OrderDetail::lists($params);
- if (empty($order_detail_list)) throw new \Exception('订单详情不存在');
- // 判断自己是否为经销商
- $distribution = Distribution::getOne([
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'status' => StatusEnum::ENABLED
- ]);
- if (empty($distribution)) throw new \Exception("当前用户非经销商");
- $commission_setting = Json::decode($setting['commission']);
- $score_setting = Json::decode($setting['score']);
- $insert_wallet = [];
- $reward_key = 'first_prize';
- $is_frozen = $setting['compute_type'] ==ComputingEnum::ORDER_FINISH;
- foreach ($order_detail_list as $orderDetail) {
- // 奖金
- // 需要判断是否独立设置
- $reward_amount = $commission_setting[$reward_key];
- $reward_score = $score_setting[$reward_key];
- if($orderDetail['is_virtual'] == StatusEnum::ENABLED){
- //虚拟商品,支付后-直接订单完成,所以直接结算
- $is_frozen = false;
- }
- switch ($setting['computing_method']) {
- case 0: // 实际支付金额
- if ($commission_setting['is_percent'] == StatusEnum::DISABLED) { // 百分比
- $reward_amount = $this->calcByPayAmount($orderDetail['goods_price'], $commission_setting[$reward_key] * 0.01);
- }
- if ($score_setting['is_percent'] == StatusEnum::DISABLED) { // 百分比
- $reward_score = $this->calcByPayAmount($orderDetail['goods_price'], $score_setting[$reward_key] * 0.01);
- }
- break;
- case 1: // 商品售价
- if ($commission_setting['is_percent'] == StatusEnum::DISABLED) {
- $reward_amount = $this->calcByGoodsSalePrice($orderDetail['price'], $orderDetail['num'], $commission_setting[$reward_key] * 0.01);
- }
- if ($score_setting['is_percent'] == StatusEnum::DISABLED) { // 百分比
- $reward_score = $this->calcByGoodsSalePrice($orderDetail['price'], $orderDetail['num'], $score_setting[$reward_key] * 0.01);
- }
- break;
- case 2: // 商品利润
- if ($commission_setting['is_percent'] == StatusEnum::DISABLED) { // 百分比
- $reward_amount = $this->calcByGoodsProfit($orderDetail['price'], $orderDetail['cost_price'],
- $orderDetail['num'], $commission_setting[$reward_key] * 0.01);
- }
- if ($score_setting['is_percent'] == StatusEnum::DISABLED) { // 百分比
- $reward_score = $this->calcByGoodsProfit($orderDetail['price'], $orderDetail['cost_price'],
- $orderDetail['num'], $score_setting[$reward_key] * 0.01);
- }
- break;
- }
- if ($reward_amount > 0) {
- $insert_wallet[] = $this->pack($user_id, $orderDetail['mall_id'],
- UserWalletService::WALLET_TYPE_COMMISSION, $reward_amount, ComputingEnum::DISTRIBUTION_FIRST_PRIZE,
- $order['order_no'], $orderDetail['id'], $orderDetail['order_id'], ComputingEnum::DISTRIBUTION_FIRST_PRIZE_REWARD_TYPE, $is_frozen);
- }
- if ($reward_score > 0) {
- $insert_wallet[] = $this->pack($user_id, $orderDetail['mall_id'],
- UserWalletService::WALLET_TYPE_SCORE, $reward_amount, ComputingEnum::DISTRIBUTION_FIRST_PRIZE,
- $order['order_no'], $orderDetail['id'], $orderDetail['order_id'], ComputingEnum::DISTRIBUTION_FIRST_PRIZE_REWARD_TYPE, $is_frozen);
- }
- }
- if (!empty($insert_wallet)) {
- $res = UserWalletService::batchHandleByQueue($insert_wallet);
- if (empty($res)) {
- throw new \Exception(UserWalletService::getStaticError());
- }
- }
- } catch (\Exception $e) {
- \Yii::error(FormatHelper::exception($e, __CLASS__ . "->" . __METHOD__));
- }
- return true;
- }
- /**
- * @param int $user_id
- * @param int $mall_id
- * @param string $commission_type
- * @param string $amount
- * @param string $capital_type
- * @param string $order_no
- * @param int $source_table_id
- * @param int $order_id
- * @param string $reward_type
- * @param bool $is_frozen
- * @return array
- */
- protected function pack(int $user_id, int $mall_id, string $commission_type, string $amount,
- string $capital_type, string $order_no, int $source_table_id, int $order_id, string $reward_type, bool $is_frozen)
- {
- $commission_name = \Yii::$app->services->setting->mall->get($mall_id, "{$commission_type}.{$commission_type}_name");
- if (empty($commission_name)) {
- switch ($commission_type) {
- case UserWalletService::WALLET_TYPE_COMMISSION:
- $commission_name = '佣金';
- break;
- default:
- $commission_name = '';
- }
- }
- $desc = "用户($user_id),获得复购订单($order_id)奖励, 奖励类型: $reward_type, 奖励: $amount $commission_name";
- return [
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'is_frozen' => $is_frozen,
- 'wallet_type' => $commission_type,
- 'money' => $amount,
- 'desc' => $desc,
- 'source_table' => OrderDetail::tableName(),
- 'source_table_id' => $source_table_id, // 取出订单ID?
- 'from_type' => DistributionEnum::ADDONS_NAME,
- 'capital_type' => $capital_type,
- 'contributor_name' => '',
- 'order_no' => $order_no, // 这里需要订单号
- ];
- }
- /**
- * @param $goods_price
- * @param $cardinality
- * @return string
- */
- protected function calcByPayAmount($goods_price, $cardinality)
- {
- return bcmul($goods_price, $cardinality, 2);
- }
- /**
- * @param $goods_price
- * @param $num
- * @param $cardinality
- * @return string
- */
- protected function calcByGoodsSalePrice($goods_price, $num, $cardinality)
- {
- return bcmul(bcmul($goods_price, $num, 2), $cardinality, 2);
- }
- /**
- * @param $goods_price
- * @param $goods_const_price
- * @param $num
- * @param $cardinality
- * @return string
- */
- protected function calcByGoodsProfit($goods_price, $goods_const_price, $num, $cardinality)
- {
- return bcmul(bcmul(bcsub($goods_price, $goods_const_price, 2), $num, 2),
- $cardinality, 2);
- }
- }
|