PointConfigService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace addons\TinyShop\services\marketing;
  3. use Yii;
  4. use common\helpers\BcHelper;
  5. use common\enums\StatusEnum;
  6. use common\components\Service;
  7. use addons\TinyShop\common\models\marketing\PointConfig;
  8. use addons\TinyShop\common\enums\PointConfigDeductionTypeEnum;
  9. /**
  10. * Class PointConfigService
  11. * @package addons\TinyShop\services\marketing
  12. * @author jianyan74 <751393839@qq.com>
  13. */
  14. class PointConfigService extends Service
  15. {
  16. /**
  17. * @param $config
  18. * @param $order_money
  19. * @param $user_point
  20. * @return array|false|void
  21. */
  22. public function getMaxConfig($orderMoney = 0, $userPoint = 0, $isBuy = false)
  23. {
  24. $config = $this->findOne(0);
  25. if (
  26. empty($config) ||
  27. $config['status'] != StatusEnum::ENABLED ||
  28. $orderMoney == 0 ||
  29. $userPoint == 0
  30. ) {
  31. return false;
  32. }
  33. // 支付情况下不满足
  34. if ($isBuy == true && $orderMoney < $config['min_order_money']) {
  35. return false;
  36. }
  37. // 最多可抵
  38. $maxMoney = BcHelper::mul($config['convert_rate'], $userPoint);
  39. switch ($config['deduction_type']) {
  40. // 总上限
  41. case PointConfigDeductionTypeEnum::MONEY :
  42. $maxMoney > $config['max_deduction_money'] && $maxMoney = $config['max_deduction_money'];
  43. // 下单判断总上限
  44. if ($isBuy == true) {
  45. $maxMoney > $orderMoney && $maxMoney = $orderMoney;
  46. }
  47. break;
  48. // 比率
  49. case PointConfigDeductionTypeEnum::RATE :
  50. // 下单判断总上限
  51. if ($isBuy == true) {
  52. $tmpMaxMoney = BcHelper::mul($orderMoney, $config['max_deduction_rate']);
  53. $tmpMaxMoney < $maxMoney && $maxMoney = $tmpMaxMoney;
  54. }
  55. break;
  56. }
  57. // 比率过小无积分不使用抵扣
  58. $maxPoint = (int)BcHelper::div($maxMoney, $config['convert_rate']);
  59. if ($maxPoint == 0) {
  60. return false;
  61. }
  62. return [
  63. 'maxMoney' => floatval($maxMoney),
  64. 'maxPoint' => $maxPoint,
  65. 'minOrderMoney' => floatval($config['min_order_money']),
  66. 'convertRate' => floatval($config['convert_rate']),
  67. ];
  68. }
  69. /**
  70. * @return array|\yii\db\ActiveRecord|null|PointConfig
  71. */
  72. public function findOne($merchant_id)
  73. {
  74. $merchant_id = 0;
  75. return PointConfig::find()
  76. ->where(['merchant_id' => $merchant_id])
  77. ->asArray()
  78. ->one();
  79. }
  80. /**
  81. * @return PointConfig
  82. */
  83. public function one($merchant_id)
  84. {
  85. /* @var $model PointConfig */
  86. if (empty(($model = PointConfig::find()->where(['merchant_id' => $merchant_id])->one()))) {
  87. $model = new PointConfig();
  88. return $model->loadDefaultValues();
  89. }
  90. return $model;
  91. }
  92. }
粤ICP备19079148号