| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- namespace Longyi\RewardPoints\Http\Controllers\Admin;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Validator;
- 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;
- class GrowthValueController extends Controller
- {
- protected $growthValueService;
- public function __construct(GrowthValueService $growthValueService,RewardPointSettingRepository $settingRepository)
- {
- $this->growthValueService = $growthValueService;
- $this->settingRepository = $settingRepository;
- }
- /**
- * 显示所有会员的成长值列表
- */
- public function index()
- {
- $search = request()->get('search');
- $growthLevel = request()->get('growth_level');
- $minGrowthValue = request()->get('min_growth_value');
- $maxGrowthValue = request()->get('max_growth_value');
- $query = GrowthValueCustomer::with('customer');
- // 搜索客户邮箱或姓名
- if ($search) {
- $query->whereHas('customer', function ($q) use ($search) {
- $q->where('email', 'like', "%{$search}%")
- ->orWhere('name', 'like', "%{$search}%");
- });
- }
- // 成长值等级筛选
- if ($growthLevel && $growthLevel !== '') {
- $query->where('growth_level', $growthLevel);
- }
- // 成长值范围筛选
- if ($minGrowthValue !== null && $minGrowthValue !== '') {
- $query->where('growth_value', '>=', (int)$minGrowthValue);
- }
- if ($maxGrowthValue !== null && $maxGrowthValue !== '') {
- $query->where('growth_value', '<=', (int)$maxGrowthValue);
- }
- $customers = $query->orderBy('growth_value', 'desc')
- ->paginate(15)
- ->appends(request()->query());
- // 统计数据
- $totalCustomers = GrowthValueCustomer::count();
- $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(
- 'customers',
- 'totalCustomers',
- 'totalGrowthValue',
- 'avgGrowthValue',
- 'firstOrderCount',
- 'allCustomers'
- ));
- }
- public function settings()
- {
- $groups = $this->settingRepository->getGroups();
- $currentGroup = request('group', 'growth_value');
- $settings = $this->settingRepository->getSettingsByGroup($currentGroup);
- // 获取分组数据(包含名称和排序)
- $groupData = $this->getGroupData($groups);
- return view('rewardpoints::admin.growth-value.settings', compact('groups', 'currentGroup', 'settings', 'groupData'));
- }
- /**
- * 保存配置
- */
- public function saveSettings(Request $request)
- {
- $this->validate($request, [
- 'settings' => 'required|array'
- ]);
- try {
- $settings = $request->input('settings');
- foreach ($settings as $code => $value) {
- $this->settingRepository->setConfigValue($code, $value);
- }
- // 清除缓存
- cache()->forget('reward_points_settings');
- session()->flash('success', '配置保存成功');
- $redirectRoute = isset($this->_config['redirect']) ? $this->_config['redirect'] : 'admin.growth-value.settings';
- return redirect()->route($redirectRoute, ['group' => $request->input('group', 'general')]);
- } catch (\Exception $e) {
- session()->flash('error', '保存配置失败:' . $e->getMessage());
- return redirect()->back()->withInput();
- }
- }
- /**
- * 获取分组数据(包含名称和排序)
- */
- protected function getGroupData($groups)
- {
- // 定义分组排序顺序
- $groupOrder = [
- 'general' => 1, // 通用设置
- ];
- $groupData = [];
- foreach ($groups as $group) {
- $groupData[$group] = [
- 'name' => $this->getGroupName($group),
- 'order' => $groupOrder[$group] ?? 99, // 如果未定义顺序,默认排在最后
- ];
- }
- // 按照排序顺序排列
- uasort($groupData, function($a, $b) {
- return $a['order'] <=> $b['order'];
- });
- return $groupData;
- }
- /**
- * 获取分组名称
- */
- protected function getGroupName($group)
- {
- $names = [
- 'general' => '成长值设置',
- ];
- return $names[$group] ?? ucfirst($group);
- }
- /**
- * 显示某个会员的成长值详情
- */
- public function show($customerId)
- {
- $growthValueInfo = $this->growthValueService->getCustomerGrowthValue($customerId);
- $history = GrowthValueHistory::where('customer_id', $customerId)
- ->orderBy('created_at', 'desc')
- ->paginate(20);
- $customer = \Webkul\Customer\Models\Customer::find($customerId);
- return view('rewardpoints::admin.growth-value.show', compact('growthValueInfo', 'history', 'customer'));
- }
- /**
- * 手动调整会员成长值(从列表页弹窗调用)
- */
- 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');
- $amount = request('amount');
- // 如果是减少操作,将金额转为负数
- if (request('action') === 'deduct') {
- $amount = -$amount;
- }
- $this->growthValueService->adjustGrowthValue(
- $customerId,
- $amount,
- request('description', '管理员调整')
- );
- session()->flash('success', '成长值调整成功');
- } catch (\Exception $e) {
- session()->flash('error', '调整失败:' . $e->getMessage());
- }
- return redirect()->route('admin.growth-value.index');
- }
- }
|