StoreCombinationDao.php 9.1 KB

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