123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- namespace app\common\model;
- use app\common\validate\KillService;
- use think\db\Query;
- use think\Model;
- /**
- * 邮箱验证码
- */
- class GoodsSku Extends Model
- {
- protected $type=[
- 'amount_ladder'=>'array',
- 'amount_kill_ladder'=>'array',
- ];
- public function scopeShow(Query $query){
- $query->where('is_del',0);
- }
- public function bindGoods(){
- return $this->belongsToMany(Goods::class,GoodsBind::class,'bind_goods_id','goods_sku_id');
- }
- public function bindSku(){
- return $this->belongsToMany(GoodsSku::class,GoodsBind::class,'bind_goods_sku_id','goods_sku_id');
- }
- public function binds(){
- return $this->hasMany(GoodsBind::class);
- }
- public static function show(){
- return self::where('is_del',0);
- }
- public function goods(){
- return $this->belongsTo(Goods::class);
- }
- public function getRealAmount(){
- $open=KillService::open() && $this['goods']['is_kill']??0;
- if($open){
- return $this['amount_kill'];
- }
- return $this['amount'];
- }
- public function getAmountAttr($amount,$model){
- $open=KillService::open() && $this['goods']['is_kill']??0;
- if($open){
- return $model['amount_kill'];
- }
- return $model['amount'];
- }
- public function calcAmount($num,$col){
- $amount=0;
- foreach ($this[$col] as $ladder){
- if($num>=$ladder['min'] && $num<=$ladder['max']){
- $amount=$ladder['amount'];
- break;
- }
- }
- if(!$amount) {
- $amount = bcAddAll($this['amount_cost'], $this['amount_cost'] * 0.5);
- }
- return bcmul($amount,$num);
- }
- protected static function init()
- {
- self::afterDelete(function (self $sku){
- GoodsCart::where('goods_sku_id',$sku['id'])->delete();
- GoodsBind::where('goods_sku_id',$sku['id'])->delete();
- GoodsBind::where('bind_goods_sku_id',$sku['id'])->delete();
- GoodsInstallLink::where('goods_sku_id',$sku['id'])->delete();
- Favourite::where('goods_sku_id',$sku['id'])->delete();
- });
- self::beforeWrite(function (self $goodsSku){
- $goodsSku['amount']=min(array_column($goodsSku['amount_ladder']?:[],'amount'));
- $goodsSku['amount_kill']=min(array_column($goodsSku['amount_kill_ladder']?:[],'amount'));
- });
- }
- }
|