StoreBargainDao.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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\bargain;
  13. use app\dao\BaseDao;
  14. use app\model\activity\bargain\StoreBargain;
  15. /**
  16. *
  17. * Class StoreBargainDao
  18. * @package app\dao\activity
  19. */
  20. class StoreBargainDao extends BaseDao
  21. {
  22. /**
  23. * 设置模型
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return StoreBargain::class;
  29. }
  30. /**
  31. * 获取砍价列表
  32. * @param array $where
  33. * @param int $page
  34. * @param int $limit
  35. * @return array
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function getList(array $where, int $page = 0, int $limit = 0)
  41. {
  42. return $this->search($where, false)->where('is_del', 0)
  43. ->when(isset($where['start_status']) && $where['start_status'] !== '', function ($query) use ($where) {
  44. $time = time();
  45. switch ($where['start_status']) {
  46. case -1:
  47. $query->where('stop_time', '<', $time);
  48. break;
  49. case 0:
  50. $query->where('start_time', '>', $time);
  51. break;
  52. case 1:
  53. $query->where('start_time', '<=', $time)->where('stop_time', '>=', $time);
  54. break;
  55. }
  56. })->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  57. $query->page($page, $limit);
  58. })->when(isset($where['product_id']) && $where['product_id'] != 0, function ($query) use ($where) {
  59. $query->where('product_id', $where['product_id']);
  60. })->order('sort desc,id desc')->select()->toArray();
  61. }
  62. /**
  63. * 获取活动开启中的砍价id以数组形式返回
  64. * @param array $ids 为空返回所有
  65. * @param array $field
  66. * @return array
  67. */
  68. public function getBargainIdsArray(array $ids = [], array $field = [])
  69. {
  70. return $this->search(['is_del' => 0, 'status' => 1])->where('start_time', '<=', time())
  71. ->where('stop_time', '>=', time())
  72. ->when($ids, function ($query) use ($ids) {
  73. $query->whereIn('product_id', $ids);
  74. })->column(implode(',', $field), 'product_id');
  75. }
  76. /**
  77. * 根据id获取砍价数据
  78. * @param array $ids
  79. * @param string $field
  80. * @return array
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. */
  85. public function idByBargainList(array $ids, string $field)
  86. {
  87. return $this->getModel()->whereIn('id', $ids)->field($field)->select()->toArray();
  88. }
  89. /**
  90. * 正在开启的砍价活动
  91. * @param int $status
  92. * @return StoreBargain
  93. */
  94. public function validWhere(int $status = 1)
  95. {
  96. return $this->getModel()->where('is_del', 0)->where('status', $status)->where('start_time', '<', time())->where('stop_time', '>', time());
  97. }
  98. /**
  99. * 砍价列表
  100. * @param int $page
  101. * @param int $limit
  102. * @param string $field
  103. * @return array
  104. * @throws \think\db\exception\DataNotFoundException
  105. * @throws \think\db\exception\DbException
  106. * @throws \think\db\exception\ModelNotFoundException
  107. */
  108. public function bargainList(int $page, int $limit, string $field = '*')
  109. {
  110. return $this->search(['is_del' => 0, 'status' => 1])
  111. ->where('start_time', '<=', time())
  112. ->where('stop_time', '>=', time())
  113. ->where('product_id', 'IN', function ($query) {
  114. $query->name('store_product')->where('is_del', 0)->field('id');
  115. })->with('product')->field($field)->page($page, $limit)->order('sort DESC,id DESC')->select()->toArray();
  116. }
  117. /**
  118. * 后台页面设计获取砍价列表
  119. * @param array $where
  120. * @param int $page
  121. * @param int $limit
  122. * @return array
  123. * @throws \think\db\exception\DataNotFoundException
  124. * @throws \think\db\exception\DbException
  125. * @throws \think\db\exception\ModelNotFoundException
  126. */
  127. public function DiyBargainList(array $where, int $page, int $limit)
  128. {
  129. return $this->search($where, false)
  130. ->when(isset($where['sid']) && $where['sid'], function ($query) use ($where) {
  131. $query->whereIn('id', function ($query) use ($where) {
  132. $query->name('store_product_cate')->where('cate_id', $where['sid'])->field('product_id')->select();
  133. });
  134. })->when(isset($where['cid']) && $where['cid'], function ($query) use ($where) {
  135. $query->whereIn('id', function ($query) use ($where) {
  136. $query->name('store_product_cate')->whereIn('cate_id', function ($query) use ($where) {
  137. $query->name('store_category')->where('pid', $where['cid'])->field('id')->select();
  138. })->field('product_id')->select();
  139. });
  140. })->where('start_time', '<=', time())
  141. ->where('stop_time', '>=', time())
  142. ->where('product_id', 'IN', function ($query) {
  143. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  144. })->with('product')->page($page, $limit)->order('sort DESC,id DESC')->select()->toArray();
  145. }
  146. /**
  147. * 首页砍价
  148. * @param array $where
  149. * @param int $page
  150. * @param int $limit
  151. * @return array
  152. * @throws \think\db\exception\DataNotFoundException
  153. * @throws \think\db\exception\DbException
  154. * @throws \think\db\exception\ModelNotFoundException
  155. */
  156. public function getHomeList(array $where, int $page, int $limit)
  157. {
  158. return $this->search($where, false)
  159. ->when(isset($where['sid']) && $where['sid'], function ($query) use ($where) {
  160. $query->whereIn('id', function ($query) use ($where) {
  161. $query->name('store_product_cate')->where('cate_id', $where['sid'])->field('product_id')->select();
  162. });
  163. })->when(isset($where['cid']) && $where['cid'], function ($query) use ($where) {
  164. $query->whereIn('id', function ($query) use ($where) {
  165. $query->name('store_product_cate')->whereIn('cate_id', function ($query) use ($where) {
  166. $query->name('store_category')->where('pid', $where['cid'])->field('id')->select();
  167. })->field('product_id')->select();
  168. });
  169. })->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  170. $query->page($page, $limit);
  171. })->when(isset($where['priceOrder']) && $where['priceOrder'] != '', function ($query) use ($where) {
  172. if ($where['priceOrder'] === 'desc') {
  173. $query->order("price desc");
  174. } else {
  175. $query->order("price asc");
  176. }
  177. })->when(isset($where['newsOrder']) && $where['newsOrder'] != '', function ($query) use ($where) {
  178. if ($where['newsOrder'] === 'news') {
  179. $query->order("id desc");
  180. }
  181. })->when(isset($where['salesOrder']) && $where['salesOrder'] != '', function ($query) use ($where) {
  182. if ($where['salesOrder'] === 'desc') {
  183. $query->order("sales desc");
  184. } else {
  185. $query->order("sales asc");
  186. }
  187. })->when(isset($where['ids']) && $where['ids'], function ($query) use ($where) {
  188. if ((isset($where['priceOrder']) && $where['priceOrder'] != '') || (isset($where['salesOrder']) && $where['salesOrder'] != '')) {
  189. $query->whereIn('id', $where['ids']);
  190. } else {
  191. $query->whereIn('id', $where['ids'])->orderField('id', $where['ids'], 'asc');
  192. }
  193. })
  194. ->where('start_time', '<=', time())
  195. ->where('stop_time', '>=', time())
  196. ->where('product_id', 'IN', function ($query) {
  197. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  198. })->with('product')->select()->toArray();
  199. }
  200. /**
  201. * 条件获取数量
  202. * @param array $where
  203. * @return int
  204. */
  205. public function getCount(array $where)
  206. {
  207. return $this->search($where, false)
  208. ->when(isset($where['sid']) && $where['sid'], function ($query) use ($where) {
  209. $query->whereIn('product_id', function ($query) use ($where) {
  210. $query->name('store_product_cate')->where('cate_id', $where['sid'])->field('product_id')->select();
  211. });
  212. })->when(isset($where['cid']) && $where['cid'], function ($query) use ($where) {
  213. $query->whereIn('product_id', function ($query) use ($where) {
  214. $query->name('store_product_cate')->whereIn('cate_id', function ($query) use ($where) {
  215. $query->name('store_category')->where('pid', $where['cid'])->field('id')->select();
  216. })->field('product_id')->select();
  217. });
  218. })->where('start_time', '<=', time())
  219. ->where('stop_time', '>=', time())
  220. ->where('product_id', 'IN', function ($query) {
  221. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  222. })->count();
  223. }
  224. /**
  225. * 修改砍价状态
  226. * @param int $id
  227. * @param string $field
  228. * @return mixed
  229. */
  230. public function addBargain(int $id, string $field)
  231. {
  232. return $this->getModel()->where('id', $id)->inc($field, 1)->update();
  233. }
  234. public function getProductExist($productIds)
  235. {
  236. return $this->getModel()->where('product_id', 'in', $productIds)->where('is_del', 0)
  237. ->group('product_id')->column('COUNT(*) as count', 'product_id');
  238. }
  239. }