123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace app\dao\activity\discounts;
- use app\dao\BaseDao;
- use app\model\activity\discounts\StoreDiscounts;
- class StoreDiscountsDao extends BaseDao
- {
-
- protected function setModel(): string
- {
- return StoreDiscounts::class;
- }
- public function search(array $where = [])
- {
- return parent::search($where)
- ->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);
- });
- });
- });
- }
-
- public function getList($where, array $with = [], $page = 0, $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);
- })->order('sort desc,id desc')->select()->toArray();
- }
-
- 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)
- ->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);
- });
- })->with(['products'])
- ->when($page && $limit, function ($query) use ($page, $limit) {
- $query->page($page, $limit);
- })->order('sort desc,id desc')->select()->toArray();
- }
-
- 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();
- }
- public function decLimitNum($id, $num = 1)
- {
- return $this->getModel()->where('id', $id)->dec('limit_num', $num)->update();
- }
- public function incLimitNum(int $id, $num = 1)
- {
- return $this->getModel()->where('id', $id)->inc('limit_num', $num)->update();
- }
- }
|