新需求: 盲盒转盘开发-转盘抽奖接口开发
This commit is contained in:
@@ -17,9 +17,10 @@ class BlindBoxTurntableGift extends Model
|
||||
protected $updateTime = 'updatetime';
|
||||
protected $table = 'fa_vs_gift';
|
||||
|
||||
//获取礼物类型
|
||||
//获取奖池礼物列表
|
||||
public function get_gift_list($gift_bag_id)
|
||||
{
|
||||
$box = db::name('vs_gift_bag')->where('id',$gift_bag_id)->find();
|
||||
$gifts = db::name('vs_gift_bag_detail')->where('gift_bag_id',$gift_bag_id)->order("id desc")->select();
|
||||
$gift_list = [];
|
||||
foreach ($gifts as $key => $value) {
|
||||
@@ -33,6 +34,174 @@ class BlindBoxTurntableGift extends Model
|
||||
$gift_list[$key]['gift_price'] = $gift_data['gift_price'];
|
||||
}
|
||||
}
|
||||
return ['code' => 1, 'msg' => '获取成功', 'data' => $gift_list];
|
||||
|
||||
$ext = json_decode($box['ext'],true);
|
||||
$box_gift = Db::name('vs_gift')->where('gid',$ext['gift_id'])->find();
|
||||
$result_data = [
|
||||
'title' => $box['name'],
|
||||
'rule_url' => get_system_config_value('web_site')."/api/Page/get_gift_box_rule?box_id=".$box["id"],
|
||||
'box_price' => $box_gift['gift_price'],
|
||||
'gift_list' => $gift_list,
|
||||
];
|
||||
return ['code' => 1, 'msg' => '获取成功', 'data' => $result_data];
|
||||
}
|
||||
/*
|
||||
* 抽奖
|
||||
*/
|
||||
public function draw_gift($gift_bag_id, $user_id, $gift_user_ids,$num=1,$room_id=0){
|
||||
//获取盲盒配置
|
||||
$bag_data = db::name("vs_gift_bag")->field('id,name,ext,periods')->where('id',$gift_bag_id)->find();
|
||||
$ext = json_decode($bag_data['ext'],true);
|
||||
$bag_gift = db::name("vs_gift")->where(['gid'=>$ext['gift_id']])->find();// 获取盲盒信息
|
||||
$toarray = explode(',',$gift_user_ids);
|
||||
foreach ($toarray as $gift_user_id){
|
||||
for($i = 0; $i < $num; $i++){
|
||||
$reslut_draw_gift_data = $this->draw_gift_one($gift_bag_id, $user_id, $gift_user_id,$bag_gift['gift_price'],$bag_data['periods'],$room_id);
|
||||
$reslut_draw_gift[] = $reslut_draw_gift_data['data'];
|
||||
}
|
||||
}
|
||||
$reslut_gift = [];
|
||||
$reslut_data = [];
|
||||
foreach ($reslut_draw_gift as $key => $value) {
|
||||
$gift_user_id = $value['gift_user_id'];
|
||||
$gift_id = $value['gift_id'];
|
||||
|
||||
// 以 gift_user_id 和 gift_id 作为键进行分组
|
||||
$group_key = $gift_user_id . '_' . $gift_id;
|
||||
|
||||
if (!isset($reslut_gift[$group_key])) {
|
||||
$reslut_gift[$group_key] = [
|
||||
'gift_user_id' => $gift_user_id,
|
||||
'gift_id' => $gift_id,
|
||||
'count' => 1,
|
||||
'gift_price' => $value['gift_price']
|
||||
];
|
||||
} else {
|
||||
$reslut_gift[$group_key]['count']++;
|
||||
}
|
||||
if (!isset($reslut_data[$gift_id])) {
|
||||
$reslut_data[$gift_id] = [
|
||||
'gift_id' => $gift_id,
|
||||
'count' => 1
|
||||
];
|
||||
}else {
|
||||
$reslut_data[$gift_id]['count']++;
|
||||
}
|
||||
}
|
||||
$reslut_gift = array_values($reslut_gift);
|
||||
//盲盒转盘抽奖记录
|
||||
$box_turntable_log = db::name('vs_blind_box_turntable_log')->insertGetId([
|
||||
'user_id' => $user_id,
|
||||
'gift_bag_id' => $gift_bag_id,
|
||||
'num' => $num,
|
||||
'room_id' => $room_id,
|
||||
'bag_price' => $bag_gift['gift_price'],
|
||||
'lottery_result' => json_encode($reslut_gift),
|
||||
'createtime' => time()
|
||||
]);
|
||||
$reslut['blind_box_turntable_id'] = $box_turntable_log;
|
||||
$reslut['reslut_list'] = array_values($reslut_data);
|
||||
return V(1,"成功", $reslut);
|
||||
}
|
||||
/*
|
||||
* 单人单次抽奖
|
||||
*/
|
||||
public function draw_gift_one($gift_bag_id, $user_id, $gift_user_id,$gift_price,$periods,$room_id=0){
|
||||
//随机获取一个礼物
|
||||
$where = [
|
||||
'gift_bag_id' => $gift_bag_id,
|
||||
'quantity' => ['>',0],
|
||||
'remaining_number' => ['>',0]
|
||||
];
|
||||
// 优化:基于剩余数量的加权随机选择
|
||||
$gift_bag_details = db::name("vs_gift_bag_detail")->where($where)->select();
|
||||
if (empty($gift_bag_details)) {
|
||||
return ['code' => 0, 'msg' => '当前盲盒无可用礼物', 'data' => []];
|
||||
}
|
||||
//奖池总的抽奖次数
|
||||
$total_quantity = db::name("vs_gift_bag_detail")->where(['gift_bag_id' => $gift_bag_id])->sum('quantity');
|
||||
// 实现加权随机算法:剩余数量越多,被抽中的概率越大
|
||||
$total_remaining = 0;
|
||||
foreach ($gift_bag_details as $gift) {
|
||||
$total_remaining += $gift['remaining_number'];
|
||||
}
|
||||
//本期当前第多少次后抽奖 总的抽奖次数- 剩余数量
|
||||
$total_draw_times = $total_quantity - $total_remaining;
|
||||
|
||||
$rand_value = mt_rand(1, $total_remaining);
|
||||
$current_sum = 0;
|
||||
$gift_bag_detail = null;
|
||||
foreach ($gift_bag_details as $gift) {
|
||||
if($gift['remaining_number'] <= 0){
|
||||
continue;
|
||||
}
|
||||
if($total_draw_times < $gift['weight']){
|
||||
continue;
|
||||
}
|
||||
$current_sum += $gift['remaining_number'];
|
||||
if ($rand_value <= $current_sum) {
|
||||
$gift_bag_detail = $gift;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if($gift_bag_detail){
|
||||
//获取开出礼物的信息
|
||||
$gift = db::name("vs_gift")->where(['gid'=>$gift_bag_detail['foreign_id']])->find();
|
||||
//处理礼包发放记录表
|
||||
$data = [];
|
||||
$data['user_id'] = $user_id;
|
||||
$data['gift_user_id'] = $gift_user_id;
|
||||
$data['gift_bag_id'] = $gift_bag_id;
|
||||
$data['gift_id'] = $gift_bag_detail['foreign_id'];
|
||||
$data['periods'] = $periods;
|
||||
$data['room_id'] = $room_id;
|
||||
$data['gift_price'] = $gift['gift_price'];
|
||||
$data['bag_price'] = $gift_price;
|
||||
$data['createtime'] = time();
|
||||
$result = db::name("vs_gift_bag_receive_log")->insert($data);
|
||||
if(!$result){
|
||||
return ['code' => 0, 'msg' => '失败,', 'data' => []];
|
||||
}
|
||||
//减去盲盒包礼物数量
|
||||
$ret = db::name("vs_gift_bag_detail")->where('id',$gift_bag_detail['id'])->setDec('remaining_number');
|
||||
if(!$ret){
|
||||
return ['code' => 0, 'msg' => '失败.', 'data' => []];
|
||||
}
|
||||
//判断剩余数量是否为0 为0重置,进入下一期
|
||||
$gift_bag_num = db::name("vs_gift_bag_detail")->where('gift_bag_id',$gift_bag_id)->sum('remaining_number');
|
||||
if($gift_bag_num <= 0){
|
||||
db::name("vs_gift_bag")->where('id',$gift_bag_id)->setInc('periods');
|
||||
db::name("vs_gift_bag_detail")->where('gift_bag_id',$gift_bag_id)->update(['remaining_number'=>db::raw('quantity')]);
|
||||
}
|
||||
}else{
|
||||
return ['code' => 0, 'msg' => '失败!', 'data' => []];
|
||||
}
|
||||
$result_data = [
|
||||
'user_id' => $user_id,
|
||||
'gift_user_id'=>$gift_user_id,
|
||||
'gift_id'=>$gift_bag_detail['foreign_id'],
|
||||
'gift_price'=>$gift['gift_price'],
|
||||
'is_public_screen'=>$gift['is_public_screen'],
|
||||
];
|
||||
return ['code' => 1, 'msg' => '成功', 'data' => $result_data];
|
||||
}
|
||||
|
||||
/*
|
||||
* 礼物发放
|
||||
*/
|
||||
public function gift_send($send_id){
|
||||
$blind_box_turntable = db('vs_blind_box_turntable_log')->where('id',$send_id)->find();
|
||||
if(!$blind_box_turntable){
|
||||
return ['code' => 0, 'msg' => '数据不存在'];
|
||||
}
|
||||
$reslut_gift = json_decode($blind_box_turntable['lottery_result'],true);
|
||||
foreach ($reslut_gift as $key => $value) {
|
||||
$res = model('GiveGift')->give_gift($blind_box_turntable['user_id'], $value['gift_user_id'], $value['gift_id'], $value['count'],2,1, $blind_box_turntable['room_id']);
|
||||
if (isset($res) && $res['code'] != 1) {
|
||||
return V(0, $res['msg']);
|
||||
}
|
||||
}
|
||||
$res = db('BlindBoxTurntable')->where('id', $blind_box_turntable['id'])->update(['is_sued' => 1, 'updatetime' => time()]);
|
||||
return ['code' => 1, 'msg' => '成功', 'data' => $res];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user