126 lines
4.3 KiB
PHP
126 lines
4.3 KiB
PHP
<?php
|
||
|
||
namespace app\admin\controller;
|
||
|
||
use app\common\controller\Backend;
|
||
use app\common\library\DatabaseRoute;
|
||
use think\facade\Db;
|
||
|
||
class DiscSpinning extends Backend
|
||
{
|
||
|
||
protected array $noNeedPermission = ['selectDiscSpinning', 'updateDiscSpinning', 'deleteDiscSpinning', 'insertDiscSpinning'];
|
||
|
||
// 查询大转盘
|
||
public function selectDiscSpinning()
|
||
{
|
||
$get = $this->request->get();
|
||
$db = Db::connect(get_slave_connect_name());
|
||
$count = $db->name('disc_spinning')->where(['disc_type' => $get['source']])->count();
|
||
$res = $db->name('disc_spinning')->where(['disc_type' => $get['source']])->limit(page($get['page'], $get['limit']), $get['limit'])->order('disc_type', 'asc')->order('odds', 'asc')->select()->toArray();
|
||
$this->n_success(['data' => [
|
||
'totalCount' => $count,
|
||
'pageSize' => $get['limit'],
|
||
'totalPage' => ceil($count / $get['limit']),
|
||
'currPage' => $get['page'],
|
||
'list' => null,
|
||
'records' => $res
|
||
]]);
|
||
}
|
||
|
||
|
||
// 修改大转盘
|
||
public function updateDiscSpinning()
|
||
{
|
||
$post = $this->request->post();
|
||
// if(empty($post['name']) || empty($post['url']) || empty($post['type']) || empty($post['odds']) || empty($post['discType']) || empty($post['id'])) {
|
||
// $this->error('参数不完整');
|
||
// }
|
||
|
||
// 查询指定类型的奖品列表(按type和id升序排列)
|
||
$prizes = Db::name('disc_spinning')
|
||
->where('disc_type', $post['discType'])
|
||
->order('type', 'asc')
|
||
->order('id', 'asc')
|
||
->select()
|
||
->toArray();
|
||
|
||
// 处理奖品概率累加
|
||
$upPrizes = [];
|
||
$number = 0; // 概率累加值(PHP使用浮点数,需注意精度问题)
|
||
|
||
foreach ($prizes as $key => $prize) {
|
||
// 如果是当前编辑的奖品,使用传入的数据覆盖
|
||
if ($prize['id'] == $post['id']) {
|
||
$prizes[$key] = $post;
|
||
$prize = $post;
|
||
}
|
||
|
||
// 累加概率
|
||
$number += $prize['odds'];
|
||
$prizes[$key]['number'] = $number; // 记录累加后的概率值
|
||
|
||
// 添加到结果列表
|
||
$upPrizes[] = convertKeysCamelToSnakeRecursive($prizes[$key]);
|
||
}
|
||
|
||
// 验证概率总和是否超过100
|
||
if ($number > 100) {
|
||
$this->error("中奖概率总和 不可超过100");
|
||
}
|
||
|
||
\app\admin\model\DiscSpinning::updateBatchById($upPrizes);
|
||
$this->success();
|
||
}
|
||
|
||
// 删除大转盘
|
||
public function deleteDiscSpinning()
|
||
{
|
||
$post = $this->request->post();
|
||
if(empty($post['id'])) {
|
||
$this->error('id不能为空');
|
||
}
|
||
$db = Db::connect(get_master_connect_name());
|
||
if($db->name('disc_spinning')->where(['id' => $post['id']])->delete()) {
|
||
$this->success();
|
||
}
|
||
$this->error();
|
||
}
|
||
|
||
// 转盘添加抽奖项
|
||
public function insertDiscSpinning()
|
||
{
|
||
$post = $this->request->post();
|
||
if(empty($post['name']) || empty($post['url']) || empty($post['type']) || empty($post['odds']) || empty($post['discType'])) {
|
||
$this->error('参数不完整');
|
||
}
|
||
// 查询指定类型的奖品列表(按type和id升序排列)
|
||
$prizes = Db::name('disc_spinning')
|
||
->where('disc_type', $post['discType'])
|
||
->order('type', 'asc')
|
||
->order('id', 'asc')
|
||
->select()
|
||
->toArray();
|
||
// 计算当前奖品总概率
|
||
$totalOdds = 0;
|
||
foreach ($prizes as &$prize) {
|
||
$totalOdds += $prize['odds'];
|
||
$prize['number'] = $totalOdds;
|
||
}
|
||
unset($prize); // 释放引用
|
||
// 计算新增奖品后的总概率
|
||
$newTotalOdds = $totalOdds + $post['odds'];
|
||
// 验证概率总和是否超过100
|
||
if ($newTotalOdds > 100) {
|
||
$this->error("中奖概率总和 不可超过100");
|
||
}
|
||
// 设置创建时间(使用当前时间戳)
|
||
$post['create_time'] = date('Y-m-d H:i:s');
|
||
$post['number'] = $post['odds'];
|
||
\app\admin\model\DiscSpinning::updateBatchById($prizes);
|
||
Db::name('disc_spinning')->insert($post);
|
||
$this->success();
|
||
}
|
||
|
||
|
||
} |