GoodsSku.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\common\model;
  3. use app\common\validate\KillService;
  4. use think\db\Query;
  5. use think\Model;
  6. /**
  7. * 邮箱验证码
  8. */
  9. class GoodsSku Extends Model
  10. {
  11. protected $type=[
  12. 'amount_ladder'=>'array',
  13. 'amount_kill_ladder'=>'array',
  14. ];
  15. public function scopeShow(Query $query){
  16. $query->where('is_del',0);
  17. }
  18. public function bindGoods(){
  19. return $this->belongsToMany(Goods::class,GoodsBind::class,'bind_goods_id','goods_sku_id');
  20. }
  21. public function bindSku(){
  22. return $this->belongsToMany(GoodsSku::class,GoodsBind::class,'bind_goods_sku_id','goods_sku_id');
  23. }
  24. public function binds(){
  25. return $this->hasMany(GoodsBind::class);
  26. }
  27. public static function show(){
  28. return self::where('is_del',0);
  29. }
  30. public function goods(){
  31. return $this->belongsTo(Goods::class);
  32. }
  33. public function getRealAmount(){
  34. $open=KillService::open() && $this['goods']['is_kill']??0;
  35. if($open){
  36. return $this['amount_kill'];
  37. }
  38. return $this['amount'];
  39. }
  40. public function getAmountAttr($amount,$model){
  41. $open=KillService::open() && $this['goods']['is_kill']??0;
  42. if($open){
  43. return $model['amount_kill'];
  44. }
  45. return $model['amount'];
  46. }
  47. public function calcAmount($num,$col){
  48. $amount=0;
  49. foreach ($this[$col] as $ladder){
  50. if($num>=$ladder['min'] && $num<=$ladder['max']){
  51. $amount=$ladder['amount'];
  52. break;
  53. }
  54. }
  55. if(!$amount) {
  56. $amount = bcAddAll($this['amount_cost'], $this['amount_cost'] * 0.5);
  57. }
  58. return bcmul($amount,$num);
  59. }
  60. protected static function init()
  61. {
  62. self::afterDelete(function (self $sku){
  63. GoodsCart::where('goods_sku_id',$sku['id'])->delete();
  64. GoodsBind::where('goods_sku_id',$sku['id'])->delete();
  65. GoodsBind::where('bind_goods_sku_id',$sku['id'])->delete();
  66. GoodsInstallLink::where('goods_sku_id',$sku['id'])->delete();
  67. Favourite::where('goods_sku_id',$sku['id'])->delete();
  68. });
  69. self::beforeWrite(function (self $goodsSku){
  70. $goodsSku['amount']=min(array_column($goodsSku['amount_ladder']?:[],'amount'));
  71. $goodsSku['amount_kill']=min(array_column($goodsSku['amount_kill_ladder']?:[],'amount'));
  72. });
  73. }
  74. }