StoreCombinationDao.php 9.0 KB

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