StorePromotionsAuxiliary.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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\model\activity\promotions;
  12. use app\model\activity\coupon\StoreCouponIssue;
  13. use app\model\product\brand\StoreBrand;
  14. use app\model\product\label\StoreProductLabel;
  15. use app\model\product\product\StoreProduct;
  16. use app\model\product\sku\StoreProductAttrValue;
  17. use crmeb\basic\BaseModel;
  18. use crmeb\traits\ModelTrait;
  19. /**
  20. * 优惠活动辅助表
  21. */
  22. class StorePromotionsAuxiliary extends BaseModel
  23. {
  24. use ModelTrait;
  25. /**
  26. * @var string
  27. */
  28. protected $key = 'id';
  29. /**
  30. * @var string
  31. */
  32. protected $name = 'store_promotions_auxiliary';
  33. /**
  34. * 商品规格
  35. * @param $value
  36. * @return array|mixed
  37. */
  38. protected function getUniqueAttr($value, $data)
  39. {
  40. if ($value) {
  41. return is_string($value) && (!isset($data['type']) || $data['type'] != 3) ? explode(',', $value) : $value;
  42. }
  43. return [];
  44. }
  45. /**
  46. * 关联优惠券
  47. * @return \think\model\relation\HasOne
  48. */
  49. public function coupon()
  50. {
  51. return $this->hasOne(StoreCouponIssue::class, 'id', 'coupon_id');
  52. }
  53. /**
  54. * 关联商品
  55. * @return \think\model\relation\HasOne
  56. */
  57. public function productInfo()
  58. {
  59. return $this->hasOne(StoreProduct::class, 'id', 'product_id');
  60. }
  61. /**
  62. * 赠送sku一对一
  63. * @return \think\model\relation\hasOne
  64. */
  65. public function giveAttrValue()
  66. {
  67. return $this->hasOne(StoreProductAttrValue::class, 'unique', 'unique')->where('type', 0);
  68. }
  69. /**
  70. * sku一对多
  71. * @return \think\model\relation\HasMany
  72. */
  73. public function attrValue()
  74. {
  75. return $this->hasMany(StoreProductAttrValue::class, 'product_id', 'product_id')->where('type', 0);
  76. }
  77. /**
  78. * 关联商品品牌
  79. * @return \think\model\relation\HasOne
  80. */
  81. public function brandInfo()
  82. {
  83. return $this->hasOne(StoreBrand::class, 'id', 'brand_id');
  84. }
  85. /**
  86. * 关联商品标签
  87. * @return \think\model\relation\HasOne
  88. */
  89. public function productLabelInfo()
  90. {
  91. return $this->hasOne(StoreProductLabel::class, 'id', 'store_label_id');
  92. }
  93. /**
  94. * type搜索器
  95. * @param Model $query
  96. * @param $value
  97. */
  98. public function searchTypeAttr($query, $value)
  99. {
  100. if ($value) $query->where('type', $value);
  101. }
  102. /**
  103. * product_partake_type搜索器
  104. * @param Model $query
  105. * @param $value
  106. */
  107. public function searchProductPartakeTypeAttr($query, $value)
  108. {
  109. if ($value !== '') {
  110. if (is_array($value)) {
  111. if ($value) $query->whereIn('product_partake_type', $value);
  112. } else {
  113. if ($value) $query->where('product_partake_type', $value);
  114. }
  115. }
  116. }
  117. /**
  118. * promotions_id搜索器
  119. * @param $query
  120. * @param $value
  121. */
  122. public function searchPromotionsIdAttr($query, $value)
  123. {
  124. if (is_array($value)) {
  125. if ($value) $query->whereIn('promotions_id', $value);
  126. } else {
  127. if ($value !== '') $query->where('promotions_id', $value);
  128. }
  129. }
  130. /**
  131. * coupon_id搜索器
  132. * @param $query
  133. * @param $value
  134. */
  135. public function searchCouponIdAttr($query, $value)
  136. {
  137. if (is_array($value)) {
  138. if ($value) $query->whereIn('coupon_id', $value);
  139. } else {
  140. if ($value !== '') $query->where('coupon_id', $value);
  141. }
  142. }
  143. /**
  144. * product_id搜索器
  145. * @param $query
  146. * @param $value
  147. */
  148. public function searchProductIdAttr($query, $value)
  149. {
  150. if (is_array($value)) {
  151. if ($value) $query->whereIn('product_id', $value);
  152. } else {
  153. if ($value !== '') $query->where('product_id', $value);
  154. }
  155. }
  156. /**
  157. * is_all搜索器
  158. * @param $query
  159. * @param $value
  160. */
  161. public function searchIsAllAttr($query, $value)
  162. {
  163. if ($value !== '') {
  164. $query->where('is_all', $value);
  165. }
  166. }
  167. }