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; } }