GoodsSku.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. protected $append=[
  16. ];
  17. public function scopeShow(Query $query){
  18. $query->where('is_del',0);
  19. }
  20. public function bindGoods(){
  21. return $this->belongsToMany(Goods::class,GoodsBind::class,'bind_goods_id','goods_sku_id');
  22. }
  23. public function bindSku(){
  24. return $this->belongsToMany(GoodsSku::class,GoodsBind::class,'bind_goods_sku_id','goods_sku_id');
  25. }
  26. public function binds(){
  27. return $this->hasMany(GoodsBind::class);
  28. }
  29. public static function show(){
  30. //return self::where('is_del',0);
  31. $query=(new self)->getQuery();
  32. $query->where('is_down',0);
  33. return $query;
  34. }
  35. public function goods(){
  36. return $this->belongsTo(Goods::class);
  37. }
  38. public function getRealAmount(){
  39. $open=KillService::open() && $this['goods']['is_kill']??0;
  40. if($open){
  41. return $this['amount_kill'];
  42. }
  43. return $this['amount'];
  44. }
  45. public function getAmountAttr($amount,$model){
  46. $open=KillService::open() && $this['goods']['is_kill']??0;
  47. if($open){
  48. return $model['amount_kill'];
  49. }
  50. return $model['amount'];
  51. }
  52. public function getTempIdsAttr($_,$model){
  53. return array_filter(explode(',',$model['temp_ids']));
  54. }
  55. public function calcAmount($num,$col){
  56. $amount=0;
  57. foreach ($this[$col] as $ladder){
  58. if($num>=$ladder['min'] && $num<=$ladder['max']){
  59. $amount=$ladder['amount'];
  60. break;
  61. }
  62. }
  63. /*if(!$amount) {
  64. $amount = bcAddAll($this['amount_cost'], $this['amount_cost'] * 0.5);
  65. }*/
  66. #单价,总价
  67. return [$amount,bcmul($amount,$num)];
  68. }
  69. protected static function init()
  70. {
  71. self::afterDelete(function (self $sku){
  72. GoodsCart::where('goods_sku_id',$sku['id'])->delete();
  73. GoodsBind::where('goods_sku_id',$sku['id'])->delete();
  74. GoodsBind::where('bind_goods_sku_id',$sku['id'])->delete();
  75. GoodsInstallLink::where('goods_sku_id',$sku['id'])->delete();
  76. Favourite::where('goods_sku_id',$sku['id'])->delete();
  77. ProgrammeGoods::where('sku_id',$sku['id'])->delete();
  78. });
  79. self::beforeWrite(function (self $goodsSku){
  80. $goodsSku['amount']=min(array_column($goodsSku['amount_ladder']?:[],'amount'));
  81. if($goodsSku['amount_kill_ladder']) {
  82. $goodsSku['amount_kill'] = min(array_column($goodsSku['amount_kill_ladder'], 'amount'));
  83. }
  84. });
  85. }
  86. }