StoreCombinationDao.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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\combination;
  13. use app\dao\BaseDao;
  14. use app\model\activity\combination\StoreCombination;
  15. /**
  16. *
  17. * Class StoreCombinationDao
  18. * @package app\dao\activity
  19. */
  20. class StoreCombinationDao extends BaseDao
  21. {
  22. /**
  23. * 设置模型
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return StoreCombination::class;
  29. }
  30. /**
  31. * 搜索
  32. * @param array $where
  33. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  34. */
  35. public function search(array $where = [])
  36. {
  37. return parent::search($where, false)->when(isset($where['pinkIngTime']), function ($query) use ($where) {
  38. $time = time();
  39. [$startTime, $stopTime] = is_array($where['pinkIngTime']) ? $where['pinkIngTime'] : [$time, $time];
  40. $query->where('start_time', '<=', $startTime)->where('stop_time', '>=', $stopTime);
  41. })->when(isset($where['storeProductId']), function ($query) {
  42. $query->where('product_id', 'IN', function ($query) {
  43. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  44. });
  45. })->when(isset($where['sid']) && $where['sid'], function ($query) use ($where) {
  46. $query->whereIn('product_id', function ($query) use ($where) {
  47. $query->name('store_product_cate')->where('cate_id', $where['sid'])->field('product_id')->select();
  48. });
  49. })->when(isset($where['cid']) && $where['cid'], function ($query) use ($where) {
  50. $query->whereIn('product_id', function ($query) use ($where) {
  51. $query->name('store_product_cate')->whereIn('cate_id', function ($query) use ($where) {
  52. $query->name('store_category')->where('pid', $where['cid'])->field('id')->select();
  53. })->field('product_id')->select();
  54. });
  55. });
  56. }
  57. /**
  58. * 获取指定条件下的条数
  59. * @param array $where
  60. * @return int
  61. */
  62. public function count(array $where = []): int
  63. {
  64. return $this->search($where)->count();
  65. }
  66. /**
  67. * 拼团商品列表
  68. * @param array $where
  69. * @param int $page
  70. * @param int $limit
  71. * @return array
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. public function getList(array $where, int $page = 0, int $limit = 0)
  77. {
  78. return $this->search($where)->where('is_del', 0)->with('getPrice')
  79. ->when(isset($where['start_status']) && $where['start_status'] !== '', function ($query) use ($where) {
  80. $time = time();
  81. switch ($where['start_status']) {
  82. case -1:
  83. $query->where('stop_time', '<', $time);
  84. break;
  85. case 0:
  86. $query->where('start_time', '>', $time);
  87. break;
  88. case 1:
  89. $query->where('start_time', '<=', $time)->where('stop_time', '>=', $time);
  90. break;
  91. }
  92. })->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  93. $query->page($page, $limit);
  94. })->order('sort desc,id desc')->select()->toArray();
  95. }
  96. /**获取列表
  97. * @param array $where
  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 getHomeList(array $where, int $page = 0, int $limit = 0)
  106. {
  107. return $this->search($where)
  108. ->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  109. $query->page($page, $limit);
  110. })->when(isset($where['priceOrder']) && $where['priceOrder'] != '', function ($query) use ($where) {
  111. if ($where['priceOrder'] === 'desc') {
  112. $query->order("price desc");
  113. } else {
  114. $query->order("price asc");
  115. }
  116. })->when(isset($where['newsOrder']) && $where['newsOrder'] != '', function ($query) use ($where) {
  117. if ($where['newsOrder'] === 'news') {
  118. $query->order("id desc");
  119. }
  120. })->when(isset($where['salesOrder']) && $where['salesOrder'] != '', function ($query) use ($where) {
  121. if ($where['salesOrder'] === 'desc') {
  122. $query->order("sales desc");
  123. } else {
  124. $query->order("sales asc");
  125. }
  126. })->when(isset($where['ids']) && $where['ids'], function ($query) use ($where) {
  127. if ((isset($where['priceOrder']) && $where['priceOrder'] != '') || (isset($where['salesOrder']) && $where['salesOrder'] != '')) {
  128. $query->whereIn('id', $where['ids']);
  129. } else {
  130. $query->whereIn('id', $where['ids'])->orderField('id', $where['ids'], 'asc');
  131. }
  132. })->with(['getPrice'])->select()->toArray();
  133. }
  134. /**
  135. * 获取正在进行拼团的商品以数组形式返回
  136. * @param array $ids ids 为空返回所有
  137. * @param array $field
  138. * @return array
  139. */
  140. public function getPinkIdsArray(array $ids = [], array $field = [])
  141. {
  142. return $this->search(['is_del' => 0, 'is_show' => 1, 'pinkIngTime' => 1])
  143. ->when($ids, function ($query) use ($ids) {
  144. $query->whereIn('product_id', $ids);
  145. })->column(implode(',', $field), 'product_id');
  146. }
  147. /**
  148. * 获取拼团列表
  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 combinationList(array $where, int $page, int $limit)
  155. {
  156. return $this->search($where)->with('getPrice')->page($page, $limit)->order('sort desc,id desc')->select()->toArray();
  157. }
  158. /**
  159. * 条件获取数量
  160. * @param array $where
  161. * @return int
  162. */
  163. public function getCount(array $where)
  164. {
  165. return $this->search($where)->count();
  166. }
  167. /**
  168. * 页面设计获取商拼团列表
  169. * @param array $where
  170. * @param int $page
  171. * @param int $limit
  172. * @return array
  173. * @throws \think\db\exception\DataNotFoundException
  174. * @throws \think\db\exception\DbException
  175. * @throws \think\db\exception\ModelNotFoundException
  176. */
  177. public function diyCombinationList(array $where, int $page, int $limit)
  178. {
  179. return $this->search($where)->with('getCategory')->page($page, $limit)->order('sort desc,id desc')->select()->toArray();
  180. }
  181. /**
  182. * 根据id获取拼团数据
  183. * @param array $ids
  184. * @param string $field
  185. * @return array
  186. * @throws \think\db\exception\DataNotFoundException
  187. * @throws \think\db\exception\DbException
  188. * @throws \think\db\exception\ModelNotFoundException
  189. */
  190. public function idCombinationList(array $ids, string $field)
  191. {
  192. return $this->getModel()->whereIn('id', $ids)->field($field)->select()->toArray();
  193. }
  194. /**
  195. * 获取一条拼团数据
  196. * @param int $id
  197. * @param string $field
  198. * @return array|\think\Model|null
  199. * @throws \think\db\exception\DataNotFoundException
  200. * @throws \think\db\exception\DbException
  201. * @throws \think\db\exception\ModelNotFoundException
  202. */
  203. public function validProduct(int $id, string $field)
  204. {
  205. $where = ['is_show' => 1, 'is_del' => 0, 'pinkIngTime' => true];
  206. return $this->search($where)->where('id', $id)->with(['total'])->field($field)->order('add_time desc')->find();
  207. }
  208. /**
  209. * 获取推荐拼团
  210. * @return array
  211. * @throws \think\db\exception\DataNotFoundException
  212. * @throws \think\db\exception\DbException
  213. * @throws \think\db\exception\ModelNotFoundException
  214. */
  215. public function getCombinationHost()
  216. {
  217. $where = ['is_del' => 0, 'is_host' => 1, 'is_show' => 1, 'pinkIngTime' => true];
  218. return $this->search($where)->order('id desc')->select()->toArray();
  219. }
  220. }