Goods.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\common\model;
  3. use app\common\validate\KillService;
  4. use think\Model;
  5. /**
  6. * 邮箱验证码
  7. */
  8. class Goods Extends Model
  9. {
  10. protected $type=[
  11. 'logo'=>'json',
  12. ];
  13. public static $read=[
  14. 'num_sell'
  15. ];
  16. // 开启自动写入时间戳字段
  17. protected $autoWriteTimestamp = true;
  18. public function sku(){
  19. return $this->hasMany(GoodsSku::class);
  20. }
  21. public function detail(){
  22. return $this->hasMany(GoodsDetail::class);
  23. }
  24. public function service(){
  25. return $this->hasMany(GoodsService::class);
  26. }
  27. public function binds(){
  28. return $this->hasMany(GoodsBind::class);
  29. }
  30. public function category(){
  31. return $this->belongsTo(Category::class);
  32. }
  33. public static $status=[
  34. 1=>'正常',
  35. 2=>'下架',
  36. ];
  37. /**
  38. * @return string[]
  39. */
  40. public static function getStatus(): array
  41. {
  42. return self::$status;
  43. }
  44. public static function show(){
  45. return self::where('status',1);
  46. }
  47. public function getAmountAttr($amount,$model){
  48. if(KillService::open() && $model['is_kill']){
  49. return $model['amount_kill'];
  50. }
  51. return $amount;
  52. }
  53. public function setAmount(){
  54. $sku=$this->sku()->find();
  55. $this->where('id',$this['id'])->update([
  56. 'amount'=>$sku['amount'],
  57. 'amount_kill'=>$sku['amount_kill'],
  58. ]);
  59. }
  60. protected static function init()
  61. {
  62. self::beforeWrite(function (self $goods){
  63. if(!empty($goods['is_hot']) && $goods['is_hot']==1){
  64. $goods['hot_at']=time();
  65. }
  66. if(!empty($goods['is_kill']) && $goods['is_kill']==1){
  67. $goods['kill_at']=time();
  68. }
  69. });
  70. self::afterDelete(function (self $goods){
  71. $goods->detail()->delete();
  72. $goods->service()->delete();
  73. $goods->sku()->delete();
  74. $goods->binds()->delete();
  75. GoodsBind::where('bind_goods_id',$goods['id'])->delete();
  76. GoodsCart::where('goods_id',$goods['id'])->delete();
  77. UserCouponGoods::where('goods_id',$goods['id'])->delete();
  78. GoodsInstallLink::where('goods_id',$goods['id'])->delete();
  79. Favourite::where('fav_id',$goods['id'])->where('fav_type','goods')->delete();
  80. });
  81. self::afterUpdate(function (self $goods){
  82. //$goods->setAmount();
  83. });
  84. self::afterInsert(function (self $goods){
  85. SiteMsg::sendMsg(
  86. SiteMsg::TYPE_NEW_GOODS,
  87. 0,
  88. compact('goods')
  89. );
  90. });
  91. }
  92. }