| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489 |
- <?php
- namespace addons\HiGo\common\logic;
- use addons\BusinessBonus\common\models\BusinessBonusStore;
- use addons\HiGo\common\enums\HiGoEnum;
- use addons\HiGo\common\models\HigoGoodsSetting;
- use addons\HiGo\common\models\HigoMerchantRewardLog;
- use addons\HiGo\common\models\HigoStoreGoodsMode;
- use addons\HiGo\common\models\HigoWallet;
- use addons\HiGo\common\models\HigoWalletFrozenLog;
- 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\AddonsEnum;
- use common\enums\StatusEnum;
- use common\models\common\CapitalLog;
- use common\models\mall\MallAddons;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\traits\ErrorTrait;
- use Exception;
- /**
- * Class HiValueLogic
- * 嗨值逻辑类
- * @Author: mz
- * @DateTime: 2022/11/08 16:39
- * @Copyright: copyright (c) 2022 广东七件事集团
- */
- class HiValueLogic
- {
- use ErrorTrait;
- /**
- * 商品设置营销赠送嗨值-商城订单、门店订单、多商户订单发放嗨值
- * @param $order
- * @throws
- * @return bool
- */
- public static function sendGoodsSettingHiValue($order)
- {
- $mall_id = $order['mall_id'];
- $user_id = $order['user_id'];
- $source_table = Order::tableName();
- $from_type= HiGoEnum::FROM_TYPE_ORDER_GOODS;
- //嗨值自定义名称
- $setting = \Yii::$app->services->setting->addons->get($mall_id, HiGoEnum::ADDONS_NAME, 'basic');
- $name = $setting['hi_value_name'] ?? '嗨值';
- //查询订单商品
- $order_goods = OrderDetail::find()->where(['order_id' => $order['id']])->asArray()->all();
- foreach ($order_goods as $info){
- $is_frozen = false;
- $source = $info['store_id'] > 0 ? 'Store' : ($info['mch_id'] > 0 ? 'Mch' : 'Main');
- $goods_setting = HigoGoodsSetting::getGoodsSetting($info['goods_id'],HiGoEnum::WALLET_TYPE_HI_VALUE,$source);
- if(!$goods_setting){
- continue;
- }
- //检测该订单是否发放过该奖励
- if(HigoWalletLog::checkSend($mall_id,$user_id,$source_table,$order['id'],$from_type,HiGoEnum::WALLET_TYPE_HI_VALUE)){
- continue;
- }
- if(HigoWalletFrozenLog::checkSend($mall_id,$user_id,$source_table,$order['id'],$from_type,HiGoEnum::WALLET_TYPE_HI_VALUE)){
- continue;
- }
- if($goods_setting['settlement_method'] == HiGoEnum::SETTLEMENT_METHOD_AFTER_FINISH){
- //订单完成时结算,先存入冻结记录表,待订单完成时去 结算该奖励
- $is_frozen = true;
- }
- $change_num = $goods_setting['give'];
- if($change_num <= 0 ) continue;
- //计算赠送嗨值
- if($goods_setting['is_percent'] == 1){
- //百分比
- $change_num = bcmul($info['goods_price'],$goods_setting['give']/100,10);
- }
- $desc = '用户('.$user_id.')获得订单('.$order['id'].')营销赠送'.$name;
- $wallet_log = [
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
- 'change_num' => $change_num,
- 'desc' => $desc,
- 'source_table' => $source_table,
- 'source_table_id' => $order['id'],
- 'from_type' => $from_type,
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
- 'is_frozen' => $is_frozen,
- 'addon_name' => HiGoEnum::ADDONS_NAME,
- 'setting' => json_encode($goods_setting)
- ];
- if($is_frozen){
- $res = HigoWalletFrozenLog::setData($wallet_log);
- if(!$res) throw new Exception( HigoWalletFrozenLog::getStaticError());
- }else{
- WalletLogic::handleInQueue($wallet_log);
- }
- }
- return true;
- }
- /**
- * 门店/多商户 订单发放嗨值奖励
- * @param $order
- * @param int $settlement_method
- * @return bool|void
- * @throws Exception
- */
- public static function sendMerchantOrderHiValue($order,$settlement_method = 0)
- {
- $mall_id = $order['mall_id'];
- $user_id = $order['user_id'];
- $source_table = Order::tableName();
- $from_type= HiGoEnum::FROM_TYPE_STORE_ORDER;
- //嗨值自定义名称
- $setting = \Yii::$app->services->setting->addons->get($mall_id, HiGoEnum::ADDONS_NAME, 'basic');
- $name = $setting['hi_value_name'] ?? '嗨值';
- if (empty($setting['merchant_buy_goods'])) return ;
- $is_frozen = false;
- if($settlement_method != $setting['payment_method'] && $settlement_method != HiGoEnum::SETTLEMENT_METHOD_AFTER_FINISH){
- $is_frozen = true;
- }
- $store_user_id = 0;
- $store_id = 0;
- if($order['store_id'] > 0){
- $store = Store::findOne(['id' => $order['store_id']]);
- $store_user_id = $store['user_id'] ?? 0;
- $store_id = $store['id'];
- }elseif($order['mch_id'] > 0){
- $mch = Mch::findOne(['id' => $order['mch_id']]);
- $store_user_id = $mch['user_id'] ?? 0;
- }
- if(!$store_user_id) return ;
- $business_user_id = 0;
- if (MallAddons::isInstall($mall_id, AddonsEnum::ADDONS_NAME_BUSINESS_BONUS) ){
- $business = BusinessBonusStore::findOne(['mall_id' => $mall_id,'store_id' => $order['store_id'] > 0 ? $order['store_id'] : $order['mch_id'],'store_type' => $order['store_id'] > 0 ? 1 : 2]);
- $business_user_id = $business['business_user_id'] ?? 0;
- }
- //查询订单商品
- $insert_wallet = [];
- $order_goods = OrderDetail::find()->where(['order_id' => $order['id']])->asArray()->all();
- if($store_id > 0){
- // 商家让利比例
- $merchant_profit_multiple = RewardLogic::getMerchantProfit($order['mall_id'], $store['user_id'], $store_id);
- }else{
- $merchant_profit_multiple = $setting['merchant_profit_multiple'];
- }
- foreach ($order_goods as $info){
- $goods_setting = HigoStoreGoodsMode::findOne(['mall_id' => $mall_id,'goods_id' =>$info['goods_id'],'is_enable' => 1 ,'status' => StatusEnum::ENABLED]);
- if(!$goods_setting){
- continue;
- }
- //检测该订单是否发放过该奖励
- if(HigoWalletLog::checkSend($mall_id,$user_id,$source_table,$order['id'],$from_type,HiGoEnum::WALLET_TYPE_HI_VALUE)){
- continue;
- }
- //商家让利部分
- // $profit = bcmul($info['goods_price'],$setting['merchant_profit_multiple']/100,10);
- $profit = bcmul($info['goods_price'], $merchant_profit_multiple/100,10);
- $consumer_rewards = bcmul($profit,$setting['consumer_rewards'],10);
- $merchant_rewards = bcmul($profit,$setting['merchant_rewards'],10);
- $business_rewards = bcmul($profit,$setting['business_rewards'],10);
- if($consumer_rewards > 0){
- $desc = '用户('.$user_id.')获得订单('.$order['id'].')消费者奖励,获得奖励'.$name;
- $insert_wallet[] = [
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
- 'change_num' => $consumer_rewards,
- 'desc' => $desc,
- 'source_table' => $source_table,
- 'source_table_id' => $order['id'],
- 'from_type' => HiGoEnum::FROM_TYPE_STORE_ORDER,
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
- 'addon_name' => HiGoEnum::ADDONS_NAME,
- 'is_send' => 1
- ];
- //WalletLogic::handleInQueue($wallet_log,false);
- }
- if($merchant_rewards && $store_user_id > 0){
- $desc = '用户下单获得订单('.$order['id'].')商家奖励,获得'.$name;
- $insert_wallet[] = [
- 'mall_id' => $mall_id,
- 'user_id' => $store_user_id,
- 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
- 'change_num' => $merchant_rewards,
- 'desc' => $desc,
- 'source_table' => $source_table,
- 'source_table_id' => $order['id'],
- 'from_type' => HiGoEnum::FROM_TYPE_STORE_ORDER,
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
- 'addon_name' => HiGoEnum::ADDONS_NAME,
- 'is_send' => 1
- ];
- //WalletLogic::handleInQueue($wallet_log,false);
- }
- if($business_user_id >0 && $business_rewards){
- $desc = '用户下单获得订单('.$order['id'].')招商员奖励获得'.$name;
- $insert_wallet[] = [
- 'mall_id' => $mall_id,
- 'user_id' => $business_user_id,
- 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
- 'change_num' => $business_rewards,
- 'desc' => $desc,
- 'source_table' => $source_table,
- 'source_table_id' => $order['id'],
- 'from_type' => HiGoEnum::FROM_TYPE_STORE_ORDER,
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
- 'addon_name' => HiGoEnum::ADDONS_NAME,
- 'is_send' => 1
- ];
- //WalletLogic::handleInQueue($wallet_log,false);
- }
- }
- if(!empty($insert_wallet)){
- foreach($insert_wallet as $wallet_log){
- if($is_frozen){
- //冻结记录
- $res = HigoWalletFrozenLog::setData($wallet_log);
- if(!$res) throw new \Exception(HigoWalletFrozenLog::getStaticError());
- }else{
- $res = WalletLogic::handleInQueue($wallet_log,false);
- if(!$res) throw new \Exception(WalletLogic::getStaticError());
- }
- }
- }
- return true;
- }
- public static function sendMerchantCashierOrderHiValue($order)
- {
- $mall_id = $order['mall_id'];
- $user_id = $order['user_id'];
- $source_table = StoreCashierOrder::tableName();
- $from_type = HiGoEnum::FROM_TYPE_STORE_ORDER;
- //嗨值自定义名称
- $setting = \Yii::$app->services->setting->addons->get($mall_id, HiGoEnum::ADDONS_NAME, 'basic');
- $name = $setting['hi_value_name'] ?? '嗨值';
- if (empty($setting['merchant_buy_goods'])) return;
- $store = Store::findOne(['id' => $order['store_id']]);
- $store_user_id = $store['user_id'] ?? 0;
- if (!$store_user_id) return;
- $business_user_id = 0;
- if (MallAddons::isInstall($mall_id, AddonsEnum::ADDONS_NAME_BUSINESS_BONUS)) {
- $business = BusinessBonusStore::findOne(['mall_id' => $mall_id, 'store_id' => $order['store_id'] > 0 ? $order['store_id'] : $order['mch_id'], 'store_type' => $order['store_id'] > 0 ? 1 : 2]);
- $business_user_id = $business['business_user_id'] ?? 0;
- }
- //查询订单商品
- $insert_wallet = [];
- //检测该订单是否发放过该奖励
- if (HigoWalletLog::checkSend($mall_id, $user_id, $source_table, $order['id'], $from_type, HiGoEnum::WALLET_TYPE_HI_VALUE)) {
- return;
- }
- $merchant_profit_multiple = RewardLogic::getMerchantProfit($order['mall_id'], $store['user_id'], $store['id']);
- //商家让利部分
- // $profit = bcmul($order['pay_money'], $setting['merchant_profit_multiple'] / 100, 10);
- $profit = bcmul($order['pay_money'], $merchant_profit_multiple / 100, 10);
- // 消费者奖励的嗨值
- $consumer_rewards = bcmul($profit, $setting['consumer_rewards'], 10);
- // 商家奖励的嗨值
- $merchant_rewards = bcmul($profit, $setting['merchant_rewards'], 10);
- // 招商员奖励的嗨值
- $business_rewards = bcmul($profit, $setting['business_rewards'], 10);
- if ($consumer_rewards > 0) {
- $desc = '用户(' . $user_id . ')获得订单(' . $order['id'] . ')消费者奖励,获得奖励' . $name;
- $insert_wallet[] = [
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
- 'change_num' => $consumer_rewards,
- 'desc' => $desc,
- 'source_table' => $source_table,
- 'source_table_id' => $order['id'],
- 'from_type' => HiGoEnum::FROM_TYPE_STORE_ORDER,
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
- 'addon_name' => HiGoEnum::ADDONS_NAME,
- 'is_send' => 1
- ];
- //WalletLogic::handleInQueue($wallet_log,false);
- }
- if ($merchant_rewards && $store_user_id > 0) {
- $desc = '用户下单获得订单(' . $order['id'] . ')商家奖励,获得' . $name;
- $insert_wallet[] = [
- 'mall_id' => $mall_id,
- 'user_id' => $store_user_id,
- 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
- 'change_num' => $merchant_rewards,
- 'desc' => $desc,
- 'source_table' => $source_table,
- 'source_table_id' => $order['id'],
- 'from_type' => HiGoEnum::FROM_TYPE_STORE_ORDER,
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
- 'addon_name' => HiGoEnum::ADDONS_NAME,
- 'is_send' => 1
- ];
- //WalletLogic::handleInQueue($wallet_log,false);
- }
- if ($business_user_id > 0 && $business_rewards) {
- $desc = '用户下单获得订单(' . $order['id'] . ')招商员奖励获得' . $name;
- $insert_wallet[] = [
- 'mall_id' => $mall_id,
- 'user_id' => $business_user_id,
- 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
- 'change_num' => $business_rewards,
- 'desc' => $desc,
- 'source_table' => $source_table,
- 'source_table_id' => $order['id'],
- 'from_type' => HiGoEnum::FROM_TYPE_STORE_ORDER,
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
- 'addon_name' => HiGoEnum::ADDONS_NAME,
- 'is_send' => 1
- ];
- //WalletLogic::handleInQueue($wallet_log,false);
- }
- if (!empty($insert_wallet)) {
- foreach ($insert_wallet as $wallet_log) {
- $res = WalletLogic::handleInQueue($wallet_log, false);
- if (!$res) throw new \Exception(WalletLogic::getStaticError());
- }
- }
- return true;
- }
- /**
- * 检测奖励是否发放过
- * @param $mall_id
- * @param $user_id
- * @param $source_table
- * @param $source_id
- * @param $from_type
- * @return HigoWalletLog|null
- */
- // private static function checkSend($mall_id,$user_id,$source_table,$source_id,$from_type)
- // {
- //
- // $log = HigoWalletLog::findOne(['mall_id' => $mall_id,'user_id' => $user_id,'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE,
- // 'change_type' => 'add','source_table' => $source_table,'source_table_id' => $source_id,'from_type' => $from_type,'status' => StatusEnum::ENABLED]);
- // return $log;
- // }
- /**
- * 嗨值销毁
- * @param $mall_id
- * @param $destroy_formula 0-按比例,1-个人今日嗨值=个人昨日持有嗨值-个人昨日持有嗨贝*昨日嗨贝价值,2-个人今日嗨值=个人昨日持有嗨值-个人昨日获取嗨贝*昨日嗨贝价值
- * @param int $destroy_ratio 销毁比例
- * @throws
- * @return
- */
- public static function destroy($mall_id,$destroy_formula,$destroy_ratio = 0)
- {
- $where = [
- 'and',
- ['mall_id' => $mall_id],
- ['>', 'hi_value', 0],
- ['status' => StatusEnum::ENABLED],
- ];
- //查询商城下嗨值大于0的用户
- foreach (HigoWallet::find()->where($where)->each(500) as $user_wallet) {
- $need_destory_value = self::computeUserDestoryHiValue($user_wallet,$destroy_formula,$destroy_ratio);
- if($need_destory_value > 0 ){
- $desc = '每天嗨值销毁';
- $wallet_log = [
- 'mall_id' => $user_wallet->mall_id,
- 'user_id' => $user_wallet->user_id,
- 'change_type' => CapitalLog::CHANGE_TYPE_SUB,
- 'change_num' => $need_destory_value * -1,
- 'desc' => $desc,
- 'source_table' => '',
- 'source_table_id' => 0,
- 'from_type' => HiGoEnum::FROM_TYPE_DESTROY,
- 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE
- ];
- WalletLogic::handleInQueue($wallet_log);
- }
- }
- return true;
- }
- /**
- * 计算用户需要销毁的嗨值
- * @param $user_wallet
- * @param $destroy_formula
- * @param $destroy_ratio
- * @return
- */
- private static function computeUserDestoryHiValue($user_wallet,$destroy_formula,$destroy_ratio)
- {
- $return = 0;
- //检测用户今天是否已销毁
- $check = HigoWalletLog::find()->where(['user_id' => $user_wallet['user_id'],'mall_id' => $user_wallet['mall_id'],'from_type' =>HiGoEnum::FROM_TYPE_DESTROY, 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_VALUE])->andWhere(['>','created_at',strtotime(date('Y-m-d 00:00:00'))])->one();
- if($check){
- return $return;
- }
- if($destroy_formula == 0){
- //按比例销毁
- $destroy_ratio = $destroy_ratio > 100 ? 100 : $destroy_ratio; //最大100
- $return = bcmul($user_wallet['hi_value'],$destroy_ratio/100,10);
- }elseif($destroy_formula == 1){
- //1-个人今日嗨值=个人昨日持有嗨值-个人昨日持有嗨贝*昨日嗨贝价值
- $now_hi_bei_price = HiBeiLogic::getNowPrice($user_wallet['mall_id']);
- $return = bcmul($now_hi_bei_price,$user_wallet['hi_bei'],10);
- }elseif ($destroy_formula == 2){
- //2-个人今日嗨值=个人昨日持有嗨值-个人昨日获取嗨贝*昨日嗨贝价值
- $now_hi_bei_price = HiBeiLogic::getNowPrice($user_wallet['mall_id']);
- //个人昨日获取嗨贝
- $yesterday = HigoWalletLog::find()->where([
- 'mall_id' => $user_wallet['mall_id'],
- 'user_id'=>$user_wallet['user_id'],
- 'wallet_type'=> HiGoEnum::WALLET_TYPE_HI_BEI,
- 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
- 'status' => StatusEnum::ENABLED
- ])->andWhere(['>','created_at',strtotime('yesterday')])
- ->andWhere(['<','created_at',strtotime(date('Y-m-d'))])
- ->select('sum(change_num) as total')->asArray()->one();
- $yesterday_hi_bei = $yesterday['total'] ?:0;
- $return = bcmul($now_hi_bei_price,$yesterday_hi_bei,10);
- }
- return $return;
- }
- }
|