1734 lines
86 KiB
PHP
1734 lines
86 KiB
PHP
<?php
|
|
|
|
namespace app\api\model;
|
|
|
|
use think\Model;
|
|
use think\db;
|
|
|
|
class BoxTen extends Model
|
|
{
|
|
|
|
//开奖池
|
|
|
|
|
|
|
|
//公池
|
|
public function open_common_box($uid, $rid, $type, $open_num)
|
|
{
|
|
|
|
$open_num = input('open_num', 0);
|
|
|
|
if (!in_array($open_num, [10])) {
|
|
return ['code' => 201, 'msg' => '开奖次数参数非法', 'data' => null];
|
|
}
|
|
$config = get_system_config();
|
|
$map = [];
|
|
$map['rid'] = $rid;
|
|
$map['is_delete'] = 1;
|
|
$room_info = db::name('room')->field('rid,is_auto_accumulate,room_owner_uid')->where($map)->find();
|
|
if (empty($room_info)) {
|
|
return ['code' => 201, 'msg' => '房间信息不存在', 'data' => null];
|
|
}
|
|
|
|
$redis = connectionRedis();
|
|
$user_gift_id_list = []; //用户抽中礼物id集合
|
|
$user_gift_id_redis_list = []; //用户抽中redis礼物id集合
|
|
$user_win_gift_data = []; //抽中礼物列表
|
|
$box_log_data = []; //抽中礼物期数数据
|
|
$box_type_data = []; //宝箱类型配置信息
|
|
$box_logs_data = [];
|
|
$user_pay_amount = 0; //用户支付总额
|
|
$user_win_gift_total_value = 0; //抽中礼物总价值
|
|
$surplus_user_money = 0; //用户剩余资金
|
|
$europe_key = 0;//用户所得皇钥匙
|
|
$now_time = time();
|
|
$tid = '';
|
|
|
|
$user_info = db::name('user')->find($uid); //获取用户信息
|
|
if(!$user_info){
|
|
return ['code' => 201, 'msg' => '用户信息不存在', 'data' => null];
|
|
}
|
|
//不同宝箱类型
|
|
//判断是否是虚拟用户
|
|
if($user_info['is_tester']==2){
|
|
if($type == 1){
|
|
$tid = 51;
|
|
}else if($type == 2){
|
|
$tid = 52;
|
|
}else if($type == 3){
|
|
$tid = 53;
|
|
}else if($type == 4){
|
|
$tid = 54;
|
|
}else if($type == 5){
|
|
$tid = 55;
|
|
}
|
|
}else{
|
|
if($type == 1){
|
|
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
|
$silver_tid = [2];
|
|
$map = [];
|
|
$map[] = ['tid','in',$silver_tid];
|
|
$box_type_lists = db::name('box_type')->where($map)->field('tid,consume')->order('consume desc')->select();
|
|
foreach ($box_type_lists as $a => $b) {
|
|
if($user_info['silver_consume'] >= $b['consume']){
|
|
$tid = $b['tid'];
|
|
break;
|
|
}
|
|
}
|
|
}else if($type == 2){
|
|
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
|
// if($user_info['gold_tid'] == 0){
|
|
// $tid = 13;
|
|
// }else{
|
|
// $tid = $user_info['gold_tid'];
|
|
// }
|
|
$gold_tid = [12];
|
|
$map = [];
|
|
$map[] = ['tid','in',$gold_tid];
|
|
$box_type_lists = db::name('box_type')->where($map)->field('tid,consume')->order('consume desc')->select();
|
|
foreach ($box_type_lists as $a => $b) {
|
|
if($user_info['gold_consume'] >= $b['consume']){
|
|
$tid = $b['tid'];
|
|
break;
|
|
}
|
|
}
|
|
// $where = [];
|
|
// $where[] = ['tid', 'in', [13,14,15]];
|
|
// $box_log_datas = db::name('box_log')->where($where)->field('bl_id,tid')->select();
|
|
// foreach ($box_log_datas as $c => &$d){
|
|
// $box_logs_data[$d['bl_id']] = $d;
|
|
// }
|
|
|
|
// $tid = 13;
|
|
}else if($type == 3){
|
|
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
|
$drill_tid = [22];
|
|
$map = [];
|
|
$map[] = ['tid','in',$drill_tid];
|
|
$box_type_lists = db::name('box_type')->where($map)->field('tid,consume')->order('consume desc')->select();
|
|
foreach ($box_type_lists as $a => $b) {
|
|
if($user_info['drill_consume'] >= $b['consume']){
|
|
$tid = $b['tid'];
|
|
break;
|
|
}
|
|
}
|
|
}else if($type == 4){
|
|
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
|
$drill_tid = [52];
|
|
$map = [];
|
|
$map[] = ['tid','in',$drill_tid];
|
|
$box_type_lists = db::name('box_type')->where($map)->field('tid,consume')->order('consume desc')->select();
|
|
foreach ($box_type_lists as $a => $b) {
|
|
if($user_info['platina_consume'] >= $b['consume']){
|
|
$tid = $b['tid'];
|
|
break;
|
|
}
|
|
}
|
|
}else if($type == 5){
|
|
|
|
$drill_tid = [42];
|
|
$map = [];
|
|
$map[] = ['tid','in',$drill_tid];
|
|
$box_type_lists = db::name('box_type')->where($map)->field('tid,consume')->order('consume desc')->select();
|
|
foreach ($box_type_lists as $a => $b) {
|
|
if($user_info['five_consume'] >= $b['consume']){
|
|
$tid = $b['tid'];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//该奖池是否配置礼物
|
|
$map = [];
|
|
$map[] = ['tid', '=', $tid];
|
|
$is_box_config = db('box_config')->where($map)->select();
|
|
if(!$is_box_config){
|
|
return ['code' => 201, 'msg' => '奖池数量不足', 'data' => null];
|
|
}
|
|
|
|
//获取宝箱类型配置信息
|
|
$box_type_list = db::name('box_type')->field('tid,open_price,open_status,show_name,consume')->cache(10)->select();
|
|
foreach ($box_type_list as $k => &$v) {
|
|
$box_type_data[$v['tid']] = $v;
|
|
}
|
|
if (empty($box_type_data[$tid])) {
|
|
return ['code' => 201, 'msg' => '开奖类型非法参数', 'data' => null];
|
|
}
|
|
if($type != 2 && $user_info['is_tester'] != 1){
|
|
if ($box_type_data[$tid]['open_status'] != 1) {
|
|
return ['code' => 201, 'msg' => '该开奖类型暂时已关闭', 'data' => null];
|
|
}
|
|
}
|
|
|
|
$user_pay_amount = $open_num * $box_type_data[$tid]['open_price'];
|
|
$tid_box_price = $box_type_data[$tid]['open_price'];
|
|
$tid_box_consume = $box_type_data[$tid]['consume'];
|
|
$keyname = "box:list:tid:".$tid;
|
|
|
|
if ($keyname == "box:list:tid:") {
|
|
return ['code' => 201, 'msg' => '开奖类型非法参数', 'data' => null];
|
|
}
|
|
// dump($tid);
|
|
// 启动事务
|
|
Db::startTrans();
|
|
try {
|
|
//获取用户必中信息 (每次补发一个)
|
|
$map = [];
|
|
$map[] = ['uid', '=', $uid];
|
|
// if($type == 2 && $user_info['is_tester'] == 1){
|
|
// $map[] = ['tid', 'in', [13,14,15]];
|
|
// }else{
|
|
$map[] = ['tid', '=', $tid];
|
|
// }
|
|
$map[] = ['is_admin', '=', 1];
|
|
$map[] = ['give_status', '=', 1];
|
|
$box_give_gift_info = db::name('box_give_gift')->where($map)->find();
|
|
//插入用户必中
|
|
$need_open_num = $open_num;
|
|
if (!empty($box_give_gift_info)) {
|
|
$data = [];
|
|
$data['bl_id'] = 0;
|
|
$data['bl_id_tid'] = $box_give_gift_info['tid'];
|
|
$data['gid'] = $box_give_gift_info['gid'];
|
|
$user_gift_id_list[] = $data;
|
|
$need_open_num--; //待取礼物总数-1
|
|
}
|
|
|
|
//获取待核销必中礼物数据
|
|
$map = [];
|
|
// $map[] = ['tid', '=', $tid];
|
|
// if($type == 2 && $user_info['is_tester'] == 1){
|
|
// $map[] = ['tid', 'in', [13,14,15]];
|
|
// }else{
|
|
$map[] = ['tid', '=', $tid];
|
|
// }
|
|
$map[] = ['give_status', '=', 2];
|
|
$map[] = ['deduction_status', '=', 1];
|
|
$box_un_deduction_give_gift_list = db::name('box_give_gift')->where($map)->order('id asc')->column('id', 'gid'); //每种礼物只核销第一个
|
|
|
|
//获取待开奖池礼物
|
|
for ($i = 0; $i < $need_open_num; $i++) {
|
|
$gift_data = $redis->blpop($keyname, 2); //1s 超时
|
|
if (empty($gift_data)) {
|
|
return ['code' => 201, 'msg' => '奖池数量不足', 'data' => null];
|
|
} else {
|
|
//获取是否有待核销必中
|
|
$gift_data_data = explode('-', $gift_data[1]);
|
|
$bl_id = $gift_data_data[0];
|
|
$gid = $gift_data_data[1];
|
|
if (!empty($box_un_deduction_give_gift_list[$gid])) {
|
|
$id = $box_un_deduction_give_gift_list[$gid];
|
|
unset($box_un_deduction_give_gift_list[$gid]);
|
|
|
|
$data = [];
|
|
$data['bl_id'] = $bl_id;
|
|
$data['gid'] = $gid;
|
|
$user_gift_id_redis_list[] = $data;
|
|
|
|
//更新对应数据
|
|
//补偿表 更新
|
|
$map = [];
|
|
$map[] = ['id', '=', $id];
|
|
// if($type == 2 && $user_info['is_tester'] == 1){
|
|
// $map[] = ['tid', 'in', [13,14,15]];
|
|
// }else{
|
|
$map[] = ['tid', '=', $tid];
|
|
// }
|
|
$map[] = ['gid', '=', $gid];
|
|
$map[] = ['deduction_status', '=', 1];
|
|
$data = [];
|
|
$data['deduction_bl_id'] = $bl_id;
|
|
$data['deduction_status'] = 2;
|
|
$data['deduction_time'] = time();
|
|
$data['update_time'] = time();
|
|
$reslut = db::name('box_give_gift')->where($map)->order('id asc')->limit(1)->update($data);
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '请重试', 'data' => null];
|
|
}
|
|
|
|
//开宝箱记录表更新
|
|
$map = [];
|
|
// if($type == 2 && $user_info['is_tester'] == 1){
|
|
// $map[] = ['tid', 'in', [13,14,15]];
|
|
// }else{
|
|
$map[] = ['tid', '=', $tid];
|
|
// }
|
|
$map[] = ['gid', '=', $gid];
|
|
$map[] = ['num', '=', 1];
|
|
$map[] = ['bl_id', '=', 0];
|
|
$data = [];
|
|
$data['bl_id'] = $bl_id;
|
|
$data['update_time'] = time();
|
|
$reslut = db::name('user_box_log')->where($map)->order('aid asc')->limit(1)->update($data);
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '请重试', 'data' => null];
|
|
}
|
|
//重新从队列获取一个礼物信息
|
|
$gift_data = $redis->blpop($keyname, 2); //1s 超时
|
|
if (empty($gift_data)) {
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '奖池数量不足', 'data' => null];
|
|
}
|
|
|
|
$gift_data_data = explode('-', $gift_data[1]);
|
|
$bl_id = $gift_data_data[0];
|
|
$gid = $gift_data_data[1];
|
|
}
|
|
$data = [];
|
|
$data['bl_id'] = $bl_id;
|
|
$data['gid'] = $gid;
|
|
// if($type == 2 && $user_info['is_tester'] == 1){
|
|
// // $data['bl_id_tid'] = db::name('box_log')->where('bl_id', $bl_id)->value('tid');
|
|
// $data['bl_id_tid'] = $box_logs_data[$bl_id]['tid'];
|
|
// }else{
|
|
$data['bl_id_tid'] = $tid;
|
|
// }
|
|
$user_gift_id_redis_list[] = $data;
|
|
$user_gift_id_list[] = $data;
|
|
}
|
|
}
|
|
|
|
if (count($user_gift_id_list) != $open_num) {
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '奖池数量不足', 'data' => null];
|
|
}
|
|
|
|
//取出所有礼物数据
|
|
$gift_list = db::name('gift')->field('gid,gift_name,gift_price,base_image,play_image,gift_type')->cache(50)->select();
|
|
$gift_list_data = [];
|
|
foreach ($gift_list as $k => &$v) {
|
|
$v['base_image'] = localpath_to_netpath($v['base_image']);
|
|
$v['play_image'] = localpath_to_netpath($v['play_image']);
|
|
$gift_list_data[$v['gid']] = $v;
|
|
}
|
|
|
|
//获取宝箱礼物配置信息
|
|
$box_config_list = db::name('box_config')->field('gid,tid,is_public_screen,is_public_server')->cache(50)->select();
|
|
$box_config_list_data = [];
|
|
foreach ($box_config_list as $k => $v) {
|
|
$box_config_list_data[$v['gid']][$v['tid']] = $v;
|
|
}
|
|
|
|
|
|
//整理用户抽中礼物数量
|
|
foreach ($user_gift_id_list as $k => $v) {
|
|
if (empty($gift_list_data[$v['gid']])) {
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '礼物信息不存在', 'data' => null];
|
|
}
|
|
|
|
if (empty($box_config_list_data[$v['gid']])) {
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '宝箱礼物信息不存在', 'data' => null];
|
|
}
|
|
|
|
|
|
if (empty($user_win_gift_data[$v['gid']])) {
|
|
$user_win_gift_data[$v['gid']] = $gift_list_data[$v['gid']];
|
|
$user_win_gift_data[$v['gid']]['open_num'] = 1;
|
|
} else {
|
|
$user_win_gift_data[$v['gid']]['open_num']++;
|
|
}
|
|
$user_win_gift_data[$v['gid']]['bl_id'] = $v['bl_id'];
|
|
$user_win_gift_data[$v['gid']]['bl_id_tid'] = $v['bl_id_tid'];
|
|
// $user_win_gift_data[$v['gid']]['is_public_screen'] = $box_config_list_data[$v['gid']][$tid]['is_public_screen'];
|
|
// $user_win_gift_data[$v['gid']]['is_public_server'] = $box_config_list_data[$v['gid']][$tid]['is_public_server'];
|
|
if($v['bl_id'] == 0){
|
|
$user_win_gift_data[$v['gid']]['is_public_screen'] = $box_config_list_data[$v['gid']][$v['bl_id_tid']]['is_public_screen'];
|
|
$user_win_gift_data[$v['gid']]['is_public_server'] = $box_config_list_data[$v['gid']][$v['bl_id_tid']]['is_public_server'];
|
|
}else{
|
|
$map = [];
|
|
$map[] = ['bl_id', '=', $v['bl_id']];
|
|
$bl_tid = db::name('box_log')->where($map)->value('tid');
|
|
$user_win_gift_data[$v['gid']]['is_public_screen'] = $box_config_list_data[$v['gid']][$bl_tid]['is_public_screen'];
|
|
$user_win_gift_data[$v['gid']]['is_public_server'] = $box_config_list_data[$v['gid']][$bl_tid]['is_public_server'];
|
|
}
|
|
|
|
if (empty($box_log_data[$v['bl_id']])) {
|
|
$box_log_data[$v['bl_id']]['open_num'] = 1;
|
|
$box_log_data[$v['bl_id']]['open_gift_value'] = $gift_list_data[$v['gid']]['gift_price'];
|
|
} else {
|
|
$box_log_data[$v['bl_id']]['open_num']++;
|
|
$box_log_data[$v['bl_id']]['open_gift_value'] += $gift_list_data[$v['gid']]['gift_price'];
|
|
}
|
|
$user_win_gift_total_value += $gift_list_data[$v['gid']]['gift_price'];
|
|
}
|
|
|
|
|
|
if($user_info['is_tester'] == 1){
|
|
//扣除账户积分
|
|
$reslut = model('admin/User')->change_user_money_by_user_info($user_info, -$user_pay_amount, 2, 16, "开宝箱抽礼物", $user_info['uid'], 0);
|
|
if ($reslut['code'] != 200) {
|
|
Db::rollback();
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => $reslut['msg'], 'data' => null];
|
|
}
|
|
$surplus_user_money = $user_info['integral'] - $user_pay_amount;
|
|
}else{
|
|
$surplus_user_money = $user_info['integral'];
|
|
}
|
|
|
|
foreach ($box_log_data as $k => $v) {
|
|
if ($k > 0) {
|
|
$map = [];
|
|
$map[] = ['bl_id', '=', $k];
|
|
$reslut = db::name('box_log')->where($map)->inc('open_num', $v['open_num'])->inc('open_gift_value', $v['open_gift_value'])->update();
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '请重试', 'data' => null];
|
|
}
|
|
}
|
|
}
|
|
|
|
//用户必中礼物状态更新
|
|
if (!empty($box_give_gift_info)) {
|
|
$map = [];
|
|
$map[] = ['id', '=', $box_give_gift_info['id']];
|
|
$data = [];
|
|
$data['give_time'] = $now_time;
|
|
$data['give_status'] = 2;
|
|
$data['update_time'] = $now_time;
|
|
$reslut = db::name('box_give_gift')->where($map)->update($data);
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '请重试', 'data' => null];
|
|
}
|
|
}
|
|
|
|
|
|
//添加用户开宝箱日志 可能存在2期奖情况 特殊处理
|
|
$user_box_data_by_bl_id = [];
|
|
foreach ($user_gift_id_list as $k => $v) {
|
|
if (empty($user_box_data_by_bl_id[$v['bl_id']][$v['gid']])) {
|
|
$user_box_data_by_bl_id[$v['bl_id']][$v['gid']]['bl_id_tid'] = $v['bl_id_tid'];
|
|
$user_box_data_by_bl_id[$v['bl_id']][$v['gid']]['bl_id'] = $v['bl_id'];
|
|
$user_box_data_by_bl_id[$v['bl_id']][$v['gid']]['gid'] = $v['gid'];
|
|
$user_box_data_by_bl_id[$v['bl_id']][$v['gid']]['open_num'] = 1;
|
|
} else {
|
|
$user_box_data_by_bl_id[$v['bl_id']][$v['gid']]['open_num']++;
|
|
}
|
|
}
|
|
|
|
foreach ($user_box_data_by_bl_id as $k => $v) {
|
|
$insert_user_box_data = [];
|
|
foreach ($v as $m => $n) {
|
|
$gift_info = $gift_list_data[$n['gid']];
|
|
$data = [];
|
|
$data['uid'] = $user_info['uid'];
|
|
$data['rid'] = $rid;
|
|
// if($type == 2 && $user_info['is_tester'] == 1){
|
|
// $data['tid'] = $n['bl_id_tid'];
|
|
// }else{
|
|
$data['tid'] = $tid;
|
|
// }
|
|
$data['bl_id'] = $n['bl_id'];
|
|
$data['gid'] = $n['gid'];
|
|
$data['num'] = $n['open_num'];
|
|
$data['gift_price'] = $gift_info['gift_price'];
|
|
$data['total_price'] = $n['open_num'] * $gift_info['gift_price'];
|
|
$data['total_open_num'] = $open_num;
|
|
$data['total_pay_price'] = $n['open_num'] * $tid_box_price;
|
|
$data['add_time'] = $now_time;
|
|
$data['update_time'] = $now_time;
|
|
$insert_user_box_data[] = $data;
|
|
}
|
|
$reslut = db::name('user_box_log')->insertAll($insert_user_box_data);
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '请重试', 'data' => null];
|
|
}
|
|
}
|
|
|
|
//添加用户开宝箱统计数据
|
|
$time = strtotime(date("Y-m-d"));
|
|
$box_type = 0;
|
|
$map = [];
|
|
$map[] = ['uid', '=', $user_info['uid']];
|
|
if ($user_info['is_tester'] == 2) {
|
|
if($type == 1){
|
|
$map[] = ['box_type','=',6];
|
|
$box_type = 6;
|
|
}else if($type == 2){
|
|
$map[] = ['box_type','=',7];
|
|
$box_type = 7;
|
|
}else if($type == 3){
|
|
$map[] = ['box_type','=',8];
|
|
$box_type = 8;
|
|
}else if($type == 4){
|
|
$map[] = ['box_type','=',9];
|
|
$box_type = 9;
|
|
}else if($type == 5){
|
|
$map[] = ['box_type','=',10];
|
|
$box_type = 10;
|
|
}
|
|
} else {
|
|
$map[] = ['box_type', '=', $type];
|
|
$box_type = $type;
|
|
}
|
|
$map[] = ['add_time', '>', $time];
|
|
$user_box_count_info = db::name('user_box_count')->where($map)->find();
|
|
|
|
if (!empty($user_box_count_info)) {
|
|
$rate = 0; //支收比
|
|
$rate = round(($user_win_gift_total_value + $user_box_count_info['in_amount']) / ($user_box_count_info['out_amount'] + $user_pay_amount), 4);
|
|
$map = [];
|
|
$map[] = ['sid', '=', $user_box_count_info['sid']];
|
|
// $map[] = ['uid', '=', $user_info['uid']];
|
|
// $map[] = ['box_type', '=', $box_type];
|
|
$data = [];
|
|
$data['rate'] = $rate;
|
|
$data['update_time'] = $now_time;
|
|
$profit = $user_win_gift_total_value - $user_pay_amount;
|
|
$reslut = db::name('user_box_count')->where($map)->inc('out_amount', $user_pay_amount)->inc('in_amount', $user_win_gift_total_value)->inc('profit', $profit)->update($data);
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '请重试', 'data' => null];
|
|
}
|
|
} else {
|
|
$rate = round($user_win_gift_total_value / $user_pay_amount, 4);
|
|
$profit = $user_win_gift_total_value - $user_pay_amount;
|
|
$data = [];
|
|
$data['uid'] = $user_info['uid'];
|
|
$data['out_amount'] = $user_pay_amount;
|
|
$data['in_amount'] = $user_win_gift_total_value;
|
|
$data['profit'] = $profit;
|
|
$data['rate'] = $rate;
|
|
$data['box_type'] = $box_type;
|
|
$data['add_time'] = $now_time;
|
|
$data['update_time'] = $now_time;
|
|
$reslut = db::name('user_box_count')->insert($data);
|
|
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '请重试', 'data' => null];
|
|
}
|
|
}
|
|
|
|
//添加房间开宝箱统计数据
|
|
$time = strtotime(date("Y-m-d"));
|
|
$box_type = 0;
|
|
$map = [];
|
|
$map[] = ['rid', '=', $rid];
|
|
if ($user_info['is_tester'] == 2) {
|
|
if($type == 1){
|
|
$map[] = ['box_type','=',6];
|
|
$box_type = 6;
|
|
}else if($type == 2){
|
|
$map[] = ['box_type','=',7];
|
|
$box_type = 7;
|
|
}else if($type == 3){
|
|
$map[] = ['box_type','=',8];
|
|
$box_type = 8;
|
|
}else if($type == 4){
|
|
$map[] = ['box_type','=',9];
|
|
$box_type = 9;
|
|
}else if($type == 5){
|
|
$map[] = ['box_type','=',10];
|
|
$box_type = 10;
|
|
}
|
|
} else {
|
|
$map[] = ['box_type', '=', $type];
|
|
$box_type = $type;
|
|
}
|
|
$map[] = ['add_time', '>', $time];
|
|
$room_box_count_info = db::name('room_box_count')->where($map)->find();
|
|
|
|
if (!empty($room_box_count_info)) {
|
|
$rate = 0; //支收比
|
|
$rate = round(($user_win_gift_total_value + $room_box_count_info['in_amount']) / ($room_box_count_info['out_amount'] + $user_pay_amount), 4);
|
|
$map = [];
|
|
$map[] = ['sid', '=', $room_box_count_info['sid']];
|
|
// $map[] = ['uid', '=', $user_info['uid']];
|
|
// $map[] = ['box_type', '=', $box_type];
|
|
$data = [];
|
|
$data['rate'] = $rate;
|
|
$data['update_time'] = $now_time;
|
|
$profit = $user_win_gift_total_value - $user_pay_amount;
|
|
$reslut = db::name('room_box_count')->where($map)->inc('out_amount', $user_pay_amount)->inc('in_amount', $user_win_gift_total_value)->inc('profit', $profit)->update($data);
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '请重试', 'data' => null];
|
|
}
|
|
} else {
|
|
$rate = round($user_win_gift_total_value / $user_pay_amount, 4);
|
|
$profit = $user_win_gift_total_value - $user_pay_amount;
|
|
$data = [];
|
|
$data['rid'] = $rid;
|
|
$data['out_amount'] = $user_pay_amount;
|
|
$data['in_amount'] = $user_win_gift_total_value;
|
|
$data['profit'] = $profit;
|
|
$data['rate'] = $rate;
|
|
$data['box_type'] = $box_type;
|
|
$data['add_time'] = $now_time;
|
|
$data['update_time'] = $now_time;
|
|
$reslut = db::name('room_box_count')->insert($data);
|
|
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '请重试', 'data' => null];
|
|
}
|
|
}
|
|
|
|
|
|
//礼物放入背包
|
|
$insert_user_gift_pack_log_data = [];
|
|
foreach ($user_win_gift_data as $k => $v) {
|
|
$map = [];
|
|
$map[] = ['uid', '=', $user_info['uid']];
|
|
$map[] = ['gid', '=', $v['gid']];
|
|
$data = [];
|
|
$data['gid'] = $v['gid'];
|
|
$data['update_time'] = $now_time;
|
|
$reslut = db::name('user_gift_pack')->where($map)->inc('num', $v['open_num'])->inc('total_num', $v['open_num'])->update($data);
|
|
if (!$reslut) {
|
|
$data = [];
|
|
$data['uid'] = $user_info['uid'];
|
|
$data['gid'] = $v['gid'];
|
|
$data['total_num'] = $v['open_num'];
|
|
$data['num'] = $v['open_num'];
|
|
$data['is_tester'] = $user_info['is_tester'];
|
|
$data['add_time'] = $now_time;
|
|
$data['update_time'] = $now_time;
|
|
$reslut = db::name('user_gift_pack')->insert($data);
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '请重试', 'data' => null];
|
|
}
|
|
}
|
|
|
|
$data = [];
|
|
$data['uid'] = $user_info['uid'];
|
|
$data['type'] = 2;
|
|
$data['bl_id'] = $v['bl_id'];
|
|
$data['gid'] = $v['gid'];
|
|
$data['gift_price'] = $gift_list_data[$v['gid']]['gift_price'];
|
|
$data['change_num'] = $v['open_num'];
|
|
$data['total_gift_price'] = $gift_list_data[$v['gid']]['gift_price'] * $v['open_num'];
|
|
$data['after_num'] = 0;
|
|
$data['is_tester'] = $user_info['is_tester'];
|
|
$data['remarks'] = "开宝箱获得";
|
|
$data['add_time'] = $now_time;
|
|
$data['update_time'] = $now_time;
|
|
$insert_user_gift_pack_log_data[] = $data;
|
|
}
|
|
|
|
if (!empty($insert_user_gift_pack_log_data)) {
|
|
$reslut = db::name('user_gift_pack_log')->insertAll($insert_user_gift_pack_log_data);
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '请重试', 'data' => null];
|
|
}
|
|
}
|
|
|
|
//用户消费累计
|
|
if($user_info['is_tester'] != 2){
|
|
$map = [];
|
|
$map[] = ['uid', '=', $user_info['uid']];
|
|
if ($type == 1) {
|
|
$reslut = db::name('user')->where($map)->inc('silver_consume', $user_pay_amount)->update();
|
|
} else if($type == 2){
|
|
$reslut = db::name('user')->where($map)->inc('gold_consume', $user_pay_amount)->update();
|
|
} else if($type == 3) {
|
|
$reslut = db::name('user')->where($map)->inc('drill_consume', $user_pay_amount)->update();
|
|
} else if($type == 4){
|
|
$reslut = db::name('user')->where($map)->inc('platina_consume', $user_pay_amount)->update();
|
|
} else if($type == 5){
|
|
$reslut = db::name('user')->where($map)->inc('five_consume', $user_pay_amount)->update();
|
|
}
|
|
if(!$reslut){
|
|
Db::rollback();
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '请重试', 'data' => null];
|
|
}
|
|
}
|
|
|
|
|
|
// 提交事务
|
|
Db::commit();
|
|
|
|
//推送礼物信息
|
|
$user_charm_info = model('User')->get_user_charm_contribution_info($user_info['uid']);
|
|
$push_all_gift_data = [];
|
|
$push_group_gift_data = [];
|
|
$push_sqb_gift_data = [];
|
|
$push_all_gift_data['user_info']['uid'] = $user_info['uid'];
|
|
$push_all_gift_data['user_info']['nick_name'] = mb_convert_encoding(base64_decode($user_info['base64_nick_name']), 'UTF-8', 'UTF-8');
|
|
$push_all_gift_data['user_info']['head_pic'] = localpath_to_netpath($user_info['head_pic']);
|
|
$push_all_gift_data['user_info']['contribution_level_image'] = $user_charm_info['data']['contribution_level_image'];
|
|
$push_group_gift_data['user_info']['uid'] = $user_info['uid'];
|
|
$push_group_gift_data['user_info']['nick_name'] = mb_convert_encoding(base64_decode($user_info['base64_nick_name']), 'UTF-8', 'UTF-8');
|
|
$push_group_gift_data['user_info']['head_pic'] = localpath_to_netpath($user_info['head_pic']);
|
|
$push_group_gift_data['user_info']['contribution_level_image'] = $user_charm_info['data']['contribution_level_image'];
|
|
// dump($user_win_gift_data);
|
|
foreach ($user_win_gift_data as $k => &$v) {
|
|
//是否大礼物展示
|
|
$v['surprise'] = 0;
|
|
if($v['gift_price'] > $config['big_gift_price']){
|
|
$v['surprise'] = 1;
|
|
}
|
|
unset($v['bl_id']);
|
|
$v['type_name'] = $box_type_data[$v['bl_id_tid']]['show_name'];
|
|
$v['box_type'] = $v['bl_id_tid'];
|
|
if ($v['is_public_server'] == 1) {
|
|
if($v['gift_price'] > $config['sqb_limit_gift_price']){
|
|
$sqb_gift_info = $v;
|
|
|
|
$sqb_gift_info['uid'] = $user_info['uid'];
|
|
$sqb_gift_info['nick_name'] = mb_convert_encoding(base64_decode($user_info['base64_nick_name']), 'UTF-8', 'UTF-8');
|
|
$sqb_gift_info['head_pic'] = localpath_to_netpath($user_info['head_pic']);
|
|
$sqb_gift_info['num'] = $v['open_num'];
|
|
$sqb_gift_info['total_open_num'] = $open_num;
|
|
$sqb_gift_info['box_type_name'] = $v['type_name'];
|
|
$push_all_gift_data['gift_list'][] = $sqb_gift_info;
|
|
}
|
|
}
|
|
if ($v['is_public_screen'] == 1) {
|
|
$push_group_gift_data['gift_list'][] = $v;
|
|
}
|
|
}
|
|
|
|
if (!empty($push_all_gift_data['gift_list'])) {
|
|
$push_data = [];
|
|
$push_data['code'] = 304;
|
|
$push_data['msg'] = "开宝箱全服播报数据";
|
|
$push_data['data'] = $push_all_gift_data;
|
|
model('api/WebSocketPush')->send_to_all($push_data);
|
|
}else if (!empty($push_group_gift_data['gift_list'])) {
|
|
$push_data = [];
|
|
$push_data['code'] = 303;
|
|
$push_data['msg'] = "开宝箱房间播报数据";
|
|
$push_data['data'] = $push_group_gift_data;
|
|
model('api/WebSocketPush')->send_to_group($rid, $push_data);
|
|
}
|
|
|
|
$data = [];
|
|
$data['win_gift_list'] = array_values($user_win_gift_data);
|
|
$data['user_win_gift_total_value'] = $user_win_gift_total_value;
|
|
$data['user_money'] = $surplus_user_money;
|
|
return ['code' => 200, 'msg' => '开启成功', 'data' => $data];
|
|
//推送礼物
|
|
} catch (\Exception $e) {
|
|
// 回滚事务
|
|
dump($e);
|
|
$this->redis_gift_data_rollback($redis, $keyname, xxx);
|
|
Db::rollback();
|
|
return ['code' => 201, 'msg' => '开启失败', 'data' => null];
|
|
}
|
|
}
|
|
|
|
//返还用户抽中礼物到奖池
|
|
private function redis_gift_data_rollback($redis, $keyname, $user_gift_id_list)
|
|
{
|
|
$insert_data = [];
|
|
foreach ($user_gift_id_list as $k => $v) {
|
|
$insert_data[] = $v['bl_id'] . '-' . $v['gid'];
|
|
}
|
|
if (!empty($insert_data)) {
|
|
array_unshift($insert_data, $keyname);
|
|
//右侧取 右侧插入
|
|
call_user_func_array([$redis, 'lPush'], $insert_data);
|
|
}
|
|
return ['code' => 200, 'msg' => '返还成功', 'data' => null];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//欧皇奖池
|
|
|
|
|
|
|
|
//个人回血奖池
|
|
public function open_person_repair_rate_box($uid, $rid, $type, $open_num)
|
|
{
|
|
|
|
$open_num = input('open_num', 0);
|
|
|
|
if (!in_array($open_num, [10])) {
|
|
return ['code' => 201, 'msg' => '开奖次数参数非法', 'data' => null];
|
|
}
|
|
|
|
$config = get_system_config();
|
|
|
|
|
|
$user_gift_id_list = []; //用户抽中礼物id集合
|
|
$user_gift_id_redis_list = []; //用户抽中redis礼物id集合
|
|
$user_win_gift_data = []; //抽中礼物列表
|
|
$box_log_data = []; //抽中礼物期数数据
|
|
$box_type_data = []; //宝箱类型配置信息
|
|
$user_pay_amount = 0; //用户支付总额
|
|
$user_win_gift_total_value = 0; //抽中礼物总价值
|
|
$surplus_user_money = 0; //用户剩余资金
|
|
$europe_key = 0; //用户所得欧皇钥匙
|
|
$now_time = time();
|
|
|
|
if($type == 1){
|
|
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
|
$consume_name = 'silver_consume';
|
|
$regain_name = 'silver_regain_num';
|
|
$regain_node = 'silver_regain_node';
|
|
$box_price = db::name('box_type')->where('tid',4)->value('open_price');
|
|
$tid = 4;
|
|
}else if($type == 2){
|
|
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
|
$consume_name = 'gold_consume';
|
|
$regain_name = 'gold_regain_num';
|
|
$regain_node = 'gold_regain_node';
|
|
$box_price = db::name('box_type')->where('tid',14)->value('open_price');
|
|
$tid = 14;
|
|
}else if($type == 3){
|
|
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
|
$consume_name = 'drill_consume';
|
|
$regain_name = 'drill_regain_num';
|
|
$regain_node = 'drill_regain_node';
|
|
$box_price = db::name('box_type')->where('tid',5)->value('open_price');
|
|
$tid = 6;
|
|
}else if($type == 4){
|
|
// return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
|
$consume_name = 'platina_consume';
|
|
$regain_name = 'platina_regain_num';
|
|
$regain_node = 'platina_regain_node';
|
|
$box_price = db::name('box_type')->where('tid',54)->value('open_price');
|
|
$tid = 54;
|
|
} else {
|
|
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
|
}
|
|
|
|
|
|
$tid_box_price = $box_price;
|
|
$user_pay_amount = $open_num * $box_price;
|
|
|
|
$user_info = db::name('user')->find($uid); //获取用户信息
|
|
if(!$user_info){
|
|
return ['code' => 201, 'msg' => '用户信息错误', 'data' => null];
|
|
}
|
|
|
|
$map = [];
|
|
$map['rid'] = $rid;
|
|
$map['is_delete'] = 1;
|
|
$room_info = db::name('room')->field('rid,is_auto_accumulate')->where($map)->find();
|
|
if (empty($room_info)) {
|
|
return ['code' => 201, 'msg' => '房间信息不存在', 'data' => null];
|
|
}
|
|
|
|
//个池数据是否充足
|
|
$redis = connectionRedis();
|
|
$gift_price_data = db::name('gift')->column('gift_price', 'gid'); //礼物价值
|
|
$map = [];
|
|
$map[] = ['tid', '=', $tid];
|
|
$box_config_lists = Db::name('box_config')->where($map)->select();
|
|
if(empty($box_config_lists)){
|
|
|
|
return ['code' => 201, 'msg' => '', 'data' => null];
|
|
}
|
|
$keyname = "box:list:tid:" . $tid . ':uid:' . $uid;
|
|
$list_len = 0;
|
|
// $del = $redis->del($keyname);
|
|
$list_len = $redis->llen($keyname);
|
|
$list_lens = $redis->lrange($keyname,0,-1);
|
|
// dump($list_lens);
|
|
// dump($list_lens);exit;
|
|
if($list_len < 1000){
|
|
$insert_data = [];
|
|
$total_gift_value = 0;
|
|
|
|
//获取0次开出的礼物
|
|
$where = [];
|
|
$where[] = ['tid', '=', $tid];
|
|
$where[] = ['open_num_obtain', '=', 0];
|
|
$box_config_zero_lists = db::name('box_config')->where($where)->select();
|
|
foreach ($box_config_zero_lists as $a => $b) {
|
|
//更新添加次数
|
|
for ($q = 0; $q < $b['num']; $q++) {
|
|
$insert_data[] = $b['gid'];
|
|
}
|
|
if (empty($gift_price_data[$b['gid']]) && $gift_price_data[$b['gid']] != 0) {
|
|
return ['code' => 201, 'msg' => '礼物id:' . $b['gid'] . '不存在', 'data' => ''];
|
|
}
|
|
$total_gift_value += $gift_price_data[$b['gid']] * $b['num'];
|
|
}
|
|
|
|
shuffle($insert_data);
|
|
//获取0次以上开出的礼物
|
|
$where = [];
|
|
$where[] = ['tid', '=', $tid];
|
|
$where[] = ['open_num_obtain', '>', 0];
|
|
$box_config_unzero_lists = db::name('box_config')->where($where)->order('open_num_obtain asc')->select();
|
|
foreach ($box_config_unzero_lists as $c => $d){
|
|
$da = [];
|
|
for ($i = 0; $i < $d['num']; $i++) {
|
|
$da[] = $d['gid'];
|
|
}
|
|
$total_gift_value += $gift_price_data[$d['gid']] * $d['num'];
|
|
|
|
$data_count = count($insert_data);
|
|
$box_open_num = $data_count - $d['open_num_obtain'];
|
|
$box_array = array_slice($insert_data,0,$d['open_num_obtain']);
|
|
$box_array_1 = array_slice($insert_data,-$box_open_num,$box_open_num);
|
|
$insert_data = array_merge($box_array_1,$da);
|
|
shuffle($insert_data);
|
|
$insert_data = array_merge($box_array,$insert_data);
|
|
|
|
}
|
|
$map = [];
|
|
$map[] = ['tid', '=', $tid];
|
|
$map[] = ['uid', '=', $uid];
|
|
$box_person_log = db::name('box_person_log')->where($map)->field('bl_id,total_num,total_gift_value')->find();
|
|
//插入期日志表
|
|
if(!empty($box_person_log)){
|
|
$bl_id = $box_person_log['bl_id'];
|
|
$data = [];
|
|
$data['total_num'] = count($insert_data) + $box_person_log['total_num'];
|
|
$data['total_gift_value'] = $total_gift_value + $box_person_log['total_gift_value'];
|
|
$data['update_time'] = time();
|
|
$reslut = db::name('box_person_log')->where(['bl_id'=>$box_person_log['bl_id']])->inc('term',1)->update($data);
|
|
if(!$reslut){
|
|
return ['code' => 201, 'msg' => '', 'data' => null];
|
|
}
|
|
}else{
|
|
$data = [];
|
|
$data['tid'] = $tid;
|
|
$data['uid'] = $uid;
|
|
$data['total_num'] = count($insert_data);
|
|
$data['total_gift_value'] = $total_gift_value;
|
|
$data['config_text'] = json_encode($box_config_lists);
|
|
$data['add_time'] = time();
|
|
$data['update_time'] = time();
|
|
$bl_id = db::name('box_person_log')->insertGetId($data);
|
|
if(!$bl_id){
|
|
return ['code' => 201, 'msg' => '', 'data' => null];
|
|
}
|
|
}
|
|
foreach ($insert_data as $p => $q) {
|
|
$insert_data[$p] = $bl_id . '-' . $insert_data[$p];
|
|
}
|
|
// shuffle($insert_data);
|
|
array_unshift($insert_data, $keyname);
|
|
call_user_func_array([$redis, 'rPush'], $insert_data);
|
|
|
|
}
|
|
//取出所有礼物数据
|
|
$gift_list = db::name('gift')->field('gid,gift_name,gift_price,base_image,play_image,gift_type')->cache(10)->select();
|
|
$gift_list_data = [];
|
|
foreach ($gift_list as $k => &$v) {
|
|
$v['base_image'] = localpath_to_netpath($v['base_image']);
|
|
$v['play_image'] = localpath_to_netpath($v['play_image']);
|
|
$gift_list_data[$v['gid']] = $v;
|
|
}
|
|
|
|
// echo 555;die;
|
|
Db::startTrans();
|
|
try {
|
|
$times = strtotime(date('Y-m-d'));
|
|
//回血机制
|
|
if($type == 1){
|
|
$regain = $config['silver_regain_config'];
|
|
$node = $config['silver_regain_rate_config'];
|
|
}else if($type == 2){
|
|
$regain = $config['gold_regain_config'];
|
|
$node = $config['gold_regain_rate_config'];
|
|
}else if($type == 3){
|
|
$regain = $config['drill_regain_config'];
|
|
$node = $config['drill_regain_rate_config'];
|
|
}else if($type == 4){
|
|
$regain = $config['platina_regain_config'];
|
|
$node = $config['platina_regain_rate_config'];
|
|
}
|
|
|
|
|
|
//获取用户必中信息 (每次补发一个)
|
|
$map = [];
|
|
$map[] = ['uid', '=', $uid];
|
|
$map[] = ['tid', '=', $tid];
|
|
$map[] = ['is_admin', '=', 1];
|
|
$map[] = ['give_status', '=', 1];
|
|
$box_give_gift_info = db::name('box_give_gift')->where($map)->find();
|
|
//插入用户必中
|
|
$need_open_num = $open_num;
|
|
|
|
if (!empty($box_give_gift_info)) {
|
|
// $data = [];
|
|
// $data['bl_id'] = 0;
|
|
// $data['gid'] = $box_give_gift_info['gid'];
|
|
// $user_gift_id_list[] = $data;
|
|
$need_open_num--; //待取礼物总数-1
|
|
|
|
if(!empty($regain)){
|
|
$regain = trim($regain,',');
|
|
$regain_list = explode(',',$regain);
|
|
$regain_value_list = [];
|
|
$regain_rate_list = [];
|
|
foreach ($regain_list as $k => $v) {
|
|
$v = explode(':',$v);
|
|
// dump($v);
|
|
$regain_value_list[] = $v[0];
|
|
$v[1] = explode('-', $v[1]);
|
|
$regain_rate_list[] = $v[1];
|
|
}
|
|
|
|
$regain_place = 0;
|
|
arsort($regain_value_list);
|
|
foreach ($regain_value_list as $x => $y) {
|
|
if($user_info[$consume_name] >= $y){
|
|
$regain_place = $x;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(isset($regain_value_list[$regain_place]) && isset($regain_rate_list[$regain_place])){
|
|
$regain_value = $regain_value_list[$regain_place];
|
|
$regain_rate = $regain_rate_list[$regain_place];
|
|
|
|
//当前奖池爆率
|
|
$where = [];
|
|
$where[] = ['uid', '=', $uid];
|
|
$where[] = ['box_type', '=', $type];
|
|
// $where[] = ['add_time', '>', $times];
|
|
$box_count_info = db::name('user_box_count')->where($where)->field('sum(out_amount) as out_amount,sum(in_amount) as in_amount')->find();
|
|
// dump($need_open_num);
|
|
if(isset($box_count_info['out_amount'])){
|
|
$regain_price_one = ($user_info[$consume_name] + $user_pay_amount) * $regain_rate[1] - $box_count_info['in_amount'];
|
|
$regain_price_two = ($user_info[$consume_name] + $user_pay_amount) * $regain_rate[0] - $box_count_info['in_amount'];
|
|
}else{
|
|
$regain_price_one = $user_pay_amount * $regain_rate[1];
|
|
$regain_price_two = $user_pay_amount * $regain_rate[0];
|
|
$box_count_info['out_amount'] = 0;
|
|
$box_count_info['in_amount'] = 0;
|
|
}
|
|
$gift_array_price = 0;
|
|
$gift_price_array = [];
|
|
//随机抽取奖池礼物
|
|
for($i = 0; $i < $need_open_num; $i++){
|
|
$redis_gift_count = $redis->llen($keyname);
|
|
$gift_rand_num = rand(0,($redis_gift_count-1));
|
|
$gift_data = $redis->lindex($keyname,$gift_rand_num);
|
|
$gift_data_data = explode('-', $gift_data);
|
|
$bl_id = $gift_data_data[0];
|
|
$gid = $gift_data_data[1];
|
|
$data = [];
|
|
$data['bl_id'] = $bl_id;
|
|
$data['gid'] = $gid;
|
|
$data['gift_price'] = (int)$gift_list_data[$gid]['gift_price'];
|
|
$gift_array_price += $gift_list_data[$gid]['gift_price'];
|
|
$gift_price_array[] = $data['gift_price'];
|
|
$user_gift_id_list[] = $data;
|
|
// $need_open_num--;
|
|
}
|
|
}
|
|
}
|
|
$user_gift_id_list = $this->gift_price_regulate($user_gift_id_list, $gift_price_array, $user_pay_amount, $gift_array_price, $box_count_info['out_amount'], $box_count_info['in_amount'], $regain_rate, $regain_price_one, $regain_price_two, $tid);
|
|
|
|
//补礼物
|
|
$data = [];
|
|
$data['bl_id'] = 0;
|
|
$data['gid'] = $box_give_gift_info['gid'];
|
|
$user_gift_id_list[] = $data;
|
|
|
|
}else{
|
|
|
|
if(!empty($node)){
|
|
$node = trim($node, ';');
|
|
$node_list = explode(';', $node);
|
|
|
|
$node_value_list = [];
|
|
$node_rate_list = [];
|
|
foreach ($node_list as $k => $v){
|
|
$v = explode(':', $v);
|
|
$node_value_list[] = $v[0];
|
|
$v[1] = explode(',', $v[1]);
|
|
$node_rate_list[] = $v[1];
|
|
}
|
|
|
|
$node_place = 0;
|
|
arsort($node_value_list);
|
|
foreach ($node_value_list as $x => $y){
|
|
if($user_info[$consume_name] >= $y){
|
|
$node_place = $x;
|
|
break;
|
|
}
|
|
}
|
|
|
|
//判断是否达到回血标准
|
|
if($user_info[$regain_name] != $node_place){
|
|
|
|
if(isset($node_rate_list[$node_place]) && isset($node_rate_list[$node_place])){
|
|
//是否已选择相应爆率
|
|
if(empty($user_info[$regain_node])){
|
|
$node_key_name = $regain_node.":key:".$node_value_list[$node_place];
|
|
$node_list_len = $redis->llen($node_key_name);
|
|
|
|
if($node_list_len < 10){
|
|
$node_insert_data = $node_rate_list[$node_place];
|
|
array_unshift($node_insert_data, $node_key_name);
|
|
call_user_func_array([$redis, 'rPush'], $node_insert_data);
|
|
}
|
|
|
|
$node_rate_data = $redis->blpop($node_key_name, 2); //1s 超时
|
|
$node_rate_data = $node_rate_data[1];
|
|
$data = [];
|
|
$data[$regain_node] = $node_rate_data;
|
|
$result = db::name('user')->where('uid', $uid)->update($data);
|
|
if(!$result){
|
|
return ['code' => 201, 'msg' => '111', 'data' => null];
|
|
}
|
|
}else{
|
|
$node_rate_data = $user_info[$regain_node];
|
|
}
|
|
// dump($node_rate_data);
|
|
//当前是否到达该爆率
|
|
$node_rate_data = explode('-', $node_rate_data);
|
|
//当前奖池爆率
|
|
$where = [];
|
|
$where[] = ['uid', '=', $uid];
|
|
$where[] = ['box_type', '=', $type];
|
|
// $where[] = ['add_time', '>', $times];
|
|
$box_count_info = db::name('user_box_count')->where($where)->field('sum(out_amount) as out_amount,sum(in_amount) as in_amount')->find();
|
|
if(isset($box_count_info['out_amount'])){
|
|
$user_box_rate = round($box_count_info['in_amount']/$box_count_info['out_amount'],6);
|
|
|
|
$regain_rate = [];
|
|
$regain_rate[0] = $node_rate_data[1] - 0.003;
|
|
$regain_rate[1] = $node_rate_data[1];
|
|
|
|
if($user_info[$consume_name] >= $node_rate_data[0] && $user_box_rate < $regain_rate[0]){//超过消费金额且尚未达到爆率
|
|
$regain_price_one = ($user_info[$consume_name] + $user_pay_amount) * $regain_rate[1] - $box_count_info['in_amount'];
|
|
$regain_price_two = ($user_info[$consume_name] + $user_pay_amount) * $regain_rate[0] - $box_count_info['in_amount'];
|
|
// dump($regain_price_one);dump($regain_price_two);
|
|
$gift_array_price = 0;
|
|
$gift_price_array = [];
|
|
//随机抽取奖池礼物
|
|
for($i = 0; $i < $need_open_num; $i++){
|
|
$redis_gift_count = $redis->llen($keyname);
|
|
$gift_rand_num = rand(0,($redis_gift_count-1));
|
|
$gift_data = $redis->lindex($keyname,$gift_rand_num);
|
|
$gift_data_data = explode('-', $gift_data);
|
|
$bl_id = $gift_data_data[0];
|
|
$gid = $gift_data_data[1];
|
|
$data = [];
|
|
$data['bl_id'] = $bl_id;
|
|
$data['gid'] = $gid;
|
|
$data['gift_price'] = (int)$gift_list_data[$gid]['gift_price'];
|
|
$gift_array_price += $gift_list_data[$gid]['gift_price'];
|
|
$gift_price_array[] = $data['gift_price'];
|
|
$user_gift_id_list[] = $data;
|
|
// dump($data);
|
|
// $need_open_num--;
|
|
}
|
|
|
|
}else if($user_info[$consume_name] >= $node_rate_data[0] && $user_box_rate > $regain_rate[0]){
|
|
$data = [];
|
|
$data[$regain_name] = $node_place;
|
|
$data[$regain_node] = '';
|
|
$data['update_time'] = time();
|
|
$result = db::name('user')->where('uid', $uid)->update($data);
|
|
if(!$result){
|
|
Db::rollback();
|
|
return ['code' => 201, 'msg' => '111', 'data' => null];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if(count($user_gift_id_list) != $open_num){
|
|
if(!empty($regain)){
|
|
$regain = trim($regain,',');
|
|
$regain_list = explode(',',$regain);
|
|
$regain_value_list = [];
|
|
$regain_rate_list = [];
|
|
foreach ($regain_list as $k => $v) {
|
|
$v = explode(':',$v);
|
|
// dump($v);
|
|
$regain_value_list[] = $v[0];
|
|
$v[1] = explode('-', $v[1]);
|
|
$regain_rate_list[] = $v[1];
|
|
}
|
|
|
|
$regain_place = 0;
|
|
arsort($regain_value_list);
|
|
foreach ($regain_value_list as $x => $y) {
|
|
if($user_info[$consume_name] >= $y){
|
|
$regain_place = $x;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(isset($regain_value_list[$regain_place]) && isset($regain_rate_list[$regain_place])){
|
|
$regain_value = $regain_value_list[$regain_place];
|
|
$regain_rate = $regain_rate_list[$regain_place];
|
|
|
|
//当前奖池爆率
|
|
$where = [];
|
|
$where[] = ['uid', '=', $uid];
|
|
$where[] = ['box_type', '=', $type];
|
|
// $where[] = ['add_time', '>', $times];
|
|
$box_count_info = db::name('user_box_count')->where($where)->field('sum(out_amount) as out_amount,sum(in_amount) as in_amount')->find();
|
|
// dump($need_open_num);
|
|
if(isset($box_count_info['out_amount'])){
|
|
// $user_box_rate = round($box_count_info['in_amount']/$box_count_info['out_amount'],4);
|
|
// $regain_price_one = intval(($regain_rate[1] - $user_box_rate) * $user_info[$consume_name]);
|
|
// $regain_price_two = intval(($regain_rate[0] - $user_box_rate) * $user_info[$consume_name]);
|
|
// dump($regain_price_one);dump($regain_price_two);
|
|
$regain_price_one = ($user_info[$consume_name] + $user_pay_amount) * $regain_rate[1] - $box_count_info['in_amount'];
|
|
$regain_price_two = ($user_info[$consume_name] + $user_pay_amount) * $regain_rate[0] - $box_count_info['in_amount'];
|
|
// dump($user_info[$consume_name]);dump($regain_value);dump($user_box_rate);dump($regain_rate);
|
|
// dump($regain_price_one);dump($regain_price_two);
|
|
// $where = [];
|
|
// $where[] = ['a.tid', '=', $tid];
|
|
// $where[] = ['b.gift_price','<=', 50];
|
|
// $regain_gift_array = db::name('box_config')->alias('a')->join('yy_gift b', 'a.gid = b.gid')->field('a.gid,b.gift_price')->where($where)->order('b.gift_price desc')->select();
|
|
|
|
}else{
|
|
$regain_price_one = $user_pay_amount * $regain_rate[1];
|
|
$regain_price_two = $user_pay_amount * $regain_rate[0];
|
|
$box_count_info['out_amount'] = 0;
|
|
$box_count_info['in_amount'] = 0;
|
|
}
|
|
$gift_array_price = 0;
|
|
$gift_price_array = [];
|
|
//随机抽取奖池礼物
|
|
for($i = 0; $i < $need_open_num; $i++){
|
|
$redis_gift_count = $redis->llen($keyname);
|
|
$gift_rand_num = rand(0,($redis_gift_count-1));
|
|
$gift_data = $redis->lindex($keyname,$gift_rand_num);
|
|
$gift_data_data = explode('-', $gift_data);
|
|
$bl_id = $gift_data_data[0];
|
|
$gid = $gift_data_data[1];
|
|
$data = [];
|
|
$data['bl_id'] = $bl_id;
|
|
$data['gid'] = $gid;
|
|
$data['gift_price'] = (int)$gift_list_data[$gid]['gift_price'];
|
|
$gift_array_price += $gift_list_data[$gid]['gift_price'];
|
|
$gift_price_array[] = $data['gift_price'];
|
|
$user_gift_id_list[] = $data;
|
|
// $need_open_num--;
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// array_multisort($user_gift_id_list, SORT_ASC, $gift_price_array);
|
|
// dump($user_gift_id_list);dump($gift_price_array);dump($user_pay_amount);dump($gift_array_price);dump($box_count_info['out_amount']);dump($box_count_info['in_amount']);dump($regain_rate);
|
|
// dump($regain_price_one);dump($regain_price_two);exit;
|
|
$user_gift_id_list = $this->gift_price_regulate($user_gift_id_list, $gift_price_array, $user_pay_amount, $gift_array_price, $box_count_info['out_amount'], $box_count_info['in_amount'], $regain_rate, $regain_price_one, $regain_price_two, $tid);
|
|
}
|
|
// dump($user_gift_id_list);
|
|
// exit;
|
|
|
|
if (count($user_gift_id_list) != $open_num) {
|
|
// $this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '奖池数量不足', 'data' => null];
|
|
}
|
|
|
|
//用户必中礼物状态更新
|
|
if (!empty($box_give_gift_info)) {
|
|
$map = [];
|
|
$map[] = ['id', '=', $box_give_gift_info['id']];
|
|
$data = [];
|
|
$data['give_time'] = $now_time;
|
|
$data['give_status'] = 2;
|
|
$data['deduction_status'] = 2;
|
|
$data['deduction_time'] = time();
|
|
$data['update_time'] = $now_time;
|
|
$reslut = db::name('box_give_gift')->where($map)->update($data);
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
// $this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '', 'data' => null];
|
|
}
|
|
}
|
|
|
|
//获取宝箱礼物配置信息
|
|
$box_config_list = db::name('box_config')->field('gid,tid,is_public_screen,is_public_server')->cache(10)->select();
|
|
$box_config_list_data = [];
|
|
foreach ($box_config_list as $k => $v) {
|
|
$box_config_list_data[$v['gid']][$v['tid']] = $v;
|
|
}
|
|
|
|
|
|
//整理用户抽中礼物数量
|
|
foreach ($user_gift_id_list as $k => $v) {
|
|
|
|
if (empty($gift_list_data[$v['gid']])) {
|
|
// $this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '礼物信息不存在', 'data' => null];
|
|
}
|
|
|
|
|
|
if (empty($box_config_list_data[$v['gid']])) {
|
|
// $this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '宝箱礼物信息不存在', 'data' => null];
|
|
}
|
|
|
|
if (empty($user_win_gift_data[$v['gid']])) {
|
|
$user_win_gift_data[$v['gid']] = $gift_list_data[$v['gid']];
|
|
$user_win_gift_data[$v['gid']]['open_num'] = 1;
|
|
} else {
|
|
$user_win_gift_data[$v['gid']]['open_num']++;
|
|
}
|
|
$user_win_gift_data[$v['gid']]['bl_id'] = $v['bl_id'];
|
|
$user_win_gift_data[$v['gid']]['is_public_screen'] = $box_config_list_data[$v['gid']][$tid]['is_public_screen'];
|
|
$user_win_gift_data[$v['gid']]['is_public_server'] = $box_config_list_data[$v['gid']][$tid]['is_public_server'];
|
|
|
|
$user_win_gift_total_value += $gift_list_data[$v['gid']]['gift_price'];
|
|
|
|
if (empty($box_log_data[$v['bl_id']])) {
|
|
$box_log_data[$v['bl_id']]['open_num'] = 1;
|
|
$box_log_data[$v['bl_id']]['open_gift_value'] = $gift_list_data[$v['gid']]['gift_price'];
|
|
} else {
|
|
$box_log_data[$v['bl_id']]['open_num']++;
|
|
$box_log_data[$v['bl_id']]['open_gift_value'] += $gift_list_data[$v['gid']]['gift_price'];
|
|
}
|
|
}
|
|
|
|
// dump($user_win_gift_total_value);
|
|
// exit;
|
|
//扣除账户积分
|
|
$reslut = model('admin/User')->change_user_money_by_user_info($user_info, -$user_pay_amount, 2, 16, "开宝箱抽礼物", $user_info['uid'], 0);
|
|
if ($reslut['code'] != 200) {
|
|
Db::rollback();
|
|
// $this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => $reslut['msg'], 'data' => null];
|
|
}
|
|
$surplus_user_money = $user_info['integral'] - $user_pay_amount;
|
|
|
|
foreach ($box_log_data as $k => $v) {
|
|
if ($k > 0) {
|
|
$map = [];
|
|
$map[] = ['bl_id', '=', $k];
|
|
$reslut = db::name('box_person_log')->where($map)->inc('open_num', $v['open_num'])->inc('open_gift_value', $v['open_gift_value'])->update();
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
// $this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '请重试2', 'data' => null];
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
//添加用户开宝箱日志
|
|
$user_box_data_by_bl_id = [];
|
|
foreach ($user_gift_id_list as $k => $v) {
|
|
if (empty($user_box_data_by_bl_id[$v['bl_id']][$v['gid']])) {
|
|
$user_box_data_by_bl_id[$v['bl_id']][$v['gid']]['bl_id'] = $v['bl_id'];
|
|
$user_box_data_by_bl_id[$v['bl_id']][$v['gid']]['gid'] = $v['gid'];
|
|
$user_box_data_by_bl_id[$v['bl_id']][$v['gid']]['open_num'] = 1;
|
|
} else {
|
|
$user_box_data_by_bl_id[$v['bl_id']][$v['gid']]['open_num']++;
|
|
}
|
|
}
|
|
|
|
foreach ($user_box_data_by_bl_id as $k => $v) {
|
|
$insert_user_box_data = [];
|
|
foreach ($v as $m => $n) {
|
|
$gift_info = $gift_list_data[$n['gid']];
|
|
$data = [];
|
|
$data['uid'] = $user_info['uid'];
|
|
$data['rid'] = $rid;
|
|
$data['tid'] = $tid;
|
|
$data['bl_id'] = $n['bl_id'];
|
|
$data['gid'] = $n['gid'];
|
|
$data['num'] = $n['open_num'];
|
|
$data['gift_price'] = $gift_info['gift_price'];
|
|
$data['total_price'] = $n['open_num'] * $gift_info['gift_price'];
|
|
$data['total_pay_price'] = $n['open_num'] * $tid_box_price;
|
|
$data['total_open_num'] = $open_num;
|
|
$data['add_time'] = $now_time;
|
|
$data['update_time'] = $now_time;
|
|
$insert_user_box_data[] = $data;
|
|
}
|
|
$reslut = db::name('user_box_log')->insertAll($insert_user_box_data);
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
// $this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '', 'data' => null];
|
|
}
|
|
}
|
|
|
|
|
|
//添加用户开宝箱数据统计
|
|
$time = strtotime(date("Y-m-d"));
|
|
$map = [];
|
|
$map[] = ['uid', '=', $user_info['uid']];
|
|
$box_type = $type;
|
|
$map[] = ['box_type', '=', $box_type];
|
|
$map[] = ['add_time', '>', $time];
|
|
$user_box_count_info = db::name('user_box_count')->where($map)->find();
|
|
if (!empty($user_box_count_info)) {
|
|
$rate = 0; //支收比
|
|
$rate = round(($user_win_gift_total_value + $user_box_count_info['in_amount']) / ($user_box_count_info['out_amount'] + $user_pay_amount), 4);
|
|
$map = [];
|
|
$map[] = ['sid', '=', $user_box_count_info['sid']];
|
|
// $map[] = ['uid', '=', $user_info['uid']];
|
|
// $map[] = ['box_type', '=', $box_type];
|
|
$data = [];
|
|
$data['rate'] = $rate;
|
|
$data['update_time'] = $now_time;
|
|
$profit = $user_win_gift_total_value - $user_pay_amount;
|
|
$reslut = db::name('user_box_count')->where($map)->inc('out_amount', $user_pay_amount)->inc('in_amount', $user_win_gift_total_value)->inc('profit', $profit)->update($data);
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
// $this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '', 'data' => null];
|
|
}
|
|
} else {
|
|
$rate = round($user_win_gift_total_value / $user_pay_amount, 4);
|
|
$profit = $user_win_gift_total_value - $user_pay_amount;
|
|
$data = [];
|
|
$data['uid'] = $user_info['uid'];
|
|
$data['box_type'] = $box_type;
|
|
$data['out_amount'] = $user_pay_amount;
|
|
$data['in_amount'] = $user_win_gift_total_value;
|
|
$data['profit'] = $profit;
|
|
$data['rate'] = $rate;
|
|
$data['add_time'] = $now_time;
|
|
$data['update_time'] = $now_time;
|
|
$reslut = db::name('user_box_count')->insert($data);
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
// $this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '', 'data' => null];
|
|
}
|
|
}
|
|
|
|
//添加房间开宝箱数据统计
|
|
$time = strtotime(date("Y-m-d"));
|
|
$map = [];
|
|
$map[] = ['rid', '=', $rid];
|
|
$box_type = $type;
|
|
$map[] = ['box_type', '=', $box_type];
|
|
$map[] = ['add_time', '>', $time];
|
|
$room_box_count_info = db::name('room_box_count')->where($map)->find();
|
|
if (!empty($room_box_count_info)) {
|
|
$rate = 0; //支收比
|
|
$rate = round(($user_win_gift_total_value + $room_box_count_info['in_amount']) / ($room_box_count_info['out_amount'] + $user_pay_amount), 4);
|
|
$map = [];
|
|
$map[] = ['sid', '=', $room_box_count_info['sid']];
|
|
// $map[] = ['uid', '=', $user_info['uid']];
|
|
// $map[] = ['box_type', '=', $box_type];
|
|
$data = [];
|
|
$data['rate'] = $rate;
|
|
$data['update_time'] = $now_time;
|
|
$profit = $user_win_gift_total_value - $user_pay_amount;
|
|
$reslut = db::name('room_box_count')->where($map)->inc('out_amount', $user_pay_amount)->inc('in_amount', $user_win_gift_total_value)->inc('profit', $profit)->update($data);
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
// $this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '', 'data' => null];
|
|
}
|
|
} else {
|
|
$rate = round($user_win_gift_total_value / $user_pay_amount, 4);
|
|
$profit = $user_win_gift_total_value - $user_pay_amount;
|
|
$data = [];
|
|
$data['rid'] = $rid;
|
|
$data['box_type'] = $box_type;
|
|
$data['out_amount'] = $user_pay_amount;
|
|
$data['in_amount'] = $user_win_gift_total_value;
|
|
$data['profit'] = $profit;
|
|
$data['rate'] = $rate;
|
|
$data['add_time'] = $now_time;
|
|
$data['update_time'] = $now_time;
|
|
$reslut = db::name('room_box_count')->insert($data);
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
// $this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '', 'data' => null];
|
|
}
|
|
}
|
|
|
|
//礼物放入背包,计入背包日志
|
|
$insert_user_gift_pack_log_data = [];
|
|
foreach ($user_win_gift_data as $k => $v) {
|
|
$map = [];
|
|
$map[] = ['uid', '=', $user_info['uid']];
|
|
$map[] = ['gid', '=', $v['gid']];
|
|
$data = [];
|
|
$data['gid'] = $v['gid'];
|
|
$data['update_time'] = $now_time;
|
|
$reslut = db::name('user_gift_pack')->where($map)->inc('num', $v['open_num'])->inc('total_num', $v['open_num'])->update($data);
|
|
if (!$reslut) {
|
|
$data = [];
|
|
$data['uid'] = $user_info['uid'];
|
|
$data['gid'] = $v['gid'];
|
|
$data['total_num'] = $v['open_num'];
|
|
$data['num'] = $v['open_num'];
|
|
$data['is_tester'] = $user_info['is_tester'];
|
|
$data['add_time'] = $now_time;
|
|
$data['update_time'] = $now_time;
|
|
|
|
$reslut = db::name('user_gift_pack')->insert($data);
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
// $this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '', 'data' => null];
|
|
}
|
|
}
|
|
|
|
$data = [];
|
|
$data['uid'] = $user_info['uid'];
|
|
$data['type'] = 2;
|
|
$data['bl_id'] = $v['bl_id'];
|
|
$data['gid'] = $v['gid'];
|
|
$data['gift_price'] = $gift_list_data[$v['gid']]['gift_price'];
|
|
$data['change_num'] = $v['open_num'];
|
|
$data['total_gift_price'] = $gift_list_data[$v['gid']]['gift_price'] * $v['open_num'];
|
|
$data['after_num'] = 0;
|
|
$data['is_tester'] = $user_info['is_tester'];
|
|
$data['remarks'] = "开宝箱获得";
|
|
$data['add_time'] = $now_time;
|
|
$data['update_time'] = $now_time;
|
|
$insert_user_gift_pack_log_data[] = $data;
|
|
}
|
|
if (!empty($insert_user_gift_pack_log_data)) {
|
|
$reslut = db::name('user_gift_pack_log')->insertAll($insert_user_gift_pack_log_data);
|
|
if (!$reslut) {
|
|
Db::rollback();
|
|
// $this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '', 'data' => null];
|
|
}
|
|
}
|
|
|
|
//用户消费累计
|
|
if($user_info['is_tester'] != 2){
|
|
$map = [];
|
|
$map[] = ['uid', '=', $user_info['uid']];
|
|
if ($type == 1) {
|
|
$reslut = db::name('user')->where($map)->inc('silver_consume', $user_pay_amount)->update();
|
|
}else if($type == 2){
|
|
$reslut = db::name('user')->where($map)->inc('gold_consume', $user_pay_amount)->update();
|
|
}else if($type == 3){
|
|
$reslut = db::name('user')->where($map)->inc('drill_consume', $user_pay_amount)->update();
|
|
}else if($type == 4){
|
|
$reslut = db::name('user')->where($map)->inc('platina_consume', $user_pay_amount)->update();
|
|
} else if($type == 5){
|
|
$reslut = db::name('user')->where($map)->inc('five_consume', $user_pay_amount)->update();
|
|
}
|
|
if(!$reslut){
|
|
Db::rollback();
|
|
// $this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
return ['code' => 201, 'msg' => '请重试1', 'data' => null];
|
|
}
|
|
}
|
|
|
|
|
|
Db::commit();
|
|
|
|
//推送礼物信息
|
|
$user_charm_info = model('User')->get_user_charm_contribution_info($user_info['uid']);
|
|
$push_all_gift_data = [];
|
|
$push_group_gift_data = [];
|
|
$push_sqb_gift_data = [];
|
|
$push_all_gift_data['user_info']['uid'] = $user_info['uid'];
|
|
$push_all_gift_data['user_info']['nick_name'] = mb_convert_encoding(base64_decode($user_info['base64_nick_name']), 'UTF-8', 'UTF-8');
|
|
$push_all_gift_data['user_info']['head_pic'] = localpath_to_netpath($user_info['head_pic']);
|
|
$push_all_gift_data['user_info']['contribution_level_image'] = $user_charm_info['data']['contribution_level_image'];
|
|
$push_group_gift_data['user_info']['uid'] = $user_info['uid'];
|
|
$push_group_gift_data['user_info']['nick_name'] = mb_convert_encoding(base64_decode($user_info['base64_nick_name']), 'UTF-8', 'UTF-8');
|
|
$push_group_gift_data['user_info']['head_pic'] = localpath_to_netpath($user_info['head_pic']);
|
|
$push_group_gift_data['user_info']['contribution_level_image'] = $user_charm_info['data']['contribution_level_image'];
|
|
|
|
foreach ($user_win_gift_data as $k => &$v) {
|
|
$v['surprise'] = 0;
|
|
if($v['gift_price'] > $config['big_gift_price']){
|
|
$v['surprise'] = 1;
|
|
}
|
|
unset($v['id']);
|
|
$v['type_name'] = db::name('box_type')->where('tid',$tid)->value('show_name');
|
|
$v['box_type'] = $tid;
|
|
if ($v['is_public_server'] == 1) {
|
|
if($v['gift_price'] > $config['sqb_limit_gift_price']){
|
|
$sqb_gift_info = $v;
|
|
|
|
$sqb_gift_info['uid'] = $user_info['uid'];
|
|
$sqb_gift_info['nick_name'] = mb_convert_encoding(base64_decode($user_info['base64_nick_name']), 'UTF-8', 'UTF-8');
|
|
$sqb_gift_info['head_pic'] = localpath_to_netpath($user_info['head_pic']);
|
|
$sqb_gift_info['num'] = $v['open_num'];
|
|
$sqb_gift_info['total_open_num'] = $open_num;
|
|
$sqb_gift_info['box_type_name'] = $v['type_name'];
|
|
$push_all_gift_data['gift_list'][] = $sqb_gift_info;
|
|
}
|
|
}
|
|
if ($v['is_public_screen'] == 1) {
|
|
$push_group_gift_data['gift_list'][] = $v;
|
|
}
|
|
}
|
|
|
|
if (!empty($push_all_gift_data['gift_list'])) {
|
|
$push_data = [];
|
|
$push_data['code'] = 304;
|
|
$push_data['msg'] = "开宝箱全服播报数据";
|
|
$push_data['data'] = $push_all_gift_data;
|
|
model('api/WebSocketPush')->send_to_all($push_data);
|
|
}else if (!empty($push_group_gift_data['gift_list'])) {
|
|
$push_data = [];
|
|
$push_data['code'] = 303;
|
|
$push_data['msg'] = "开宝箱房间播报数据";
|
|
$push_data['data'] = $push_group_gift_data;
|
|
model('api/WebSocketPush')->send_to_group($rid, $push_data);
|
|
}
|
|
|
|
$data = [];
|
|
$data['win_gift_list'] = array_values($user_win_gift_data);
|
|
$data['user_win_gift_total_value'] = $user_win_gift_total_value;
|
|
$data['user_money'] = $surplus_user_money;
|
|
|
|
return ['code' => 200, 'msg' => '开奖成功', 'data' => $data];
|
|
} catch (\Exception $e) {
|
|
//回滚事务
|
|
dump($e);
|
|
$this->redis_gift_data_rollback($redis, $keyname, $user_gift_id_redis_list);
|
|
Db::rollback();
|
|
return ['code' => 201, 'msg' => '开奖失败', 'data' => null];
|
|
}
|
|
}
|
|
|
|
//调控礼物
|
|
public function gift_price_regulate($user_gift_id_list, $gift_price_array, $user_pay_amount, $gift_array_price, $out_amount, $in_amount, $regain_rate, $regain_price_one, $regain_price_two, $tid){
|
|
//计算爆率
|
|
$in_amounts = $in_amount + $gift_array_price;
|
|
$out_amounts = $out_amount + $user_pay_amount;
|
|
$gift_rate = round(($in_amounts/$out_amounts), 4);
|
|
|
|
if(count($user_gift_id_list) < 100){
|
|
if($gift_rate < $regain_rate[0]){//低于最小爆率
|
|
array_multisort($user_gift_id_list, SORT_NUMERIC, $gift_price_array);
|
|
$user_gift_id_list = array_splice($user_gift_id_list,1);//移除第一个礼物
|
|
|
|
$gift_price_sum = 0;
|
|
foreach ($user_gift_id_list as $k => $v){
|
|
$gift_price_sum += $v['gift_price'];
|
|
}
|
|
$regain_price_ones = (int)($regain_price_one - $gift_price_sum);
|
|
$regain_price_twos = (int)($regain_price_two - $gift_price_sum);
|
|
//补充一个礼物
|
|
$where = [];
|
|
$where[] = ['a.tid', '=', $tid];
|
|
$where[] = ['b.gift_price', '<', $regain_price_ones];
|
|
$where[] = ['b.gift_price', '>', $regain_price_twos];
|
|
$gift_data = db::name('box_config')->alias('a')->join('yy_gift b', 'a.gid = b.gid')->field('a.gid,b.gift_price')->where($where)->orderRaw('rand()')->find();
|
|
if(!$gift_data){
|
|
$where = [];
|
|
$where[] = ['a.tid', '=', $tid];
|
|
$where[] = ['b.gift_price', '<', $regain_price_ones];
|
|
$gift_data = db::name('box_config')->alias('a')->join('yy_gift b', 'a.gid = b.gid')->field('a.gid,b.gift_price')->where($where)->order('b.gift_price desc')->find();
|
|
}
|
|
$data = [];
|
|
$data['bl_id'] = 0;
|
|
$data['gid'] = $gift_data['gid'];
|
|
$data['gift_price'] = (int)$gift_data['gift_price'];
|
|
$user_gift_id_list[] = $data;
|
|
// $regain_price_one = $regain_price_ones - $data['gift_price'];
|
|
// $regain_price_two = $regain_price_twos - $data['gift_price'];
|
|
$gift_price_array = [];
|
|
$gift_array_price = 0;
|
|
foreach ($user_gift_id_list as $k => $v){
|
|
$gift_price_array[] = $v['gift_price'];
|
|
$gift_array_price += $v['gift_price'];
|
|
}
|
|
|
|
// $user_gift_id_list = $this->gift_price_regulate($user_gift_id_list, $gift_price_array, $user_pay_amount, $gift_array_price, $out_amount, $in_amount, $regain_rate, $regain_price_one, $regain_price_two, $tid);
|
|
return $user_gift_id_list;
|
|
}
|
|
}else{
|
|
if($gift_rate < $regain_rate[0]){//低于最小爆率
|
|
array_multisort($user_gift_id_list, SORT_NUMERIC, $gift_price_array);
|
|
$user_gift_id_list = array_splice($user_gift_id_list,1);//移除第一个礼物
|
|
|
|
$gift_price_sum = 0;
|
|
foreach ($user_gift_id_list as $k => $v){
|
|
$gift_price_sum += $v['gift_price'];
|
|
}
|
|
$regain_price_ones = (int)($regain_price_one - $gift_price_sum);
|
|
$regain_price_twos = (int)($regain_price_two - $gift_price_sum);
|
|
//补充一个礼物
|
|
$where = [];
|
|
$where[] = ['a.tid', '=', $tid];
|
|
$where[] = ['b.gift_price', '<', $regain_price_ones];
|
|
$where[] = ['b.gift_price', '>', $regain_price_twos];
|
|
$gift_data = db::name('box_config')->alias('a')->join('yy_gift b', 'a.gid = b.gid')->field('a.gid,b.gift_price')->where($where)->orderRaw('rand()')->find();
|
|
if(!$gift_data){
|
|
$where = [];
|
|
$where[] = ['a.tid', '=', $tid];
|
|
$where[] = ['b.gift_price', '<', $regain_price_ones];
|
|
$gift_data = db::name('box_config')->alias('a')->join('yy_gift b', 'a.gid = b.gid')->field('a.gid,b.gift_price')->where($where)->order('b.gift_price desc')->find();
|
|
}
|
|
$data = [];
|
|
$data['bl_id'] = 0;
|
|
$data['gid'] = $gift_data['gid'];
|
|
$data['gift_price'] = (int)$gift_data['gift_price'];
|
|
$user_gift_id_list[] = $data;
|
|
// $regain_price_one = $regain_price_ones - $data['gift_price'];
|
|
// $regain_price_two = $regain_price_twos - $data['gift_price'];
|
|
$gift_price_array = [];
|
|
$gift_array_price = 0;
|
|
foreach ($user_gift_id_list as $k => $v){
|
|
$gift_price_array[] = $v['gift_price'];
|
|
$gift_array_price += $v['gift_price'];
|
|
}
|
|
|
|
$user_gift_id_list = $this->gift_price_regulate($user_gift_id_list, $gift_price_array, $user_pay_amount, $gift_array_price, $out_amount, $in_amount, $regain_rate, $regain_price_one, $regain_price_two, $tid);
|
|
// return $user_gift_id_list;
|
|
}
|
|
}
|
|
|
|
if($gift_rate > $regain_rate[1]){//高于最大爆率
|
|
|
|
if($gift_array_price > $user_pay_amount){
|
|
array_multisort($user_gift_id_list, SORT_NUMERIC, $gift_price_array);
|
|
array_splice($user_gift_id_list, -1, 1);
|
|
//补充一个礼物
|
|
$where = [];
|
|
$where[] = ['a.tid', '=', $tid];
|
|
$gift_data = db::name('box_config')->alias('a')->join('yy_gift b', 'a.gid = b.gid')->field('a.gid,b.gift_price')->where($where)->order('b.gift_price asc')->find();
|
|
// dump($gift_data);exit;
|
|
$data = [];
|
|
$data['bl_id'] = 0;
|
|
$data['gid'] = $gift_data['gid'];
|
|
$data['gift_price'] = (int)$gift_data['gift_price'];
|
|
$user_gift_id_list[] = $data;
|
|
$gift_price_array = [];
|
|
$gift_array_price = 0;
|
|
foreach ($user_gift_id_list as $k => $v){
|
|
$gift_price_array[] = $v['gift_price'];
|
|
$gift_array_price += $v['gift_price'];
|
|
}
|
|
$user_gift_id_list = $this->gift_price_regulate($user_gift_id_list, $gift_price_array, $user_pay_amount, $gift_array_price, $out_amount, $in_amount, $regain_rate, $regain_price_one, $regain_price_two, $tid);
|
|
}
|
|
|
|
}
|
|
|
|
return $user_gift_id_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|