StoreCombinationDao.php 9.2 KB

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