Goods.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. /**
  5. * 邮箱验证码
  6. */
  7. class Goods Extends Model
  8. {
  9. protected $type=[
  10. 'logo'=>'json',
  11. ];
  12. public static $read=[
  13. 'num_sell'
  14. ];
  15. // 开启自动写入时间戳字段
  16. protected $autoWriteTimestamp = true;
  17. public function sku(){
  18. return $this->hasMany(GoodsSku::class)->where('is_del',0);
  19. }
  20. public function detail(){
  21. return $this->hasMany(GoodsDetail::class);
  22. }
  23. public function service(){
  24. return $this->hasMany(GoodsService::class);
  25. }
  26. public function binds(){
  27. return $this->hasMany(GoodsBind::class);
  28. }
  29. public function category(){
  30. return $this->belongsTo(Category::class);
  31. }
  32. public static $status=[
  33. 1=>'正常',
  34. 2=>'下架',
  35. ];
  36. /**
  37. * @return string[]
  38. */
  39. public static function getStatus(): array
  40. {
  41. return self::$status;
  42. }
  43. public static function show(){
  44. return self::where('status',1);
  45. }
  46. protected static function init()
  47. {
  48. self::beforeWrite(function (self $goods){
  49. if(!empty($goods['is_hot']) && $goods['is_hot']==1){
  50. $goods['hot_at']=time();
  51. }
  52. if(!empty($goods['is_kill']) && $goods['is_kill']==1){
  53. $goods['kill_at']=time();
  54. }
  55. });
  56. self::afterDelete(function (self $goods){
  57. $goods->detail()->delete();
  58. $goods->service()->delete();
  59. $goods->sku()->delete();
  60. $goods->binds()->delete();
  61. GoodsBind::where('bind_goods_id',$goods['id'])->delete();
  62. });
  63. }
  64. }