StoreBargainDao.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. })->order('sort desc,id desc')->select()->toArray();
  59. }
  60. /**
  61. * 获取活动开启中的砍价id以数组形式返回
  62. * @param array $ids 为空返回所有
  63. * @param array $field
  64. * @return array
  65. */
  66. public function getBargainIdsArray(array $ids = [], array $field = [])
  67. {
  68. return $this->search(['is_del' => 0, 'status' => 1])->where('start_time', '<=', time())
  69. ->where('stop_time', '>=', time())
  70. ->when($ids, function ($query) use ($ids) {
  71. $query->whereIn('product_id', $ids);
  72. })->column(implode(',', $field), 'product_id');
  73. }
  74. /**
  75. * 根据id获取砍价数据
  76. * @param array $ids
  77. * @param string $field
  78. * @return array
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public function idByBargainList(array $ids, string $field)
  84. {
  85. return $this->getModel()->whereIn('id', $ids)->field($field)->select()->toArray();
  86. }
  87. /**
  88. * 正在开启的砍价活动
  89. * @param int $status
  90. * @return StoreBargain
  91. */
  92. public function validWhere(int $status = 1)
  93. {
  94. return $this->getModel()->where('is_del', 0)->where('status', $status)->where('start_time', '<', time())->where('stop_time', '>', time());
  95. }
  96. /**
  97. * 砍价列表
  98. * @param int $page
  99. * @param int $limit
  100. * @param string $field
  101. * @return array
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. public function bargainList(int $page, int $limit, string $field = '*')
  107. {
  108. return $this->search(['is_del' => 0, 'status' => 1])
  109. ->where('start_time', '<=', time())
  110. ->where('stop_time', '>=', time())
  111. ->where('product_id', 'IN', function ($query) {
  112. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  113. })->with('product')->field($field)->page($page, $limit)->order('sort DESC,id DESC')->select()->toArray();
  114. }
  115. /**
  116. * 后台页面设计获取砍价列表
  117. * @param array $where
  118. * @param int $page
  119. * @param int $limit
  120. * @return array
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\DbException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. */
  125. public function DiyBargainList(array $where, int $page, int $limit)
  126. {
  127. return $this->search($where, false)
  128. ->when(isset($where['sid']) && $where['sid'], function ($query) use ($where) {
  129. $query->whereIn('id', function ($query) use ($where) {
  130. $query->name('store_product_cate')->where('cate_id', $where['sid'])->field('product_id')->select();
  131. });
  132. })->when(isset($where['cid']) && $where['cid'], function ($query) use ($where) {
  133. $query->whereIn('id', function ($query) use ($where) {
  134. $query->name('store_product_cate')->whereIn('cate_id', function ($query) use ($where) {
  135. $query->name('store_category')->where('pid', $where['cid'])->field('id')->select();
  136. })->field('product_id')->select();
  137. });
  138. })->where('start_time', '<=', time())
  139. ->where('stop_time', '>=', time())
  140. ->where('product_id', 'IN', function ($query) {
  141. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  142. })->with('product')->page($page, $limit)->order('sort DESC,id DESC')->select()->toArray();
  143. }
  144. /**
  145. * 首页砍价
  146. * @param array $where
  147. * @param int $page
  148. * @param int $limit
  149. * @return array
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\DbException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. */
  154. public function getHomeList(array $where, int $page, int $limit)
  155. {
  156. return $this->search($where, false)
  157. ->when(isset($where['sid']) && $where['sid'], function ($query) use ($where) {
  158. $query->whereIn('id', function ($query) use ($where) {
  159. $query->name('store_product_cate')->where('cate_id', $where['sid'])->field('product_id')->select();
  160. });
  161. })->when(isset($where['cid']) && $where['cid'], function ($query) use ($where) {
  162. $query->whereIn('id', function ($query) use ($where) {
  163. $query->name('store_product_cate')->whereIn('cate_id', function ($query) use ($where) {
  164. $query->name('store_category')->where('pid', $where['cid'])->field('id')->select();
  165. })->field('product_id')->select();
  166. });
  167. })->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  168. $query->page($page, $limit);
  169. })->when(isset($where['priceOrder']) && $where['priceOrder'] != '', function ($query) use ($where) {
  170. if ($where['priceOrder'] === 'desc') {
  171. $query->order("price desc");
  172. } else {
  173. $query->order("price asc");
  174. }
  175. })->when(isset($where['newsOrder']) && $where['newsOrder'] != '', function ($query) use ($where) {
  176. if ($where['newsOrder'] === 'news') {
  177. $query->order("id desc");
  178. }
  179. })->when(isset($where['salesOrder']) && $where['salesOrder'] != '', function ($query) use ($where) {
  180. if ($where['salesOrder'] === 'desc') {
  181. $query->order("sales desc");
  182. } else {
  183. $query->order("sales asc");
  184. }
  185. })->when(isset($where['ids']) && $where['ids'], function ($query) use ($where) {
  186. if ((isset($where['priceOrder']) && $where['priceOrder'] != '') || (isset($where['salesOrder']) && $where['salesOrder'] != '')) {
  187. $query->whereIn('id', $where['ids']);
  188. } else {
  189. $query->whereIn('id', $where['ids'])->orderField('id', $where['ids'], 'asc');
  190. }
  191. })
  192. ->where('start_time', '<=', time())
  193. ->where('stop_time', '>=', time())
  194. ->where('product_id', 'IN', function ($query) {
  195. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  196. })->with('product')->select()->toArray();
  197. }
  198. /**
  199. * 条件获取数量
  200. * @param array $where
  201. * @return int
  202. */
  203. public function getCount(array $where)
  204. {
  205. return $this->search($where, false)
  206. ->when(isset($where['sid']) && $where['sid'], function ($query) use ($where) {
  207. $query->whereIn('product_id', function ($query) use ($where) {
  208. $query->name('store_product_cate')->where('cate_id', $where['sid'])->field('product_id')->select();
  209. });
  210. })->when(isset($where['cid']) && $where['cid'], function ($query) use ($where) {
  211. $query->whereIn('product_id', function ($query) use ($where) {
  212. $query->name('store_product_cate')->whereIn('cate_id', function ($query) use ($where) {
  213. $query->name('store_category')->where('pid', $where['cid'])->field('id')->select();
  214. })->field('product_id')->select();
  215. });
  216. })->where('start_time', '<=', time())
  217. ->where('stop_time', '>=', time())
  218. ->where('product_id', 'IN', function ($query) {
  219. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  220. })->count();
  221. }
  222. /**
  223. * 修改砍价状态
  224. * @param int $id
  225. * @param string $field
  226. * @return mixed
  227. */
  228. public function addBargain(int $id, string $field)
  229. {
  230. return $this->getModel()->where('id', $id)->inc($field, 1)->update();
  231. }
  232. }