Files
huangjingfen/pro_v3.5.1/app/dao/activity/lottery/LuckLotteryDao.php
panchengyong c1e74d8e68 chore(php): 统一 ScottPan 文件头与注释域名替换
- 按 docs/renew-code-comment.md 将 PHP 文件头改为带边框的 Author 注释\n- 注释中的 crmeb.com 替换为 uj345.cn(代码字符串中的外链未改)\n- 新增 docs/renew-code-comment.md 说明

Made-with: Cursor
2026-03-29 11:22:58 +08:00

146 lines
5.5 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | Author: ScottPan Team
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\dao\activity\lottery;
use app\dao\BaseDao;
use app\model\activity\lottery\LuckLottery;
/**
* 抽奖活动
* Class LuckLotteryDao
* @package app\dao\activity\lottery
*/
class LuckLotteryDao extends BaseDao
{
/**
* 设置模型
* @return string
*/
protected function setModel(): string
{
return LuckLottery::class;
}
/**
* 搜索查询
* @param array $data 查询条件
* @param bool $search 是否调用父类搜索
* @return \think\db\BaseQuery
*/
public function search(array $data = [], bool $search = false)
{
return parent::search($data, $search)->when(isset($data['id']) && $data['id'], function ($query) use ($data) {
$query->where('id', $data['id']);
})->when(isset($data['start']) && $data['start'] !== '', function ($query) use ($data) {
$time = time();
switch ($data['start']) {
case 0:
$query->where('start_time', '>', $time)->where('status', 1);
break;
case 1:
$query->where('status', 1)->where(function ($query1) use ($time) {
$query1->where(function ($query2) use ($time) {
$query2->where('start_time', '<=', $time)->where('end_time', '>=', $time);
})->whereOr(function ($query3) {
$query3->where('start_time', 0)->where('end_time', 0);
});
});
break;
case 2:
$query->where(function ($query1) use ($time) {
$query1->where('end_time', '<', $time)->whereOr('status', 0);
});
break;
}
})->when(isset($data['time_ranges']) && $data['time_ranges'] !== '', function ($query) use ($data) {
$time_ranges = explode('-', $data['time_ranges']);
$query->where(function ($query) use ($time_ranges) {
$time_range1 = strtotime($time_ranges[0]);
$time_range2 = strtotime($time_ranges[1]);
$query->where(function ($query1) use ($time_range1) {
$query1->where('start_time', '<=', $time_range1)->where('end_time', '>=',$time_range1);
})->whereOr(function ($query2) use ($time_range2) {
$query2->where('start_time', '<=', $time_range2)->where('end_time', '>=', $time_range2);
});
});
});
}
/**
* 抽奖活动列表
* @param array $where
* @param string $field
* @param array $with
* @param int $page
* @param int $limit
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getList(array $where, string $field = '*', array $with = [], int $page = 0, int $limit = 0)
{
return $this->search($where)->field($field)->when($with, function ($query) use ($with) {
$query->with($with);
})->when($page && $limit, function ($query) use ($page, $limit) {
$query->page($page, $limit);
})->with(['records' => function ($query) {
$query->field([
'lottery_id',
'COUNT(DISTINCT uid) AS total_user', // 总参与人数
'COUNT(DISTINCT CASE WHEN type != 1 THEN uid END) AS wins_user', // 中奖人数
'COUNT(*) AS total_num', // 总参与次数
'SUM(type != 1) AS wins_num', // 中奖次数
])->group('lottery_id');
}])->order('add_time desc')->select()->toArray();
}
/**
* 获取单个活动
* @param int $id
* @param string $field
* @param array|string[] $with
* @param bool $is_doing
* @return array|\think\Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getLottery(int $id, string $field = '*', array $with = ['prize'], bool $is_doing = false)
{
$where = ['id' => $id];
$where['is_del'] = 0;
if ($is_doing) $where['start'] = 1;
return $this->search($where)->field($field)->when($with, function ($query) use ($with) {
$query->with($with);
})->find();
}
/**
* 获取某个抽奖类型的一条抽奖数据
* @param int $factor
* @param string $field
* @param array|string[] $with
* @param bool $is_doing
* @return array|\think\Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getFactorLottery(int $factor = 1, string $field = '*', array $with = ['prize'], bool $is_doing = true)
{
$where = ['factor' => $factor, 'is_del' => 0, 'is_use' => 1];
if ($is_doing) $where['start'] = 1;
return $this->search($where)->field($field)->when($with, function ($query) use ($with) {
$query->with($with);
})->order('id desc')->find();
}
}