StoreBargainDao.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\bargain;
  13. use app\dao\BaseDao;
  14. use app\model\activity\bargain\StoreBargain;
  15. /**
  16. * 砍价商品
  17. * Class StoreBargainDao
  18. * @package app\dao\activity\bargain
  19. */
  20. class StoreBargainDao extends BaseDao
  21. {
  22. /**
  23. * 设置模型
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return StoreBargain::class;
  29. }
  30. /**
  31. * @param array $where
  32. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  33. */
  34. public function search(array $where = [])
  35. {
  36. return parent::search($where)
  37. ->when(isset($where['start_status']) && $where['start_status'] !== '', function ($query) use ($where) {
  38. $time = time();
  39. switch ($where['start_status']) {
  40. case -1:
  41. $query->where('stop_time', '<', $time);
  42. break;
  43. case 0:
  44. $query->where('start_time', '>', $time);
  45. break;
  46. case 1:
  47. $query->where('start_time', '<=', $time)->where('stop_time', '>=', $time);
  48. break;
  49. }
  50. });
  51. }
  52. /**
  53. * 获取砍价列表
  54. * @param array $where
  55. * @param int $page
  56. * @param int $limit
  57. * @return array
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\DbException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. */
  62. public function getList(array $where, int $page = 0, int $limit = 0)
  63. {
  64. return $this->search($where)->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  65. $query->page($page, $limit);
  66. })->order('sort desc,id desc')->select()->toArray();
  67. }
  68. /**
  69. * 获取活动开启中的砍价id以数组形式返回
  70. * @param array $ids
  71. * @param array $field
  72. * @return array
  73. */
  74. public function getBargainIdsArray(array $ids, array $field = [])
  75. {
  76. return $this->search(['is_del' => 0, 'status' => 1])->where('start_time', '<=', time())
  77. ->where('stop_time', '>=', time())->whereIn('product_id', $ids)->column(implode(',', $field), 'product_id');
  78. }
  79. /**
  80. * 根据id获取砍价数据
  81. * @param array $ids
  82. * @param string $field
  83. * @return array
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. */
  88. public function idByBargainList(array $ids, string $field)
  89. {
  90. return $this->getModel()->whereIn('id', $ids)->field($field)->select()->toArray();
  91. }
  92. /**
  93. * 正在开启的砍价活动
  94. * @param int $status
  95. * @return StoreBargain
  96. */
  97. public function validWhere(int $status = 1)
  98. {
  99. return $this->getModel()->where('is_del', 0)->where('status', $status)->where('start_time', '<', time())->where('stop_time', '>', time());
  100. }
  101. /**
  102. * 砍价列表
  103. * @param int $page
  104. * @param int $limit
  105. * @return array
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. */
  110. public function bargainList(int $page = 0, int $limit = 0)
  111. {
  112. return $this->search(['is_del' => 0, 'status' => 1])
  113. ->where('start_time', '<=', time())
  114. ->where('stop_time', '>=', time())
  115. ->where('product_id', 'IN', function ($query) {
  116. $query->name('store_product')->where('is_show', 1)->where('is_del', 0)->field('id');
  117. })->with('product')->when($page && $limit, function ($query) use ($page, $limit) {
  118. $query->page($page, $limit);
  119. })->order('sort DESC,id DESC')->select()->toArray();
  120. }
  121. /**
  122. * 修改砍价状态
  123. * @param int $id
  124. * @param string $field
  125. * @return mixed
  126. */
  127. public function addBargain(int $id, string $field)
  128. {
  129. return $this->getModel()->where('id', $id)->inc($field, 1)->update();
  130. }
  131. }