Goods.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 size(){
  22. return $this->hasMany(GoodsSize::class)->where('type','size');
  23. }
  24. public function spec(){
  25. return $this->hasMany(GoodsSize::class)->where('type','spec');
  26. }
  27. public function detail(){
  28. return $this->hasMany(GoodsDetail::class);
  29. }
  30. public function service(){
  31. return $this->hasMany(GoodsService::class);
  32. }
  33. public function binds(){
  34. return $this->hasMany(GoodsBind::class);
  35. }
  36. public function category(){
  37. return $this->belongsTo(Category::class);
  38. }
  39. const STATUS_NORMAL=1;
  40. const STATUS_DOWN=2;
  41. public static $status=[
  42. self::STATUS_NORMAL=>'正常',
  43. self::STATUS_DOWN=>'下架',
  44. ];
  45. /**
  46. * @return string[]
  47. */
  48. public static function getStatus(): array
  49. {
  50. return self::$status;
  51. }
  52. public static function show(){
  53. return self::where('status',self::STATUS_NORMAL);
  54. }
  55. public function getAmountAttr($amount,$model){
  56. if(KillService::open() && $model['is_kill']){
  57. return $model['amount_kill'];
  58. }
  59. return $amount;
  60. }
  61. public function setAmount(){
  62. $sku=$this->sku()->select();
  63. $amount=$amountKill=0;
  64. foreach ($sku as $skuModel){
  65. foreach ($skuModel['amount_ladder'] as $ladder){
  66. $amount==0 && $amount=$ladder['amount'];
  67. if($amount>$ladder['amount']){
  68. $amount=$ladder['amount'];
  69. }
  70. }
  71. foreach ($skuModel['amount_kill_ladder'] as $ladder){
  72. $amountKill==0 && $amountKill=$ladder['amount'];
  73. if($amountKill>$ladder['amount']){
  74. $amountKill=$ladder['amount'];
  75. }
  76. }
  77. }
  78. $this->where('id',$this['id'])->update([
  79. 'amount'=>$amount,
  80. 'amount_kill'=>$amountKill,
  81. ]);
  82. }
  83. #维修费商品
  84. public static function getFixGoods(){
  85. static $goods;
  86. if(!$goods) {
  87. $goods = self::show()->where('is_fix', 1)->order('id', 'desc')->find();
  88. }
  89. return $goods;
  90. }
  91. protected static function init()
  92. {
  93. self::beforeWrite(function (self $goods){
  94. if(!empty($goods['is_hot']) && $goods['is_hot']==1){
  95. $goods['hot_at']=time();
  96. }
  97. if(!empty($goods['is_kill']) && $goods['is_kill']==1){
  98. $goods['kill_at']=time();
  99. }
  100. });
  101. self::afterDelete(function (self $goods){
  102. $goods->detail()->delete();
  103. $goods->service()->delete();
  104. $goods->sku()->delete();
  105. $goods->binds()->delete();
  106. GoodsBind::where('bind_goods_id',$goods['id'])->delete();
  107. GoodsCart::where('goods_id',$goods['id'])->delete();
  108. UserCouponGoods::where('goods_id',$goods['id'])->delete();
  109. GoodsInstallLink::where('goods_id',$goods['id'])->delete();
  110. Favourite::where('fav_id',$goods['id'])->where('fav_type','goods')->delete();
  111. });
  112. self::afterUpdate(function (self $goods){
  113. //$goods->setAmount();
  114. });
  115. self::afterInsert(function (self $goods){
  116. SiteMsg::sendMsg(
  117. SiteMsg::TYPE_NEW_GOODS,
  118. 0,
  119. compact('goods')
  120. );
  121. });
  122. }
  123. }