StorePromotionsAuxiliaryDao.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\promotions;
  12. use app\dao\BaseDao;
  13. use app\model\activity\promotions\StorePromotions;
  14. use app\model\activity\promotions\StorePromotionsAuxiliary;
  15. /**
  16. * 优惠活动辅助表
  17. */
  18. class StorePromotionsAuxiliaryDao extends BaseDao
  19. {
  20. /**
  21. * @return string
  22. */
  23. protected function setModel(): string
  24. {
  25. return StorePromotionsAuxiliary::class;
  26. }
  27. public function joinModel(): string
  28. {
  29. return StorePromotions::class;
  30. }
  31. /**
  32. * 获取所有的分销员等级
  33. * @param array $where
  34. * @param string $field
  35. * @param array $with
  36. * @param int $page
  37. * @param int $limit
  38. * @return array
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function getList(array $where = [], string $field = '*', array $with = [], int $page = 0, int $limit = 0)
  44. {
  45. return $this->search($where)->field($field)
  46. ->when($with, function ($query) use ($with) {
  47. $query->with($with);
  48. })->when($page && $limit, function ($query) use ($page, $limit) {
  49. $query->page($page, $limit);
  50. })->select()->toArray();
  51. }
  52. /**
  53. * 关联模型
  54. * @param string $alias
  55. * @param string $join_alias
  56. * @return \crmeb\basic\BaseModel
  57. */
  58. public function getJoinModel(string $alias = 'a', string $join_alias = 'p', $join = 'left')
  59. {
  60. $this->alias = $alias;
  61. $this->joinAlis = $join_alias;
  62. /** @var StorePromotions $storePromotions */
  63. $storePromotions = app()->make($this->joinModel());
  64. $table = $storePromotions->getName();
  65. return parent::getModel()->alias($alias)->join($table . ' ' . $join_alias, $alias . '.promotions_id = ' . $join_alias . '.id', $join);
  66. }
  67. /**
  68. * 获取ids
  69. * @param array $product_id
  70. * @param int $product_partake_type
  71. * @param string $field
  72. * @return array
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. */
  77. public function getProductsPromotionsIds(array $product_id, int $product_partake_type = 2, string $field = '')
  78. {
  79. $time = time();
  80. return $this->getJoinModel()->field($field)
  81. ->where($this->joinAlis . '.type', 1)
  82. ->where($this->joinAlis . '.status', 1)
  83. ->where($this->joinAlis . '.is_del', 0)
  84. ->where($this->joinAlis . '.start_time', '<=', $time)
  85. ->where($this->joinAlis . '.stop_time', '>=', $time)
  86. ->where($this->alias . '.type', 1)
  87. ->when(in_array($product_partake_type, [2, 3]), function ($query) use ($product_partake_type, $product_id) {
  88. if ($product_partake_type == 2) {
  89. $query->where(function ($q) use ($product_id) {
  90. $q->whereOr( $this->joinAlis.'.product_partake_type', 1)
  91. ->whereOr(function ($w) use ($product_id) {
  92. $w->where($this->joinAlis.'.product_partake_type', 2)->where(function ($p) use ($product_id) {
  93. if (is_array($product_id)) {
  94. $p->whereIn($this->alias.'.product_id', $product_id);
  95. } else {
  96. $p->where($this->alias.'.product_id', $product_id);
  97. }
  98. });
  99. })->whereOr(function ($e) use ($product_id) {
  100. $e->where($this->joinAlis.'.product_partake_type', 3)->where($this->alias.'.is_all', 1)->where(function ($p) use ($product_id) {
  101. if (is_array($product_id)) {
  102. $p->whereNotIn($this->alias.'.product_id', $product_id);
  103. } else {
  104. $p->where($this->alias.'.product_id', '<>', $product_id);
  105. }
  106. });
  107. });
  108. });
  109. } else {
  110. $query->where($this->alias.'.product_partake_type', 3)->where($this->alias.'.is_all', 1)->where(function ($p) use ($product_id){
  111. if(is_array($product_id)){
  112. $p->whereNotIn($this->alias.'.product_id', $product_id);
  113. }else{
  114. $p->where($this->alias.'.product_id', $product_id);
  115. }
  116. });
  117. }
  118. })->select()->toArray();
  119. }
  120. }