StoreBargainDao.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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\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)->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. * @return array
  101. * @throws \think\db\exception\DataNotFoundException
  102. * @throws \think\db\exception\DbException
  103. * @throws \think\db\exception\ModelNotFoundException
  104. */
  105. public function bargainList(int $page, int $limit)
  106. {
  107. return $this->search(['is_del' => 0, 'status' => 1])
  108. ->where('start_time', '<=', time())
  109. ->where('stop_time', '>=', time())
  110. ->where('product_id', 'IN', function ($query) {
  111. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  112. })->with('product')->page($page, $limit)->order('sort DESC,id DESC')->select()->toArray();
  113. }
  114. /**
  115. * 后台页面设计获取砍价列表
  116. * @param array $where
  117. * @param int $page
  118. * @param int $limit
  119. * @return array
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\DbException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. */
  124. public function DiyBargainList(array $where,int $page, int $limit){
  125. return $this->search($where)
  126. ->when(isset($where['sid']) && $where['sid'], function ($query) use ($where) {
  127. $query->whereIn('id', function ($query) use ($where) {
  128. $query->name('store_product_cate')->where('cate_id', $where['sid'])->field('product_id')->select();
  129. });
  130. })->when(isset($where['cid']) && $where['cid'], function ($query) use ($where) {
  131. $query->whereIn('id', function ($query) use ($where) {
  132. $query->name('store_product_cate')->whereIn('cate_id', function ($query) use ($where) {
  133. $query->name('store_category')->where('pid', $where['cid'])->field('id')->select();
  134. })->field('product_id')->select();
  135. });
  136. })->where('start_time', '<=', time())
  137. ->where('stop_time', '>=', time())
  138. ->where('product_id', 'IN', function ($query) {
  139. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  140. })->with('product')->page($page, $limit)->order('sort DESC,id DESC')->select()->toArray();
  141. }
  142. /**
  143. * 首页砍价
  144. * @param array $where
  145. * @param int $page
  146. * @param int $limit
  147. * @return array
  148. * @throws \think\db\exception\DataNotFoundException
  149. * @throws \think\db\exception\DbException
  150. * @throws \think\db\exception\ModelNotFoundException
  151. */
  152. public function getHomeList(array $where,int $page, int $limit){
  153. return $this->search($where)
  154. ->when(isset($where['sid']) && $where['sid'], function ($query) use ($where) {
  155. $query->whereIn('id', function ($query) use ($where) {
  156. $query->name('store_product_cate')->where('cate_id', $where['sid'])->field('product_id')->select();
  157. });
  158. })->when(isset($where['cid']) && $where['cid'], function ($query) use ($where) {
  159. $query->whereIn('id', function ($query) use ($where) {
  160. $query->name('store_product_cate')->whereIn('cate_id', function ($query) use ($where) {
  161. $query->name('store_category')->where('pid', $where['cid'])->field('id')->select();
  162. })->field('product_id')->select();
  163. });
  164. })->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  165. $query->page($page, $limit);
  166. })->when(isset($where['priceOrder']) && $where['priceOrder'] != '', function ($query) use ($where) {
  167. if ($where['priceOrder'] === 'desc') {
  168. $query->order("price desc");
  169. } else {
  170. $query->order("price asc");
  171. }
  172. })->when(isset($where['newsOrder']) && $where['newsOrder'] != '', function ($query) use ($where) {
  173. if ($where['newsOrder'] === 'news') {
  174. $query->order("id desc");
  175. }
  176. })->when(isset($where['salesOrder']) && $where['salesOrder'] != '', function ($query) use ($where) {
  177. if ($where['salesOrder'] === 'desc') {
  178. $query->order("sales desc");
  179. } else {
  180. $query->order("sales asc");
  181. }
  182. })->when(isset($where['ids']) && $where['ids'], function ($query) use ($where) {
  183. if ((isset($where['priceOrder']) && $where['priceOrder'] != '') || (isset($where['salesOrder']) && $where['salesOrder'] != '')) {
  184. $query->whereIn('id', $where['ids']);
  185. } else {
  186. $query->whereIn('id', $where['ids'])->orderField('id', $where['ids'], 'asc');
  187. }
  188. })
  189. ->where('start_time', '<=', time())
  190. ->where('stop_time', '>=', time())
  191. ->where('product_id', 'IN', function ($query) {
  192. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  193. })->with('product')->select()->toArray();
  194. }
  195. /**
  196. * 条件获取数量
  197. * @param array $where
  198. * @return int
  199. */
  200. public function getCount(array $where)
  201. {
  202. return $this->search($where)
  203. ->when(isset($where['sid']) && $where['sid'], function ($query) use ($where) {
  204. $query->whereIn('product_id', function ($query) use ($where) {
  205. $query->name('store_product_cate')->where('cate_id', $where['sid'])->field('product_id')->select();
  206. });
  207. })->when(isset($where['cid']) && $where['cid'], function ($query) use ($where) {
  208. $query->whereIn('product_id', function ($query) use ($where) {
  209. $query->name('store_product_cate')->whereIn('cate_id', function ($query) use ($where) {
  210. $query->name('store_category')->where('pid', $where['cid'])->field('id')->select();
  211. })->field('product_id')->select();
  212. });
  213. })->where('start_time', '<=', time())
  214. ->where('stop_time', '>=', time())
  215. ->where('product_id', 'IN', function ($query) {
  216. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  217. })->count();
  218. }
  219. /**
  220. * 修改砍价状态
  221. * @param int $id
  222. * @param string $field
  223. * @return mixed
  224. */
  225. public function addBargain(int $id, string $field)
  226. {
  227. return $this->getModel()->where('id', $id)->inc($field, 1)->update();
  228. }
  229. }