TradeGoodsPointsServer.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/10/28
  8. * Time: 18:01
  9. */
  10. namespace app\frontend\modules\goods\services;
  11. class TradeGoodsPointsServer
  12. {
  13. const SEARCH_PAGE = 'search_page'; //商品搜索页/分类页
  14. const GOODS_PAGE = 'goods_page'; //商品详情页
  15. const SINGLE_PAGE = 'single_page'; //下单页
  16. const ORDER_PAGE = 'order_page'; //订单详情页
  17. protected $pointSet;
  18. protected $pointValueSet;
  19. protected $goodsModel;
  20. public function __construct()
  21. {
  22. $this->pointSet = \Setting::get('point.set');
  23. }
  24. /**
  25. * @description 得到积分设置
  26. * @param $goods
  27. */
  28. public function getPointSet($goods)
  29. {
  30. $this->goodsModel = $goods;
  31. //获取商城设置: 判断 积分、余额 是否有自定义名称
  32. $goodsDetail = app('GoodsDetail');
  33. $goodsDetail->setDetailInstance($this->goodsModel);
  34. $this->pointValueSet = [];
  35. $this->pointValueSet = $goodsDetail->make('GoodsDetailInstance')->getPointSet() ?: [];
  36. }
  37. /*
  38. * 设置单例商品模型
  39. */
  40. public function setSingletonGoodsModel($goodsModel)
  41. {
  42. app()->singleton(self::class, function () use ($goodsModel) {
  43. $this->getPointSet($goodsModel);
  44. return $this;
  45. });
  46. }
  47. /**
  48. * @description 查看积分设置是否开启
  49. * @param $on
  50. * @return bool
  51. */
  52. public function close($on)
  53. {
  54. return !$this->pointSet['goods_point'][$on];
  55. }
  56. /**
  57. * @description 得到最高优先级积分比例或者固定值
  58. * @param string $points
  59. * @return int|mixed|string
  60. */
  61. public function finalSetPoint($points = '')
  62. {
  63. if ((strlen($this->goodsModel->hasOneSale->point) === 0) || $this->goodsModel->hasOneSale->point != 0) {
  64. if ($this->goodsModel->hasOneSale->point) {
  65. $points = $this->goodsModel->hasOneSale->point;
  66. } elseif (!empty($this->pointValueSet['value']['set']['give_point']) && $this->pointValueSet['value']['set']['give_point'] != 0) {
  67. $points = $this->pointValueSet['value']['set']['give_point'] . '%';
  68. } else {
  69. $points = $this->pointSet['give_point'] ?: 0;
  70. }
  71. if ($points == 0) {
  72. $points = '';
  73. }
  74. }
  75. return $points;
  76. }
  77. /**
  78. * @description 计算后的积分
  79. * @param $points
  80. * @param $price
  81. * @param $cost_price
  82. * @return mixed|string
  83. */
  84. public function getPoint($points, $price, $cost_price)
  85. {
  86. // 区分类型: 如果是变量包含百分比.那么需要计算积分
  87. if (strstr($points, '%') && $this->pointSet['data_display_type']) {
  88. // 去除百分比符号
  89. $points = str_replace('%', '', $points);
  90. // give_type = 1 要计算利润再算积分
  91. if ($this->pointSet['give_type']) {
  92. $price = bcsub($price, $cost_price, 2);
  93. }
  94. // 百分比
  95. $percentage = bcdiv($points, 100, 2);
  96. // 积分
  97. $points = bcmul($price, $percentage, 2);
  98. $points = ($points <= 0) ? '' : $points;
  99. }
  100. return ($points <= 0) ? '' : $points;
  101. }
  102. }