Files
huangjingfen/pro_v3.5.1/app/dao/activity/discounts/StoreDiscountsDao.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
4.8 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | Author: ScottPan Team
// +----------------------------------------------------------------------
namespace app\dao\activity\discounts;
use app\dao\BaseDao;
use app\model\activity\discounts\StoreDiscounts;
/**
* 优惠套餐
* Class StoreDiscountsDao
* @package app\dao\activity\discounts
*/
class StoreDiscountsDao extends BaseDao
{
/**
* 设置模型
* @return string
*/
protected function setModel(): string
{
return StoreDiscounts::class;
}
/**
* 搜索查询
* @param array $where 查询条件
* @param bool $search 是否调用父类搜索
* @return \think\db\BaseQuery
*/
public function search(array $where = [], bool $search = false)
{
return parent::search($where, $search)
->when(isset($where['is_time']) && $where['is_time'] == 1, function ($query) {
$query->where(function ($q) {
$q->where(function ($query) {
$query->where('start_time', '<=', time())->where('stop_time', '>=', time());
})->whereOr(function ($query) {
$query->where('start_time', 0)->where('stop_time', 0);
});
});
});
}
/**
* 获取列表
* @param $where
* @param array $with
* @param $page
* @param $limit
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getList($where, array $with = [], int $page = 0, int $limit = 0)
{
return $this->search($where)->where('is_del', 0)
->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
$query->page($page, $limit);
})->when($with, function ($query) use ($with) {
$query->with($with);
})->when(!$page && $limit, function ($query) use ($limit) {
$query->limit($limit);
})->order('sort desc,id desc')->select()->toArray();
}
/**
* 优惠套餐列表
* @param int $product_id
* @param string $field
* @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 getDiscounts(int $product_id, string $field = '*', int $page = 0, int $limit = 0)
{
return $this->search(['product_ids' => $product_id])->field($field)
->where('is_del', 0)->where('status', 1)
->with(['products' => function ($query) {
$query->field('*,title as store_name')->with(['product']);
}])->where(function ($query) {
$query->where(function ($query) {
$query->where('start_time', '<=', time())->where('stop_time', '>=', time());
})->whereOr(function ($query) {
$query->where('start_time', 0)->where('stop_time', 0);
});
})->where(function ($query) {
$query->where(function ($query) {
$query->where('is_limit', 0);
})->whereOr(function ($query) {
$query->where('is_limit', 1)->where('limit_num', '>', 0);
});
})->when($page && $limit, function ($query) use ($page, $limit) {
$query->page($page, $limit);
})->when(!$page && $limit, function ($query) use ($limit) {
$query->limit($limit);
})->order('sort desc,id desc')->select()->toArray();
}
/**
* 优惠套餐数量
* @return int
*/
public function getDiscountsCount()
{
return $this->getModel()
->where('is_del', 0)->where('status', 1)
->where(function ($query) {
$query->where(function ($query) {
$query->where('start_time', '<=', time())->where('stop_time', '>=', time());
})->whereOr(function ($query) {
$query->where('start_time', 0)->where('stop_time', 0);
});
})->count();
}
/**
* 减少限购数量
* @param int $id 套餐ID
* @param int $num 减少数量
* @return int
*/
public function decLimitNum($id, $num = 1)
{
return $this->getModel()->where('id', $id)->dec('limit_num', $num)->update();
}
/**
* 增加限购数量
* @param int $id 套餐ID
* @param int $num 增加数量
* @return int
*/
public function incLimitNum(int $id, $num = 1)
{
return $this->getModel()->where('id', $id)->inc('limit_num', $num)->update();
}
}