176 lines
9.8 KiB
TypeScript
176 lines
9.8 KiB
TypeScript
import { Seeder } from 'typeorm-extension';
|
|
import { DataSource } from 'typeorm';
|
|
import { Dict } from '../../entity/dict.entity';
|
|
import { DictItem } from '../../entity/dict_item.entity';
|
|
|
|
export default class DictSeeder implements Seeder {
|
|
/**
|
|
* 格式化名称为 kebab-case
|
|
* @param name 需要格式化的名称
|
|
* @returns 格式化后的名称
|
|
*/
|
|
private formatName(name: string): string {
|
|
// return String(name).replace(/[\_\s.]+/g, '-').toLowerCase();
|
|
// 只替换空格和下划线
|
|
return String(name).replace(/[\_\s]+/g, '-').toLowerCase();
|
|
}
|
|
|
|
public async run(
|
|
dataSource: DataSource,
|
|
): Promise<any> {
|
|
const dictRepository = dataSource.getRepository(Dict);
|
|
const dictItemRepository = dataSource.getRepository(DictItem);
|
|
|
|
const flavorsData = [
|
|
{ name: 'bellini', title: 'Bellini', titleCn: '贝利尼', shortName: 'BL' },
|
|
{ name: 'max-polarmint', title: 'Max Polarmint', titleCn: '马克斯薄荷', shortName: 'MP' },
|
|
{ name: 'blueberry', title: 'Blueberry', titleCn: '蓝莓', shortName: 'BB' },
|
|
{ name: 'citrus', title: 'Citrus', titleCn: '柑橘', shortName: 'CT' },
|
|
{ name: 'wintergreen', title: 'Wintergreen', titleCn: '冬绿薄荷', shortName: 'WG' },
|
|
{ name: 'cool-mint', title: 'COOL MINT', titleCn: '清凉薄荷', shortName: 'CM' },
|
|
{ name: 'juicy-peach', title: 'JUICY PEACH', titleCn: '多汁蜜桃', shortName: 'JP' },
|
|
{ name: 'orange', title: 'ORANGE', titleCn: '橙子', shortName: 'OR' },
|
|
{ name: 'peppermint', title: 'PEPPERMINT', titleCn: '胡椒薄荷', shortName: 'PP' },
|
|
{ name: 'spearmint', title: 'SPEARMINT', titleCn: '绿薄荷', shortName: 'SM' },
|
|
{ name: 'strawberry', title: 'STRAWBERRY', titleCn: '草莓', shortName: 'SB' },
|
|
{ name: 'watermelon', title: 'WATERMELON', titleCn: '西瓜', shortName: 'WM' },
|
|
{ name: 'coffee', title: 'COFFEE', titleCn: '咖啡', shortName: 'CF' },
|
|
{ name: 'lemonade', title: 'LEMONADE', titleCn: '柠檬水', shortName: 'LN' },
|
|
{ name: 'apple-mint', title: 'apple mint', titleCn: '苹果薄荷', shortName: 'AM' },
|
|
{ name: 'peach', title: 'PEACH', titleCn: '桃子', shortName: 'PC' },
|
|
{ name: 'mango', title: 'Mango', titleCn: '芒果', shortName: 'MG' },
|
|
{ name: 'ice-wintergreen', title: 'ICE WINTERGREEN', titleCn: '冰冬绿薄荷', shortName: 'IWG' },
|
|
{ name: 'pink-lemonade', title: 'Pink Lemonade', titleCn: '粉红柠檬水', shortName: 'PLN' },
|
|
{ name: 'blackcherry', title: 'Blackcherry', titleCn: '黑樱桃', shortName: 'BC' },
|
|
{ name: 'fresh-mint', title: 'fresh mint', titleCn: '清新薄荷', shortName: 'FM' },
|
|
{ name: 'strawberry-lychee', title: 'Strawberry Lychee', titleCn: '草莓荔枝', shortName: 'SBL' },
|
|
{ name: 'passion-fruit', title: 'Passion Fruit', titleCn: '百香果', shortName: 'PF' },
|
|
{ name: 'banana-lce', title: 'Banana lce', titleCn: '香蕉冰', shortName: 'BI' },
|
|
{ name: 'bubblegum', title: 'Bubblegum', titleCn: '泡泡糖', shortName: 'BG' },
|
|
{ name: 'mango-lce', title: 'Mango lce', titleCn: '芒果冰', shortName: 'MI' },
|
|
{ name: 'grape-lce', title: 'Grape lce', titleCn: '葡萄冰', shortName: 'GI' },
|
|
{ name: 'apple', title: 'apple', titleCn: '苹果', shortName: 'AP' },
|
|
{ name: 'grape', title: 'grape', titleCn: '葡萄', shortName: 'GR' },
|
|
{ name: 'cherry', title: 'cherry', titleCn: '樱桃', shortName: 'CH' },
|
|
{ name: 'lemon', title: 'lemon', titleCn: '柠檬', shortName: 'LM' },
|
|
{ name: 'razz', title: 'razz', titleCn: '覆盆子', shortName: 'RZ' },
|
|
{ name: 'pineapple', title: 'pineapple', titleCn: '菠萝', shortName: 'PA' },
|
|
{ name: 'berry', title: 'berry', titleCn: '浆果', shortName: 'BR' },
|
|
{ name: 'fruit', title: 'fruit', titleCn: '水果', shortName: 'FR' },
|
|
{ name: 'mint', title: 'mint', titleCn: '薄荷', shortName: 'MT' },
|
|
{ name: 'menthol', title: 'menthol', titleCn: '薄荷醇', shortName: 'MH' },
|
|
];
|
|
|
|
const brandsData = [
|
|
{ name: 'yoone', title: 'Yoone', titleCn: '', shortName: 'YN' },
|
|
{ name: 'white-fox', title: 'White Fox', titleCn: '', shortName: 'WF' },
|
|
{ name: 'zyn', title: 'ZYN', titleCn: '', shortName: 'ZN' },
|
|
{ name: 'zonnic', title: 'Zonnic', titleCn: '', shortName: 'ZC' },
|
|
{ name: 'zolt', title: 'Zolt', titleCn: '', shortName: 'ZT' },
|
|
{ name: 'velo', title: 'Velo', titleCn: '', shortName: 'VL' },
|
|
{ name: 'lucy', title: 'Lucy', titleCn: '', shortName: 'LC' },
|
|
{ name: 'egp', title: 'EGP', titleCn: '', shortName: 'EP' },
|
|
{ name: 'bridge', title: 'Bridge', titleCn: '', shortName: 'BR' },
|
|
{ name: 'zex', title: 'ZEX', titleCn: '', shortName: 'ZX' },
|
|
{ name: 'sesh', title: 'Sesh', titleCn: '', shortName: 'SH' },
|
|
{ name: 'pablo', title: 'Pablo', titleCn: '', shortName: 'PB' },
|
|
];
|
|
|
|
const strengthsData = [
|
|
{ name: '2mg', title: '2MG', titleCn: '2毫克', shortName: '2M' },
|
|
{ name: '3mg', title: '3MG', titleCn: '3毫克', shortName: '3M' },
|
|
{ name: '4mg', title: '4MG', titleCn: '4毫克', shortName: '4M' },
|
|
{ name: '6mg', title: '6MG', titleCn: '6毫克', shortName: '6M' },
|
|
{ name: '6.5mg', title: '6.5MG', titleCn: '6.5毫克', shortName: '6.5M' },
|
|
{ name: '9mg', title: '9MG', titleCn: '9毫克', shortName: '9M' },
|
|
{ name: '12mg', title: '12MG', titleCn: '12毫克', shortName: '12M' },
|
|
{ name: '16.5mg', title: '16.5MG', titleCn: '16.5毫克', shortName: '16.5M' },
|
|
{ name: '18mg', title: '18MG', titleCn: '18毫克', shortName: '18M' },
|
|
{ name: '30mg', title: '30MG', titleCn: '30毫克', shortName: '30M' },
|
|
];
|
|
|
|
// 初始化语言字典
|
|
const locales = [
|
|
{ name: 'zh-cn', title: '简体中文', titleCn: '简体中文', shortName: 'CN' },
|
|
{ name: 'en-us', title: 'English', titleCn: '英文', shortName: 'EN' },
|
|
];
|
|
|
|
for (const locale of locales) {
|
|
await this.createOrFindDict(dictRepository, locale);
|
|
}
|
|
|
|
// 添加示例翻译条目
|
|
const zhDict = await dictRepository.findOne({ where: { name: 'zh-cn' } });
|
|
const enDict = await dictRepository.findOne({ where: { name: 'en-us' } });
|
|
|
|
const translations = [
|
|
{ name: 'common-save', zh: '保存', en: 'Save' },
|
|
{ name: 'common-cancel', zh: '取消', en: 'Cancel' },
|
|
{ name: 'common-success', zh: '操作成功', en: 'Success' },
|
|
{ name: 'common-failure', zh: '操作失败', en: 'Failure' },
|
|
];
|
|
|
|
for (const t of translations) {
|
|
// 添加中文翻译
|
|
let item = await dictItemRepository.findOne({ where: { name: t.name, dict: { id: zhDict.id } } });
|
|
if (!item) {
|
|
await dictItemRepository.save({ name: t.name, title: t.zh, titleCn: t.zh, shortName: t.zh.substring(0, 2).toUpperCase(), dict: zhDict });
|
|
}
|
|
|
|
// 添加英文翻译
|
|
item = await dictItemRepository.findOne({ where: { name: t.name, dict: { id: enDict.id } } });
|
|
if (!item) {
|
|
await dictItemRepository.save({ name: t.name, title: t.en, titleCn: t.en, shortName: t.en.substring(0, 2).toUpperCase(), dict: enDict });
|
|
}
|
|
}
|
|
|
|
const brandDict = await this.createOrFindDict(dictRepository, { name: 'brand', title: '品牌', titleCn: '品牌', shortName: 'BR' });
|
|
const flavorDict = await this.createOrFindDict(dictRepository, { name: 'flavor', title: '口味', titleCn: '口味', shortName: 'FL' });
|
|
const strengthDict = await this.createOrFindDict(dictRepository, { name: 'strength', title: '强度', titleCn: '强度', shortName: 'ST' });
|
|
|
|
// 遍历品牌数据
|
|
await this.seedDictItems(dictItemRepository, brandDict, brandsData);
|
|
|
|
// 遍历口味数据
|
|
await this.seedDictItems(dictItemRepository, flavorDict, flavorsData);
|
|
|
|
// 遍历强度数据
|
|
await this.seedDictItems(dictItemRepository, strengthDict, strengthsData);
|
|
}
|
|
|
|
/**
|
|
* 创建或查找字典
|
|
* @param repo DictRepository
|
|
* @param dictInfo 字典信息
|
|
* @returns Dict 实例
|
|
*/
|
|
private async createOrFindDict(repo: any, dictInfo: { name: string; title: string; titleCn: string; shortName: string }): Promise<Dict> {
|
|
// 格式化 name
|
|
const formattedName = this.formatName(dictInfo.name);
|
|
let dict = await repo.findOne({ where: { name: formattedName } });
|
|
if (!dict) {
|
|
// 如果字典不存在,则使用格式化后的 name 创建新字典
|
|
dict = await repo.save({ name: formattedName, title: dictInfo.title, titleCn: dictInfo.titleCn, shortName: dictInfo.shortName });
|
|
}
|
|
return dict;
|
|
}
|
|
|
|
/**
|
|
* 填充字典项
|
|
* @param repo DictItemRepository
|
|
* @param dict 字典实例
|
|
* @param items 字典项数组
|
|
*/
|
|
private async seedDictItems(repo: any, dict: Dict, items: { name: string; title: string; titleCn: string; shortName: string }[]): Promise<void> {
|
|
for (const item of items) {
|
|
// 格式化 name
|
|
const formattedName = this.formatName(item.name);
|
|
const existingItem = await repo.findOne({ where: { name: formattedName, dict: { id: dict.id } } });
|
|
if (!existingItem) {
|
|
// 如果字典项不存在,则使用格式化后的 name 创建新字典项
|
|
await repo.save({ name: formattedName, title: item.title, titleCn: item.titleCn, shortName: item.shortName, dict });
|
|
}
|
|
}
|
|
}
|
|
}
|