Spu.php 5.0 KB

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