createTestData.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. require('dotenv').config();
  2. const bcrypt = require('bcryptjs');
  3. const pool = require('../src/config/database');
  4. const crypto = require('crypto');
  5. const moment = require('moment');
  6. async function createTestData() {
  7. const connection = await pool.getConnection();
  8. try {
  9. console.log('开始创建测试数据...\n');
  10. // 创建测试用户
  11. const users = [
  12. {
  13. id: crypto.randomUUID(),
  14. username: 'sales001',
  15. password: await bcrypt.hash('123456', 10),
  16. real_name: '张三',
  17. email: 'zhangsan@example.com',
  18. phone: '13800138001',
  19. role: 'sales',
  20. department: '销售部',
  21. team: '华北团队'
  22. },
  23. {
  24. id: crypto.randomUUID(),
  25. username: 'sales002',
  26. password: await bcrypt.hash('123456', 10),
  27. real_name: '李四',
  28. email: 'lisi@example.com',
  29. phone: '13800138002',
  30. role: 'sales',
  31. department: '销售部',
  32. team: '华北团队'
  33. },
  34. {
  35. id: crypto.randomUUID(),
  36. username: 'manager001',
  37. password: await bcrypt.hash('123456', 10),
  38. real_name: '王经理',
  39. email: 'wangmanager@example.com',
  40. phone: '13800138003',
  41. role: 'sales_manager',
  42. department: '销售部',
  43. team: '华北团队'
  44. }
  45. ];
  46. for (const user of users) {
  47. await connection.query(
  48. `INSERT INTO users (id, username, password, real_name, email, phone, role, department, team)
  49. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
  50. ON DUPLICATE KEY UPDATE username=username`,
  51. [user.id, user.username, user.password, user.real_name, user.email, user.phone, user.role, user.department, user.team]
  52. );
  53. console.log(`✅ 创建用户: ${user.real_name} (${user.username}) - 密码: 123456`);
  54. }
  55. // 创建测试客户
  56. const industries = ['互联网/软件', '制造业', '金融', '教育', '医疗', '零售'];
  57. const regions = ['北京', '上海', '广州', '深圳', '杭州', '成都'];
  58. const sources = ['转介绍', '展会', '自主开发', '电话营销', '网络推广'];
  59. const statuses = ['following', 'following', 'following', 'won', 'lost'];
  60. const customers = [];
  61. for (let i = 1; i <= 20; i++) {
  62. const status = statuses[Math.floor(Math.random() * statuses.length)];
  63. const salesOwner = users[Math.floor(Math.random() * 2)]; // 随机分配给销售
  64. const daysAgo = Math.floor(Math.random() * 60);
  65. const reportTime = moment().subtract(daysAgo, 'days');
  66. customers.push({
  67. id: crypto.randomUUID(),
  68. customer_name: `测试客户${String(i).padStart(3, '0')}有限公司`,
  69. industry: industries[Math.floor(Math.random() * industries.length)],
  70. region: regions[Math.floor(Math.random() * regions.length)],
  71. contact_person: `联系人${i}`,
  72. contact_phone: `138${String(Math.floor(Math.random() * 100000000)).padStart(8, '0')}`,
  73. demand_description: `需求描述${i}:希望采购CRM系统,预算${Math.floor(Math.random() * 50) + 10}万元`,
  74. source: sources[Math.floor(Math.random() * sources.length)],
  75. sales_owner: salesOwner.id,
  76. report_time: reportTime.format('YYYY-MM-DD HH:mm:ss'),
  77. protected_end_date: moment(reportTime).add(30, 'days').format('YYYY-MM-DD HH:mm:ss'),
  78. status: status,
  79. last_followup: status === 'following' ? moment().subtract(Math.floor(Math.random() * 7), 'days').format('YYYY-MM-DD HH:mm:ss') : null,
  80. is_in_pool: false
  81. });
  82. }
  83. // 创建一些公海池客户
  84. for (let i = 21; i <= 25; i++) {
  85. customers.push({
  86. id: crypto.randomUUID(),
  87. customer_name: `公海客户${String(i).padStart(3, '0')}有限公司`,
  88. industry: industries[Math.floor(Math.random() * industries.length)],
  89. region: regions[Math.floor(Math.random() * regions.length)],
  90. contact_person: `联系人${i}`,
  91. contact_phone: `138${String(Math.floor(Math.random() * 100000000)).padStart(8, '0')}`,
  92. demand_description: `需求描述${i}`,
  93. source: sources[Math.floor(Math.random() * sources.length)],
  94. sales_owner: users[0].id,
  95. report_time: moment().subtract(60, 'days').format('YYYY-MM-DD HH:mm:ss'),
  96. protected_end_date: moment().subtract(10, 'days').format('YYYY-MM-DD HH:mm:ss'),
  97. status: 'released',
  98. release_reason: '保护期自动到期',
  99. is_in_pool: true
  100. });
  101. }
  102. for (const customer of customers) {
  103. await connection.query(
  104. `INSERT INTO customers
  105. (id, customer_name, industry, region, contact_person, contact_phone,
  106. demand_description, source, sales_owner, report_time, protected_end_date,
  107. status, last_followup, release_reason, is_in_pool)
  108. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
  109. [
  110. customer.id, customer.customer_name, customer.industry, customer.region,
  111. customer.contact_person, customer.contact_phone, customer.demand_description,
  112. customer.source, customer.sales_owner, customer.report_time, customer.protected_end_date,
  113. customer.status, customer.last_followup, customer.release_reason, customer.is_in_pool
  114. ]
  115. );
  116. }
  117. console.log(`\n✅ 创建 ${customers.length} 个测试客户`);
  118. // 创建跟进记录
  119. const followupTypes = ['call', 'visit', 'email', 'wechat', 'other'];
  120. let followupCount = 0;
  121. for (const customer of customers.slice(0, 15)) {
  122. if (customer.status === 'following') {
  123. const recordCount = Math.floor(Math.random() * 5) + 1;
  124. for (let i = 0; i < recordCount; i++) {
  125. await connection.query(
  126. `INSERT INTO followup_records (id, customer_id, user_id, followup_type, content, next_plan, created_at)
  127. VALUES (?, ?, ?, ?, ?, ?, ?)`,
  128. [
  129. crypto.randomUUID(),
  130. customer.id,
  131. customer.sales_owner,
  132. followupTypes[Math.floor(Math.random() * followupTypes.length)],
  133. `跟进记录${i + 1}:与客户进行了沟通`,
  134. i < recordCount - 1 ? '继续跟进' : '下周再联系',
  135. moment().subtract(Math.floor(Math.random() * 30), 'days').format('YYYY-MM-DD HH:mm:ss')
  136. ]
  137. );
  138. followupCount++;
  139. }
  140. }
  141. }
  142. console.log(`✅ 创建 ${followupCount} 条跟进记录`);
  143. console.log('\n🎉 测试数据创建完成!\n');
  144. console.log('测试账号信息:');
  145. console.log('====================================');
  146. console.log('管理员: admin / admin123');
  147. console.log('销售1: sales001 / 123456 (张三)');
  148. console.log('销售2: sales002 / 123456 (李四)');
  149. console.log('经理: manager001 / 123456 (王经理)');
  150. console.log('====================================\n');
  151. } catch (error) {
  152. console.error('创建测试数据失败:', error);
  153. process.exit(1);
  154. } finally {
  155. connection.release();
  156. await pool.end();
  157. }
  158. }
  159. createTestData();