alias('a') ->leftJoin('gift b', 'a.gid = b.gid') ->field('b.gid,b.gift_name,b.gift_price,a.num') ->where($map)->order('b.gift_price desc') ->select(); return ['code' => 200, 'msg' => '获取数据成功', 'data' => $list]; } //获取所有不可购买礼物 public function get_gift_list() { $tid = 102; $map = []; $map[] = ['is_show', '=', 1]; $map[] = ['is_delete', '=', 1]; $map[] = ['is_can_buy', '=', 2]; $list = db::name('gift') ->field('gid,gift_name,gift_price') ->where($map)->order('gift_price desc') ->select(); return ['code' => 200, 'msg' => '获取数据成功', 'data' => $list]; } //限定礼物配置 public function get_config_list($stage, $gid, $page = 1, $limit = 20) { $map = []; if(!empty($stage)){ $map[] = ['a.stage', '=', $stage]; } $list = db::name('blind_box_config')->alias('a') ->leftJoin('gift b', 'a.gid = b.gid') ->leftJoin('box_type c', 'a.tid = c.tid') ->leftJoin('box_config d', 'a.tid = d.tid and a.gid = d.gid') ->leftJoin('blind_box_stage e', 'a.stage = e.stage') ->field('a.*,b.gift_name,b.gift_price,b.base_image,c.show_name,d.num,e.stage_name') ->where($map)->order('id desc') ->page($page, $limit)->select(); foreach ($list as $k => &$v) { $total_bind_num = Db::name('blind_box_config')->where('stage', $v['stage'])->sum('bind_rate'); $v['base_image'] = localpath_to_netpath($v['base_image']); if($total_bind_num > 0) { $v['bind_rate'] = bcmul(bcdiv($v['bind_rate'], $total_bind_num,4), 100, 2) . '%'; } else { $v['bind_rate'] = '50%'; } } $data = []; $data['count'] = db::name('blind_box_config')->alias('a')->where($map)->count(); $data['list'] = $list; return ['code' => 200, 'msg' => '获取成功', 'data' => $data]; } //添加配置 public function add_config($data) { if(empty($data['stage'])) { return ['code' => 201, 'msg' => '请选择阶段', 'data' => null]; } if(empty($data['gid'])) { return ['code' => 201, 'msg' => '请选择限定礼物', 'data' => null]; } if(empty($data['time_long'])) { return ['code' => 201, 'msg' => '请输入限定礼物增加时长', 'data' => null]; } $map = []; $map[] = ['is_delete', '=', 1]; $map[] = ['stage', '=', $data['stage']]; $map[] = ['gid', '=', $data['gid']]; $info = db::name('blind_box_config')->where($map)->find(); if($info){ return ['code' => 201, 'msg' => '限定礼物已添加', 'data' => null]; } Db::startTrans(); try { //添加 $insert = []; $insert['stage'] = $data['stage']; $insert['gid'] = $data['gid']; $insert['time_long'] = $data['time_long']; $insert['tid'] = 102; $insert['add_time'] = time(); $insert['bind_rate'] = $data['bind_rate']; $reslut = db::name('blind_box_config')->insert($insert); if(!$reslut){ Db::rollback(); return ['code' => 201, 'msg' => '添加失败', 'data' => null]; } Db::commit(); return ['code' => 200, 'msg' => '添加成功', 'data' => null]; } catch (\Exception $e) { // 回滚事务 Db::rollback(); return ['code' => 201, 'msg' => '添加失败', 'data' => null]; } } //修改配置 public function edit_config($data) { if(empty($data['stage'])) { return ['code' => 201, 'msg' => '请选择阶段', 'data' => null]; } if(empty($data['gid'])) { return ['code' => 201, 'msg' => '请选择限定礼物', 'data' => null]; } if(empty($data['time_long'])) { return ['code' => 201, 'msg' => '请输入限定礼物增加时长', 'data' => null]; } $map = []; $map[] = ['is_delete', '=', 1]; $map[] = ['gid', '=', $data['gid']]; $map[] = ['stage', '=', $data['stage']]; $map[] = ['id', 'neq', $data['id']]; $info = db::name('blind_box_config')->where($map)->find(); if($info){ return ['code' => 201, 'msg' => '限定礼物已添加', 'data' => null]; } Db::startTrans(); try { //添加 $insert = []; $insert['stage'] = $data['stage']; $insert['gid'] = $data['gid']; $insert['time_long'] = $data['time_long']; $insert['update_time'] = time(); $insert['bind_rate'] = $data['bind_rate']; $reslut = db::name('blind_box_config')->where('id', $data['id'])->update($insert); if(!$reslut){ Db::rollback(); return ['code' => 201, 'msg' => '修改失败', 'data' => null]; } Db::commit(); return ['code' => 200, 'msg' => '修改成功', 'data' => null]; } catch (\Exception $e) { // 回滚事务 Db::rollback(); return ['code' => 201, 'msg' => '修改失败', 'data' => null]; } } //获取配置信息 public function get_config_info($id) { $map = []; $map[] = ['id', '=', $id]; $config_info = db::name('blind_box_config')->where($map)->find(); if (empty($config_info)) { return ['code' => 201, 'msg' => '信息不存在', 'data' => '']; } return ['code' => 200, 'msg' => '获取成功', 'data' => $config_info]; } public function del_config($id) { $map = []; $map[] = ['id', '=', $id]; $config_info = db::name('blind_box_config')->where($map)->find(); if (empty($config_info)) { return ['code' => 201, 'msg' => '信息不存在', 'data' => '']; } Db::startTrans(); try { $result = db::name('blind_box_config')->where('id', $id)->delete(); if(!$result){ Db::rollback(); return ['code' => 201, 'msg' => '删除失败', 'data' => null]; } Db::commit(); return ['code' => 200, 'msg' => '删除成功', 'data' => null]; } catch (\Exception $e) { dump($e); // 回滚事务 Db::rollback(); return ['code' => 201, 'msg' => '删除失败', 'data' => null]; } } //阶段列表 public function get_stage_list($pattern_id, $page = 1, $limit = 20) { $map = []; if ($pattern_id) { $map[] = ['pattern_id', '=', $pattern_id]; } $list = db::name('blind_box_stage')->alias('a') ->leftJoin('gift b', 'a.gid = b.gid') ->leftJoin('gift c', 'a.gid_r = c.gid') ->field('a.*,b.gift_name,b.gift_price,b.base_image,c.gift_price as gift_price_r,c.gift_name as gift_name_r') ->where($map)->order('id desc') ->page($page, $limit)->select(); foreach ($list as &$v) { $v['base_image'] = localpath_to_netpath($v['base_image']); } $data = []; $data['count'] = db::name('blind_box_stage')->where($map)->count(); $data['list'] = $list; return ['code' => 200, 'msg' => '获取成功', 'data' => $data]; } public function edit_stage($data) { if(empty($data['num'])) { if ($data['id'] != 1) { return ['code' => 201, 'msg' => '请输入阶段数量', 'data' => null]; } } if (empty($data['gid'])) { return ['code' => 201, 'msg' => '请选择奖励礼物', 'data' => null]; } $map = []; $map[] = ['num', '=', $data['num']]; $map[] = ['id', 'neq', $data['id']]; $info = db::name('blind_box_stage')->where($map)->find(); if($info){ return ['code' => 201, 'msg' => '阶段数量已添加', 'data' => null]; } Db::startTrans(); try { //添加 $insert = []; $insert['num'] = $data['num']; $insert['update_time'] = time(); $insert['gid'] = $data['gid']; $insert['gid_r'] = $data['gid_r']; $reslut = db::name('blind_box_stage')->where('id', $data['id'])->update($insert); if(!$reslut){ Db::rollback(); return ['code' => 201, 'msg' => '修改失败', 'data' => null]; } Db::commit(); return ['code' => 200, 'msg' => '修改成功', 'data' => null]; } catch (\Exception $e) { // 回滚事务 Db::rollback(); dump($e); return ['code' => 201, 'msg' => '修改失败', 'data' => null]; } } //获取阶段信息 public function get_stage_info($id) { $map = []; $map[] = ['id', '=', $id]; $config_info = db::name('blind_box_stage')->where($map)->find(); if (empty($config_info)) { return ['code' => 201, 'msg' => '信息不存在', 'data' => '']; } return ['code' => 200, 'msg' => '获取成功', 'data' => $config_info]; } //获取期数 public function get_term_list($status, $page = 1, $limit = 20) { $map = []; $map[] = ['a.is_delete', '=', 1]; if ($status) { $map[] = ['a.status', '=', $status]; } $list = db::name('blind_box_term')->alias('a') ->leftJoin('box_type b', 'a.tid = b.tid') ->field('a.*,b.show_name') ->where($map)->order('id desc') ->page($page, $limit)->select(); $data = []; $data['count'] = db::name('blind_box_term')->alias('a')->where($map)->count(); $data['list'] = $list; return ['code' => 200, 'msg' => '获取成功', 'data' => $data]; } //添加期数 public function add_term($data) { if(empty($data['time_long'])) { return ['code' => 201, 'msg' => '请输入时长', 'data' => null]; } $map = []; $map[] = ['status', '=', 1]; $map[] = ['is_delete', '=', 1]; $info = db::name('blind_box_term')->where($map)->find(); if($info){ return ['code' => 201, 'msg' => '已存在未开启活动,请勿重新添加', 'data' => null]; } try { //添加 $insert = []; $insert['time_long'] = $data['time_long']; $insert['tid'] = 102; $insert['add_time'] = time(); $result = db::name('blind_box_term')->insert($insert); return ['code' => 200, 'msg' => '添加成功', 'data' => null]; } catch (\Exception $e) { dump($e); return ['code' => 201, 'msg' => '添加失败', 'data' => null]; } } //修改期数 public function edit_term($data) { $term_info = Db::name('blind_box_term')->find($data['id']); if(empty($term_info)){ return ['code' => 201, 'msg' => '信息不存在', 'data' => null]; } if ($term_info['status'] != 1) { return ['code' => 201, 'msg' => '该期已启用,无法修改', 'data' => null]; } if(empty($data['time_long'])) { return ['code' => 201, 'msg' => '请输入活动时长', 'data' => null]; } try { //添加 $insert = []; $insert['time_long'] = $data['time_long']; $insert['update_time'] = time(); $result = db::name('blind_box_term')->where('id', $data['id'])->update($insert); return ['code' => 200, 'msg' => '修改成功', 'data' => null]; } catch (\Exception $e) { return ['code' => 201, 'msg' => '修改失败', 'data' => null]; } } //获取期数信息 public function get_term_info($id) { $map = []; $map[] = ['id', '=', $id]; $config_info = db::name('blind_box_term')->where($map)->find(); if (empty($config_info)) { return ['code' => 201, 'msg' => '信息不存在', 'data' => '']; } return ['code' => 200, 'msg' => '获取成功', 'data' => $config_info]; } public function del_term($id) { $map = []; $map[] = ['id', '=', $id]; $term_info = db::name('blind_box_term')->where($map)->find(); if (empty($term_info)) { return ['code' => 201, 'msg' => '期数不存在', 'data' => '']; } if ($term_info['status'] != 1) { return ['code' => 201, 'msg' => '该期已启用,无法删除', 'data' => null]; } try { $result = db::name('blind_box_term')->where('id', $id)->update(['is_delete' => 2, 'delete_time' => time()]); return ['code' => 200, 'msg' => '删除成功', 'data' => null]; } catch (\Exception $e) { dump($e); return ['code' => 201, 'msg' => '删除失败', 'data' => null]; } } //开启活动 public function open_term($id) { $map = []; $map[] = ['id', '=', $id]; $map[] = ['is_delete', '=', 1]; $term_info = db::name('blind_box_term')->where($map)->find(); if (empty($term_info)) { return ['code' => 201, 'msg' => '期数不存在', 'data' => '']; } if ($term_info['status'] != 1) { return ['code' => 201, 'msg' => '该期已启用,无法开启', 'data' => null]; } $map = []; $map[] = ['is_delete', '=', 1]; $map[] = ['status', '=', 2]; $has_open_term = db::name('blind_box_term')->where($map)->find(); if($has_open_term){ return ['code' => 201, 'msg' => '已有进行中的期数,请勿重复开启', 'data' => null]; } //获取锁定礼物 $lock_gift_list = db::name('blind_box_config') ->where('tid', 102) ->select(); $lock_gid_arr = []; $lock_stage_gid_list = []; foreach($lock_gift_list as $v) { if ($v['stage'] > 0) { if($v['bind_rate'] > 0) { for($i=1; $i<=$v['bind_rate']; $i++) { $lock_gid_arr[$v['stage']][] = $v['gid']; } } else { $lock_gid_arr[$v['stage']][] = $v['gid']; } $lock_stage_gid_list[$v['stage'] . '-' . $v['gid']] = $v; } } $stage_list = Db::name('blind_box_stage')->order('stage asc')->select(); $params = []; foreach ($stage_list as $v) { $stage = $v['stage']; $gid_arr = $lock_gid_arr[$v['stage']] ?? []; if (empty($gid_arr)) { return ['code' => 201, 'msg' => '阶段' . $stage . '未配置礼物', 'data' => null]; } shuffle($gid_arr); $gid = $gid_arr[array_rand($gid_arr)]; $gid_info = $lock_stage_gid_list[$stage . '-' . $gid] ?? []; if (empty($gid_info)) { return ['code' => 201, 'msg' => '阶段' . $stage . '未配置礼物', 'data' => null]; } $temp = [ 'term_id' => $id, 'gid' => $gid, 'num' => $v['num'], 'multi' => $v['multi'], 'time_long' => $gid_info['time_long'], 'stage' => $v['stage'], 'add_time' => time(), 'pattern_id' => $v['pattern_id'], 'reward_gid' => $v['gid'], 'reward_gid_r' => $v['gid_r'], ]; $params[] = $temp; } $end_time = $term_info['time_long'] * 60; $end_time = $end_time + time(); $update_data = [ 'status' => 2, 'start_time' => time(), 'end_time' => $end_time, 'real_end_time' => $end_time, 'update_time' => time(), 'pattern_id' => 1, ]; Db::startTrans(); try { Db::name('blind_box_term')->where('id', $id)->update($update_data); Db::name('blind_box_term_params')->insertAll($params); Db::commit(); $push_data = [ 'code' => 312, 'msg' => '盲盒巡乐开始', 'data' => [ 'term_id' => $id, 'over_time' => $term_info['time_long'] * 60 ] ]; model('api/WebSocketPush')->send_to_all($push_data); return ['code' => 200, 'msg' => '开启成功', 'data' => null]; } catch (\Exception $e) { Db::rollback(); // dump($e); // 回滚事务 return ['code' => 201, 'msg' => '开启失败', 'data' => null]; } } }