StorePromotionsDao.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\dao\activity\promotions;
  13. use app\dao\BaseDao;
  14. use app\model\activity\promotions\StorePromotions;
  15. /**
  16. * 促销活动
  17. * Class StorePromotionsDao
  18. * @package app\dao\activity\promotions
  19. */
  20. class StorePromotionsDao extends BaseDao
  21. {
  22. /**
  23. * 设置模型
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return StorePromotions::class;
  29. }
  30. /**
  31. * 搜索
  32. * @param array $where
  33. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  34. */
  35. protected function search(array $where = [])
  36. {
  37. return parent::search($where)->when(isset($where['promotionsTime']), function ($query) use ($where) {
  38. [$startTime, $stopTime] = is_array($where['promotionsTime']) ? $where['promotionsTime'] : [time(), time()];
  39. $query->where('start_time', '<=', $startTime)->where('stop_time', '>=', $stopTime);
  40. })->when(isset($where['ids']) && $where['ids'], function($query) use($where) {
  41. $query->whereIn('id', $where['ids']);
  42. })->when(isset($where['not_ids']) && $where['not_ids'], function($query) use($where) {
  43. $query->whereNotIn('id', $where['not_ids']);
  44. })->when(isset($where['applicable_type']) && $where['applicable_type'], function($query) use($where) {
  45. $query->whereIn('applicable_type', $where['applicable_type']);
  46. })->when(isset($where['start_status']) && $where['start_status'] !== '', function ($query) use ($where) {
  47. $time = time();
  48. switch ($where['start_status']) {
  49. case -1:
  50. $query->where(function ($q) use ($time) {
  51. $q->where('stop_time', '<', $time)->whereOr('status', '0');
  52. });
  53. break;
  54. case 0:
  55. $query->where('start_time', '>', $time)->where('status', 1);
  56. break;
  57. case 1:
  58. $query->where('start_time', '<=', $time)->where('stop_time', '>=', $time)->where('status', 1);
  59. break;
  60. }
  61. })->when(isset($where['product_id']) && $where['product_id'], function ($query) use ($where) {
  62. $query->where(function ($q) use ($where) {
  63. $q->whereOr('product_partake_type', 1)
  64. ->whereOr(function ($w) use ($where) {
  65. $w->where('product_partake_type', 2)->whereIn('id', function ($a) use ($where) {
  66. $a->name('store_promotions_auxiliary')->field('promotions_id')->where('type', 1)->where('product_partake_type', 2)->where(function ($p) use ($where) {
  67. if(is_array($where['product_id'])){
  68. $p->whereIn('product_id', $where['product_id']);
  69. } else {
  70. $p->where('product_id', $where['product_id']);
  71. }
  72. });
  73. });
  74. })->whereOr(function ($b) use ($where) {
  75. $b->where('product_partake_type', 4)->whereIn('id', function ($a) use ($where) {
  76. $a->name('store_promotions_auxiliary')->field('promotions_id')->where('type', 1)->where('product_partake_type', 2)->where(function ($p) use ($where) {
  77. if(is_array($where['product_id'])){
  78. $p->whereIn('product_id', $where['product_id']);
  79. } else {
  80. $p->where('product_id', $where['product_id']);
  81. }
  82. });
  83. });
  84. });
  85. });
  86. });
  87. }
  88. /**
  89. * 获取促销活动列表
  90. * @param array $where
  91. * @param string $field
  92. * @param int $page
  93. * @param int $limit
  94. * @param array $with
  95. * @param string $order
  96. * @param string $group
  97. * @return array
  98. * @throws \think\db\exception\DataNotFoundException
  99. * @throws \think\db\exception\DbException
  100. * @throws \think\db\exception\ModelNotFoundException
  101. */
  102. public function getList(array $where, string $field = '*', int $page = 0, int $limit = 0, array $with = [], string $order = 'update_time desc,id desc', string $group = '')
  103. {
  104. return $this->search($where)->field($field)
  105. ->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  106. $query->page($page, $limit);
  107. })->when($with, function ($query) use ($with) {
  108. $query->with($with);
  109. })->order($order)->when($group, function($query) use($group) {
  110. $query->group($group);
  111. })->select()->toArray();
  112. }
  113. /**
  114. * 获取一条活动
  115. * @param int $id
  116. * @param string $field
  117. * @return array|\think\Model|null
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\DbException
  120. * @throws \think\db\exception\ModelNotFoundException
  121. */
  122. public function validPromotions(int $id, string $field = '*')
  123. {
  124. $where = ['status' => 1, 'is_del' => 0];
  125. $time = time();
  126. return $this->search($where)
  127. ->where('id', $id)
  128. ->where('start_time', '<=', $time)
  129. ->where('stop_time', '>=', $time)
  130. ->field($field)->find();
  131. }
  132. /**
  133. * 获取一个活动包含子集的所有ID
  134. * @param int $id
  135. * @return array
  136. */
  137. public function getOnePromotionsIds(int $id)
  138. {
  139. $result = $this->getModel()->where('id|pid', $id)->column('id');
  140. $res = [];
  141. if ($result) {
  142. $res = array_column($result, 'id');
  143. }
  144. return $res;
  145. }
  146. }