StoreSeckillDao.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. });
  57. }
  58. /**
  59. * 条件获取数量
  60. * @param array $where
  61. * @return int
  62. * @throws \ReflectionException
  63. */
  64. public function getCount(array $where)
  65. {
  66. return $this->search($where)->count();
  67. }
  68. /**获取秒杀列表
  69. * @param array $where
  70. * @param int $page
  71. * @param int $limit
  72. * @return array
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. */
  77. public function getList(array $where, int $page = 0, int $limit = 0)
  78. {
  79. return $this->search($where)->where('is_del', 0)
  80. ->when(isset($where['start_status']) && $where['start_status'] !== '', function ($query) use ($where) {
  81. $time = time();
  82. switch ($where['start_status']) {
  83. case -1:
  84. $query->where('stop_time', '<', $time - 86400)->whereOr('status', 0);
  85. break;
  86. case 0:
  87. $query->where('start_time', '>', $time)->whereOr('status', 1);
  88. break;
  89. case 1:
  90. $query->where('start_time', '<=', $time)->where('stop_time', '>=', $time - 86400)->whereOr('status', 1);
  91. break;
  92. }
  93. })->when(isset($where['ids']) && $where['ids'], function ($query) use ($where) {
  94. if ((isset($where['priceOrder']) && $where['priceOrder'] != '') || (isset($where['salesOrder']) && $where['salesOrder'] != '')) {
  95. $query->whereIn('id', $where['ids']);
  96. } else {
  97. $query->whereIn('id', $where['ids'])->orderField('id', $where['ids'], 'asc');
  98. }
  99. })->when(isset($where['product_id']) && $where['product_id'] != 0, function ($query) use ($where) {
  100. $query->where('product_id', $where['product_id']);
  101. })->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  102. $query->page($page, $limit);
  103. })->when(isset($where['activity_name']) && $where['activity_name'] != '', function ($query) use ($where) {
  104. $query->whereIn('activity_id', function ($query) use ($where) {
  105. $query->name('store_activity')->whereLike('title', '%' . $where['activity_name'] . '%')->field('id')->select();
  106. });
  107. })->with(['product', 'attrs'])->order('sort desc,id desc')->select()->toArray();
  108. }
  109. /**获取秒杀列表
  110. * @param array $where
  111. * @param int $page
  112. * @param int $limit
  113. * @return array
  114. * @throws \think\db\exception\DataNotFoundException
  115. * @throws \think\db\exception\DbException
  116. * @throws \think\db\exception\ModelNotFoundException
  117. */
  118. public function getHomeList(array $where, int $page = 0, int $limit = 0)
  119. {
  120. return $this->search($where)->where('is_del', 0)->where('status', 1)
  121. ->where('start_time', '<=', time())
  122. ->where('stop_time', '>=', time() - 86400)
  123. ->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  124. $query->page($page, $limit);
  125. })->when(isset($where['priceOrder']) && $where['priceOrder'] != '', function ($query) use ($where) {
  126. if ($where['priceOrder'] === 'desc') {
  127. $query->order("price desc");
  128. } else {
  129. $query->order("price asc");
  130. }
  131. })->when(isset($where['newsOrder']) && $where['newsOrder'] != '', function ($query) use ($where) {
  132. if ($where['newsOrder'] === 'news') {
  133. $query->order("id desc");
  134. }
  135. })->when(isset($where['salesOrder']) && $where['salesOrder'] != '', function ($query) use ($where) {
  136. if ($where['salesOrder'] === 'desc') {
  137. $query->order("sales desc");
  138. } else {
  139. $query->order("sales asc");
  140. }
  141. })->when(isset($where['ids']) && $where['ids'], function ($query) use ($where) {
  142. if ((isset($where['priceOrder']) && $where['priceOrder'] != '') || (isset($where['salesOrder']) && $where['salesOrder'] != '')) {
  143. $query->whereIn('id', $where['ids']);
  144. } else {
  145. $query->whereIn('id', $where['ids'])->orderField('id', $where['ids'], 'asc');
  146. }
  147. })->when(isset($where['activity_name']) && $where['activity_name'] != '', function ($query) use ($where) {
  148. $query->whereIn('activity_id', function ($query) use ($where) {
  149. $query->name('store_activity')->whereLike('title', $where['activity_name'])->field('id')->select();
  150. });
  151. })->with(['product'])->select()->toArray();
  152. }
  153. /**
  154. * 根据商品id获取当前正在开启秒杀产品的列表以数组返回
  155. * @param array $ids 为空查询所有
  156. * @param array $field
  157. * @return array
  158. */
  159. public function getSeckillIdsArray(array $ids = [], array $field = [])
  160. {
  161. return $this->search(['is_del' => 0, 'status' => 1])
  162. ->where('start_time', '<=', time())
  163. ->where('stop_time', '>=', time() - 86400)
  164. ->when($ids, function ($query) use ($ids) {
  165. $query->whereIn('product_id', $ids);
  166. })->field($field)->select()->toArray();
  167. }
  168. /**
  169. * 获取某个时间段的秒杀列表
  170. * @param int $time
  171. * @param int $page
  172. * @param int $limit
  173. * @return array
  174. * @throws \think\db\exception\DataNotFoundException
  175. * @throws \think\db\exception\DbException
  176. * @throws \think\db\exception\ModelNotFoundException
  177. */
  178. public function getListByTime(int $time, int $page, int $limit)
  179. {
  180. return $this->search(['is_del' => 0, 'status' => 1])->with(['product'])
  181. ->where('start_time', '<=', time())
  182. ->where('stop_time', '>=', time() - 86400)
  183. ->whereFindInSet('time_id', $time)
  184. ->where('product_id', 'IN', function ($query) {
  185. $query->name('store_product')->where('is_del', 0)->field('id');
  186. })->when($page != 0, function ($query) use ($page, $limit) {
  187. $query->page($page, $limit);
  188. })->order('sort desc,id desc')->select()->toArray();
  189. }
  190. /**
  191. * 根据id获取秒杀数据
  192. * @param array $ids
  193. * @param string $field
  194. * @return array
  195. * @throws \think\db\exception\DataNotFoundException
  196. * @throws \think\db\exception\DbException
  197. * @throws \think\db\exception\ModelNotFoundException
  198. */
  199. public function idSeckillList(array $ids, string $field)
  200. {
  201. return $this->getModel()->whereIn('id', $ids)->field($field)->select()->toArray();
  202. }
  203. /**获取一条秒杀商品
  204. * @param $id
  205. * @param $field
  206. * @return array|\think\Model|null
  207. * @throws \think\db\exception\DataNotFoundException
  208. * @throws \think\db\exception\DbException
  209. * @throws \think\db\exception\ModelNotFoundException
  210. */
  211. public function validProduct($id, $field)
  212. {
  213. $where = ['status' => 1, 'is_del' => 0];
  214. $time = time();
  215. return $this->search($where)
  216. ->where('id', $id)
  217. ->where('start_time', '<', $time)
  218. ->where('stop_time', '>', $time - 86400)
  219. ->field($field)->with(['product'])->find();
  220. }
  221. public function getProductExist($productIds)
  222. {
  223. return $this->getModel()->where('product_id', 'in', $productIds)->where('is_show', 1)->where('is_del', 0)
  224. ->group('product_id')->column('COUNT(*) as count', 'product_id');
  225. }
  226. }