| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- <?php
- namespace addons\HiGo\common\logic;
- use addons\HiGo\common\enums\HiGoEnum;
- use addons\HiGo\common\models\HigoGoodsSetting;
- use addons\HiGo\common\models\HigoHibeiLog;
- use addons\HiGo\common\models\HigoMall;
- use addons\HiGo\common\models\HigoMerchantRewardLog;
- use addons\HiGo\common\models\HigoStoreGoodsMode;
- use addons\HiGo\common\models\HigoWallet;
- use addons\HiGo\common\models\HigoWalletFreezeLog;
- use addons\HiGo\common\models\HigoWalletLog;
- use addons\Mch\common\models\Mch;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreCashierOrder;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\models\common\CapitalLog;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\user\User;
- use common\traits\ErrorTrait;
- use Exception;
- /**
- * Class HiValueLogic
- * 打赏逻辑类
- * @Author: mz
- * @DateTime: 2022/11/18 16:39
- * @Copyright: copyright (c) 2022 广东七件事集团
- */
- class RewardLogic
- {
- use ErrorTrait;
- /**
- * 获取门店订单对应打赏金额
- * @param $order_no
- * @param array $mch 门店身份/多商户身份
- * @return array|bool
- */
- public static function getOrderMerchantReward($order_no,$mch)
- {
- try{
- if(isset($mch['store'])){
- $mch_id = $mch['store']['mch_id'];
- $order = Order::findOne(['order_no' => $order_no,'store_id' => $mch_id,'pay_status' => 1,'status' => StatusEnum::ENABLED]);
- }
- if(!$order && isset($mch['mch'])){
- $mch_id = $mch['mch']['mch_id'];
- $order = Order::findOne(['order_no' => $order_no,'mch_id' => $mch_id,'pay_status' => 1,'status' => StatusEnum::ENABLED]);
- }
- if(!$order) throw new Exception('订单不存在');
- $setting = \Yii::$app->services->setting->addons->get($order['mall_id'], HiGoEnum::ADDONS_NAME, 'basic');
- if (empty($setting['merchant_buy_goods'])) throw new Exception('系统不支持该操作');
- //查询是否已打赏
- if(HigoMerchantRewardLog::checkReward($order['mall_id'],Order::tableName(),$order->id)){
- throw new Exception('订单已打赏');
- }
- //商家让利部分
- $profit = bcmul($order['pay_money'],$setting['merchant_profit_multiple']/100,10);
- //金额 换算成 嗨呗
- $now_price = HiBeiLogic::getNowPrice($order['mall_id']);
- //$hi_bei_num = bcmul($profit,$now_price,10);
- $hi_bei_num = bcdiv($profit,$now_price,10);
- $return = [
- 'hi_bei' => $hi_bei_num,
- 'hi_value' => $hi_bei_num,
- 'pay_money' => $order['pay_money']
- ];
- return $return;
- }catch(Exception $e){
- self::setStaticError($e->getMessage(),1);
- return false;
- }
- }
- /**
- * 订单进行打赏
- * @param $order_no
- * @param $mch_id
- * @param $mch_user_id
- * @param $mch_name
- * @return
- */
- public static function doMerchantReward($order_no,$mch_id,$mch_user_id,$mch_name)
- {
- try{
- $is_store = true;
- $order = Order::findOne(['order_no' => $order_no,'store_id' => $mch_id,'pay_status' => 1,'status' => StatusEnum::ENABLED]);
- if(!$order){
- $order = Order::findOne(['order_no' => $order_no,'mch_id' => $mch_id,'pay_status' => 1,'status' => StatusEnum::ENABLED]);
- $is_store = false;
- }
- if(!$order) throw new Exception('订单不存在');
- $setting = \Yii::$app->services->setting->addons->get($order['mall_id'], HiGoEnum::ADDONS_NAME, 'basic');
- $hi_bei_name = $setting['hi_bei_name'] ?? '嗨贝';
- if (empty($setting['merchant_buy_goods'])) return ;
- //查询是否已打赏
- if(HigoMerchantRewardLog::checkReward($order['mall_id'],Order::tableName(),$order->id)){
- throw new Exception('订单已打赏');
- }
- //商家让利部分
- $profit = bcmul($order['pay_money'],$setting['merchant_profit_multiple']/100,10);
- $consumer_rewards_hi_value = bcmul($profit,$setting['consumer_rewards'],10);
- $merchant_rewards_hi_value = bcmul($profit,$setting['merchant_rewards'],10);
- //金额 换算成 嗨呗
- $now_price = HiBeiLogic::getNowPrice($order['mall_id']);
- if(!$now_price) throw new Exception('嗨呗价格为0');
- //$hi_bei_num = bcmul($profit,$now_price,10);
- $hi_bei_num = bcdiv($profit,$now_price,10);
- //扣除商家 嗨呗
- if($is_store){
- $store = Store::findOne(['id' => $mch_id]);
- if(!$store) throw new Exception('商家不存在');
- }else{
- $mch = Mch::findOne(['id' => $mch_id]);
- if(!$mch) throw new Exception('商家不存在');
- }
- //说明:对订单(1122)进行打赏了20个嗨贝扣除多商户账户嗨贝
- $desc = '对订单('.$order->id.')进行打赏了'.$hi_bei_num.'个'.$hi_bei_name.'扣除多商户账户'.$hi_bei_name;
- $wallet_log = [
- 'mall_id' => $order->mall_id,
- 'user_id' => $mch_user_id,
- 'change_type' => CapitalLog::CHANGE_TYPE_SUB,
- 'change_num' => $hi_bei_num * -1,
- 'desc' => $desc,
- 'source_table' => Order::tableName(),
- 'source_table_id' => $order->id,
- 'from_type' => HiGoEnum::FROM_TYPE_MERCHANT_REWARD,
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_BEI,
- 'is_send' => false
- ];
- $res = WalletLogic::handleInQueue($wallet_log,true);
- if(!$res) throw new Exception('打赏失败');
- $platform_account_user = $setting['platform_account_user'] ? json_decode($setting['platform_account_user'],true) : [];
- $platform_account_uid = $setting['enable_platform_account'] ? $platform_account_user['user_id'] : 0;
- if($setting['enable_platform_account']){
- $platform_account_uid = $platform_account_user['user_id'] ?? 0;
- $radio = $platform_account_user['ratio'] ?? 0;
- $platform_account_reward = bcmul($profit,$radio/100,10);
- }
- //添加打赏记录
- HigoMerchantRewardLog::setData([
- 'mall_id' => $order->mall_id,
- 'user_id' => $order->user_id,
- 'source_table' => Order::tableName(),
- 'source_table_id' => $order->id,
- 'profit_price' => $profit,
- 'profit' => $hi_bei_num,
- 'destroy' => bcmul($profit,$setting['destroy_settings']/100,10),
- 'platform_account_uid' => $platform_account_uid,
- 'platform_account_reward' => $platform_account_reward ?? 0,
- 'remaining_rewards' => bcmul($profit,$setting['remaining_rewards']/100,10),
- 'mch_name' => $mch_name,
- 'merchant_hi_value' => $merchant_rewards_hi_value,
- 'consumer_hi_value' => $consumer_rewards_hi_value,
- ]);
- //打赏后需要进行发放嗨值/嗨呗
- //打赏成功,发放对应嗨值,嗨呗模式
- $reward = [
- 'mall_id' => $order->mall_id,
- 'order_id' => $order['id'],
- 'operator_id' => 0,
- 'operator_name' => ''
- ];
- $event = new \addons\HiGo\common\events\RewardEvent($order->mall_id);
- \Yii::$app->services->events->dispatch($event, $reward, $event->listeners);
- return true;
- }catch(Exception $e){
- self::setStaticError($e->getMessage().',line:'.$e->getLine(),1);
- return false;
- }
- }
- /**
- * 打赏后处理
- * @param $log
- * @return
- * @throws
- */
- public static function afterReward($log)
- {
- if($log->handle_status) return true;
- $setting = \Yii::$app->services->setting->addons->get($log->mall_id, HiGoEnum::ADDONS_NAME, 'basic');
- //销毁
- if($log->destroy > 0){
- $order_no = '';
- //查询对应订单号
- if($log->source_table == Order::tableName()){
- $order = Order::findOne($log->source_table_id);
- $order_no = $order->order_no;
- }else if($log->source_table == StoreCashierOrder::tableName()){
- $order = StoreCashierOrder::findOne($log->source_table_id);
- $order_no = $order->order_no;
- }
- $res = HigoHibeiLog::setData([
- 'mall_id' => $log->mall_id,
- 'change_type' => CapitalLog::CHANGE_TYPE_SUB,
- 'change_num' => $log->destroy,
- 'desc' => '商家打赏销毁嗨呗,订单号:'.$order_no,
- 'from_type' => HigoHibeiLog::FROM_TYPE_DESTROY,
- 'setting' => json_encode($setting)
- ]);
- if(!$res) throw new Exception('添加嗨呗销毁记录失败');
- $higo_mall_model = HigoMall::findOne(['mall_id' => $log->mall_id]);
- $higo_mall_model->available_hi_bei = bcsub($higo_mall_model->available_hi_bei,$log->destroy,10);
- $higo_mall_model->save();
- }
- //平台账户奖励划分
- if($log->platform_account_uid > 0 && $log->platform_account_reward){
- $wallet_log = [
- 'mall_id' => $log->mall_id,
- 'user_id' => $log->platform_account_uid,
- 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
- 'change_num' => $log->platform_account_reward,
- 'desc' => '商家打赏平台账户奖励划分',
- 'source_table' => $log->source_table,
- 'source_table_id' => $log->source_table_id,
- 'from_type' => HiGoEnum::FROM_TYPE_MERCHANT_REWARD,
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_BEI,
- 'is_send' => 0,
- 'addon_name' => HiGoEnum::ADDONS_NAME,
- ];
- WalletLogic::handleInQueue($wallet_log);
- }
- $log->handle_status = 1;
- if(!$log->save()) throw new Exception('更新记录表状态失败');
- return true;
- }
- /**
- * 解冻 注册、复购等冻结嗨值
- * @param $order
- * @return null|void
- * @throws Exception
- */
- public static function unfreezeByOrder($order)
- {
- $mall_id = $order['mall_id'];
- $order_id = $order['id'];
- $user_id = $order['user_id'];
- $setting = \Yii::$app->services->setting->addons->get($mall_id, HiGoEnum::ADDONS_NAME, 'marketing');
- if(empty($setting)){
- return null;
- }
- $active_goods_list_ids = $goods_list_ids = [];
- //激活注册的商品
- $active_goods_list = $setting['active_goods_list'] ? json_decode($setting['active_goods_list'],true) : [];
- if($active_goods_list){
- $active_goods_list_ids = array_column($active_goods_list,'id');
- }
- //激活复购冻结的商品
- $goods_list = $setting['goods_list'] ? json_decode($setting['goods_list'],true) : [];
- if($goods_list){
- $goods_list_ids = array_column($goods_list,'id');
- }
- //判断商品
- $params = [];
- $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)) return;
- $goods_id_arr = array_column($order_detail_list, 'goods_id');
- if($goods_list_ids && array_intersect($goods_list_ids,$goods_id_arr)){
- //复购激活
- //检测是否有冻结的嗨值
- $repurchase_freeze = HigoWalletFreezeLog::find()->where([
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- // 'source_table' => User::tableName(),
- // 'source_table_id' => $user_id,
- 'from_type' => HiGoEnum::FROM_TYPE_REPURCHASE_ACTIVE,
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
- 'status' => CapitalLog::STATUS_WAIT_SEND
- ])->asArray()->all();
- if($repurchase_freeze){
- foreach($repurchase_freeze as $freeze_log){
- $freeze_log['freeze_log_id'] = $freeze_log['id'];
- $freeze_log['unfreeze'] = true;
- unset($freeze_log['id']);
- WalletLogic::handleInQueue($freeze_log);
- }
- }
- }
- if($active_goods_list_ids && array_intersect($active_goods_list_ids,$goods_id_arr)){
- //注册激活
- //检测是否有冻结的嗨值
- $register_freeze = HigoWalletFreezeLog::find()->where(['mall_id' => $mall_id,
- // 'user_id' => $user_id,
- 'source_table' => User::tableName(),
- 'source_table_id' => $user_id,
- 'from_type' => [HiGoEnum::FROM_TYPE_REGISTER_REWARD,HiGoEnum::FROM_TYPE_REGISTER_PARENT],
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
- 'status' => CapitalLog::STATUS_WAIT_SEND
- ])->asArray()->all();
- if($register_freeze){
- foreach($register_freeze as $freeze_log){
- //解冻
- $freeze_log['freeze_log_id'] = $freeze_log['id'];
- $freeze_log['unfreeze'] = true;
- unset($freeze_log['id']);
- WalletLogic::handleInQueue($freeze_log);
- }
- }
- }
- return true;
- }
- /**
- * @Description: 获取商家让利比例
- * @Author: zsan
- * @DateTime 2023-08-22 15:46:55
- * @param integer $mall_id
- * @param integer $user_id
- * @param integer $store_id
- * @return mixed
- */
- public static function getMerchantProfit($mall_id, $user_id, $store_id)
- {
- $profit_service_class = \addons\Store\common\services\MerchantReduceProfitService::class;
- /** @var \addons\Store\common\services\MerchantReduceProfitService $profit_service */
- $profit_service = new $profit_service_class();
- $profit = $profit_service->getProfit($mall_id, $user_id, $store_id);
- if($profit === false) {
- $setting = \Yii::$app->services->setting->addons->get($mall_id, HiGoEnum::ADDONS_NAME, 'basic');
- $profit = $setting['merchant_profit_multiple'] ?? 0;
- }
- return $profit;
- }
- }
|