StoreSeckillDao.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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
  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. * @param bool $search
  34. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  35. * @throws \ReflectionException
  36. */
  37. public function search(array $where = [], bool $search = false)
  38. {
  39. return parent::search($where)->when(isset($where['seckllTime']), function ($query) use ($where) {
  40. [$startTime, $stopTime] = is_array($where['seckllTime']) ? $where['seckllTime'] : [time(), time() - 86400];
  41. $query->where('start_time', '<=', $startTime)->where('stop_time', '>=', $stopTime);
  42. })->when(isset($where['sid']) && $where['sid'], function ($query) use ($where) {
  43. $query->whereIn('product_id', function ($query) use ($where) {
  44. $query->name('store_product_cate')->where('cate_id', $where['sid'])->field('product_id')->select();
  45. });
  46. })->when(isset($where['cid']) && $where['cid'], function ($query) use ($where) {
  47. $query->whereIn('product_id', function ($query) use ($where) {
  48. $query->name('store_product_cate')->whereIn('cate_id', function ($query) use ($where) {
  49. $query->name('store_category')->where('pid', $where['cid'])->field('id')->select();
  50. })->field('product_id')->select();
  51. });
  52. })->when(isset($where['storeProductId']), function ($query) {
  53. $query->where('product_id', 'IN', function ($query) {
  54. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  55. });
  56. })->when(isset($where['time_id']) && $where['time_id'], function ($query) use ($where) {
  57. $query->where('time_id', $where['time_id']);
  58. });
  59. }
  60. /**
  61. * 条件获取数量
  62. * @param array $where
  63. * @return int
  64. * @throws \ReflectionException
  65. */
  66. public function getCount(array $where)
  67. {
  68. return $this->search($where)->count();
  69. }
  70. /**获取秒杀列表
  71. * @param array $where
  72. * @param int $page
  73. * @param int $limit
  74. * @return array
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public function getList(array $where, int $page = 0, int $limit = 0)
  80. {
  81. return $this->search($where)->where('is_del', 0)
  82. ->when(isset($where['start_status']) && $where['start_status'] !== '', function ($query) use ($where) {
  83. $time = time();
  84. switch ($where['start_status']) {
  85. case -1:
  86. $query->where('stop_time', '<', $time - 86400)->whereOr('status', 0);
  87. break;
  88. case 0:
  89. $query->where('start_time', '>', $time)->whereOr('status', 1);
  90. break;
  91. case 1:
  92. $query->where('start_time', '<=', $time)->where('stop_time', '>=', $time - 86400)->whereOr('status', 1);
  93. break;
  94. }
  95. })->when(isset($where['ids']) && $where['ids'], function ($query) use ($where) {
  96. if ((isset($where['priceOrder']) && $where['priceOrder'] != '') || (isset($where['salesOrder']) && $where['salesOrder'] != '')) {
  97. $query->whereIn('id', $where['ids']);
  98. } else {
  99. $query->whereIn('id', $where['ids'])->orderField('id', $where['ids'], 'asc');
  100. }
  101. })->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  102. $query->page($page, $limit);
  103. })->with(['product'])->order('sort desc,id desc')->select()->toArray();
  104. }
  105. /**获取秒杀列表
  106. * @param array $where
  107. * @param int $page
  108. * @param int $limit
  109. * @return array
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\DbException
  112. * @throws \think\db\exception\ModelNotFoundException
  113. */
  114. public function getHomeList(array $where, int $page = 0, int $limit = 0)
  115. {
  116. return $this->search($where)->where('is_del', 0)->where('status', 1)
  117. ->where('start_time', '<=', time())
  118. ->where('stop_time', '>=', time() - 86400)
  119. ->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  120. $query->page($page, $limit);
  121. })->when(isset($where['priceOrder']) && $where['priceOrder'] != '', function ($query) use ($where) {
  122. if ($where['priceOrder'] === 'desc') {
  123. $query->order("price desc");
  124. } else {
  125. $query->order("price asc");
  126. }
  127. })->when(isset($where['newsOrder']) && $where['newsOrder'] != '', function ($query) use ($where) {
  128. if ($where['newsOrder'] === 'news') {
  129. $query->order("id desc");
  130. }
  131. })->when(isset($where['salesOrder']) && $where['salesOrder'] != '', function ($query) use ($where) {
  132. if ($where['salesOrder'] === 'desc') {
  133. $query->order("sales desc");
  134. } else {
  135. $query->order("sales asc");
  136. }
  137. })->when(isset($where['ids']) && $where['ids'], function ($query) use ($where) {
  138. if ((isset($where['priceOrder']) && $where['priceOrder'] != '') || (isset($where['salesOrder']) && $where['salesOrder'] != '')) {
  139. $query->whereIn('id', $where['ids']);
  140. } else {
  141. $query->whereIn('id', $where['ids'])->orderField('id', $where['ids'], 'asc');
  142. }
  143. })->with(['product'])->select()->toArray();
  144. }
  145. /**
  146. * 根据商品id获取当前正在开启秒杀产品的列表以数组返回
  147. * @param array $ids 为空查询所有
  148. * @param array $field
  149. * @return array
  150. */
  151. public function getSeckillIdsArray(array $ids = [], array $field = [])
  152. {
  153. return $this->search(['is_del' => 0, 'status' => 1])
  154. ->where('start_time', '<=', time())
  155. ->where('stop_time', '>=', time() - 86400)
  156. ->when($ids, function ($query) use ($ids) {
  157. $query->whereIn('product_id', $ids);
  158. })->field($field)->select()->toArray();
  159. }
  160. /**
  161. * 获取某个时间段的秒杀列表
  162. * @param int $time
  163. * @param int $page
  164. * @param int $limit
  165. * @return array
  166. * @throws \think\db\exception\DataNotFoundException
  167. * @throws \think\db\exception\DbException
  168. * @throws \think\db\exception\ModelNotFoundException
  169. */
  170. public function getListByTime(int $time, int $page, int $limit)
  171. {
  172. return $this->search(['is_del' => 0, 'status' => 1])
  173. ->where('start_time', '<=', time())
  174. ->where('stop_time', '>=', time() - 86400)
  175. ->where('time_id', $time)
  176. ->where('product_id', 'IN', function ($query) {
  177. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  178. })->when($page != 0, function ($query) use ($page, $limit) {
  179. $query->page($page, $limit);
  180. })->order('sort desc,id desc')->select()->toArray();
  181. }
  182. /**
  183. * 根据id获取秒杀数据
  184. * @param array $ids
  185. * @param string $field
  186. * @return array
  187. * @throws \think\db\exception\DataNotFoundException
  188. * @throws \think\db\exception\DbException
  189. * @throws \think\db\exception\ModelNotFoundException
  190. */
  191. public function idSeckillList(array $ids, string $field)
  192. {
  193. return $this->getModel()->whereIn('id', $ids)->field($field)->select()->toArray();
  194. }
  195. /**获取一条秒杀商品
  196. * @param $id
  197. * @param $field
  198. * @return array|\think\Model|null
  199. * @throws \think\db\exception\DataNotFoundException
  200. * @throws \think\db\exception\DbException
  201. * @throws \think\db\exception\ModelNotFoundException
  202. */
  203. public function validProduct($id, $field)
  204. {
  205. $where = ['status' => 1, 'is_del' => 0];
  206. $time = time();
  207. return $this->search($where)
  208. ->where('id', $id)
  209. ->where('start_time', '<', $time)
  210. ->where('stop_time', '>', $time - 86400)
  211. ->field($field)->with(['product'])->find();
  212. }
  213. }