Files
webman_duanju/app/czg/controller/DiscSpinningAmountController.php
2025-08-19 17:30:51 +08:00

122 lines
3.8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\czg\controller;
use app\common\controller\Backend;
use app\common\library\DatabaseRoute;
use think\facade\Cache;
use think\facade\Db;
class DiscSpinningAmountController extends Backend
{
protected array $noNeedPermission = ['selectDiscSpinningAmount', 'insertDiscSpinningAmount', 'updateDiscSpinningAmount', 'deleteDiscSpinningAmount'];
// 查询现金红包 抽奖配置
public function selectDiscSpinningAmount()
{
$get = $this->request->get();
if(empty($get['page'])) {
$get['page'] = 1;
}
if(empty($get['limit'])) {
$get['limit'] = 20;
}
$db = Db::connect(get_slave_connect_name());
$count = $db->name('disc_spinning_amount')->where('type', $get['type'])->count();
// 构建查询条件
$res = $db->name('disc_spinning_amount')
->where('type', $get['type'])
->order('status', 'desc')
->order('num', 'asc')
->order('random', 'asc')
->order('max_amount', 'asc')
->limit(page($get['page'], $get['limit']))
->select()
->toArray();
foreach ($res as $k => &$v) {
$scaleFactor = 100;
$intValue = (int)round($v['max_amount'] * $scaleFactor, 0);
$v['max_amount'] = $intValue / $scaleFactor;
$intValue = (int)round($v['random'] * $scaleFactor, 0);
$v['random'] = $intValue / $scaleFactor;
}
// 转换为前端需要的格式
$data = [
'totalCount' => $count,
'pageSize' => $get['limit'],
'totalPage' => ceil($count / $get['limit']),
'currPage' => $get['page'],
'list' => null,
'records' => $res
];
$this->n_success(['data' => $data]);
}
// 添加现金红包 抽奖配置
public function insertDiscSpinningAmount()
{
$post = $this->request->post();
// 保存数据到数据库
$result = Db::name('disc_spinning_amount')->insert([
'name' => $post['name'],
'random' => $post['random'],
'max_amount' => $post['maxAmount'],
'type' => $post['type'],
'status' => $post['status'],
'num' => $post['num'] ?? 0,
]);
if (!$result) {
$this->error("保存转盘金额配置失败");
}
// 删除Redis缓存使用Cache门面
$cacheKey = "spinning:amount:{$post['type']}";
Cache::delete($cacheKey);
$this->success();
}
// 修改现金红包 抽奖配置
public function updateDiscSpinningAmount()
{
$post = $this->request->post();
// 保存数据到数据库
Db::name('disc_spinning_amount')->where(['id' => $post['id']])->update([
'name' => $post['name'],
'random' => $post['random'],
'max_amount' => $post['maxAmount'],
'type' => $post['type'],
'status' => $post['status'],
'num' => $post['num'],
]);
// if (!$result) {
// $this->error("保存转盘金额配置失败");
// }
// 删除Redis缓存使用Cache门面
$cacheKey = "spinning:amount:{$post['type']}";
Cache::delete($cacheKey);
$this->success();
}
public function deleteDiscSpinningAmount()
{
$post = $this->request->param();
// 保存数据到数据库
$result = Db::name('disc_spinning_amount')->where(['id' => $post['id']])->delete();
if (!$result) {
$this->error("保存转盘金额配置失败");
}
// 删除Redis缓存使用Cache门面
$cacheKey = "spinning:amount";
Cache::delete($cacheKey);
$this->success();
}
}