GoodsSku.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. */
  9. class GoodsSku Extends Model
  10. {
  11. public function scopeShow(Query $query){
  12. $query->where('is_del',0);
  13. }
  14. public function bindGoods(){
  15. return $this->belongsToMany(Goods::class,GoodsBind::class,'bind_goods_id','goods_sku_id');
  16. }
  17. public function bindSku(){
  18. return $this->belongsToMany(GoodsSku::class,GoodsBind::class,'bind_goods_sku_id','goods_sku_id');
  19. }
  20. public function binds(){
  21. return $this->hasMany(GoodsBind::class);
  22. }
  23. public static function show(){
  24. return self::where('is_del',0);
  25. }
  26. public function goods(){
  27. return $this->belongsTo(Goods::class);
  28. }
  29. public function getRealAmount(){
  30. $open=KillService::open();
  31. if($open){
  32. return $this['amount_kill'];
  33. }
  34. return $this['amount'];
  35. }
  36. public function getAmountAttr($amount){
  37. return $this->getRealAmount();
  38. }
  39. protected static function init()
  40. {
  41. self::afterDelete(function (self $sku){
  42. GoodsCart::where('goods_sku_id',$sku['id'])->delete();
  43. GoodsBind::where('goods_sku_id',$sku['id'])->delete();
  44. GoodsBind::where('bind_goods_sku_id',$sku['id'])->delete();
  45. GoodsInstallLink::where('goods_sku_id',$sku['id'])->delete();
  46. Favourite::where('goods_sku_id',$sku['id'])->delete();
  47. });
  48. }
  49. }