'小模型', self::MODEL_ALLIANCE => '大模型', ); /** * @inheritdoc */ public static function tableName() { return '{{%plugin_pool_config}}'; } public function behaviors() { return [ [ 'class' => TimestampBehavior::class, 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'], ] ] ]; } /** * @inheritdoc */ public function rules() { return [ [['is_delete', 'store_id', 'status', 'time_value','reward_type'], 'integer'], [['name', 'time_type'], 'string'], [['rate'], 'number'], [['created_at'], 'safe'] ]; } public static function transfer($order) { if (!$order->is_pay) return false; $store_id = $order->store_id; $setting = Option::getDecode(OptionSetting::SHARE_STRING_CODE_DEFAULT_SETTING, $store_id, OptionSetting::SHARE_GROUP_NAME); /*$check_res = self::checkSettingByStoreId($store_id, $setting); if (!$check_res) return false;*/ $is_scan = $order instanceof ScanOrder ? 1 : 0; if ($is_scan) return false; $t = \Yii::$app->db->beginTransaction(); try { $config = PluginPoolConfig::poolSetting($store_id); if (empty($config)) throw new \Exception('无配置'); if(($order->channel_id) == 9999){ $pv0 = 0; $pv1 = $order->balance; }else{ $pv0 = OrderDetail::find()->andWhere(['order_id' => $order['id'], 'is_delete' => 0])->select(['sum(pv_0)'])->scalar(); $pv1 = OrderDetail::find()->andWhere(['order_id' => $order['id'], 'is_delete' => 0])->select(['sum(pv_1)'])->scalar(); } //$pv0 = 0; //debug_log([__METHOD__, __LINE__, 'configPoolPush start'], "app_debug.log"); self::configPoolPush($config, $order, $setting, $pv0, $pv1);//插入池子 $add_money0 = bcmul($pv0, $setting['string_code_pv_scale_0'] / 100, 4); $add_money1 = bcmul($pv1, $setting['string_code_pv_scale_1'] / 100, 4); //self::areaAward($config, $store_id, $order, $add_money0, 0); self::areaAward($config, $store_id, $order, $add_money1, 1); self::appointAward($config, $order, $add_money0, $add_money1); self::currencyAward($config, $order, $add_money0, $add_money1); $t->commit(); } catch (\Exception $e) { $t->rollBack(); \Yii::$app->db->createCommand()->insert('cyy_pay_log', [ 'order_no'=>$order->order_no, 'desc'=>'奖励错误'.$e->getMessage(), 'add_time'=>time(), ])->execute(); ActionLog::addLog(1, '/PluginPoolConfig/transfer', $e->getMessage()); } $UserStringCodeOrderlist = UserStringCodeOrder::findAll([ 'order_id' => $order->id, 'is_scan' => $is_scan, 'type' => [ UserStringCodeOrder::TYPE_STORE_USER_REFERRER, UserStringCodeOrder::TYPE_STORE_USER_AFFILIATED, UserStringCodeOrder::TYPE_STORE_REFERRER, UserStringCodeOrder::TYPE_BRAND_USER_REFERRER, UserStringCodeOrder::TYPE_BRAND_USER_AFFILIATED, UserStringCodeOrder::TYPE_BRAND_REFERRER, UserStringCodeOrder::TYPE_AREA_AWARD, UserStringCodeOrder::TYPE_BRAND_USER_STORE_REFERRER, UserStringCodeOrder::TYPE_SUPRISE_RED, ] // 用数组指定多个类型 ]); $league_arr = UserStringCodeOrder::TYPE_LEAGUE_PRICE; $Leaguelist = SaaSLeaguePriceLog::findAll([ 'order_id' => $order->id, 'is_scan' => $is_scan, 'type' => [ $league_arr[UserStringCodeOrder::TYPE_STORE_USER_REFERRER], $league_arr[UserStringCodeOrder::TYPE_STORE_USER_AFFILIATED], $league_arr[UserStringCodeOrder::TYPE_STORE_REFERRER], $league_arr[UserStringCodeOrder::TYPE_BRAND_USER_REFERRER], $league_arr[UserStringCodeOrder::TYPE_BRAND_USER_AFFILIATED], $league_arr[UserStringCodeOrder::TYPE_BRAND_REFERRER], $league_arr[UserStringCodeOrder::TYPE_AREA_AWARD], $league_arr[UserStringCodeOrder::TYPE_BRAND_USER_STORE_REFERRER], $league_arr[UserStringCodeOrder::TYPE_SUPRISE_RED], ] ]); if (count($UserStringCodeOrderlist) > 0 || count($Leaguelist) > 0) { $order->team_reward_status = 1; $order->save(); } } public static function configPoolPush($config, $order, $setting, $pv0, $pv1) { try { if (!isset($config['pools']) || empty($config['pools'])) throw new \Exception('无配置分红池'); foreach ($config['pools'] as $p) { $pool = self::find()->where(['status' => 1, 'is_delete' => 0, 'id' => $p['config_id']])->one(); if (empty($pool)) continue; if (!isset($p['rate']) || $p['rate'] <= 0) continue; self::pushCP($p['config_id'], $order, $pv0, $setting['string_code_pv_scale_0'], $p['rate'], $setting['string_code_model'], 0); self::pushCP($p['config_id'], $order, $pv1, $setting['string_code_pv_scale_1'], $p['rate'], $setting['string_code_model'], 1); } } catch (\Exception $e) { ActionLog::addLog(1, '/configPoolPush', $e->getMessage()); } } public static function pushCP($config_id, $order, $order_pv, $code_percent, $rate, $string_code_model, $model_val) { try { $code_percent = 100; //if (in_array($model_val, $string_code_model)) { //debug_log([__METHOD__, __LINE__, "大模型比例 : " . ($code_percent / 100)], "app_debug.log"); $add_money = bcmul($order_pv, $code_percent / 100, 4); $money = bcmul($add_money, $rate / 100, 4); //debug_log([__METHOD__, __LINE__, "大模型:order_pv : " . $order_pv . ',$add_money:' . $add_money . ',$money:' . $money], "app_debug.log"); if (bccomp($money, 0, 4) > 0) { $desc = self::MODEL_NAME_LIST[$model_val] . " " . "订单号:【{$order->order_no}】分红增加"; PluginPool::poolPush($config_id, $order, $money, $desc, $order_pv, $rate, $code_percent, $model_val, $add_money); } // } } catch (\Exception $e) { ActionLog::addLog(1, 'pushCP', $e->getMessage()); } } public static function appointAward($config, $order, $pv0, $pv1) { try { if (!isset($config['users']) || empty($config['users'])) throw new \Exception('无配置分红池'); foreach ($config['users'] as $p) { if (!isset($p['rate']) || $p['rate'] <= 0 || !isset($p['saas_id']) || !$p['saas_id'] || !isset($p['remark']) || !$p['remark']) continue; /*$money = bcmul($pv0, $p['rate'] / 100, 6); if ($money > 0){ //UserStringCodeOrder::configAddUserWallet($order, $p['saas_id'], $money, UserStringCodeOrder::TYPE_APPOINT_AWARD, 0, $p['remark']); UserStringCodeOrder::goCouponAdd($order->store_id, $p['saas_id'], $money, UserStringCodeOrder::STORE_DESIGNATED_PROFIT_SHARING,$order); }*/ $money = bcmul($pv1, $p['rate'] / 100, 6); if ($money > 0){ //UserStringCodeOrder::configAddUserWallet($order, $p['saas_id'], $money, UserStringCodeOrder::TYPE_APPOINT_AWARD, 1, $p['remark']); UserStringCodeOrder::goCouponAdd($order->store_id, $p['saas_id'], $money, UserStringCodeOrder::STORE_DESIGNATED_PROFIT_SHARING,$order); } } } catch (\Exception $e) { //ActionLog::addLog(1, 'appointAward', $e->getMessage()); } } public static function currencyAward($config, $order, $pv0, $pv1) { try { if (!isset($config['currencys']) || empty($config['currencys'])) throw new \Exception('无配置分红池'); $saas_id = SaasUser::findSaasIdByUserId($order->user_id); foreach ($config['currencys'] as $p) { if (!isset($p['rate']) || $p['rate'] <= 0 || !isset($p['mark']) || !$p['mark']|| !isset($p['currency_id']) || !$p['currency_id']) continue; $currency = CurrencyCoin::findOne($p['currency_id']); if (!$currency) continue; //$currency_money_0 = bcdiv($pv0, $currency->cny_price, 6); $currency_money_1 = bcdiv($pv1, $currency->cny_price, 6); //$currency_money_0 = bcmul($currency_money_0, $p['rate']/100, 6); $currency_money_1 = bcmul($currency_money_1, $p['rate']/100, 6); /*if ($currency_money_0 > 0){ $res = CurrencyCoin::addSaasMoney($currency->id,$saas_id, 1, $currency_money_0, $p['mark']); if (!$res) { continue; } }*/ if ($currency_money_1 > 0){ $res = CurrencyCoin::addSaasMoney($currency->id,$saas_id, 1, $currency_money_1, $p['mark'],$order->store_id); if (!$res) { continue; } } /*$wallet = UserWalletCoin::findOne(['currency_id'=>$currency->id,'store_id'=>$order->store_id,'is_open'=>1]); if (!$wallet) continue; $money = truncateDecimal(bcmul($pv0, $p['rate'] / 100, 6)); if ($money > 0){ $data = [ "order_id"=>$order->id, "order_no"=>$order->order_no, "pay_price"=>$money, "desc"=>$p['mark'], 'huifu_id'=>$wallet->huifu_id, 'huifu_id_name'=>self::getHuifuName($wallet->huifu_id), 'percent'=>0, "create_time"=>time(), "is_type"=>2 ]; \Yii::$app->db->createCommand()->insert('cyy_scan_code_pay_order_percent', $data)->execute(); } $money = truncateDecimal(bcmul($pv1, $p['rate'] / 100, 6)); if ($money > 0){ $data = [ "order_id"=>$order->id, "order_no"=>$order->order_no, "pay_price"=>$money, "desc"=>$p['mark'], 'is_type'=>2, 'huifu_id'=>$wallet->huifu_id, 'huifu_id_name'=>self::getHuifuName($wallet->huifu_id), 'percent'=>0, "create_time"=>time(), ]; \Yii::$app->db->createCommand()->insert('cyy_scan_code_pay_order_percent', $data)->execute(); }*/ } } catch (\Exception $e) { ActionLog::addLog(1, UserStringCodeOrder::TYPE_CURRENCY_AWARD, $e->getMessage()); } } public static function getHuifuName($huifuId) { $storebanklist = Storebanklist::findOne(['huifu_id' => $huifuId]); if (empty($storebanklist)) { $localHuifuMerchant = LocalHuifuMerchant::findOne(['huifu_id' => $huifuId]); return $localHuifuMerchant->huifu_name ?? ''; } return $storebanklist->short_name ?? ''; } public static function areaAward($config, $store_id, $order, $order_pv, $model) { try { if ($order_pv <= 0) throw new Exception('pv为0,' . self::MODEL_NAME_LIST[$model]); self::stringCodeStoreArea($config, $store_id, $order, $order_pv, 4, $model);//区代理 self::stringCodeStoreArea($config, $store_id, $order, $order_pv, 3, $model);//区代理 self::stringCodeStoreArea($config, $store_id, $order, $order_pv, 2, $model);//市代理 self::stringCodeStoreArea($config, $store_id, $order, $order_pv, 1, $model);//省代理 //debug_log([__METHOD__, __LINE__, "PV:" . $order_pv], "app_debug.log"); //self::stringCodeStoreReferrer($order, $config, $store_id, $order_pv, $model); //self::stringCodeStoreUserAffiliated($order, $config, $order_pv, $model); //self::stringCodeStoreUserReferrer($order, $config, $order_pv, $model); //self::supriseRed($order, $config, $order_pv, $model); self::brandUserStoreReferrer($order, $config, $order_pv, $model); } catch (\Exception $e) { ActionLog::addLog(1, 'areaAward', $e->getMessage()); }catch (\Throwable $e) { ActionLog::addLog(1, 'areaAward', $e->getMessage()); } } public static function stringCodeStoreArea($config, $store_id, $order, $order_pv, $agent_type, $model) { try { $holder = null; $area_type = ""; switch ($agent_type) { case 4: $desc = '街道代理'; //$rate = $config['street_rate']; $area_type = UserStringCodeOrder::STORE_T_PROFIT_SHARING; $rate = 0; $holder = PoolConfigHolder::find()->where(['store_id' => $store_id, 'status' => 1, 'is_delete' => 0, 'agent_type' => 4, 'town_id' => $order['town_id']])->one(); if (!empty($holder) && $holder->user_id) break; case 3: if ($agent_type == 3){ $desc = '区代理'; $rate = $config['area_rate']; $area_type = UserStringCodeOrder::STORE_A_PROFIT_SHARING; } $holder = PoolConfigHolder::find()->where(['store_id' => $store_id, 'status' => 1, 'is_delete' => 0, 'agent_type' => 3, 'district_id' => $order['district_id']])->one(); if (!empty($holder) && $holder->user_id) break; case 2: if ($agent_type == 2) { $desc = '市代理'; $rate = $config['city_rate']; $area_type = UserStringCodeOrder::STORE_C_PROFIT_SHARING; } $holder = PoolConfigHolder::find()->where(['store_id' => $store_id, 'status' => 1, 'is_delete' => 0, 'agent_type' => 2, 'city_id' => $order['city_id']])->one(); if (!empty($holder) && $holder->user_id) break; case 1: if ($agent_type == 1) { $desc = '省代理'; $rate = $config['province_rate']; $area_type = UserStringCodeOrder::STORE_P_PROFIT_SHARING; } $holder = PoolConfigHolder::find()->where(['store_id' => $store_id, 'status' => 1, 'is_delete' => 0, 'agent_type' => 1, 'province_id' => $order['province_id']])->one(); break; default: $rate = 0; break; } $agent_id = !empty($holder) && $holder->user_id ? SaasUser::findSaasIdByUserId($holder->user_id) : ($config['string_code_store_push_id'] ?: 0); if ($rate <= 0 || !$agent_id) { throw new \Exception('rate:' . $rate . ',agent_id:' . $agent_id); } $money = bcmul($order_pv, $rate / 100, 4); //UserStringCodeOrder::configAddUserWallet($order, $agent_id, $money, UserStringCodeOrder::TYPE_AREA_AWARD, $model, $desc); UserStringCodeOrder::goCouponAdd($order->store_id, $agent_id, $money, $area_type,$order); } catch (\Exception $e) { ActionLog::addLog(1, 'stringCodeStoreArea', $e->getMessage()); } } public static function stringCodeStoreReferrer($order, $setting, $store_id, $transfer_money, $model_val) { $referrer = 0; if ($order instanceof ScanOrder) { if (isset($setting['string_code_store_referrer']) && $setting['string_code_store_referrer'] > 0) { $referrer = $setting['string_code_store_referrer']; } } else { if (isset($setting['string_code_brand_referrer']) && $setting['string_code_brand_referrer'] > 0) { $referrer = $setting['string_code_brand_referrer']; } } if ($referrer > 0) { $store_referral = SaasStoreReferral::findOne(['store_id' => $store_id]); $saas_id = $store_referral['referral_id']; $money = bcmul($transfer_money, $referrer / 100, 4); //debug_log([__METHOD__, __LINE__, "stringCodeStoreReferrer:" . $transfer_money.'$referrer'.$referrer], "app_debug.log"); $type = UserStringCodeOrder::TYPE_STORE_REFERRER; UserStringCodeOrder::transferAddUserWallet($order, $saas_id, $money, $type, $model_val); } } public static function brandUserStoreReferrer($order, $setting, $transfer_money, $model_val) { $referrer = 0; if ($order instanceof ScanOrder) { if (isset($setting['string_code_store_referrer']) && $setting['string_code_store_referrer'] > 0) { $referrer = $setting['string_code_store_referrer']; } } else { if (isset($setting['brand_user_store_referrer']) && $setting['brand_user_store_referrer'] > 0) { $referrer = $setting['brand_user_store_referrer']; } } $user = User::findOne($order->user_id); $user->parent_id = $user->old_parent_id; \Yii::$app->db->createCommand()->insert('cyy_pay_log', [ 'order_no'=>$order->order_no, 'desc'=>"推荐人".$user->parent_id."百分比:".$referrer, 'add_time'=>time(), ])->execute(); if ($user->parent_id > 0 && $referrer > 0) { $saas_id = SaasUser::findSaasIdByUserId($user->parent_id); $money = bcmul($transfer_money, $referrer / 100, 4); //$type = UserStringCodeOrder::TYPE_BRAND_USER_STORE_REFERRER; $type = UserStringCodeOrder::STORE_DIRECT_REFERRAL_REWARD; //debug_log([__METHOD__, __LINE__, "brandUserStoreReferrer:" . $transfer_money.'$referrer'.$referrer], "app_debug.log"); //UserStringCodeOrder::transferAddUserWallet($order, $saas_id, $money, $type, $model_val); UserStringCodeOrder::goCouponAdd($order->store_id, $saas_id, $money, $type,$order); } } public static function supriseRed($order, $setting, $transfer_money, $model_val) { $referrer = 0; if ($order instanceof ScanOrder) { if (isset($setting['store_surprise_Red']) && $setting['store_surprise_Red'] > 0) { $referrer = $setting['store_surprise_Red']; } } else { if (isset($setting['surprise_Red']) && $setting['surprise_Red'] > 0) { $referrer = $setting['surprise_Red']; } } $saas_id = SaasUser::findSaasIdByUserId($order->user_id); if ($saas_id > 0 && $referrer > 0){ $money = bcmul($transfer_money, $referrer / 100, 4); $type = UserStringCodeOrder::TYPE_SUPRISE_RED; UserStringCodeOrder::transferAddUserWallet($order, $saas_id, $money, $type, $model_val); } } public static function stringCodeStoreUserAffiliated($order, $setting, $transfer_money, $model_val) { $affiliated = 0; if ($order instanceof ScanOrder) { if (isset($setting['string_code_store_user_affiliated']) && $setting['string_code_store_user_affiliated'] > 0) { $affiliated = $setting['string_code_store_user_affiliated']; } } else { if (isset($setting['string_code_brand_user_affiliated']) && $setting['string_code_brand_user_affiliated'] > 0) { $affiliated = $setting['string_code_brand_user_affiliated']; } } if ($affiliated > 0) { $first_store_id = SaasUser::findFirstStoreIdByUserId($order->user_id); $store_admin = Admin::findOne(['type' => 'store', 'type_id' => $first_store_id, 'is_delete' => 0]); $saas_id = $store_admin['saas_user_id']; $money = bcmul($transfer_money, $affiliated / 100, 4); $type = UserStringCodeOrder::TYPE_STORE_USER_AFFILIATED; //debug_log([__METHOD__, __LINE__, "stringCodeStoreUserAffiliated:" . $transfer_money.'$referrer'.$affiliated], "app_debug.log"); UserStringCodeOrder::transferAddUserWallet($order, $saas_id, $money, $type, $model_val); } } public static function stringCodeStoreUserReferrer($order, $setting, $transfer_money, $model_val) { $referrer = 0; if ($order instanceof ScanOrder) { if (isset($setting['string_code_store_user_referrer']) && $setting['string_code_store_user_referrer'] > 0) { $referrer = $setting['string_code_store_user_referrer']; } } else { if (isset($setting['string_code_brand_user_referrer']) && $setting['string_code_brand_user_referrer'] > 0) { $referrer = $setting['string_code_brand_user_referrer']; } } if ($referrer > 0) { $user = User::findOne($order->user_id); if ($user->id > 0) { $saas_id = SaasUser::findSaasParentIdByUserId($order->user_id); $money = bcmul($transfer_money, $referrer / 100, 4); $type = UserStringCodeOrder::TYPE_STORE_USER_REFERRER; //debug_log([__METHOD__, __LINE__, "stringCodeStoreUserReferrer:" . $transfer_money.'$referrer'.$referrer], "app_debug.log"); UserStringCodeOrder::transferAddUserWallet($order, $saas_id, $money, $type, $model_val); } } } /** * @param $store_id * @return bool */ public static function checkSettingByStoreId($store_id, $setting) { if (!$setting) { //debug_log([__METHOD__, __LINE__, "店铺:【{$store_id}】未配置串码设置"], "app_debug.log"); return false; } if (!isset($setting['string_code_store_switch']) || $setting['string_code_store_switch'] != 1) { //debug_log([__METHOD__, __LINE__, "店铺:【{$store_id}】未开启串码功能"], "app_debug.log"); return false; } if (!isset($setting['string_code_model'])) { //debug_log([__METHOD__, __LINE__, "店铺:【{$store_id}】未设置串码模式"], "app_debug.log"); return false; } if (!is_array($setting['string_code_model'])) { //debug_log([__METHOD__, __LINE__, "店铺:【{$store_id}】串码模式配置不是数组"], "app_debug.log"); return false; } return true; } public static function getDefPools($bd_pools, $store_pools) { // 确保都是数组格式 $bd_pools = array_map(function($item) { return is_array($item) ? $item : (array)$item; }, $bd_pools); $store_pools = array_map(function($item) { return is_array($item) ? $item : (array)$item; }, $store_pools); $currentIds = ArrayHelper::getColumn($bd_pools, 'config_id'); $targetIds = ArrayHelper::getColumn($store_pools, 'config_id'); $idsToDelete = array_diff($currentIds, $targetIds); $idsToAdd = array_diff($targetIds, $currentIds); // 删除操作 if (!empty($idsToDelete)) { $bd_pools = array_values(array_filter($bd_pools, function($pool) use ($idsToDelete) { return !in_array($pool['config_id'], $idsToDelete); })); } // 新增操作 if (!empty($idsToAdd)) { foreach ($store_pools as $item) { if (in_array($item['config_id'], $idsToAdd)) { $newItem = $item; $newItem['rate'] = 0; $bd_pools[] = $newItem; } } } return $bd_pools; } public static function getAwardType($store_id, $is_scan,$award_type = 'award_type') { if (!$is_scan){ $store_setting = Option::getDecode(OptionSetting::PLUGIN_CONFIG_POOL_SETTING, $store_id, OptionSetting::SHARE_GROUP_NAME); $type = $store_setting[$award_type] ?? 1; }else{ $bd_admin = Admin::findOne(['store_id'=>$store_id,'type'=>'bd_agent','is_delete'=>0,'is_enable'=>1]); if (empty($bd_admin)){ $admin_id = Salesman::getBdId($store_id); if ($admin_id){ $bd_admin = Admin::findOne(['id'=>$admin_id,'type'=>'bd_agent','is_delete'=>0,'is_enable'=>1]); } } $bd_setting = []; if ($bd_admin){ $bd_setting = Option::getDecode(OptionSetting::SHARE_STRING_CODE_SALE_SETTING_BD, $bd_admin->id, OptionSetting::SHARE_GROUP_NAME); } $type = $bd_setting[$award_type] ?? 1; } return $type; } public static function poolSetting($store_id) { $plugin_setting = Option::getDecode(OptionSetting::PLUGIN_CONFIG_POOL_SETTING, $store_id, OptionSetting::SHARE_GROUP_NAME); if (empty($plugin_setting) || !isset($plugin_setting['is_enable']) || !$plugin_setting['is_enable']) { $plugin_setting = Option::getDecode(OptionSetting::PLUGIN_CONFIG_POOL_SETTING, 197, OptionSetting::SHARE_GROUP_NAME); if (empty($plugin_setting) || !isset($plugin_setting['is_enable']) || !$plugin_setting['is_enable']) { return false; } } return $plugin_setting; } }