123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- declare (strict_types=1);
- namespace app\dao\activity\promotions;
- use app\dao\BaseDao;
- use app\model\activity\promotions\StorePromotions;
- class StorePromotionsDao extends BaseDao
- {
-
- protected function setModel(): string
- {
- return StorePromotions::class;
- }
-
- protected function search(array $where = [])
- {
- return parent::search($where)->when(isset($where['promotionsTime']), function ($query) use ($where) {
- [$startTime, $stopTime] = is_array($where['promotionsTime']) ? $where['promotionsTime'] : [time(), time()];
- $query->where('start_time', '<=', $startTime)->where('stop_time', '>=', $stopTime);
- })->when(isset($where['ids']) && $where['ids'], function($query) use($where) {
- $query->whereIn('id', $where['ids']);
- })->when(isset($where['not_ids']) && $where['not_ids'], function($query) use($where) {
- $query->whereNotIn('id', $where['not_ids']);
- })->when(isset($where['applicable_type']) && $where['applicable_type'], function($query) use($where) {
- $query->whereIn('applicable_type', $where['applicable_type']);
- })->when(isset($where['start_status']) && $where['start_status'] !== '', function ($query) use ($where) {
- $time = time();
- switch ($where['start_status']) {
- case -1:
- $query->where(function ($q) use ($time) {
- $q->where('stop_time', '<', $time)->whereOr('status', '0');
- });
- break;
- case 0:
- $query->where('start_time', '>', $time)->where('status', 1);
- break;
- case 1:
- $query->where('start_time', '<=', $time)->where('stop_time', '>=', $time)->where('status', 1);
- break;
- }
- })->when(isset($where['product_id']) && $where['product_id'], function ($query) use ($where) {
- $query->where(function ($q) use ($where) {
- $q->whereOr('product_partake_type', 1)
- ->whereOr(function ($w) use ($where) {
- $w->where('product_partake_type', 2)->whereIn('id', function ($a) use ($where) {
- $a->name('store_promotions_auxiliary')->field('promotions_id')->where('type', 1)->where('product_partake_type', 2)->where(function ($p) use ($where) {
- if(is_array($where['product_id'])){
- $p->whereIn('product_id', $where['product_id']);
- } else {
- $p->where('product_id', $where['product_id']);
- }
- });
- });
- })->whereOr(function ($b) use ($where) {
- $b->where('product_partake_type', 4)->whereIn('id', function ($a) use ($where) {
- $a->name('store_promotions_auxiliary')->field('promotions_id')->where('type', 1)->where('product_partake_type', 2)->where(function ($p) use ($where) {
- if(is_array($where['product_id'])){
- $p->whereIn('product_id', $where['product_id']);
- } else {
- $p->where('product_id', $where['product_id']);
- }
- });
- });
- });
- });
- });
- }
-
- public function getList(array $where, string $field = '*', int $page = 0, int $limit = 0, array $with = [], string $order = 'update_time desc,id desc', string $group = '')
- {
- return $this->search($where)->field($field)
- ->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
- $query->page($page, $limit);
- })->when($with, function ($query) use ($with) {
- $query->with($with);
- })->order($order)->when($group, function($query) use($group) {
- $query->group($group);
- })->select()->toArray();
- }
-
- public function validPromotions(int $id, string $field = '*')
- {
- $where = ['status' => 1, 'is_del' => 0];
- $time = time();
- return $this->search($where)
- ->where('id', $id)
- ->where('start_time', '<=', $time)
- ->where('stop_time', '>=', $time)
- ->field($field)->find();
- }
-
- public function getOnePromotionsIds(int $id)
- {
- $result = $this->getModel()->where('id|pid', $id)->column('id');
- $res = [];
- if ($result) {
- $res = array_column($result, 'id');
- }
- return $res;
- }
- }
|