|
|
@@ -5,18 +5,24 @@ namespace Longyi\RewardPoints\Http\Controllers\Admin;
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
use Illuminate\Http\Request;
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
+use Carbon\Carbon;
|
|
|
use Longyi\RewardPoints\Repositories\RewardPointSettingRepository;
|
|
|
use Longyi\RewardPoints\Services\GrowthValueService;
|
|
|
use Longyi\RewardPoints\Models\GrowthValueCustomer;
|
|
|
use Longyi\RewardPoints\Models\GrowthValueHistory;
|
|
|
use Longyi\RewardPoints\Models\GrowthValueLevel;
|
|
|
use Webkul\Admin\Http\Controllers\Controller;
|
|
|
+use Webkul\Customer\Models\Customer;
|
|
|
+use Webkul\Customer\Models\CustomerGroup;
|
|
|
|
|
|
class GrowthValueController extends Controller
|
|
|
{
|
|
|
protected $growthValueService;
|
|
|
+ protected $settingRepository;
|
|
|
|
|
|
- public function __construct(GrowthValueService $growthValueService,RewardPointSettingRepository $settingRepository)
|
|
|
+ public function __construct(GrowthValueService $growthValueService, RewardPointSettingRepository $settingRepository)
|
|
|
{
|
|
|
$this->growthValueService = $growthValueService;
|
|
|
$this->settingRepository = $settingRepository;
|
|
|
@@ -65,12 +71,6 @@ class GrowthValueController extends Controller
|
|
|
$totalGrowthValue = GrowthValueCustomer::sum('growth_value') ?? 0;
|
|
|
$avgGrowthValue = $totalCustomers > 0 ? round($totalGrowthValue / $totalCustomers, 2) : 0;
|
|
|
$firstOrderCount = GrowthValueCustomer::whereNotNull('first_order_completed_at')->count();
|
|
|
-
|
|
|
- // 所有客户列表(用于模态框下拉)
|
|
|
- $allCustomers = GrowthValueCustomer::with('customer')
|
|
|
- ->orderBy('growth_value', 'desc')
|
|
|
- ->get();
|
|
|
-
|
|
|
$view = $this->_config['view'] ?? 'rewardpoints::admin.growth-value.index';
|
|
|
|
|
|
return view($view, compact(
|
|
|
@@ -78,8 +78,7 @@ class GrowthValueController extends Controller
|
|
|
'totalCustomers',
|
|
|
'totalGrowthValue',
|
|
|
'avgGrowthValue',
|
|
|
- 'firstOrderCount',
|
|
|
- 'allCustomers'
|
|
|
+ 'firstOrderCount'
|
|
|
));
|
|
|
}
|
|
|
|
|
|
@@ -94,6 +93,7 @@ class GrowthValueController extends Controller
|
|
|
|
|
|
return view('rewardpoints::admin.growth-value.settings', compact('groups', 'currentGroup', 'settings', 'groupData'));
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 保存配置
|
|
|
*/
|
|
|
@@ -123,6 +123,7 @@ class GrowthValueController extends Controller
|
|
|
return redirect()->back()->withInput();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 获取分组数据(包含名称和排序)
|
|
|
*/
|
|
|
@@ -137,7 +138,7 @@ class GrowthValueController extends Controller
|
|
|
foreach ($groups as $group) {
|
|
|
$groupData[$group] = [
|
|
|
'name' => $this->getGroupName($group),
|
|
|
- 'order' => $groupOrder[$group] ?? 99, // 如果未定义顺序,默认排在最后
|
|
|
+ 'order' => $groupOrder[$group] ?? 99,
|
|
|
];
|
|
|
}
|
|
|
|
|
|
@@ -148,6 +149,7 @@ class GrowthValueController extends Controller
|
|
|
|
|
|
return $groupData;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 获取分组名称
|
|
|
*/
|
|
|
@@ -158,6 +160,7 @@ class GrowthValueController extends Controller
|
|
|
];
|
|
|
return $names[$group] ?? ucfirst($group);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 显示某个会员的成长值详情
|
|
|
*/
|
|
|
@@ -167,49 +170,165 @@ class GrowthValueController extends Controller
|
|
|
$history = GrowthValueHistory::where('customer_id', $customerId)
|
|
|
->orderBy('created_at', 'desc')
|
|
|
->paginate(20);
|
|
|
- $customer = \Webkul\Customer\Models\Customer::find($customerId);
|
|
|
+ $customer = Customer::find($customerId);
|
|
|
|
|
|
return view('rewardpoints::admin.growth-value.show', compact('growthValueInfo', 'history', 'customer'));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 手动调整会员成长值(从列表页弹窗调用)
|
|
|
+ * 从列表页调整成长值(使用 Eloquent Model)
|
|
|
*/
|
|
|
public function adjustFromList()
|
|
|
{
|
|
|
- $validator = Validator::make(request()->all(), [
|
|
|
- 'customer_id' => 'required|exists:customers,id',
|
|
|
- 'action' => 'required|in:add,deduct',
|
|
|
- 'amount' => 'required|integer|min:1',
|
|
|
- 'description' => 'nullable|string|max:255',
|
|
|
- ]);
|
|
|
-
|
|
|
- if ($validator->fails()) {
|
|
|
- return redirect()->back()->withErrors($validator)->withInput();
|
|
|
- }
|
|
|
-
|
|
|
try {
|
|
|
- $customerId = request('customer_id');
|
|
|
+ $identifier = request('customer_identifier');
|
|
|
$amount = request('amount');
|
|
|
+ $description=request('description', '管理员调整');
|
|
|
+ $customer = null;
|
|
|
+
|
|
|
+ if (filter_var($identifier, FILTER_VALIDATE_EMAIL)) {
|
|
|
+ $customer = \Webkul\Customer\Models\Customer::where('email', $identifier)->first();
|
|
|
+ } elseif (is_numeric($identifier)) {
|
|
|
+ $customer = \Webkul\Customer\Models\Customer::find((int)$identifier);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$customer) {
|
|
|
+ return response()->json([
|
|
|
+ 'success' => false,
|
|
|
+ 'message' => '未找到该客户,请检查邮箱或ID是否正确'
|
|
|
+ ], 404);
|
|
|
+ }
|
|
|
|
|
|
- // 如果是减少操作,将金额转为负数
|
|
|
+ $customerId = $customer->id;
|
|
|
if (request('action') === 'deduct') {
|
|
|
$amount = -$amount;
|
|
|
}
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ // 使用 Eloquent Model 获取成长值记录(带锁)
|
|
|
+ $growthValue = GrowthValueCustomer::where('customer_id', $customerId)
|
|
|
+ ->lockForUpdate()
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ $oldValue = $growthValue ? $growthValue->growth_value : 0;
|
|
|
+ $newValue = max(0, $oldValue + $amount);
|
|
|
+ $oldLevel = $growthValue ? $growthValue->growth_level : 'V0';
|
|
|
+
|
|
|
+ // 计算新等级
|
|
|
+ $newLevel = $this->calculateLevel($newValue);
|
|
|
+ $levelChanged = ($oldLevel !== $newLevel);
|
|
|
+
|
|
|
+ // 获取对应的客户组ID
|
|
|
+ $newCustomerGroupId = $this->getCustomerGroupIdByLevel($newLevel);
|
|
|
+ $oldCustomerGroupId = null;
|
|
|
+
|
|
|
+ if ($growthValue) {
|
|
|
+ // 获取旧的客户组ID
|
|
|
+ $customer = Customer::find($customerId);
|
|
|
+ $oldCustomerGroupId = $customer ? $customer->customer_group_id : null;
|
|
|
+
|
|
|
+ // 使用 Eloquent Model 更新现有记录
|
|
|
+ $growthValue->growth_value = $newValue;
|
|
|
+ if ($levelChanged) {
|
|
|
+ $growthValue->growth_level = $newLevel;
|
|
|
+ $growthValue->level_updated_at = Carbon::now();
|
|
|
+ }
|
|
|
+ $growthValue->updated_at = Carbon::now();
|
|
|
+ $growthValue->save();
|
|
|
+ } else {
|
|
|
+ // 使用 Eloquent Model 创建新记录
|
|
|
+ $growthValue = GrowthValueCustomer::create([
|
|
|
+ 'customer_id' => $customerId,
|
|
|
+ 'growth_value' => $newValue,
|
|
|
+ 'growth_level' => $newLevel,
|
|
|
+ 'level_updated_at' => $levelChanged ? Carbon::now() : null,
|
|
|
+ 'created_at' => Carbon::now(),
|
|
|
+ 'updated_at' => Carbon::now(),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果等级发生变化,更新用户表的 customer_group_id
|
|
|
+ if ($levelChanged && $newCustomerGroupId) {
|
|
|
+ Customer::where('id', $customerId)->update([
|
|
|
+ 'customer_group_id' => $newCustomerGroupId,
|
|
|
+ 'updated_at' => Carbon::now(),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
|
|
|
- $this->growthValueService->adjustGrowthValue(
|
|
|
- $customerId,
|
|
|
- $amount,
|
|
|
- request('description', '管理员调整')
|
|
|
- );
|
|
|
+ // 使用 Eloquent Model 记录历史
|
|
|
+ GrowthValueHistory::create([
|
|
|
+ 'customer_id' => $customerId,
|
|
|
+ 'order_id' => null,
|
|
|
+ 'order_amount' => 0,
|
|
|
+ 'growth_value_earned' => $amount,
|
|
|
+ 'total_growth_value' => $newValue,
|
|
|
+ 'level_before' => $levelChanged ? $oldLevel : null,
|
|
|
+ 'level_after' => $levelChanged ? $newLevel : null,
|
|
|
+ 'event_type' => 'admin_adjust',
|
|
|
+ 'description' => $description,
|
|
|
+ 'created_at' => Carbon::now(),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+
|
|
|
+ return response()->json([
|
|
|
+ 'success' => true,
|
|
|
+ 'message' => '成长值调整成功',
|
|
|
+ 'new_value' => $newValue,
|
|
|
+ 'new_level' => $newLevel,
|
|
|
+ 'level_changed' => $levelChanged,
|
|
|
+ 'old_group_id' => $oldCustomerGroupId,
|
|
|
+ 'new_group_id' => $newCustomerGroupId,
|
|
|
+ ]);
|
|
|
|
|
|
- session()->flash('success', '成长值调整成功');
|
|
|
} catch (\Exception $e) {
|
|
|
- session()->flash('error', '调整失败:' . $e->getMessage());
|
|
|
+ DB::rollBack();
|
|
|
+ Log::error('成长值调整失败:' . $e->getMessage());
|
|
|
+
|
|
|
+ return response()->json([
|
|
|
+ 'success' => false,
|
|
|
+ 'message' => '调整失败:' . $e->getMessage(),
|
|
|
+ ], 500);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据等级名称获取对应的客户组ID
|
|
|
+ */
|
|
|
+ private function getCustomerGroupIdByLevel($levelName)
|
|
|
+ {
|
|
|
+ // 根据等级名称查找对应的客户组
|
|
|
+ $customerGroup = CustomerGroup::where('growth_level_name', $levelName)->first();
|
|
|
+
|
|
|
+ if ($customerGroup) {
|
|
|
+ return $customerGroup->id;
|
|
|
}
|
|
|
|
|
|
- return redirect()->route('admin.growth-value.index');
|
|
|
+ // 如果没有找到,尝试根据 code 查找
|
|
|
+ $customerGroup = CustomerGroup::where('code', strtolower($levelName))->first();
|
|
|
+
|
|
|
+ if ($customerGroup) {
|
|
|
+ return $customerGroup->id;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 默认返回 general 组
|
|
|
+ $defaultGroup = CustomerGroup::where('code', 'general')->first();
|
|
|
+ return $defaultGroup ? $defaultGroup->id : null;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据成长值计算等级(使用 Eloquent Model)
|
|
|
+ */
|
|
|
+ private function calculateLevel($growthValue)
|
|
|
+ {
|
|
|
+ // 使用 Eloquent Model 查询等级配置
|
|
|
+ $level = CustomerGroup::where('min_growth_value', '<=', $growthValue)
|
|
|
+ ->orderBy('min_growth_value', 'desc')
|
|
|
+ ->first();
|
|
|
|
|
|
+ if ($level && $level->growth_level_name) {
|
|
|
+ return $level->growth_level_name;
|
|
|
+ }
|
|
|
+ return 'V0';
|
|
|
+ }
|
|
|
}
|