本文主要是介绍PHP之 链式邀请储值三级邀请所得计算算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
情景解说
A B C D E F G H I J K L ,12个人, A储值了一笔钱,A邀请B和C储值了一笔钱,A从B和C获利他们充值金额的10%,即为邀请所得;然后,B又邀请D、F、G充值了一笔钱,B又分别从D F G那里分别获得一份邀请所得金额;这时,C 也邀请了 H I 冲值了一笔钱,C 即获得了两笔邀请所得;最后,G又邀请了J充值了一笔钱,获得一份邀请所得,I 又邀请了K L充值了一笔钱,获得两份邀请所得。
规则说明
自己邀请所得的金额,全额归自己;而自己发展的下线,可以从所有下线的每笔邀请所得,获利百分之十;到了下线的下线,也就是第三级,自己可以从下线的下线的每笔邀请所得,获利百分之一。
计算要求
统计这12个人,每个人最终的获利金额,以及自己从多少人身上获利。
数据表设计
------------------------------------------------------------------------------------------------------------------- CREATE TABLE `infinite_invite_log` (`invite_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID',`invite_p_id` int(11) NOT NULL COMMENT '父ID',`userid` int(11) NOT NULL DEFAULT '0' COMMENT '邀请人-用户ID',`nickname` varchar(30) NOT NULL DEFAULT '' COMMENT '邀请人-用户昵称',`invite_code` varchar(30) NOT NULL DEFAULT '' COMMENT '邀请人-自己邀请码',`be_invited_code` varchar(30) DEFAULT NULL COMMENT '邀请人-被邀请码',`invite_get` decimal(7,2) NOT NULL DEFAULT '0.00' COMMENT '邀请人-邀请所得金额',`invite_status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否算过:0,否;1,是',`invite_create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`invite_update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更改时间',PRIMARY KEY (`invite_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='储值邀请所得链式记录表'CREATE TABLE `infinite_invite_summary` (`sum_id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '汇总ID',`userid` int(11) NOT NULL DEFAULT '0' COMMENT '用户ID',`nickname` varchar(30) NOT NULL DEFAULT '' COMMENT '用户昵称',`sum_amount` decimal(9,2) NOT NULL DEFAULT '0.00' COMMENT '汇总金额',`sum_p_num` smallint(5) NOT NULL DEFAULT '0' COMMENT '汇总人数',`sum_create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`sum_update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更改时间',`sum_status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否汇总到邀请人余额:0,否;1,是',PRIMARY KEY (`sum_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='储值邀请所得无限级汇总表' -------------------------------------------------------------------------------------------------------------------
相关数据
------------------------------------------------------------------------------------------------------------------- insert into `infinite_invite_log` (`invite_id`, `invite_p_id`, `userid`, `nickname`, `invite_code`, `be_invited_code`, `invite_get`, `invite_status`, `invite_create_time`, `invite_update_time`) values('11','0','2','','EE828LK','','86.80','1','2020-04-12 16:34:31','2020-04-13 00:05:51'); insert into `infinite_invite_log` (`invite_id`, `invite_p_id`, `userid`, `nickname`, `invite_code`, `be_invited_code`, `invite_get`, `invite_status`, `invite_create_time`, `invite_update_time`) values('13','11','50236','','P4M6C1MG','EE828LK','86.80','1','2020-04-12 16:47:14','2020-04-13 00:05:51'); insert into `infinite_invite_log` (`invite_id`, `invite_p_id`, `userid`, `nickname`, `invite_code`, `be_invited_code`, `invite_get`, `invite_status`, `invite_create_time`, `invite_update_time`) values('16','11','50236','','P4M6C1MG','EE828LK','296.80','1','2020-04-12 17:26:06','2020-04-13 00:05:51'); insert into `infinite_invite_log` (`invite_id`, `invite_p_id`, `userid`, `nickname`, `invite_code`, `be_invited_code`, `invite_get`, `invite_status`, `invite_create_time`, `invite_update_time`) values('17','13','50133','','LQUNLJHH','P4M6C1MG','6.60','1','2020-04-12 17:39:03','2020-04-13 00:05:51'); insert into `infinite_invite_log` (`invite_id`, `invite_p_id`, `userid`, `nickname`, `invite_code`, `be_invited_code`, `invite_get`, `invite_status`, `invite_create_time`, `invite_update_time`) values('18','11','50089','','1K3BMA6V','EE828LK','6.60','1','2020-04-12 17:44:28','2020-04-13 00:05:51'); insert into `infinite_invite_log` (`invite_id`, `invite_p_id`, `userid`, `nickname`, `invite_code`, `be_invited_code`, `invite_get`, `invite_status`, `invite_create_time`, `invite_update_time`) values('19','18','50080','','69KMUNIA','1K3BMA6V','296.80','1','2020-04-12 17:51:02','2020-04-13 00:05:51'); insert into `infinite_invite_log` (`invite_id`, `invite_p_id`, `userid`, `nickname`, `invite_code`, `be_invited_code`, `invite_get`, `invite_status`, `invite_create_time`, `invite_update_time`) values('20','11','50089','','1K3BMA6V','EE828LK','86.80','1','2020-04-12 17:55:20','2020-04-13 00:05:51'); insert into `infinite_invite_log` (`invite_id`, `invite_p_id`, `userid`, `nickname`, `invite_code`, `be_invited_code`, `invite_get`, `invite_status`, `invite_create_time`, `invite_update_time`) values('22','16','27','','KUIV342','P4M6C1MG','96.90','1','2020-04-12 21:46:03','2020-04-13 00:05:51'); -------------------------------------------------------------------------------------------------------------------
实现代码
/*** 链式储值 - 处理逻辑*/public function link_invite_calculator(){$sum_data = $this->sum($this->calculator());$res = $this->recharge_dao->batch_insert_table('infinite_invite_summary', $sum_data);$return = true;if($res){$this->load->library('dbredis');$point = $this->dbredis->get('link_select_time');$this->recharge_dao->up_tab_data('infinite_invite_log',['invite_status'=>1],['invite_create_time<='=>$point]);$return = $this->recharge_dao->link_invite_batch_increase($sum_data);}return $return;}/*** 链式储值 - 汇总 计算* @param array $data* @return array*/private function sum($data){$result = [];foreach ($data as $key=>$val) {$result[$key]['userid'] = $key;$result[$key]['sum_amount'] = array_sum($val['self']);$result[$key]['sum_p_num'] = count($val['self']);if(isset($val['son']) && $val['son']){foreach ($val['son'] as $ks=>$vs){$result[$key]['sum_amount'] += array_sum($vs)*self::SON;$result[$key]['sum_p_num'] += count($vs);}}if(isset($val['g_son']) && $val['g_son']){foreach ($val['g_son'] as $k=>$v) {$result[$key]['sum_amount'] += array_sum($v)*self::G_SON;$result[$key]['sum_p_num'] += count($v);}}$result[$key]['sum_amount'] = number_format($result[$key]['sum_amount'], 2);}return $result;}/*** 链式储值 - 算法*/private function calculator(){$this->load->library('dbredis');$point = $this->dbredis->get('link_select_time');if(!$point){$point = date('Y-m-d H:i:s');$this->dbredis->set('link_select_time', $point, 30*60);}$invite_data = $this->recharge_dao->get_invite_data($point);if(!$invite_data) return [];$res = $invite_data;$data = [];$pid_arr = array_column($invite_data, 'invite_p_id');$u_pid_arr = array_unique($pid_arr);foreach ($pid_arr as $key=>$val) {foreach ($u_pid_arr as $k=>$v){if(in_array($val,[$v])){$data[$v][] = $key;}}}$arr = [];foreach ($data as $key=>$val) {foreach ($val as $k=>$v) {$arr[$key][$k]['invite_id'] = $res[$v]['invite_id'];$arr[$key][$k]['invite_p_id'] = $res[$v]['invite_p_id'];$arr[$key][$k]['userid'] = $res[$v]['userid'];$arr[$key][$k]['invite_get'] = $res[$v]['invite_get'];}}$result = [];foreach($invite_data as $key=>$val) {$result[$val['userid']]['self'][] = $val['invite_get'];if(isset($arr[$val['invite_id']])){$result[$val['userid']]['son'][] = array_column($arr[$val['invite_id']], 'invite_get');}if(isset($arr[$val['invite_id']])){foreach ($arr[$val['invite_id']] as $k=>$v) {if(isset($arr[$v['invite_id']])){$g_arr = array_column($arr[$v['invite_id']], 'invite_get');$result[$val['userid']]['g_son'][$k] = $g_arr;}}}}return $result;}
计算数据
1、初步汇总数据array(6) {[2]=>array(3) {["self"]=>array(1) {[0]=>string(5) "86.80"}["son"]=>array(1) {[0]=>array(4) {[0]=>string(5) "86.80"[1]=>string(6) "296.80"[2]=>string(4) "6.60"[3]=>string(5) "86.80"}}["g_son"]=>array(3) {[0]=>array(1) {[0]=>string(4) "6.60"}[1]=>array(1) {[0]=>string(5) "96.90"}[2]=>array(1) {[0]=>string(6) "296.80"}}}[50236]=>array(2) {["self"]=>array(2) {[0]=>string(5) "86.80"[1]=>string(6) "296.80"}["son"]=>array(2) {[0]=>array(1) {[0]=>string(4) "6.60"}[1]=>array(1) {[0]=>string(5) "96.90"}}}[50133]=>array(1) {["self"]=>array(1) {[0]=>string(4) "6.60"}}[50089]=>array(2) {["self"]=>array(2) {[0]=>string(4) "6.60"[1]=>string(5) "86.80"}["son"]=>array(1) {[0]=>array(1) {[0]=>string(6) "296.80"}}}[50080]=>array(1) {["self"]=>array(1) {[0]=>string(6) "296.80"}}[27]=>array(1) {["self"]=>array(1) {[0]=>string(5) "96.90"}}
}2、最终计算数据array(6) {[2]=>array(3) {["userid"]=>int(2)["sum_amount"]=>string(6) "138.50"["sum_p_num"]=>int(8)}[50236]=>array(3) {["userid"]=>int(50236)["sum_amount"]=>string(6) "393.95"["sum_p_num"]=>int(4)}[50133]=>array(3) {["userid"]=>int(50133)["sum_amount"]=>string(4) "6.60"["sum_p_num"]=>int(1)}[50089]=>array(3) {["userid"]=>int(50089)["sum_amount"]=>string(6) "123.08"["sum_p_num"]=>int(3)}[50080]=>array(3) {["userid"]=>int(50080)["sum_amount"]=>string(6) "296.80"["sum_p_num"]=>int(1)}[27]=>array(3) {["userid"]=>int(27)["sum_amount"]=>string(5) "96.90"["sum_p_num"]=>int(1)}
}
最终结果
这篇关于PHP之 链式邀请储值三级邀请所得计算算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!