StoreCombinationDao.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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\combination;
  13. use app\dao\BaseDao;
  14. use app\model\activity\combination\StoreCombination;
  15. /**
  16. * 拼团商品
  17. * Class StoreCombinationDao
  18. * @package app\dao\activity\combination
  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)->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['start_status']) && $where['start_status'] !== '', function ($query) use ($where) {
  46. $time = time();
  47. switch ($where['start_status']) {
  48. case -1:
  49. $query->where(function ($query) use ($time) {
  50. $query->where('stop_time', '<', $time)->whereOr('is_show', 0);
  51. });
  52. break;
  53. case 0:
  54. $query->where('start_time', '>', $time)->where('is_show', 1);
  55. break;
  56. case 1:
  57. $query->where('start_time', '<=', $time)->where('stop_time', '>=', $time)->where('is_show', 1);
  58. break;
  59. }
  60. });
  61. }
  62. /**
  63. * 拼团商品列表
  64. * @param array $where
  65. * @param int $page
  66. * @param int $limit
  67. * @return array
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function getList(array $where, int $page = 0, int $limit = 0)
  73. {
  74. return $this->search($where)->with('getPrice')
  75. ->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  76. $query->page($page, $limit);
  77. })->order('sort desc,id desc')->select()->toArray();
  78. }
  79. /**
  80. * 获取正在进行拼团的商品以数组形式返回
  81. * @param array $ids
  82. * @param array $field
  83. * @return array
  84. */
  85. public function getPinkIdsArray(array $ids, array $field = [])
  86. {
  87. return $this->search(['is_del' => 0, 'is_show' => 1, 'pinkIngTime' => 1])->whereIn('product_id', $ids)->column(implode(',', $field), 'product_id');
  88. }
  89. /**
  90. * 获取拼团列表
  91. * @return array
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\DbException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. */
  96. public function combinationList(array $where, int $page = 0, int $limit = 0)
  97. {
  98. return $this->search($where)->with('getPrice')->when($page && $limit, function ($query) use ($page, $limit) {
  99. $query->page($page, $limit);
  100. })->order('sort desc,id desc')->select()->toArray();
  101. }
  102. /**
  103. * 根据id获取拼团数据
  104. * @param array $ids
  105. * @param string $field
  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 idCombinationList(array $ids, string $field)
  112. {
  113. return $this->getModel()->whereIn('id', $ids)->field($field)->select()->toArray();
  114. }
  115. /**
  116. * 获取一条拼团数据
  117. * @param int $id
  118. * @param string $field
  119. * @return array|\think\Model|null
  120. * @throws \think\db\exception\DataNotFoundException
  121. * @throws \think\db\exception\DbException
  122. * @throws \think\db\exception\ModelNotFoundException
  123. */
  124. public function validProduct(int $id, string $field)
  125. {
  126. $where = ['is_show' => 1, 'is_del' => 0, 'pinkIngTime' => true];
  127. return $this->search($where)->where('id', $id)->with(['total'])->field($field)->order('add_time desc')->find();
  128. }
  129. /**
  130. * 获取推荐拼团
  131. * @return array
  132. * @throws \think\db\exception\DataNotFoundException
  133. * @throws \think\db\exception\DbException
  134. * @throws \think\db\exception\ModelNotFoundException
  135. */
  136. public function getCombinationHost()
  137. {
  138. $where = ['is_del' => 0, 'is_host' => 1, 'is_show' => 1, 'pinkIngTime' => true];
  139. return $this->search($where)->order('id desc')->select()->toArray();
  140. }
  141. }