| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- namespace Longyi\RewardPoints\Services;
- use Webkul\Customer\Models\CustomerGroup;
- trait LevelCalculationTrait
- {
- /**
- * 根据成长值计算等级
- */
- protected function calculateLevel($growthValue)
- {
- $level = CustomerGroup::where('min_growth_value', '<=', $growthValue)
- ->orderBy('min_growth_value', 'desc')
- ->first();
- return $level && $level->growth_level_name
- ? $level->growth_level_name
- : 'V1';
- }
- /**
- * 根据等级名称获取对应的客户组ID
- */
- protected function getCustomerGroupIdByLevel($levelName)
- {
- // 根据等级名称查找
- $customerGroup = CustomerGroup::where('growth_level_name', $levelName)->first();
- if ($customerGroup) {
- return $customerGroup->id;
- }
- // 根据 code 查找
- $customerGroup = CustomerGroup::where('code', strtolower($levelName))->first();
- if ($customerGroup) {
- return $customerGroup->id;
- }
- // 返回默认组
- return optional(CustomerGroup::where('code', 'general')->first())->id;
- }
- /**
- * 获取配置值(带缓存)
- */
- protected function getSetting($code, $default = null)
- {
- $cacheKey = "growth_value_setting_{$code}";
- return cache()->remember($cacheKey, 3600, function () use ($code, $default) {
- $setting = \Longyi\RewardPoints\Models\RewardPointSetting::where('code', $code)->first();
- return $setting ? $setting->value : $default;
- });
- }
- }
|