StoreSeckillDao.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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\seckill;
  13. use app\dao\BaseDao;
  14. use app\model\activity\seckill\StoreSeckill;
  15. /**
  16. * 秒杀商品
  17. * Class StoreSeckillDao
  18. * @package app\dao\activity\seckill
  19. */
  20. class StoreSeckillDao extends BaseDao
  21. {
  22. /**
  23. * 设置模型
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return StoreSeckill::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['seckllTime']), function ($query) use ($where) {
  38. [$startTime, $stopTime] = is_array($where['seckllTime']) ? $where['seckllTime'] : [time(), time() - 86400];
  39. $query->where('start_time', '<=', $startTime)->where('stop_time', '>=', $stopTime);
  40. })->when(isset($where['storeProductId']), function ($query) {
  41. $query->where('product_id', 'IN', function ($query) {
  42. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  43. });
  44. })->when(isset($where['start_status']) && $where['start_status'] !== '', function ($query) use ($where) {
  45. $time = time();
  46. switch ($where['start_status']) {
  47. case -1:
  48. $query->where(function ($q) use ($time) {
  49. $q->where('stop_time', '<', $time - 86400)->whereOr('status', '0');
  50. });
  51. break;
  52. case 0:
  53. $query->where('start_time', '>', $time)->where('status', 1);
  54. break;
  55. case 1:
  56. $query->where('start_time', '<=', $time)->where('stop_time', '>=', $time - 86400)->where('status', 1);
  57. break;
  58. }
  59. });
  60. }
  61. /**
  62. * 获取某个活动下的秒杀商品ids
  63. * @param array $where
  64. * @return array
  65. */
  66. public function getActivitySeckillIds(array $where)
  67. {
  68. return $this->search($where)->column('id');
  69. }
  70. /**
  71. * 获取秒杀列表
  72. * @param array $where
  73. * @param int $page
  74. * @param int $limit
  75. * @param array $with
  76. * @return array
  77. * @throws \think\db\exception\DataNotFoundException
  78. * @throws \think\db\exception\DbException
  79. * @throws \think\db\exception\ModelNotFoundException
  80. */
  81. public function getList(array $where, int $page = 0, int $limit = 0, array $with = [])
  82. {
  83. return $this->search($where)
  84. ->when($with, function ($query) use ($with) {
  85. $query->with($with);
  86. })->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  87. $query->page($page, $limit);
  88. })->order('sort desc,id desc')->select()->toArray();
  89. }
  90. /**
  91. * 根据商品id获取当前正在开启秒杀产品的列表以数组返回
  92. * @param array $ids
  93. * @param array $field
  94. * @return array
  95. */
  96. public function getSeckillIdsArray(array $ids, array $field = [])
  97. {
  98. return $this->search(['is_del' => 0, 'status' => 1])
  99. ->where('start_time', '<=', time())
  100. ->where('stop_time', '>=', time() - 86400)
  101. ->whereIn('product_id', $ids)
  102. ->field($field)->select()->toArray();
  103. }
  104. /**
  105. * 获取某个时间段的秒杀列表
  106. * @param array $activity_id
  107. * @param array $ids
  108. * @param int $page
  109. * @param int $limit
  110. * @param bool $isStore
  111. * @return array
  112. * @throws \think\db\exception\DataNotFoundException
  113. * @throws \think\db\exception\DbException
  114. * @throws \think\db\exception\ModelNotFoundException
  115. */
  116. public function getListByTime(array $activity_id, array $ids = [], int $page = 0, int $limit = 0, bool $isStore = false)
  117. {
  118. if ($activity_id == 0) return [];
  119. return $this->search(['is_del' => 0, 'status' => 1])
  120. ->where('start_time', '<=', time())
  121. ->where('stop_time', '>=', time() - 86400)
  122. ->when($activity_id, function ($query) use ($activity_id) {
  123. $query->whereIn('activity_id', $activity_id);
  124. })->where('product_id', 'IN', function ($query) {
  125. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  126. })->when($ids, function($query) use($ids) {
  127. $query->whereIn('product_id', $ids);
  128. })->when($isStore, function($query) {
  129. $query->where(function ($q) {
  130. $q->whereOr(function ($c) {
  131. $c->whereFindInSet('delivery_type', 2);
  132. })->whereOr(function ($d) {
  133. $d->whereFindInSet('delivery_type', 3);
  134. });
  135. });
  136. })->when($page && $limit, function ($query) use ($page, $limit) {
  137. $query->page($page, $limit);
  138. })->order('sort desc,id desc')->select()->toArray();
  139. }
  140. /**
  141. * 获取正在开启的秒杀总数
  142. * @param array $activity_id
  143. * @param $ids
  144. * @param $not_ids
  145. * @param bool $isStore
  146. * @return int
  147. * @throws \think\db\exception\DbException
  148. */
  149. public function getTimeCount(array $activity_id, $ids = [], $not_ids = [], bool $isStore = true)
  150. {
  151. if ($activity_id == 0) return 0;
  152. return $this->search(['is_del' => 0, 'status' => 1])
  153. ->where('start_time', '<=', time())
  154. ->where('stop_time', '>=', time() - 86400)
  155. ->when($activity_id, function ($query) use ($activity_id) {
  156. $query->whereIn('activity_id', $activity_id);
  157. })->where('product_id', 'IN', function ($query) {
  158. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  159. })->when($ids, function($query) use($ids) {
  160. $query->whereIn('product_id', $ids);
  161. })->when($not_ids, function($query) use($not_ids) {
  162. $query->whereNotIn('product_id', $not_ids);
  163. })->when($isStore, function($query) {
  164. $query->where(function ($q) {
  165. $q->whereOr(function ($c) {
  166. $c->whereFindInSet('delivery_type', 2);
  167. })->whereOr(function ($d) {
  168. $d->whereFindInSet('delivery_type', 3);
  169. });
  170. });
  171. })->count();
  172. }
  173. /**
  174. * 根据id获取秒杀数据
  175. * @param array $ids
  176. * @param string $field
  177. * @return array
  178. * @throws \think\db\exception\DataNotFoundException
  179. * @throws \think\db\exception\DbException
  180. * @throws \think\db\exception\ModelNotFoundException
  181. */
  182. public function idSeckillList(array $ids, string $field)
  183. {
  184. return $this->getModel()->whereIn('id', $ids)->field($field)->select()->toArray();
  185. }
  186. /**获取一条秒杀商品
  187. * @param $id
  188. * @param $field
  189. * @return array|\think\Model|null
  190. * @throws \think\db\exception\DataNotFoundException
  191. * @throws \think\db\exception\DbException
  192. * @throws \think\db\exception\ModelNotFoundException
  193. */
  194. public function validProduct($id, $field)
  195. {
  196. $where = ['status' => 1, 'is_del' => 0];
  197. $time = time();
  198. return $this->search($where)
  199. ->where('id', $id)
  200. ->where('start_time', '<', $time)
  201. ->where('stop_time', '>', $time - 86400)
  202. ->field($field)->with(['product'])->find();
  203. }
  204. }