| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- <?php
- /**
- * 红包拓客
- */
- namespace addons\Redpack\common\logic;
- use addons\Agent\common\models\Agent;
- use addons\Agent\common\models\AgentLevel;
- use addons\Redpack\common\enums\RedpackEnum;
- use addons\Redpack\common\helper\RedpackHelper;
- use addons\Redpack\common\models\Redpack;
- use addons\Redpack\common\models\RedpackTask;
- use addons\Redpack\common\models\RedpackWallet;
- use addons\BlueScore\common\models\BlueScore;
- use addons\BlueScore\common\enums\BlueScoreEnum;
- use common\enums\AddonsEnum;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\models\common\AddonsSetting;
- use common\models\mall\Mall;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- use common\models\user\UserLevel;
- use common\models\user\UserPartitionRelationship;
- use common\traits\ErrorTrait;
- use phpDocumentor\Reflection\Types\Self_;
- use yii\db\Exception;
- use Yii;
- use yii\redis\Connection;
- class FrozenToActiveLogic
- {
- use ErrorTrait;
- /**
- * 执行冻结转激活红包之前进行记录
- * @Author: ltm
- * @DateTime: 2022/3/24 0024 11:07
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return string
- */
- public static function FrozenToActive(){
- //查询当天是否执行过
- $time = time();
- $date = date("Y-m-d");
- $find_data = RedpackTask::find()->where(['day'=>$date])->orderBy('id desc')->one();
- $executed_ids = [];
- if(!empty($find_data['content'])){
- $executed_ids = json_decode($find_data['content']);
- }
- $open_mall_ids = self::getOpenRedpackMall();// 开启每日冻结转激活商城
- $where = [
- 'or',
- [//查询未过期商城
- 'and',
- ['=','status',StatusEnum::ENABLED],
- ['>','expired_at',time()],
- ],
- [//查询永久商城
- 'and',
- ['=','status',StatusEnum::ENABLED],
- ['=','expired_at',StatusEnum::DISABLED],
- ],
- ];
- //遍历所有商城
- $model = Mall::find()->select('id,name,status')->orderBy('id')->andwhere($where);
- foreach ($model->asArray()->batch(500) as $mall_ids_list) {
- $mall_ids = array_column($mall_ids_list, 'id');
- $mall_execute_ids = array_diff($mall_ids,$executed_ids);
- //获取开启红包拓客冻结红包转激活红包的 mall_id
- if(count($mall_ids)>0 && !empty($mall_execute_ids)) {
- $mall_config_time = self::getMallConfigtime($mall_execute_ids); //获取开启红包拓客设置定时时间
- $mall_config = self::getMallConfignum($mall_execute_ids); //获取需要进行任务商城红包转换比率
- $mall_blue_score_config = self::getOpenBlueScoreMall($mall_execute_ids);
- //执行冻结红包进行激活
- $is_execute = false;
- foreach ($mall_execute_ids as $k => $v){
- if(!empty($mall_config_time['activation_time'][$v])){
- $activation_time = $mall_config_time['activation_time'][$v];
- $activation_time = strtotime($activation_time);
- if($time>=$activation_time){
- if(!$executed_ids || !in_array($v,$executed_ids)){
- if (!in_array($v, $open_mall_ids)) continue;// 未开启配置直接跳过
- $res = self::redpackexecute($v,$mall_config, $mall_blue_score_config);
- if($res){
- $executed_ids[] = $v;
- if($find_data){
- $add_data['id'] = $find_data->id;
- }
- if($executed_ids) {
- $add_data['content'] = json_encode($executed_ids,true);
- $add_data['day'] = date("Y-m-d");
- $res = RedpackTask::setData($add_data,$is_execute,$find_data);
- }
- }
- }
- }
- }
- }
- }
- }
- //更新活动的活动时间
- self::changeactivetime();
- return true;
- }
- /**
- * 修改每日活动时间
- * @Author: ltm
- * @DateTime: 2022/3/25 0025 10:35
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return string
- */
- public static function changeactivetime(){
- $where = [
- 'and',
- ['time_type' => '1'],
- ['status' => StatusEnum::ENABLED],
- ];
- $redis = \Yii::$app->redis;
- $update_time = $redis->get(RedpackEnum::REDIS_REDPACK_STAK_KEY);
- $daytime = strtotime(date("Y-m-d"));
- $last_execute_time = strtotime(date('Y-m-d'));
- if(!$update_time || $update_time < $last_execute_time){
- $model = Redpack::find()->select('time_type,mall_id,start_time,end_time,day_award,award,id')->where($where);
- foreach ($model->batch(500) as $data) {
- foreach ($data as $key => $value) {
- $params['start_time'] = $daytime + strtotime(date("Y-m-d H:i:s",$value['start_time'])) - strtotime(date("Y-m-d",$value['start_time']));
- $params['end_time'] = $daytime + strtotime(date("Y-m-d H:i:s",$value['end_time'])) - strtotime(date("Y-m-d",$value['end_time']));
- $params['id'] = $value['id'];
- $params['mall_id'] = $value['mall_id'];
- $params['award'] = $value['day_award'];//每日总奖金每日凌晨将被覆盖
- $update = Redpack::setData($params);
- }
- }
- //更新标记执行过时间
- self::setRedpackTaskStatus(time());
- }
- }
- /**
- * @Author: ltm
- * @DateTime: 2022/3/25 0025 14:01
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return string
- */
- public static function setRedpackTaskStatus($data) {
- $config = \Yii::$app->getComponents()['redis'];
- unset($config['class']);
- $redis = new Connection($config);
- $redis->set(RedpackEnum::REDIS_REDPACK_STAK_KEY,$data);
- $redis->close();
- }
- /**
- * 执行冻结红包转激活
- * @Author: ltm
- * @DateTime: 2022/3/24 0024 14:48
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return string
- */
- public static function redpackexecute($mall_id,$mall_config, $mall_blue_score_config){
- //获取会员记录
- $where = [
- 'and',
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED],
- ['>','frozen_redpack',0],
- ];
- $model = RedpackWallet::find()->select('frozen_redpack,mall_id,user_id,id')->where($where);
- //冻结红包转激活红包
- $text = RedpackEnum::getRedpackText(RedpackEnum::WALLET_REDPACK_FROZEN, $mall_id).'转'.RedpackEnum::getRedpackText(RedpackEnum::WALLET_REDPACK, $mall_id);
- foreach ($model->asArray()->batch(500) as $walletdata) {
- foreach ($walletdata as $key => $value){
- $calculate_money = $value['frozen_redpack'] * $mall_config['activation_num'][$mall_id] / 100;
- $money = sprintf("%.2f",substr(sprintf("%.3f", $calculate_money), 0, -1));
- //小于一分钱,则不转出
- if ($money < 0.01) continue;
- //获取蓝色积分配置
- $blue_score_config = $mall_blue_score_config[$mall_id] ?? [];
- $t = Yii::$app->db->beginTransaction();
- try {
- if($money>0){
- //扣除冻结红包
- $res = RedpackWallet::setData([
- 'mall_id' => $mall_id,
- 'user_id' => $value['user_id'],
- 'is_frozen' => true,
- 'is_deduct' => true,
- 'money' => - $money,
- 'from_type' => $blue_score_config['is_enable'] == StatusEnum::ENABLED ? RedpackEnum::FROM_FROZEN_AUTO_ROLL_OUT : RedpackEnum::FROM_FROZEN_TO_ACTIVE,
- 'desc' => $text,
- ]);
- if($res === false) throw new Exception('执行失败');
- //是否安装and开启了蓝色积分转换
- $has_blue_score = \common\models\mall\MallAddons::isInstall($mall_id,AddonsEnum::ADDONS_BLUE_SCORE);
- if ($has_blue_score && !empty($blue_score_config) && $blue_score_config['is_enable'] == StatusEnum::ENABLED) {
- if (!empty($blue_score_config['transfer_rate']) || $blue_score_config['transfer_rate'] > 0) {
- //计算激活红包转换的蓝色积分
- $integral = round($money * $blue_score_config['transfer_rate'] / 100,2);
- //计算剩余的激活红包
- $money = round($money * (1 - ($blue_score_config['transfer_rate'] / 100)),2);;
- //转换蓝色积分
- BlueScore::setData([
- 'mall_id' => $mall_id,
- 'user_id' => $value['user_id'],
- 'integral' => $integral,
- 'from_type' => BlueScoreEnum::FROM_ACTIVE_AUTO_TO_BLUE_SCORE,
- 'desc' => '冻结红包自动转蓝色积分,蓝色积分+' . $integral,
- ]);
- }
- }
- //转激活红包
- $res = RedpackWallet::setData([
- 'mall_id' => $mall_id,
- 'user_id' => $value['user_id'],
- 'is_frozen' => false,
- 'is_deduct' => true,
- 'money' => $money,
- 'from_type' => RedpackEnum::FROM_FROZEN_TO_ACTIVE,
- 'desc' => $text,
- ]);
- }
- if($res === false) throw new Exception("执行失败");
- //加速激活冻结红包
- self::computeActive($mall_id, $value['user_id'], $money);
- $t->commit();
- } catch (\Exception $e) {
- $t->rollBack();
- echo '冻结红包转激活红包失败,' . $e->getMessage() . ',行数:' . $e->getLine();
- }
- }
- }
- return true;
- }
- /**
- * 获取已经开启冻结红包转激活红包商城配置
- * @Author: ltm
- * @DateTime: 2022/3/24 0024 10:21
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return array
- */
- public static function getOpenRedpackMall()
- {
- //获取开启的商城ID
- $ids = [];
- $where[] = ['name' => "frozen_status"];
- $where[] = ['value' => 1];
- $where[] = ['addons_name' => AddonsEnum::ADDONS_NAME_REDPACK];
- $redpack_config = AddonsSetting::lists(['where'=>$where],true);
- if($redpack_config){
- $ids = array_column($redpack_config,'mall_id');
- }
- return $ids;
- }
- /**
- *
- * @Author: ltm
- * @DateTime: 2022/3/24 0024 10:51
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return string
- */
- public static function getMallConfigtime($mall_ids){
- //获取开启时间
- $where[] = ['in','mall_id',$mall_ids];
- $where[] = ['name' => "activation_time"];
- $where[] = ['addons_name' => AddonsEnum::ADDONS_NAME_REDPACK];
- $redpack_config = AddonsSetting::lists(['where'=>$where,'select'=>'value,mall_id'],true);
- if($redpack_config){
- $list['activation_time'] = array_column($redpack_config,'value','mall_id');
- }
- return $list;
- }
- /**
- *
- * @Author: ltm
- * @DateTime: 2022/3/24 0024 10:51
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return array
- */
- public static function getMallConfignum($mall_ids){
- //获取转换比率
- $where[] = ['in','mall_id',$mall_ids];
- $where[] = ['name' => "activation_num"];
- $where[] = ['addons_name' => AddonsEnum::ADDONS_NAME_REDPACK];
- $redpack_config = AddonsSetting::lists(['where'=>$where,'select'=>'value,mall_id'],true);
- $list = [];
- if($redpack_config){
- $list['activation_num'] = array_column($redpack_config,'value','mall_id');
- }
- return $list;
- }
- /**
- * 获取蓝色积分商城配置
- * @Author:
- * @DateTime:
- * @Copyright:
- * @param $mall_ids
- * @return array
- */
- public static function getOpenBlueScoreMall($mall_ids)
- {
- $where[] = ['addons_name' => AddonsEnum::ADDONS_BLUE_SCORE];
- $where[] = ['status' => StatusEnum::ENABLED];
- $where[] = ['mall_id' => $mall_ids];
- $blue_score_config = AddonsSetting::lists(['where'=>$where, 'select'=>'name,value,mall_id'],true);
- $return_array = [];
- if ($blue_score_config) {
- foreach ($blue_score_config as $config) {
- $return_array[$config['mall_id']][$config['name']] = $config['value'];
- }
- }
- return $return_array;
- }
- /**
- * 冻结红包加速激活计算
- * @Author:
- * @DateTime:
- * @Copyright:
- * @param $mall_ids
- * @return bool|void
- */
- public static function computeActive($mall_id, $user_id, $init_money)
- {
- if ($init_money < 0.01) {
- return true;
- }
- //获取商城配置
- $config = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_REDPACK, 'basic');
- $config = RedpackHelper::initConfig($config);
- if (empty($config['is_set_activate'])) {
- //冻结红包加速激活设置未开启
- return true;
- }
- //获取用户路径,并获取奖励用户
- $parent_id_arr = UserPartitionRelationship::getParentIdArr($user_id);
- if (empty($parent_id_arr)) {
- //没有上级
- return true;
- }
- //倒转数组,直属在最前面
- $parent_id_arr = array_reverse($parent_id_arr);
- //获取直推用户的加速设置
- $setting_user_level_list = json_decode($config['setting_user_level_list'], true) ?? [];
- //查询直属上级信息
- $parent_id = $parent_id_arr[0];
- $parent_user_info = User::find()
- ->where([
- 'id' => $parent_id,
- 'status' => StatusEnum::ENABLED
- ])
- ->select('id,level')
- ->asArray()
- ->one();
- $parent_redpack_wallets = RedpackWallet::find()
- ->where([
- 'mall_id' => $mall_id,
- 'user_id' => $parent_id_arr
- ])
- ->indexBy('user_id')
- ->asArray()
- ->all();
- if ($parent_user_info) {
- $user_level = UserLevel::find()
- ->where([
- 'mall_id' => $mall_id,
- 'level' => $parent_user_info['level'],
- 'status' => StatusEnum::ENABLED
- ])
- ->one();
- if ($user_level && isset($setting_user_level_list[$parent_user_info['level']]) && isset($parent_redpack_wallets[$parent_id])) {
- //直属上级冻结红包数量需要大于0
- if ($parent_redpack_wallets[$parent_id]['frozen_redpack'] > 0) {
- $money = bcmul($init_money, $setting_user_level_list[$parent_user_info['level']] / 100, 2);
- $money = min($money, $parent_redpack_wallets[$parent_id]['frozen_redpack']);
- if ($money > 0.01) {
- //直属上级 加速转激活冻结红包
- $res = RedpackWallet::setData([
- 'mall_id' => $mall_id,
- 'user_id' => $parent_id,
- 'is_frozen' => true,
- 'is_deduct' => true,
- 'money' => - $money,
- 'from_type' => RedpackEnum::FROM_PUSH_ACCELERATION,
- 'desc' => '直推加速激活',
- ]);
- if($res === false) throw new Exception('直属上级加速-扣除冻结红包执行失败');
- //直属上级 加速转激活红包
- $res = RedpackWallet::setData([
- 'mall_id' => $mall_id,
- 'user_id' => $parent_id,
- 'is_frozen' => false,
- 'is_deduct' => true,
- 'money' => $money,
- 'from_type' => RedpackEnum::FROM_PUSH_ACCELERATION,
- 'desc' => '直推加速激活',
- ]);
- if ($res === false) throw new Exception("直属上级加速-转激活红包执行失败");
- $parent_redpack_wallets[$parent_id]['frozen_redpack'] = bcsub($parent_redpack_wallets[$parent_id]['frozen_redpack'], $money, 2);
- }
- }
- }
- }
- if (MallAddons::isInstall($mall_id, AddonsEnum::ADDONS_NAME_AGENT)) {
- $agents = Agent::find()
- ->where([
- 'user_id' => $parent_id_arr,
- 'status' => StatusEnum::ENABLED
- ])
- ->select('id,user_id,level')
- ->asArray()
- ->all();
- // 按$parent_id_arr顺序重新排序
- usort($agents, function ($a, $b) use ($parent_id_arr) {
- return array_search($a['user_id'], $parent_id_arr) - array_search($b['user_id'], $parent_id_arr);
- });
- //渠道分红等级配置
- $setting_agent_level_list = json_decode($config['setting_agent_level_list'], true) ?? [];
- //安装了渠道分红
- $agent_level_list = AgentLevel::find()
- ->where([
- 'mall_id' => $mall_id,
- 'status' => StatusEnum::ENABLED
- ])
- ->select('id,name,level')
- ->indexBy('level')
- ->asArray()
- ->all();
- $has_reward_ratio = 0;
- foreach ($agents as $agent) {
- if (isset($agent_level_list[$agent['level']]) && isset($setting_agent_level_list[$agent['level']])) {
- if (!isset($parent_redpack_wallets[$agent['user_id']]) || empty($parent_redpack_wallets[$agent['user_id']]['frozen_redpack'])) {
- continue;
- }
- $reward_money = bcmul($init_money, ($setting_agent_level_list[$agent['level']] - $has_reward_ratio) / 100, 2);
- $reward_money = min($reward_money, $parent_redpack_wallets[$agent['user_id']]['frozen_redpack']);
- if ($reward_money > 0.01) {
- $has_reward_ratio = max($setting_agent_level_list[$agent['level']], $has_reward_ratio);
- $res = RedpackWallet::setData([
- 'mall_id' => $mall_id,
- 'user_id' => $agent['user_id'],
- 'is_frozen' => true,
- 'is_deduct' => true,
- 'money' => - $reward_money,
- 'from_type' => RedpackEnum::FROM_AGENT_ACCELERATION,
- 'desc' => '渠道加速激活',
- ]);
- if($res === false) throw new Exception('渠道分红团队加速-扣除冻结红包执行失败');
- // 团队加速转激活红包
- $res = RedpackWallet::setData([
- 'mall_id' => $mall_id,
- 'user_id' => $agent['user_id'],
- 'is_frozen' => false,
- 'is_deduct' => true,
- 'money' => $reward_money,
- 'from_type' => RedpackEnum::FROM_AGENT_ACCELERATION,
- 'desc' => '渠道加速激活',
- ]);
- if ($res === false) throw new Exception("渠道分红团队加速-转激活红包执行失败");
- }
- }
- }
- }
- return true;
- }
- }
|