dataGenerator.ts 7.65 KB
import * as fs from 'fs';
import * as path from 'path';

//商品管理-随机商品名
export function generateUniqueProductName(baseName: string = '测试商品'): string {
  // 使用时间戳,精确到毫秒,保证每次都不一样
//   const timestamp = Date.now();
  // 再加一个随机数,防止同一毫秒内多次调用
  const random = Math.floor(Math.random() * 100);
  return `${baseName}_${random}`;
}

//代销入库-随机批次号
export function generateOtherName(baseName: string = '批次别名'): string {
  // 使用时间戳,精确到毫秒,保证每次都不一样
  const timestamp = Date.now();
  // 再加一个随机数,防止同一毫秒内多次调用
//   const random = Math.floor(Math.random() * 100);
  return `${baseName}${timestamp}`;
}

// 随机生成中文客户名称
export function generateCustomerName(): string {
  const familyNames = ['张', '王', '李', '刘', '陈', '杨', '黄', '赵', '周', '吴', '徐', '孙', '马', '朱', '胡', '郭', '何', '林', '罗', '高'];
  const givenNames = ['伟', '芳', '娜', '敏', '静', '丽', '强', '磊', '军', '洋', '勇', '艳', '杰', '涛', '明', '超', '秀英', '华', '平', '刚', '桂英', '文', '志强', '建华', '建国', '建军', '晓明', '小红', '建军', '婷婷', '秀兰', '伟', '芳', '军', '勇', '艳', '杰', '娟', '涛', '明', '超', '秀英', '华'];
  
  const familyName = familyNames[Math.floor(Math.random() * familyNames.length)];
  const givenName = givenNames[Math.floor(Math.random() * givenNames.length)];
  
  return familyName + givenName;
}

// 随机生成身份证号码(18位)
export function generateIdCard(): string {
  // 省份代码
  const provinceCodes = ['11', '12', '13', '14', '15', '21', '22', '23', '31', '32', '33', '34', '35', '36', '37', '41', '42', '43', '44', '45', '46', '50', '51', '52', '53', '54', '61', '62', '63', '64', '65'];
  
  // 随机选择省份
  const provinceCode = provinceCodes[Math.floor(Math.random() * provinceCodes.length)];
  
  // 随机生成城市和区县代码(各2位)
  const cityCode = String(Math.floor(Math.random() * 99) + 1).padStart(2, '0');
  const districtCode = String(Math.floor(Math.random() * 99) + 1).padStart(2, '0');
  
  // 随机生成出生日期(1950-01-01 到 2005-12-31)
  const startYear = 1950;
  const endYear = 2005;
  const year = Math.floor(Math.random() * (endYear - startYear + 1)) + startYear;
  const month = String(Math.floor(Math.random() * 12) + 1).padStart(2, '0');
  const day = String(Math.floor(Math.random() * 28) + 1).padStart(2, '0');
  const birthDate = `${year}${month}${day}`;
  
  // 随机生成顺序码(3位)
  const sequenceCode = String(Math.floor(Math.random() * 999)).padStart(3, '0');
  
  // 前17位
  const id17 = provinceCode + cityCode + districtCode + birthDate + sequenceCode;
  
  // 计算校验码
  const weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  const checkCodes = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
  
  let sum = 0;
  for (let i = 0; i < 17; i++) {
    sum += parseInt(id17[i]) * weights[i];
  }
  const checkCode = checkCodes[sum % 11];
  
  return id17 + checkCode;
}

// 隨机生成手机号码
export function generatePhoneNumber(): string {
  // 中国移动号段
  const mobilePrefixes = ['134', '135', '136', '137', '138', '139', '147', '150', '151', '152', '157', '158', '159', '178', '182', '183', '184', '187', '188'];
  // 中国联通号段
  const unicomPrefixes = ['130', '131', '132', '145', '155', '156', '175', '176', '185', '186'];
  // 中国电信号段
  const telecomPrefixes = ['133', '149', '153', '177', '180', '181', '189'];
  
  const allPrefixes = [...mobilePrefixes, ...unicomPrefixes, ...telecomPrefixes];
  
  const prefix = allPrefixes[Math.floor(Math.random() * allPrefixes.length)];
  const suffix = String(Math.floor(Math.random() * 100000000)).padStart(8, '0');
  
  return prefix + suffix;
}

