InitializeGrowthSettings.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace Longyi\RewardPoints\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Longyi\RewardPoints\Repositories\RewardPointSettingRepository;
  5. use Longyi\DynamicMenu\Models\MenuItem;
  6. use Webkul\Customer\Models\CustomerGroup;
  7. class InitializeGrowthSettings extends Command
  8. {
  9. protected $signature = 'reward-points:init-growth-settings';
  10. protected $description = 'Initialize reward points growth value settings and menu items';
  11. protected $settingRepository;
  12. public function __construct(RewardPointSettingRepository $settingRepository)
  13. {
  14. parent::__construct();
  15. $this->settingRepository = $settingRepository;
  16. }
  17. public function handle()
  18. {
  19. $this->info('Initializing reward points growth value settings...');
  20. try {
  21. $this->initializeGrowthLevelsInCustomerGroups();
  22. $this->initializeGrowthSettings();
  23. $this->addMenuItems();
  24. $this->info('✓ Growth value settings initialized successfully!');
  25. $this->info('✓ Menu items added to admin panel!');
  26. $this->info('You can now configure growth value in: Admin > Settings > Growth Value');
  27. return 0;
  28. } catch (\Exception $e) {
  29. $this->error('Error initializing settings: ' . $e->getMessage());
  30. $this->error($e->getTraceAsString());
  31. return 1;
  32. }
  33. }
  34. /**
  35. * 在 ly_customer_groups 表中初始化成长值等级数据
  36. */
  37. protected function initializeGrowthLevelsInCustomerGroups()
  38. {
  39. $this->info('Initializing growth value levels in customer groups...');
  40. $generalGroup = CustomerGroup::where('code', 'general')->first();
  41. $wholesaleGroup = CustomerGroup::where('code', 'wholesale')->first();
  42. if ($generalGroup) {
  43. $generalGroup->update([
  44. 'min_growth_value' => 0,
  45. 'require_first_order' => false,
  46. 'growth_level_name' => 'General-V0',
  47. 'growth_discount_rate' => 0,
  48. 'growth_benefits' => ['基础会员服务'],
  49. 'growth_level_icon' => null,
  50. ]);
  51. $this->info('✓ General group updated with V0 level');
  52. }
  53. if ($generalGroup) {
  54. CustomerGroup::updateOrCreate(
  55. ['code' => 'general_v1'],
  56. [
  57. 'name' => '普通会员-V1',
  58. 'min_growth_value' => 0,
  59. 'require_first_order' => true,
  60. 'growth_level_name' => 'V1',
  61. 'growth_discount_rate' => 2,
  62. 'growth_benefits' => ['完成首单升级', '享受2%额外折扣', '优先客服支持'],
  63. 'growth_level_icon' => null,
  64. 'is_user_defined' => 1,
  65. ]
  66. );
  67. $this->info('✓ Created V1 level (General V1)');
  68. }
  69. if ($generalGroup) {
  70. CustomerGroup::updateOrCreate(
  71. ['code' => 'general_v2'],
  72. [
  73. 'name' => '普通会员-V2',
  74. 'min_growth_value' => 1000,
  75. 'require_first_order' => false,
  76. 'growth_level_name' => 'V2',
  77. 'growth_discount_rate' => 5,
  78. 'growth_benefits' => ['享受5%额外折扣', '专属优惠券', '生日礼品', '优先发货'],
  79. 'growth_level_icon' => null,
  80. 'is_user_defined' => 1,
  81. ]
  82. );
  83. $this->info('✓ Created V2 level (General V2)');
  84. }
  85. if ($wholesaleGroup) {
  86. $wholesaleGroup->update([
  87. 'min_growth_value' => 5000,
  88. 'require_first_order' => false,
  89. 'growth_level_name' => 'V3',
  90. 'growth_discount_rate' => 10,
  91. 'growth_benefits' => ['享受10%额外折扣', '专属客服经理', '新品优先体验', '免费配送', '专属活动邀请'],
  92. 'growth_level_icon' => null,
  93. ]);
  94. $this->info('✓ Wholesale group updated with V3 level');
  95. }
  96. $this->info('✓ Growth value levels initialized in ly_customer_groups table');
  97. }
  98. /**
  99. * 初始化成长值配置项
  100. */
  101. protected function initializeGrowthSettings()
  102. {
  103. $this->info('Initializing growth value settings...');
  104. $settings = [
  105. [
  106. 'code' => 'growth_value_ratio',
  107. 'name' => '成长值计算比例',
  108. 'value' => '1',
  109. 'type' => 'number',
  110. 'group' => 'growth_value',
  111. 'description' => '每消费1元获得的成长值数量',
  112. 'is_enabled' => true,
  113. ],
  114. [
  115. 'code' => 'growth_value_validity_days',
  116. 'name' => '成长值有效期(天)',
  117. 'value' => '365',
  118. 'type' => 'number',
  119. 'group' => 'growth_value',
  120. 'description' => '成长值的有效期,0表示永久有效',
  121. 'is_enabled' => true,
  122. ],
  123. ];
  124. foreach ($settings as $settingData) {
  125. \Longyi\RewardPoints\Models\RewardPointSetting::updateOrCreate(
  126. ['code' => $settingData['code']],
  127. $settingData
  128. );
  129. }
  130. $this->info('✓ Growth value settings initialized');
  131. }
  132. /**
  133. * 添加成长值模块的菜单项到动态菜单
  134. */
  135. protected function addMenuItems()
  136. {
  137. $this->info('Adding growth value menu items to dynamic menu...');
  138. // 检查是否已存在成长值模块的菜单项
  139. $existingParent = MenuItem::where('key', 'settings.growth-value')->first();
  140. if ($existingParent) {
  141. $this->warn('Growth value menu items already exist in dynamic menu.');
  142. return;
  143. }
  144. // 创建子菜单项
  145. $childItems = [
  146. [
  147. 'name' => '会员成长值',
  148. 'key' => 'customers.growth-value',
  149. 'route' => 'admin.growth-value.index',
  150. 'icon' => 'icon-users',
  151. 'sort_order' => 4,
  152. ],
  153. [
  154. 'name' => '会员成长值记录',
  155. 'key' => 'customers.growth-history',
  156. 'route' => 'admin.growth-value.history',
  157. 'icon' => 'icon-list',
  158. 'sort_order' => 5,
  159. ],
  160. [
  161. 'name' => '成长值设置',
  162. 'key' => 'customers.growth-settings',
  163. 'route' => 'admin.growth-value.settings',
  164. 'icon' => 'icon-setting',
  165. 'sort_order' => 6,
  166. ],
  167. ];
  168. foreach ($childItems as $item) {
  169. MenuItem::create([
  170. 'name' => $item['name'],
  171. 'key' => $item['key'],
  172. 'route' => $item['route'],
  173. 'icon' => $item['icon'],
  174. 'sort_order' => $item['sort_order'],
  175. 'parent_id' => 999,
  176. 'status' => 1,
  177. 'created_by' => 1,
  178. ]);
  179. }
  180. $this->info('✓ Growth value menu items added to dynamic menu successfully!');
  181. }
  182. }