123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace app\common\model;
- use app\common\validate\KillService;
- use think\Model;
- /**
- * 邮箱验证码
- */
- class Goods Extends Model
- {
- protected $type=[
- 'logo'=>'json',
- ];
- public static $read=[
- 'num_sell'
- ];
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = true;
- public function sku(){
- return $this->hasMany(GoodsSku::class);
- }
- public function size(){
- return $this->hasMany(GoodsSize::class)->where('type','size');
- }
- public function spec(){
- return $this->hasMany(GoodsSize::class)->where('type','spec');
- }
- public function detail(){
- return $this->hasMany(GoodsDetail::class);
- }
- public function service(){
- return $this->hasMany(GoodsService::class);
- }
- public function binds(){
- return $this->hasMany(GoodsBind::class);
- }
- public function category(){
- return $this->belongsTo(Category::class);
- }
- const STATUS_NORMAL=1;
- const STATUS_DOWN=2;
- public static $status=[
- self::STATUS_NORMAL=>'正常',
- self::STATUS_DOWN=>'下架',
- ];
- /**
- * @return string[]
- */
- public static function getStatus(): array
- {
- return self::$status;
- }
- public static function show(){
- return self::where('status',self::STATUS_NORMAL);
- }
- public function getAmountAttr($amount,$model){
- if(KillService::open() && $model['is_kill']){
- return $model['amount_kill'];
- }
- return $amount;
- }
- public function setAmount(){
- $sku=$this->sku()->select();
- $amount=$amountKill=0;
- foreach ($sku as $skuModel){
- foreach ($skuModel['amount_ladder'] as $ladder){
- $amount==0 && $amount=$ladder['amount'];
- if($amount>$ladder['amount']){
- $amount=$ladder['amount'];
- }
- }
- foreach ($skuModel['amount_kill_ladder'] as $ladder){
- $amountKill==0 && $amountKill=$ladder['amount'];
- if($amountKill>$ladder['amount']){
- $amountKill=$ladder['amount'];
- }
- }
- }
- $this->where('id',$this['id'])->update([
- 'amount'=>$amount,
- 'amount_kill'=>$amountKill,
- ]);
- }
- #维修费商品
- public static function getFixGoods(){
- static $goods;
- if(!$goods) {
- $goods = self::show()->where('is_fix', 1)->order('id', 'desc')->find();
- }
- return $goods;
- }
- protected static function init()
- {
- self::beforeWrite(function (self $goods){
- if(!empty($goods['is_hot']) && $goods['is_hot']==1){
- $goods['hot_at']=time();
- }
- if(!empty($goods['is_kill']) && $goods['is_kill']==1){
- $goods['kill_at']=time();
- }
- });
- self::afterDelete(function (self $goods){
- $goods->detail()->delete();
- $goods->service()->delete();
- $goods->sku()->delete();
- $goods->binds()->delete();
- GoodsBind::where('bind_goods_id',$goods['id'])->delete();
- GoodsCart::where('goods_id',$goods['id'])->delete();
- UserCouponGoods::where('goods_id',$goods['id'])->delete();
- GoodsInstallLink::where('goods_id',$goods['id'])->delete();
- Favourite::where('fav_id',$goods['id'])->where('fav_type','goods')->delete();
- });
- self::afterUpdate(function (self $goods){
- //$goods->setAmount();
- });
- self::afterInsert(function (self $goods){
- SiteMsg::sendMsg(
- SiteMsg::TYPE_NEW_GOODS,
- 0,
- compact('goods')
- );
- });
- }
- }
|