StoreDiscountsDao.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. namespace app\dao\activity\discounts;
  12. use app\dao\BaseDao;
  13. use app\model\activity\discounts\StoreDiscounts;
  14. /**
  15. * 优惠套餐
  16. * Class StoreDiscountsDao
  17. * @package app\dao\activity\discounts
  18. */
  19. class StoreDiscountsDao extends BaseDao
  20. {
  21. /**
  22. * 设置模型
  23. * @return string
  24. */
  25. protected function setModel(): string
  26. {
  27. return StoreDiscounts::class;
  28. }
  29. public function search(array $where = [])
  30. {
  31. return parent::search($where)
  32. ->when(isset($where['is_time']) && $where['is_time'] == 1, function ($query) {
  33. $query->where(function ($q) {
  34. $q->where(function ($query) {
  35. $query->where('start_time', '<=', time())->where('stop_time', '>=', time());
  36. })->whereOr(function ($query) {
  37. $query->where('start_time', 0)->where('stop_time', 0);
  38. });
  39. });
  40. });
  41. }
  42. /**
  43. * 获取列表
  44. * @param $where
  45. * @param array $with
  46. * @param $page
  47. * @param $limit
  48. * @return array
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public function getList($where, array $with = [], $page = 0, $limit = 0)
  54. {
  55. return $this->search($where)->where('is_del', 0)
  56. ->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  57. $query->page($page, $limit);
  58. })->when($with, function ($query) use ($with) {
  59. $query->with($with);
  60. })->order('sort desc,id desc')->select()->toArray();
  61. }
  62. /**
  63. * 优惠套餐列表
  64. * @param int $product_id
  65. * @param string $field
  66. * @param int $page
  67. * @param int $limit
  68. * @return array
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public function getDiscounts(int $product_id, string $field = '*', int $page = 0, int $limit = 0)
  74. {
  75. return $this->search(['product_ids' => $product_id])->field($field)
  76. ->where('is_del', 0)->where('status', 1)
  77. ->where(function ($query) {
  78. $query->where(function ($query) {
  79. $query->where('start_time', '<=', time())->where('stop_time', '>=', time());
  80. })->whereOr(function ($query) {
  81. $query->where('start_time', 0)->where('stop_time', 0);
  82. });
  83. })->where(function ($query) {
  84. $query->where(function ($query) {
  85. $query->where('is_limit', 0);
  86. })->whereOr(function ($query) {
  87. $query->where('is_limit', 1)->where('limit_num', '>', 0);
  88. });
  89. })->with(['products'])
  90. ->when($page && $limit, function ($query) use ($page, $limit) {
  91. $query->page($page, $limit);
  92. })->order('sort desc,id desc')->select()->toArray();
  93. }
  94. /**
  95. * 优惠套餐数量
  96. * @return int
  97. */
  98. public function getDiscountsCount()
  99. {
  100. return $this->getModel()
  101. ->where('is_del', 0)->where('status', 1)
  102. ->where(function ($query) {
  103. $query->where(function ($query) {
  104. $query->where('start_time', '<=', time())->where('stop_time', '>=', time());
  105. })->whereOr(function ($query) {
  106. $query->where('start_time', 0)->where('stop_time', 0);
  107. });
  108. })->count();
  109. }
  110. public function decLimitNum($id, $num = 1)
  111. {
  112. return $this->getModel()->where('id', $id)->dec('limit_num', $num)->update();
  113. }
  114. public function incLimitNum(int $id, $num = 1)
  115. {
  116. return $this->getModel()->where('id', $id)->inc('limit_num', $num)->update();
  117. }
  118. }