// +---------------------------------------------------------------------- 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(); } }