MemberLevel.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/2/27
  6. * Time: 上午11:18
  7. */
  8. namespace app\common\models;
  9. use app\frontend\modules\orderGoods\price\adapter\BaseGoodsPriceAdapter;
  10. use Illuminate\Database\Eloquent\Builder;
  11. use Illuminate\Database\Eloquent\SoftDeletes;
  12. class MemberLevel extends BaseModel
  13. {
  14. use SoftDeletes;
  15. public $table = 'yz_member_level';
  16. protected $guarded = [''];
  17. /**
  18. * 设置全局作用域 拼接 uniacid()
  19. */
  20. public static function boot()
  21. {
  22. parent::boot();
  23. static::addGlobalScope('uniacid',function (Builder $builder) {
  24. return $builder->uniacid();
  25. });
  26. }
  27. public function scopeRecords($query)
  28. {
  29. return $query->select('id','level','level_name');
  30. }
  31. public static function getMemberLevel($level_id)
  32. {
  33. return self::uniacid()->where("id",$level_id)->first();
  34. }
  35. public static function getNextMemberLevel($level)
  36. {
  37. return self::uniacid()->where('level',">",$level->level)->orderBy('level','ASC')->first();
  38. }
  39. public static function getFirstLevel()
  40. {
  41. return self::uniacid()->orderBy('level','ASC')->first();
  42. }
  43. /**
  44. * 获取默认等级
  45. *
  46. * @return mixed
  47. */
  48. public static function getDefaultLevelId()
  49. {
  50. return self::select('id')
  51. ->uniacid()
  52. ->where('is_default', 1);
  53. }
  54. /**
  55. * @param BaseGoodsPriceAdapter $priceClass
  56. */
  57. public function getDiscountCalculation($priceClass)
  58. {
  59. //获取设置的计算方式
  60. $level_discount_calculation = \Setting::get('shop.member.level_discount_calculation');
  61. switch ($level_discount_calculation) {
  62. case 1:
  63. //取商品成本价
  64. $discountAmount = $this->getMemberLevelGoodsCostPriceDiscountAmount($priceClass->getCostPrice());
  65. break;
  66. default:
  67. ///为空为0,默认取商品现价
  68. $discountAmount = $this->getMemberLevelGoodsPriceDiscountAmount($priceClass->getDealPrice());
  69. break;
  70. }
  71. return max($discountAmount, 0);
  72. }
  73. public function getMemberLevelGoodsPriceDiscountAmount($goodsPrice)
  74. {
  75. // 商品折扣 默认 10折
  76. $this->discount = trim($this->discount);
  77. $this->discount = $this->discount == false ? 10 : $this->discount;
  78. if ($this->discount > 10) {
  79. $this->discount = 10;
  80. }
  81. // 折扣/10 得到折扣百分比
  82. return (1 - $this->discount / 10) * $goodsPrice;
  83. }
  84. public function getMemberLevelGoodsCostPriceDiscountAmount($goodsCostPrice)
  85. {
  86. $discount = trim($this->discount);
  87. if (empty($discount)) {
  88. return 0;
  89. }
  90. return ($discount / 100) * $goodsCostPrice;
  91. }
  92. /**
  93. * 商品全局等级折扣后价格
  94. * @param $goodsPrice
  95. * @return float|int
  96. */
  97. public function getMemberLevelGoodsDiscountAmount($priceClass)
  98. {
  99. return $this->getDiscountCalculation($priceClass);
  100. }
  101. }