Spu.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\common\model\store\product;
  12. use app\common\model\BaseModel;
  13. use app\common\model\store\coupon\StoreCouponProduct;
  14. use app\common\model\store\StoreSeckillActive;
  15. use app\common\model\system\merchant\Merchant;
  16. use think\db\BaseQuery;
  17. class Spu extends BaseModel
  18. {
  19. /**
  20. * TODO
  21. * @return string
  22. * @author Qinii
  23. * @day 12/18/20
  24. */
  25. public static function tablePk(): string
  26. {
  27. return 'spu_id';
  28. }
  29. /**
  30. * TODO
  31. * @return string
  32. * @author Qinii
  33. * @day 12/18/20
  34. */
  35. public static function tableName(): string
  36. {
  37. return 'store_spu';
  38. }
  39. /*
  40. * -----------------------------------------------------------------------------------------------------------------
  41. * 属性
  42. * -----------------------------------------------------------------------------------------------------------------
  43. */
  44. public function getMinExtensionAttr($value)
  45. {
  46. if(!isset($this->product)) return 0;
  47. if($this->product->extension_type){
  48. return ($this->product->attrValue()->order('extension_two ASC')->value('extension_two'));
  49. } else {
  50. return bcmul(($this->product->attrValue()->order('price ASC')->value('price')) , systemConfig('extension_one_rate'),2);
  51. }
  52. }
  53. public function getMaxExtensionAttr($value)
  54. {
  55. if(!isset($this->product)) return 0;
  56. if($this->product->extension_type){
  57. return ($this->product->attrValue()->order('extension_two DESC')->value('extension_one'));
  58. } else {
  59. return bcmul(($this->product->attrValue()->order('price DESC')->value('price')) , systemConfig('extension_one_rate'),2);
  60. }
  61. }
  62. public function getStopTimeAttr()
  63. {
  64. if($this->product_type == 1){
  65. $day = date('Y-m-d',time());
  66. $_day = strtotime($day);
  67. $end_day = strtotime($this->seckillActive['end_day']);
  68. if($end_day >= $_day)
  69. return strtotime($day.$this->seckillActive['end_time'].':00:00');
  70. if($end_day < strtotime($day))
  71. return strtotime(date('Y-m-d',$end_day).$this->seckillActive['end_time'].':00:00');
  72. }
  73. }
  74. /*
  75. * -----------------------------------------------------------------------------------------------------------------
  76. * 关联表
  77. * -----------------------------------------------------------------------------------------------------------------
  78. */
  79. public function product()
  80. {
  81. return $this->hasOne(Product::class,'product_id','product_id');
  82. }
  83. public function merchant()
  84. {
  85. return $this->hasOne(Merchant::class,'mer_id','mer_id');
  86. }
  87. public function issetCoupon()
  88. {
  89. return $this->hasOne(StoreCouponProduct::class, 'product_id', 'product_id')->alias('A')
  90. ->rightJoin('StoreCoupon B', 'A.coupon_id = B.coupon_id')->where(function (BaseQuery $query) {
  91. $query->where('B.is_limited', 0)->whereOr(function (BaseQuery $query) {
  92. $query->where('B.is_limited', 1)->where('B.remain_count', '>', 0);
  93. });
  94. })->where(function (BaseQuery $query) {
  95. $query->where('B.is_timeout', 0)->whereOr(function (BaseQuery $query) {
  96. $time = date('Y-m-d H:i:s');
  97. $query->where('B.is_timeout', 1)->where('B.start_time', '<', $time)->where('B.end_time', '>', $time);
  98. });
  99. })->field('A.product_id,B.*')->where('status', 1)->where('type', 1)->where('send_type', 0)->where('is_del', 0)
  100. ->order('sort DESC,coupon_id DESC')->hidden(['is_del', 'status']);
  101. }
  102. public function merCateId()
  103. {
  104. return $this->hasMany(ProductCate::class,'product_id','product_id')->field('product_id,mer_cate_id');
  105. }
  106. public function seckillActive()
  107. {
  108. return $this->hasOne(StoreSeckillActive::class,'seckill_active_id','activity_id');
  109. }
  110. /*
  111. * -----------------------------------------------------------------------------------------------------------------
  112. * 搜索器
  113. * -----------------------------------------------------------------------------------------------------------------
  114. */
  115. public function searchMerIdAttr($query,$value)
  116. {
  117. $query->where('mer_id',$value);
  118. }
  119. public function searchProductIdAttr($query,$value)
  120. {
  121. $query->where('product_id',$value);
  122. }
  123. public function searchProductTypeAttr($query,$value)
  124. {
  125. $query->where('product_type',$value);
  126. }
  127. public function searchActivityIdAttr($query,$value)
  128. {
  129. $query->where('activity_id',$value);
  130. }
  131. public function searchKeyworkAttr($query,$value)
  132. {
  133. $query->whereLike('store_name|keyword',$value);
  134. }
  135. public function searchPriceOnAttr($query, $value)
  136. {
  137. $query->where('price','>=',$value);
  138. }
  139. public function searchPriceOffAttr($query, $value)
  140. {
  141. $query->where('price','<=',$value);
  142. }
  143. }