12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?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',
- ];
- protected $append=[
- ];
- 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);
- $query=(new self)->getQuery();
- $query->where('is_down',0);
- return $query;
- }
- 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 getTempIdsAttr($_,$model){
- return array_filter(explode(',',$model['temp_ids']));
- }
- 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 [$amount,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();
- ProgrammeGoods::where('sku_id',$sku['id'])->delete();
- });
- self::beforeWrite(function (self $goodsSku){
- $goodsSku['amount']=min(array_column($goodsSku['amount_ladder']?:[],'amount'));
- if($goodsSku['amount_kill_ladder']) {
- $goodsSku['amount_kill'] = min(array_column($goodsSku['amount_kill_ladder'], 'amount'));
- }
- });
- }
- }
|