GrowthValueController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace Longyi\RewardPoints\Http\Controllers\Admin;
  3. use Illuminate\Http\JsonResponse;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Validator;
  6. use Longyi\RewardPoints\Repositories\RewardPointSettingRepository;
  7. use Longyi\RewardPoints\Services\GrowthValueService;
  8. use Longyi\RewardPoints\Models\GrowthValueCustomer;
  9. use Longyi\RewardPoints\Models\GrowthValueHistory;
  10. use Longyi\RewardPoints\Models\GrowthValueLevel;
  11. use Webkul\Admin\Http\Controllers\Controller;
  12. class GrowthValueController extends Controller
  13. {
  14. protected $growthValueService;
  15. public function __construct(GrowthValueService $growthValueService,RewardPointSettingRepository $settingRepository)
  16. {
  17. $this->growthValueService = $growthValueService;
  18. $this->settingRepository = $settingRepository;
  19. }
  20. /**
  21. * 显示所有会员的成长值列表
  22. */
  23. public function index()
  24. {
  25. $search = request()->get('search');
  26. $growthLevel = request()->get('growth_level');
  27. $minGrowthValue = request()->get('min_growth_value');
  28. $maxGrowthValue = request()->get('max_growth_value');
  29. $query = GrowthValueCustomer::with('customer');
  30. // 搜索客户邮箱或姓名
  31. if ($search) {
  32. $query->whereHas('customer', function ($q) use ($search) {
  33. $q->where('email', 'like', "%{$search}%")
  34. ->orWhere('name', 'like', "%{$search}%");
  35. });
  36. }
  37. // 成长值等级筛选
  38. if ($growthLevel && $growthLevel !== '') {
  39. $query->where('growth_level', $growthLevel);
  40. }
  41. // 成长值范围筛选
  42. if ($minGrowthValue !== null && $minGrowthValue !== '') {
  43. $query->where('growth_value', '>=', (int)$minGrowthValue);
  44. }
  45. if ($maxGrowthValue !== null && $maxGrowthValue !== '') {
  46. $query->where('growth_value', '<=', (int)$maxGrowthValue);
  47. }
  48. $customers = $query->orderBy('growth_value', 'desc')
  49. ->paginate(15)
  50. ->appends(request()->query());
  51. // 统计数据
  52. $totalCustomers = GrowthValueCustomer::count();
  53. $totalGrowthValue = GrowthValueCustomer::sum('growth_value') ?? 0;
  54. $avgGrowthValue = $totalCustomers > 0 ? round($totalGrowthValue / $totalCustomers, 2) : 0;
  55. $firstOrderCount = GrowthValueCustomer::whereNotNull('first_order_completed_at')->count();
  56. // 所有客户列表(用于模态框下拉)
  57. $allCustomers = GrowthValueCustomer::with('customer')
  58. ->orderBy('growth_value', 'desc')
  59. ->get();
  60. $view = $this->_config['view'] ?? 'rewardpoints::admin.growth-value.index';
  61. return view($view, compact(
  62. 'customers',
  63. 'totalCustomers',
  64. 'totalGrowthValue',
  65. 'avgGrowthValue',
  66. 'firstOrderCount',
  67. 'allCustomers'
  68. ));
  69. }
  70. public function settings()
  71. {
  72. $groups = $this->settingRepository->getGroups();
  73. $currentGroup = request('group', 'growth_value');
  74. $settings = $this->settingRepository->getSettingsByGroup($currentGroup);
  75. // 获取分组数据(包含名称和排序)
  76. $groupData = $this->getGroupData($groups);
  77. return view('rewardpoints::admin.growth-value.settings', compact('groups', 'currentGroup', 'settings', 'groupData'));
  78. }
  79. /**
  80. * 保存配置
  81. */
  82. public function saveSettings(Request $request)
  83. {
  84. $this->validate($request, [
  85. 'settings' => 'required|array'
  86. ]);
  87. try {
  88. $settings = $request->input('settings');
  89. foreach ($settings as $code => $value) {
  90. $this->settingRepository->setConfigValue($code, $value);
  91. }
  92. // 清除缓存
  93. cache()->forget('reward_points_settings');
  94. session()->flash('success', '配置保存成功');
  95. $redirectRoute = isset($this->_config['redirect']) ? $this->_config['redirect'] : 'admin.growth-value.settings';
  96. return redirect()->route($redirectRoute, ['group' => $request->input('group', 'general')]);
  97. } catch (\Exception $e) {
  98. session()->flash('error', '保存配置失败:' . $e->getMessage());
  99. return redirect()->back()->withInput();
  100. }
  101. }
  102. /**
  103. * 获取分组数据(包含名称和排序)
  104. */
  105. protected function getGroupData($groups)
  106. {
  107. // 定义分组排序顺序
  108. $groupOrder = [
  109. 'general' => 1, // 通用设置
  110. ];
  111. $groupData = [];
  112. foreach ($groups as $group) {
  113. $groupData[$group] = [
  114. 'name' => $this->getGroupName($group),
  115. 'order' => $groupOrder[$group] ?? 99, // 如果未定义顺序,默认排在最后
  116. ];
  117. }
  118. // 按照排序顺序排列
  119. uasort($groupData, function($a, $b) {
  120. return $a['order'] <=> $b['order'];
  121. });
  122. return $groupData;
  123. }
  124. /**
  125. * 获取分组名称
  126. */
  127. protected function getGroupName($group)
  128. {
  129. $names = [
  130. 'general' => '成长值设置',
  131. ];
  132. return $names[$group] ?? ucfirst($group);
  133. }
  134. /**
  135. * 显示某个会员的成长值详情
  136. */
  137. public function show($customerId)
  138. {
  139. $growthValueInfo = $this->growthValueService->getCustomerGrowthValue($customerId);
  140. $history = GrowthValueHistory::where('customer_id', $customerId)
  141. ->orderBy('created_at', 'desc')
  142. ->paginate(20);
  143. $customer = \Webkul\Customer\Models\Customer::find($customerId);
  144. return view('rewardpoints::admin.growth-value.show', compact('growthValueInfo', 'history', 'customer'));
  145. }
  146. /**
  147. * 手动调整会员成长值(从列表页弹窗调用)
  148. */
  149. public function adjustFromList()
  150. {
  151. $validator = Validator::make(request()->all(), [
  152. 'customer_id' => 'required|exists:customers,id',
  153. 'action' => 'required|in:add,deduct',
  154. 'amount' => 'required|integer|min:1',
  155. 'description' => 'nullable|string|max:255',
  156. ]);
  157. if ($validator->fails()) {
  158. return redirect()->back()->withErrors($validator)->withInput();
  159. }
  160. try {
  161. $customerId = request('customer_id');
  162. $amount = request('amount');
  163. // 如果是减少操作,将金额转为负数
  164. if (request('action') === 'deduct') {
  165. $amount = -$amount;
  166. }
  167. $this->growthValueService->adjustGrowthValue(
  168. $customerId,
  169. $amount,
  170. request('description', '管理员调整')
  171. );
  172. session()->flash('success', '成长值调整成功');
  173. } catch (\Exception $e) {
  174. session()->flash('error', '调整失败:' . $e->getMessage());
  175. }
  176. return redirect()->route('admin.growth-value.index');
  177. }
  178. }