StoreSeckillDao.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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;
  13. use app\dao\BaseDao;
  14. use app\model\activity\StoreSeckill;
  15. use think\facade\Db;
  16. /**
  17. *
  18. * Class StoreSeckillDao
  19. * @package app\dao\activity
  20. */
  21. class StoreSeckillDao extends BaseDao
  22. {
  23. /**
  24. * 设置模型
  25. * @return string
  26. */
  27. protected function setModel(): string
  28. {
  29. return StoreSeckill::class;
  30. }
  31. /**
  32. * 搜索
  33. * @param array $where
  34. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  35. */
  36. protected function search(array $where = [])
  37. {
  38. return parent::search($where)->when(isset($where['seckllTime']), function ($query) use ($where) {
  39. [$startTime, $stopTime] = is_array($where['seckllTime']) ? $where['seckllTime'] : [time(), time() - 86400];
  40. $query->where('start_time', '<=', $startTime)->where('stop_time', '>=', $stopTime);
  41. })->when(isset($where['sid']) && $where['sid'], function ($query) use ($where) {
  42. $query->whereIn('product_id', function ($query) use ($where) {
  43. $query->name('store_product_cate')->where('cate_id', $where['sid'])->field('product_id')->select();
  44. });
  45. })->when(isset($where['cid']) && $where['cid'], function ($query) use ($where) {
  46. $query->whereIn('product_id', function ($query) use ($where) {
  47. $query->name('store_product_cate')->whereIn('cate_id', function ($query) use ($where) {
  48. $query->name('store_category')->where('pid', $where['cid'])->field('id')->select();
  49. })->field('product_id')->select();
  50. });
  51. })->when(isset($where['storeProductId']), function ($query) {
  52. $query->where('product_id', 'IN', function ($query) {
  53. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  54. });
  55. })->when(isset($where['time_id']) && $where['time_id'], function ($query) use ($where) {
  56. $query->where('time_id', $where['time_id']);
  57. });
  58. }
  59. /**
  60. * 条件获取数量
  61. * @param array $where
  62. * @return int
  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($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  100. $query->page($page, $limit);
  101. })->with(['product'])->order('sort desc,id desc')->select()->toArray();
  102. }
  103. /**获取秒杀列表
  104. * @param array $where
  105. * @param int $page
  106. * @param int $limit
  107. * @return array
  108. * @throws \think\db\exception\DataNotFoundException
  109. * @throws \think\db\exception\DbException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. */
  112. public function getHomeList(array $where, int $page = 0, int $limit = 0)
  113. {
  114. return $this->search($where)->where('is_del', 0)->where('status',1)
  115. ->where('start_time', '<=', time())
  116. ->where('stop_time', '>=', time() - 86400)
  117. ->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  118. $query->page($page, $limit);
  119. })->when(isset($where['priceOrder']) && $where['priceOrder'] != '', function ($query) use ($where) {
  120. if ($where['priceOrder'] === 'desc') {
  121. $query->order("price desc");
  122. } else {
  123. $query->order("price asc");
  124. }
  125. })->when(isset($where['newsOrder']) && $where['newsOrder'] != '', function ($query) use ($where) {
  126. if ($where['newsOrder'] === 'news') {
  127. $query->order("id desc");
  128. }
  129. })->when(isset($where['salesOrder']) && $where['salesOrder'] != '', function ($query) use ($where) {
  130. if ($where['salesOrder'] === 'desc') {
  131. $query->order("sales desc");
  132. } else {
  133. $query->order("sales asc");
  134. }
  135. })->when(isset($where['ids']) && $where['ids'], function ($query) use ($where) {
  136. if ((isset($where['priceOrder']) && $where['priceOrder'] != '') || (isset($where['salesOrder']) && $where['salesOrder'] != '')) {
  137. $query->whereIn('id', $where['ids']);
  138. } else {
  139. $query->whereIn('id', $where['ids'])->orderField('id', $where['ids'], 'asc');
  140. }
  141. })->with(['product'])->select()->toArray();
  142. }
  143. /**
  144. * 根据商品id获取当前正在开启秒杀产品的列表以数组返回
  145. * @param array $ids 为空查询所有
  146. * @param array $field
  147. * @return array
  148. */
  149. public function getSeckillIdsArray(array $ids = [], array $field = [])
  150. {
  151. return $this->search(['is_del' => 0, 'status' => 1])
  152. ->where('start_time', '<=', time())
  153. ->where('stop_time', '>=', time() - 86400)
  154. ->when($ids, function ($query) use ($ids) {
  155. $query->whereIn('product_id', $ids);
  156. })->field($field)->select()->toArray();
  157. }
  158. /**
  159. * 获取某个时间段的秒杀列表
  160. * @param int $time
  161. * @param int $page
  162. * @param int $limit
  163. * @return array
  164. * @throws \think\db\exception\DataNotFoundException
  165. * @throws \think\db\exception\DbException
  166. * @throws \think\db\exception\ModelNotFoundException
  167. */
  168. public function getListByTime(int $time, int $page, int $limit)
  169. {
  170. return $this->search(['is_del' => 0, 'status' => 1])
  171. ->where('start_time', '<=', time())
  172. ->where('stop_time', '>=', time() - 86400)
  173. ->where('time_id', $time)
  174. ->where('product_id', 'IN', function ($query) {
  175. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  176. })->when($page != 0, function ($query) use ($page, $limit) {
  177. $query->page($page, $limit);
  178. })->order('sort desc,id desc')->select()->toArray();
  179. }
  180. /**
  181. * 根据id获取秒杀数据
  182. * @param array $ids
  183. * @param string $field
  184. * @return array
  185. * @throws \think\db\exception\DataNotFoundException
  186. * @throws \think\db\exception\DbException
  187. * @throws \think\db\exception\ModelNotFoundException
  188. */
  189. public function idSeckillList(array $ids, string $field)
  190. {
  191. return $this->getModel()->whereIn('id', $ids)->field($field)->select()->toArray();
  192. }
  193. /**获取一条秒杀商品
  194. * @param $id
  195. * @param $field
  196. * @return array|\think\Model|null
  197. * @throws \think\db\exception\DataNotFoundException
  198. * @throws \think\db\exception\DbException
  199. * @throws \think\db\exception\ModelNotFoundException
  200. */
  201. public function validProduct($id, $field)
  202. {
  203. $where = ['status' => 1, 'is_del' => 0];
  204. $time = time();
  205. return $this->search($where)
  206. ->where('id', $id)
  207. ->where('start_time', '<', $time)
  208. ->where('stop_time', '>', $time - 86400)
  209. ->field($field)->with(['product'])->find();
  210. }
  211. }