// 随机生成详细地址(街道、门牌号等,不含省市区)
export function generateDetailedAddress(): string {
  const streets = ['中山路', '人民路', '建设路', '解放路', '和平路', '幸福路', '文化路', '科技路', '商业街', '繁华大道', '朝阳路', '胜利路', '长江路', '黄河路', '振兴路', '发展大道', '创业路', '创新路', '东风路', '西山路'];
  const communities = ['阳光小区', '幸福花园', '和谐家园', '金色年华', '锦绣华庭', '碧水蓝天', '盛世华城', '锦绣江南', '紫荆花园', '翠竹苑', '莲花小区', '牡丹园', '玫瑰园', '百合苑', '茉莉花园'];
  const buildings = ['1栋', '2栋', '3栋', '5栋', '6栋', '8栋', '9栋', '10栋', 'A座', 'B座', 'C座', 'D座'];
  const units = ['1单元', '2单元', '3单元', '一单元', '二单元', '三单元'];
  const floors = ['1楼', '2楼', '3楼', '5楼', '6楼', '8楼', '10楼', '12楼', '15楼', '18楼', '20楼'];
  const rooms = ['101室', '201室', '301室', '401室', '501室', '601室', '701室', '801室', '1001室', '1101室', '1201室'];
  
  const street = streets[Math.floor(Math.random() * streets.length)];
  const community = communities[Math.floor(Math.random() * communities.length)];
  const building = buildings[Math.floor(Math.random() * buildings.length)];
  const unit = units[Math.floor(Math.random() * units.length)];
  const floor = floors[Math.floor(Math.random() * floors.length)];
  const room = rooms[Math.floor(Math.random() * rooms.length)];
  
  // 随机组合地址格式
  const formats = [
    `${street}${Math.floor(Math.random() * 999) + 1}号`,
    `${street}${Math.floor(Math.random() * 999) + 1}号${community}`,
    `${community}${building}${unit}${room}`,
    `${street}${Math.floor(Math.random() * 999) + 1}号${community}${building}${room}`,
    `${street}${Math.floor(Math.random() * 999) + 1}号${floor}${room}`,
    `${community}${building}${unit}${floor}${room}`
  ];
  
  return formats[Math.floor(Math.random() * formats.length)];
}

// 生成完整的客户信息对象
export function generateCustomerInfo(): {
  name: string;
  idCard: string;
  phone: string;
  detailedAddress: string;
} {
  return {
    name: generateCustomerName(),
    idCard: generateIdCard(),
    phone: generatePhoneNumber(),
    detailedAddress: generateDetailedAddress()
  };
}

/**
 * 从指定目录随机选择一张图片
 * @param imageDir 图片目录路径,默认为 'test-data/img'
 * @returns 随机选择的图片完整路径,如果目录不存在或没有图片则返回 null
 */
export function getRandomImage(imageDir: string = 'test-data/img'): string | null {
  try {
    // 获取项目根目录
    const projectRoot = process.cwd();
    const fullImagePath = path.join(projectRoot, imageDir);
    
    // 检查目录是否存在
    if (!fs.existsSync(fullImagePath)) {
      console.warn(`图片目录不存在: ${fullImagePath}`);
      return null;
    }
    
    // 读取目录中的所有文件
    const files = fs.readdirSync(fullImagePath);
    
    // 过滤出图片文件(支持常见图片格式)
    const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp'];
    const imageFiles = files.filter(file => {
      const ext = path.extname(file).toLowerCase();
      return imageExtensions.includes(ext);
    });
    
    if (imageFiles.length === 0) {
      console.warn(`图片目录中没有图片文件: ${fullImagePath}`);
      return null;
    }
    
    // 随机选择一张图片
    const randomIndex = Math.floor(Math.random() * imageFiles.length);
    const selectedImage = imageFiles[randomIndex];
    
    // 返回相对于项目根目录的路径
    return path.join(imageDir, selectedImage);
  } catch (error) {
    console.error('获取随机图片失败:', error);
    return null;
  }
}