Goods.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\common\model;
  3. use app\common\validate\KillService;
  4. use think\db\Query;
  5. use think\Model;
  6. /**
  7. * 邮箱验证码
  8. * @method $this|static|Query filterNormal()
  9. * @method $this|static|Query filterKill()
  10. * @method $this|static|Query filterHot()
  11. */
  12. class Goods Extends Model
  13. {
  14. protected $type=[
  15. 'logo'=>'json',
  16. ];
  17. public static $read=[
  18. 'num_sell'
  19. ];
  20. protected $append=[
  21. 'detail_url',
  22. ];
  23. // 开启自动写入时间戳字段
  24. protected $autoWriteTimestamp = true;
  25. public function sku(){
  26. return $this->hasMany(GoodsSku::class);
  27. }
  28. public function size(){
  29. return $this->hasMany(GoodsSize::class);
  30. }
  31. public function detail(){
  32. return $this->hasMany(GoodsDetail::class);
  33. }
  34. public function service(){
  35. return $this->hasMany(GoodsService::class);
  36. }
  37. public function binds(){
  38. return $this->hasMany(GoodsBind::class);
  39. }
  40. public function installLink(){
  41. return $this->hasMany(GoodsInstallLink::class);
  42. }
  43. public function category(){
  44. return $this->belongsTo(Category::class);
  45. }
  46. const STATUS_NORMAL=1;
  47. const STATUS_DOWN=2;
  48. public static $status=[
  49. self::STATUS_NORMAL=>'正常',
  50. self::STATUS_DOWN=>'下架',
  51. ];
  52. /**
  53. * @return string[]
  54. */
  55. public static function getStatus(): array
  56. {
  57. return self::$status;
  58. }
  59. public static function show(){
  60. return self::where('status',self::STATUS_NORMAL);
  61. }
  62. public function getAmountAttr($amount,$model){
  63. if(KillService::open() && $model['is_kill']){
  64. return $model['amount_kill'];
  65. }
  66. return $amount;
  67. }
  68. public function setAmount(){
  69. $sku=$this->sku()->select();
  70. $amount=$amountKill=0;
  71. foreach ($sku as $skuModel){
  72. foreach ($skuModel['amount_ladder'] as $ladder){
  73. $amount==0 && $amount=$ladder['amount'];
  74. if($amount>$ladder['amount']){
  75. $amount=$ladder['amount'];
  76. }
  77. }
  78. foreach ($skuModel['amount_kill_ladder'] as $ladder){
  79. $amountKill==0 && $amountKill=$ladder['amount'];
  80. if($amountKill>$ladder['amount']){
  81. $amountKill=$ladder['amount'];
  82. }
  83. }
  84. }
  85. $this->where('id',$this['id'])->update([
  86. 'amount'=>$amount,
  87. 'amount_kill'=>$amountKill,
  88. ]);
  89. }
  90. #维修费商品
  91. public static function getFixGoods(){
  92. static $goods;
  93. if(!$goods) {
  94. $goods = self::show()->where('is_fix', 1)->order('id', 'desc')->find();
  95. }
  96. return $goods;
  97. }
  98. protected static function init()
  99. {
  100. self::beforeWrite(function (self $goods){
  101. if(!empty($goods['is_hot']) && $goods['is_hot']==1){
  102. $goods['hot_at']=time();
  103. }
  104. if(!empty($goods['is_kill']) && $goods['is_kill']==1){
  105. $goods['kill_at']=time();
  106. }
  107. });
  108. self::afterDelete(function (self $goods){
  109. $goods->detail()->delete();
  110. $goods->service()->delete();
  111. $goods->sku()->delete();
  112. $goods->size()->delete();
  113. $goods->binds()->delete();
  114. GoodsBind::where('bind_goods_id',$goods['id'])->delete();
  115. GoodsCart::where('goods_id',$goods['id'])->delete();
  116. UserCouponGoods::where('goods_id',$goods['id'])->delete();
  117. GoodsInstallLink::where('goods_id',$goods['id'])->delete();
  118. Favourite::where('fav_id',$goods['id'])->where('fav_type','goods')->delete();
  119. ProgrammeGoods::where('goods_id',$goods['id'])->delete();
  120. CouponGoods::where('goods_id',$goods['id'])->delete();
  121. });
  122. self::afterUpdate(function (self $goods){
  123. //$goods->setAmount();
  124. });
  125. self::afterInsert(function (self $goods){
  126. SiteMsg::sendMsg(
  127. SiteMsg::TYPE_NEW_GOODS,
  128. 0,
  129. compact('goods')
  130. );
  131. });
  132. }
  133. public function scopeFilterNormal(Query $query){
  134. $query->where('is_kill',0)->where('is_hot',0);
  135. }
  136. public function scopeFilterKill(Query $query){
  137. $query->where('is_kill',1);
  138. }
  139. public function scopeFilterHot(Query $query){
  140. $query->where('is_hot',1);
  141. }
  142. public function getDetailUrlAttr($_,$model){
  143. return sprintf('%s/goodsDetail?id=%d',request()->domain(),$model['id']??0);
  144. }
  145. }