ai-engineering-init 1.16.3 → 1.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/commands/init-config.md +154 -0
- package/.claude/skills/{leniu-java-amount-handling/SKILL.md → leniu-report-scenario/references/amount-handling.md} +0 -13
- package/.claude/skills/{leniu-java-export/SKILL.md → leniu-report-scenario/references/export.md} +0 -17
- package/{.cursor/skills/leniu-mealtime/SKILL.md → .claude/skills/leniu-report-scenario/references/mealtime.md} +0 -18
- package/{.codex/skills/leniu-java-report-query-param/SKILL.md → .claude/skills/leniu-report-scenario/references/query-param.md} +0 -17
- package/.claude/skills/leniu-report-scenario/references/standard-customization.md +112 -0
- package/{.codex/skills/leniu-java-total-line/SKILL.md → .claude/skills/leniu-report-scenario/references/total-line.md} +0 -17
- package/.claude/templates/env-config.md +27 -0
- package/.codex/skills/leniu-report-scenario/references/amount-handling.md +448 -0
- package/.codex/skills/{leniu-java-export/SKILL.md → leniu-report-scenario/references/export.md} +0 -17
- package/{.claude/skills/leniu-mealtime/SKILL.md → .codex/skills/leniu-report-scenario/references/mealtime.md} +0 -18
- package/{.cursor/skills/leniu-java-report-query-param/SKILL.md → .codex/skills/leniu-report-scenario/references/query-param.md} +0 -17
- package/.codex/skills/leniu-report-scenario/references/standard-customization.md +112 -0
- package/{.cursor/skills/leniu-java-total-line/SKILL.md → .codex/skills/leniu-report-scenario/references/total-line.md} +0 -17
- package/.cursor/skills/leniu-report-scenario/references/amount-handling.md +448 -0
- package/.cursor/skills/{leniu-java-export/SKILL.md → leniu-report-scenario/references/export.md} +0 -17
- package/{.codex/skills/leniu-mealtime/SKILL.md → .cursor/skills/leniu-report-scenario/references/mealtime.md} +0 -18
- package/{.claude/skills/leniu-java-report-query-param/SKILL.md → .cursor/skills/leniu-report-scenario/references/query-param.md} +0 -17
- package/.cursor/skills/leniu-report-scenario/references/standard-customization.md +112 -0
- package/{.claude/skills/leniu-java-total-line/SKILL.md → .cursor/skills/leniu-report-scenario/references/total-line.md} +0 -17
- package/.cursor/templates/env-config.md +27 -0
- package/bin/index.js +235 -1
- package/package.json +1 -1
- package/.claude/skills/leniu-marketing-price-rule-customizer/SKILL.md +0 -301
- package/.claude/skills/leniu-marketing-recharge-rule-customizer/SKILL.md +0 -285
- package/.claude/skills/leniu-report-customization/SKILL.md +0 -415
- package/.claude/skills/leniu-report-standard-customization/SKILL.md +0 -391
- package/.codex/skills/leniu-marketing-price-rule-customizer/SKILL.md +0 -301
- package/.codex/skills/leniu-marketing-recharge-rule-customizer/SKILL.md +0 -285
- package/.codex/skills/leniu-report-customization/SKILL.md +0 -415
- package/.codex/skills/leniu-report-standard-customization/SKILL.md +0 -391
- package/.cursor/skills/leniu-marketing-price-rule-customizer/SKILL.md +0 -301
- package/.cursor/skills/leniu-marketing-recharge-rule-customizer/SKILL.md +0 -285
- package/.cursor/skills/leniu-report-customization/SKILL.md +0 -415
- package/.cursor/skills/leniu-report-standard-customization/SKILL.md +0 -391
- /package/.claude/skills/{leniu-report-standard-customization → leniu-report-scenario}/references/analysis-module.md +0 -0
- /package/.claude/skills/{leniu-report-customization/references/table-fields.md → leniu-report-scenario/references/customization-table-fields.md} +0 -0
- /package/.claude/skills/{leniu-report-standard-customization/references/table-fields.md → leniu-report-scenario/references/standard-table-fields.md} +0 -0
- /package/.codex/skills/{leniu-report-standard-customization → leniu-report-scenario}/references/analysis-module.md +0 -0
- /package/.codex/skills/{leniu-report-customization/references/table-fields.md → leniu-report-scenario/references/customization-table-fields.md} +0 -0
- /package/.codex/skills/{leniu-report-standard-customization/references/table-fields.md → leniu-report-scenario/references/standard-table-fields.md} +0 -0
- /package/.cursor/skills/{leniu-report-standard-customization → leniu-report-scenario}/references/analysis-module.md +0 -0
- /package/.cursor/skills/{leniu-report-customization/references/table-fields.md → leniu-report-scenario/references/customization-table-fields.md} +0 -0
- /package/.cursor/skills/{leniu-report-standard-customization/references/table-fields.md → leniu-report-scenario/references/standard-table-fields.md} +0 -0
package/bin/index.js
CHANGED
|
@@ -55,6 +55,7 @@ let submitIssue = false; // sync-back --submit
|
|
|
55
55
|
let configType = ''; // config --type <mysql|loki|all>
|
|
56
56
|
let configScope = ''; // config --scope <local|global>
|
|
57
57
|
let configAdd = false; // config --add
|
|
58
|
+
let configFrom = ''; // config --from <file.md>
|
|
58
59
|
|
|
59
60
|
for (let i = 0; i < args.length; i++) {
|
|
60
61
|
const arg = args[i];
|
|
@@ -121,6 +122,13 @@ for (let i = 0; i < args.length; i++) {
|
|
|
121
122
|
case '--add':
|
|
122
123
|
configAdd = true;
|
|
123
124
|
break;
|
|
125
|
+
case '--from':
|
|
126
|
+
if (i + 1 >= args.length || args[i + 1].startsWith('-')) {
|
|
127
|
+
console.error(fmt('red', `错误:${arg} 需要一个文件路径`));
|
|
128
|
+
process.exit(1);
|
|
129
|
+
}
|
|
130
|
+
configFrom = path.resolve(args[++i]);
|
|
131
|
+
break;
|
|
124
132
|
case '--help': case '-h':
|
|
125
133
|
printHelp();
|
|
126
134
|
process.exit(0);
|
|
@@ -154,6 +162,7 @@ function printHelp() {
|
|
|
154
162
|
console.log(' --type <类型> config 时指定配置类型: mysql | loki | all');
|
|
155
163
|
console.log(' --scope <范围> config 时指定范围: local(当前项目) | global(全局 ~/)');
|
|
156
164
|
console.log(' --add config 时追加环境(不覆盖已有配置)');
|
|
165
|
+
console.log(' --from <文件> config 时从 Markdown 文件解析配置(跳过交互)');
|
|
157
166
|
console.log(' --help, -h 显示此帮助\n');
|
|
158
167
|
console.log('示例:');
|
|
159
168
|
console.log(' npx ai-engineering-init --tool claude');
|
|
@@ -173,6 +182,7 @@ function printHelp() {
|
|
|
173
182
|
console.log(' npx ai-engineering-init config --type all # 配置全部');
|
|
174
183
|
console.log(' npx ai-engineering-init config --type mysql --scope global # 全局配置(所有项目共享)');
|
|
175
184
|
console.log(' npx ai-engineering-init config --type mysql --add # 追加环境到已有配置');
|
|
185
|
+
console.log(' npx ai-engineering-init config --from env-config.md --scope global # 从 MD 文件一键初始化');
|
|
176
186
|
}
|
|
177
187
|
|
|
178
188
|
// ── 工具定义(init 用)────────────────────────────────────────────────────
|
|
@@ -1151,8 +1161,14 @@ function runSyncBack(selectedTool, selectedSkill, doSubmit) {
|
|
|
1151
1161
|
// ── 环境配置初始化(MySQL / Loki)─────────────────────────────────────────
|
|
1152
1162
|
|
|
1153
1163
|
function runConfig() {
|
|
1164
|
+
// --from 模式:从 MD 文件解析配置(非交互式)
|
|
1165
|
+
if (configFrom) {
|
|
1166
|
+
runConfigFromFile(configFrom, configScope || 'global');
|
|
1167
|
+
return;
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1154
1170
|
if (!process.stdin.isTTY) {
|
|
1155
|
-
console.error(fmt('red', '错误:config
|
|
1171
|
+
console.error(fmt('red', '错误:config 命令需要交互式终端(或使用 --from <file> 跳过交互)'));
|
|
1156
1172
|
process.exit(1);
|
|
1157
1173
|
}
|
|
1158
1174
|
|
|
@@ -1571,6 +1587,224 @@ function writeLokiConfig(config, configPath, isGlobal) {
|
|
|
1571
1587
|
console.log(fmt('green', 'Loki 日志查询配置完成!'));
|
|
1572
1588
|
}
|
|
1573
1589
|
|
|
1590
|
+
// ── 从 Markdown 文件解析配置(非交互式)──────────────────────────────────────
|
|
1591
|
+
|
|
1592
|
+
function runConfigFromFile(filePath, scope) {
|
|
1593
|
+
if (!fs.existsSync(filePath)) {
|
|
1594
|
+
console.error(fmt('red', `错误:文件不存在 "${filePath}"`));
|
|
1595
|
+
process.exit(1);
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
const content = fs.readFileSync(filePath, 'utf-8');
|
|
1599
|
+
const isGlobal = scope === 'global';
|
|
1600
|
+
|
|
1601
|
+
console.log(fmt('blue', fmt('bold', `从 Markdown 文件解析配置:${filePath}`)));
|
|
1602
|
+
console.log(fmt('magenta', `配置范围:${isGlobal ? '全局(~/.claude/)' : '本地(当前项目)'}`));
|
|
1603
|
+
console.log('');
|
|
1604
|
+
|
|
1605
|
+
// 解析 MySQL 表格
|
|
1606
|
+
const mysqlConfig = parseMysqlFromMd(content);
|
|
1607
|
+
if (mysqlConfig) {
|
|
1608
|
+
const mysqlPath = isGlobal
|
|
1609
|
+
? path.join(HOME_DIR, '.claude', 'mysql-config.json')
|
|
1610
|
+
: path.join(targetDir, '.claude', 'mysql-config.json');
|
|
1611
|
+
|
|
1612
|
+
const configJson = JSON.stringify(mysqlConfig, null, 2) + '\n';
|
|
1613
|
+
const dir = path.dirname(mysqlPath);
|
|
1614
|
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
1615
|
+
fs.writeFileSync(mysqlPath, configJson, 'utf-8');
|
|
1616
|
+
console.log(` ${fmt('green', '✔')} MySQL 配置(${Object.keys(mysqlConfig.environments).length} 个环境)→ ${mysqlPath}`);
|
|
1617
|
+
|
|
1618
|
+
// 全局同步到 cursor
|
|
1619
|
+
if (isGlobal && fs.existsSync(path.join(HOME_DIR, '.cursor'))) {
|
|
1620
|
+
const cursorPath = path.join(HOME_DIR, '.cursor', 'mysql-config.json');
|
|
1621
|
+
fs.writeFileSync(cursorPath, configJson, 'utf-8');
|
|
1622
|
+
console.log(` ${fmt('green', '✔')} 已同步 → ${cursorPath}`);
|
|
1623
|
+
}
|
|
1624
|
+
}
|
|
1625
|
+
|
|
1626
|
+
// 解析 Loki 表格
|
|
1627
|
+
const lokiConfig = parseLokiFromMd(content);
|
|
1628
|
+
if (lokiConfig) {
|
|
1629
|
+
const lokiPath = isGlobal
|
|
1630
|
+
? path.join(HOME_DIR, '.claude', 'loki-config.json')
|
|
1631
|
+
: path.join(targetDir, '.claude', 'loki-config.json');
|
|
1632
|
+
|
|
1633
|
+
const configJson = JSON.stringify(lokiConfig, null, 2) + '\n';
|
|
1634
|
+
const dir = path.dirname(lokiPath);
|
|
1635
|
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
1636
|
+
fs.writeFileSync(lokiPath, configJson, 'utf-8');
|
|
1637
|
+
console.log(` ${fmt('green', '✔')} Loki 配置(${Object.keys(lokiConfig.environments).length} 个环境)→ ${lokiPath}`);
|
|
1638
|
+
|
|
1639
|
+
if (isGlobal && fs.existsSync(path.join(HOME_DIR, '.cursor'))) {
|
|
1640
|
+
const cursorPath = path.join(HOME_DIR, '.cursor', 'loki-config.json');
|
|
1641
|
+
fs.writeFileSync(cursorPath, configJson, 'utf-8');
|
|
1642
|
+
console.log(` ${fmt('green', '✔')} 已同步 → ${cursorPath}`);
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
|
|
1646
|
+
if (!mysqlConfig && !lokiConfig) {
|
|
1647
|
+
console.error(fmt('red', '未在文件中找到 MySQL 或 Loki 配置表格。'));
|
|
1648
|
+
console.log('');
|
|
1649
|
+
console.log('期望格式(MySQL):');
|
|
1650
|
+
console.log(' | 环境 | host | port | user | password | range | 描述 |');
|
|
1651
|
+
console.log('');
|
|
1652
|
+
console.log('期望格式(Loki):');
|
|
1653
|
+
console.log(' | 环境 | 名称 | URL | Token | 别名 | range |');
|
|
1654
|
+
process.exit(1);
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
if (!isGlobal) ensureGitignore(['mysql-config.json', 'loki-config.json']);
|
|
1658
|
+
|
|
1659
|
+
console.log('');
|
|
1660
|
+
console.log(fmt('green', fmt('bold', '配置初始化完成!')));
|
|
1661
|
+
if (isGlobal) {
|
|
1662
|
+
console.log(fmt('cyan', '技能按 本地(.claude/) → 全局(~/.claude/) 顺序查找,本地优先。'));
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1666
|
+
/** 从 Markdown 内容中解析 MySQL 配置表格 */
|
|
1667
|
+
function parseMysqlFromMd(content) {
|
|
1668
|
+
// 找到 "MySQL" 标题后的表格
|
|
1669
|
+
const mysqlSection = extractSection(content, /mysql|数据库/i);
|
|
1670
|
+
if (!mysqlSection) return null;
|
|
1671
|
+
|
|
1672
|
+
const rows = parseTable(mysqlSection);
|
|
1673
|
+
if (rows.length === 0) return null;
|
|
1674
|
+
|
|
1675
|
+
const environments = {};
|
|
1676
|
+
for (const row of rows) {
|
|
1677
|
+
const env = row['环境'] || row['env'] || '';
|
|
1678
|
+
if (!env) continue;
|
|
1679
|
+
|
|
1680
|
+
const host = row['host'] || '';
|
|
1681
|
+
const port = parseInt(row['port'] || '3306', 10);
|
|
1682
|
+
const user = row['user'] || '';
|
|
1683
|
+
const password = row['password'] || '';
|
|
1684
|
+
const range = row['range'] || '';
|
|
1685
|
+
const desc = row['描述'] || row['desc'] || `${env}环境`;
|
|
1686
|
+
|
|
1687
|
+
if (!host || host.startsWith('YOUR_')) continue; // 跳过未填写的占位符
|
|
1688
|
+
|
|
1689
|
+
environments[env] = { host, port, user, password, _desc: desc };
|
|
1690
|
+
if (range) environments[env].range = range;
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
if (Object.keys(environments).length === 0) return null;
|
|
1694
|
+
|
|
1695
|
+
// 提取默认环境
|
|
1696
|
+
const defaultMatch = mysqlSection.match(/默认环境[::]\s*(\S+)/);
|
|
1697
|
+
const defaultEnv = defaultMatch ? defaultMatch[1] : Object.keys(environments)[0];
|
|
1698
|
+
|
|
1699
|
+
return {
|
|
1700
|
+
environments,
|
|
1701
|
+
default: defaultEnv,
|
|
1702
|
+
_comment: '从 Markdown 文件解析生成。支持 range 字段,查找顺序:本地 > 全局 ~/.claude/',
|
|
1703
|
+
};
|
|
1704
|
+
}
|
|
1705
|
+
|
|
1706
|
+
/** 从 Markdown 内容中解析 Loki 配置表格 */
|
|
1707
|
+
function parseLokiFromMd(content) {
|
|
1708
|
+
const lokiSection = extractSection(content, /loki|日志/i);
|
|
1709
|
+
if (!lokiSection) return null;
|
|
1710
|
+
|
|
1711
|
+
const rows = parseTable(lokiSection);
|
|
1712
|
+
if (rows.length === 0) return null;
|
|
1713
|
+
|
|
1714
|
+
const environments = {};
|
|
1715
|
+
for (const row of rows) {
|
|
1716
|
+
const env = row['环境'] || row['env'] || '';
|
|
1717
|
+
if (!env) continue;
|
|
1718
|
+
|
|
1719
|
+
const name = row['名称'] || row['name'] || env;
|
|
1720
|
+
const url = row['url'] || row['URL'] || '';
|
|
1721
|
+
const token = row['token'] || row['Token'] || '';
|
|
1722
|
+
const aliasStr = row['别名'] || row['aliases'] || env;
|
|
1723
|
+
const aliases = aliasStr.split(',').map(s => s.trim()).filter(Boolean);
|
|
1724
|
+
const rangeStr = row['range'] || '';
|
|
1725
|
+
|
|
1726
|
+
if (!url || url.startsWith('YOUR_')) continue;
|
|
1727
|
+
|
|
1728
|
+
const envData = { name, url, token: token.startsWith('YOUR_') ? '' : token, aliases };
|
|
1729
|
+
if (rangeStr) {
|
|
1730
|
+
envData.range = rangeStr;
|
|
1731
|
+
envData.projects = expandRange(rangeStr);
|
|
1732
|
+
} else {
|
|
1733
|
+
envData.projects = [];
|
|
1734
|
+
}
|
|
1735
|
+
|
|
1736
|
+
environments[env] = envData;
|
|
1737
|
+
}
|
|
1738
|
+
|
|
1739
|
+
if (Object.keys(environments).length === 0) return null;
|
|
1740
|
+
|
|
1741
|
+
const defaultMatch = lokiSection.match(/默认环境[::]\s*(\S+)/);
|
|
1742
|
+
const activeEnv = defaultMatch ? defaultMatch[1] : Object.keys(environments)[0];
|
|
1743
|
+
|
|
1744
|
+
return {
|
|
1745
|
+
_comment: '从 Markdown 文件解析生成。支持 range 字段,查找顺序:本地 > 全局 ~/.claude/',
|
|
1746
|
+
_setup: 'Token:Grafana → Administration → Service accounts → Add(Viewer)→ Add token',
|
|
1747
|
+
active: activeEnv,
|
|
1748
|
+
environments,
|
|
1749
|
+
};
|
|
1750
|
+
}
|
|
1751
|
+
|
|
1752
|
+
/** 从 Markdown 中按标题提取段落 */
|
|
1753
|
+
function extractSection(content, titlePattern) {
|
|
1754
|
+
const lines = content.split('\n');
|
|
1755
|
+
let start = -1;
|
|
1756
|
+
let end = lines.length;
|
|
1757
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1758
|
+
const line = lines[i];
|
|
1759
|
+
if (line.match(/^#+\s/) && titlePattern.test(line)) {
|
|
1760
|
+
start = i;
|
|
1761
|
+
const level = line.match(/^(#+)/)[1].length;
|
|
1762
|
+
// 找到同级或更高级标题作为结束
|
|
1763
|
+
for (let j = i + 1; j < lines.length; j++) {
|
|
1764
|
+
const nextMatch = lines[j].match(/^(#+)\s/);
|
|
1765
|
+
if (nextMatch && nextMatch[1].length <= level) {
|
|
1766
|
+
end = j;
|
|
1767
|
+
break;
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
break;
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
if (start === -1) return null;
|
|
1774
|
+
return lines.slice(start, end).join('\n');
|
|
1775
|
+
}
|
|
1776
|
+
|
|
1777
|
+
/** 解析 Markdown 表格为对象数组 */
|
|
1778
|
+
function parseTable(text) {
|
|
1779
|
+
const lines = text.split('\n');
|
|
1780
|
+
let headerLine = -1;
|
|
1781
|
+
|
|
1782
|
+
// 找到表头行(含 | 的行,下一行是分隔线 |---|)
|
|
1783
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
1784
|
+
if (lines[i].includes('|') && lines[i + 1] && lines[i + 1].match(/^\|[\s-:|]+\|$/)) {
|
|
1785
|
+
headerLine = i;
|
|
1786
|
+
break;
|
|
1787
|
+
}
|
|
1788
|
+
}
|
|
1789
|
+
if (headerLine === -1) return [];
|
|
1790
|
+
|
|
1791
|
+
const parseRow = (line) => line.split('|').map(s => s.trim()).filter(Boolean);
|
|
1792
|
+
const headers = parseRow(lines[headerLine]);
|
|
1793
|
+
const rows = [];
|
|
1794
|
+
|
|
1795
|
+
for (let i = headerLine + 2; i < lines.length; i++) {
|
|
1796
|
+
const line = lines[i];
|
|
1797
|
+
if (!line.includes('|')) break;
|
|
1798
|
+
const cells = parseRow(line);
|
|
1799
|
+
if (cells.length === 0) break;
|
|
1800
|
+
const row = {};
|
|
1801
|
+
headers.forEach((h, idx) => { row[h] = cells[idx] || ''; });
|
|
1802
|
+
rows.push(row);
|
|
1803
|
+
}
|
|
1804
|
+
|
|
1805
|
+
return rows;
|
|
1806
|
+
}
|
|
1807
|
+
|
|
1574
1808
|
// ── Config 工具函数 ─────────────────────────────────────────────────────────
|
|
1575
1809
|
|
|
1576
1810
|
function getLokiConfigPath() {
|
package/package.json
CHANGED
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: leniu-marketing-price-rule-customizer
|
|
3
|
-
description: |
|
|
4
|
-
leniu-tengyun-core 项目营销计费(price)规则定制指南。当需要定制营销计费规则时使用,支持新增规则类型、重写规则逻辑、扩展 DTO 字段。
|
|
5
|
-
|
|
6
|
-
触发场景:
|
|
7
|
-
- 新增营销计费规则类型(折扣、满减、限额、补贴等)
|
|
8
|
-
- 重写现有计费规则逻辑(@Primary 模式)
|
|
9
|
-
- 扩展规则 DTO 字段(向后兼容)
|
|
10
|
-
- 定制规则计算行为(handle 方法实现)
|
|
11
|
-
- 注册规则枚举(RulePriceEnum)
|
|
12
|
-
|
|
13
|
-
适用项目:
|
|
14
|
-
- leniu-tengyun-core:/Users/xujiajun/Developer/gongsi_proj/leniu-api/leniu-tengyun-core
|
|
15
|
-
- leniu-yunshitang:/Users/xujiajun/Developer/gongsi_proj/leniu-api/leniu-tengyun/leniu-yunshitang
|
|
16
|
-
|
|
17
|
-
触发词:营销计费、计价规则、RulePriceHandler、RulePriceEnum、折扣规则、满减规则、限额规则、补贴规则
|
|
18
|
-
---
|
|
19
|
-
|
|
20
|
-
# leniu-tengyun-core 营销计费规则定制
|
|
21
|
-
|
|
22
|
-
## 概述
|
|
23
|
-
|
|
24
|
-
leniu-tengyun-core 项目的营销计费(price)规则功能采用扩展点设计,支持灵活的规则定制。
|
|
25
|
-
|
|
26
|
-
营销计费规则是营销系统的核心组件,负责计算订单的优惠金额、限制消费行为、提供补贴等功能。
|
|
27
|
-
|
|
28
|
-
## 何时使用此 Skill
|
|
29
|
-
|
|
30
|
-
- 需要新增一个计价规则类型
|
|
31
|
-
- 需要修改现有规则的计算逻辑
|
|
32
|
-
- 需要为规则添加新的配置字段
|
|
33
|
-
- 需要针对特定项目定制规则行为
|
|
34
|
-
- 参考现有规则实现新的定制需求
|
|
35
|
-
|
|
36
|
-
## 规则定制工作流
|
|
37
|
-
|
|
38
|
-
### 步骤1:确定定制模式
|
|
39
|
-
|
|
40
|
-
根据需求选择合适的定制模式:
|
|
41
|
-
|
|
42
|
-
**模式A:新增规则**
|
|
43
|
-
- 适用场景:创建全新的规则类型
|
|
44
|
-
- 需要创建:DTO、扩展接口、默认实现
|
|
45
|
-
|
|
46
|
-
**模式B:重写规则(@Primary)**
|
|
47
|
-
- 适用场景:完全替换现有规则行为
|
|
48
|
-
- 需要创建:定制实现类(使用@Primary注解)
|
|
49
|
-
- 可选:扩展DTO字段
|
|
50
|
-
|
|
51
|
-
**模式C:重写规则(Ordered)**
|
|
52
|
-
- 适用场景:多个实现共存
|
|
53
|
-
- 需要创建:定制实现类(实现Ordered接口)
|
|
54
|
-
|
|
55
|
-
### 步骤2:理解规则结构
|
|
56
|
-
|
|
57
|
-
了解:
|
|
58
|
-
- 规则接口层次(RulePriceHandler → Extension → Implementation)
|
|
59
|
-
- 规则DTO结构
|
|
60
|
-
- 规则计算入参(RulePriceResultOrderDTO、MarketRuleVO、orderResults)
|
|
61
|
-
- 规则计算流程
|
|
62
|
-
- 常用工具类(MarketUtil、MarketRuleRangeService)
|
|
63
|
-
|
|
64
|
-
### 步骤3:参考实际案例
|
|
65
|
-
|
|
66
|
-
查看实际案例,了解如何实现具体的定制需求:
|
|
67
|
-
- **案例1**:支持自定义场景类型选择(@Primary + DTO扩展)
|
|
68
|
-
- **案例2**:移除订单类型过滤(直接修改)
|
|
69
|
-
|
|
70
|
-
### 步骤4:实现规则定制
|
|
71
|
-
|
|
72
|
-
#### 新增规则的实现步骤
|
|
73
|
-
|
|
74
|
-
1. **创建规则DTO**
|
|
75
|
-
- 位置:`net.xnzn.core.marketing.v2.rule.price.handler.[ruletype].dto`
|
|
76
|
-
- 包含规则配置字段
|
|
77
|
-
- 实现 `toString()` 方法返回可读描述
|
|
78
|
-
|
|
79
|
-
2. **创建扩展接口**
|
|
80
|
-
- 位置:`net.xnzn.core.marketing.v2.rule.price.handler.[ruletype].extension`
|
|
81
|
-
- 继承 `RulePriceHandler`
|
|
82
|
-
- 实现 `getRuleType()` 和 `checkRuleInfo()` 方法
|
|
83
|
-
|
|
84
|
-
3. **创建默认实现**
|
|
85
|
-
- 位置:`net.xnzn.core.marketing.v2.rule.price.handler.[ruletype].extension.impl`
|
|
86
|
-
- 实现扩展接口
|
|
87
|
-
- 添加 `@Service` 注解
|
|
88
|
-
- 实现 `handle()` 方法
|
|
89
|
-
|
|
90
|
-
4. **注册规则枚举**
|
|
91
|
-
- 在 `RulePriceEnum` 中添加新的规则类型
|
|
92
|
-
|
|
93
|
-
#### 重写规则的实现步骤(@Primary模式)
|
|
94
|
-
|
|
95
|
-
1. **(可选)扩展DTO字段**
|
|
96
|
-
- 在定制项目中覆盖核心DTO类
|
|
97
|
-
- 添加新字段并保持向后兼容
|
|
98
|
-
|
|
99
|
-
2. **创建定制实现**
|
|
100
|
-
- 位置:定制项目包(如 `net.xnzn.yunshitang.marketing.handler`)
|
|
101
|
-
- 继承默认实现类(可选,用于复用逻辑)
|
|
102
|
-
- 实现扩展接口
|
|
103
|
-
- 添加 `@Service` 和 `@Primary` 注解
|
|
104
|
-
- 重写 `handle()` 方法
|
|
105
|
-
|
|
106
|
-
3. **实现定制逻辑**
|
|
107
|
-
- 解析规则配置(包含新字段)
|
|
108
|
-
- 实现自定义计算逻辑
|
|
109
|
-
- 更新订单优惠金额或抛出限制异常
|
|
110
|
-
|
|
111
|
-
### 步骤5:测试规则
|
|
112
|
-
|
|
113
|
-
1. 测试向后兼容性(新字段为null的场景)
|
|
114
|
-
2. 测试新功能(新字段有值的场景)
|
|
115
|
-
3. 测试边界条件
|
|
116
|
-
4. 测试与其他规则的组合使用
|
|
117
|
-
|
|
118
|
-
## 代码模板
|
|
119
|
-
|
|
120
|
-
### 新增规则模板
|
|
121
|
-
|
|
122
|
-
```java
|
|
123
|
-
// 1. DTO
|
|
124
|
-
@Data
|
|
125
|
-
@ApiModel("计价规则详情-[规则名称]")
|
|
126
|
-
public class [RuleType]DTO {
|
|
127
|
-
@ApiModelProperty("[字段说明]")
|
|
128
|
-
private [Type] fieldName;
|
|
129
|
-
|
|
130
|
-
@Override
|
|
131
|
-
public String toString() {
|
|
132
|
-
return "字段:" + fieldName;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// 2. 扩展接口
|
|
137
|
-
public interface [RuleType]HandlerExtension extends RulePriceHandler {
|
|
138
|
-
@Override
|
|
139
|
-
default Integer getRuleType() {
|
|
140
|
-
return RulePriceEnum.[RULE_TYPE_ENUM].getKey();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
@Override
|
|
144
|
-
default String checkRuleInfo(String ruleInfo) {
|
|
145
|
-
[RuleType]DTO rule = JSON.parseObject(ruleInfo, [RuleType]DTO.class);
|
|
146
|
-
return rule.toString();
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// 3. 默认实现
|
|
151
|
-
@Slf4j
|
|
152
|
-
@Service
|
|
153
|
-
public class Default[RuleType]HandlerImpl implements [RuleType]HandlerExtension {
|
|
154
|
-
|
|
155
|
-
@Autowired
|
|
156
|
-
private MarketRuleRangeService rangeService;
|
|
157
|
-
|
|
158
|
-
@Override
|
|
159
|
-
public void handle(RulePriceResultOrderDTO order, MarketRuleVO rule,
|
|
160
|
-
List<RulePriceResultOrderDTO> orderResults) {
|
|
161
|
-
// 1. 解析规则配置
|
|
162
|
-
[RuleType]DTO ruleInfo = JSON.parseObject(rule.getRuleInfo(), [RuleType]DTO.class);
|
|
163
|
-
|
|
164
|
-
// 2. 查询规则适用范围
|
|
165
|
-
List<MarketRuleRange> rangeList = rangeService.listRuleRangeLatest(rule.getRuleId());
|
|
166
|
-
|
|
167
|
-
// 3. 实现规则计算逻辑
|
|
168
|
-
// ...
|
|
169
|
-
|
|
170
|
-
// 4. 更新订单优惠金额或抛出异常
|
|
171
|
-
order.setDiscountsAmount(order.getDiscountsAmount().add(discountAmount));
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
### 重写规则模板(@Primary)
|
|
177
|
-
|
|
178
|
-
```java
|
|
179
|
-
@Slf4j
|
|
180
|
-
@Service
|
|
181
|
-
@Primary // 标记为主要实现
|
|
182
|
-
public class Custom[RuleType]HandlerImpl extends Default[RuleType]HandlerImpl
|
|
183
|
-
implements [RuleType]HandlerExtension {
|
|
184
|
-
|
|
185
|
-
@Autowired
|
|
186
|
-
private MarketRuleRangeService rangeService;
|
|
187
|
-
|
|
188
|
-
@Override
|
|
189
|
-
public void handle(RulePriceResultOrderDTO order, MarketRuleVO rule,
|
|
190
|
-
List<RulePriceResultOrderDTO> orderResults) {
|
|
191
|
-
// 解析规则配置(可能包含扩展字段)
|
|
192
|
-
[RuleType]DTO ruleInfo = JSON.parseObject(rule.getRuleInfo(), [RuleType]DTO.class);
|
|
193
|
-
|
|
194
|
-
// 实现定制逻辑
|
|
195
|
-
// 可以调用父类方法:super.handle(order, rule, orderResults);
|
|
196
|
-
// 或完全自定义实现
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
## 常见规则类型
|
|
202
|
-
|
|
203
|
-
### 优惠类规则
|
|
204
|
-
- 折扣规则:`PriceDiscountHandlerExtension`
|
|
205
|
-
- 菜品折扣:`PriceDishDiscountHandlerExtension`
|
|
206
|
-
- 商品折扣:`PriceGoodsDiscountHandlerExtension`
|
|
207
|
-
- 满减规则:`PriceReductionFixedHandlerExtension`
|
|
208
|
-
|
|
209
|
-
### 限制类规则
|
|
210
|
-
- 限额-单次金额:`PriceLimitMaxAmountHandlerExtension`
|
|
211
|
-
- 限额-累计次数:`PriceLimitMaxCountHandlerExtension`
|
|
212
|
-
- 限额-累计金额:`PriceLimitSumAmountHandlerExtension`
|
|
213
|
-
|
|
214
|
-
### 补贴类规则
|
|
215
|
-
- 固定补贴:`PriceSubsidyFixedHandlerExtension`
|
|
216
|
-
- 累计赠送:`PriceSumGiveHandlerExtension`
|
|
217
|
-
|
|
218
|
-
## 最佳实践
|
|
219
|
-
|
|
220
|
-
### 包名规范
|
|
221
|
-
- 覆盖核心类:使用核心工程的包名(`net.xnzn.core.marketing.v2.rule.price.handler.[ruletype]`)
|
|
222
|
-
- 定制实现:使用项目特定包名(如 `net.xnzn.yunshitang.marketing.handler`)
|
|
223
|
-
|
|
224
|
-
### 类名规范
|
|
225
|
-
- 扩展接口:`[RuleType]HandlerExtension`
|
|
226
|
-
- 默认实现:`Default[RuleType]HandlerImpl`
|
|
227
|
-
- 定制实现:`Custom[RuleType]HandlerImpl` 或描述性名称
|
|
228
|
-
|
|
229
|
-
### 注解使用
|
|
230
|
-
- 所有实现类必须添加 `@Service` 注解
|
|
231
|
-
- 重写规则使用 `@Primary` 注解(推荐)
|
|
232
|
-
- 日志记录使用 `@Slf4j` 注解
|
|
233
|
-
|
|
234
|
-
### 向后兼容
|
|
235
|
-
- 扩展DTO字段时,新字段应支持null值
|
|
236
|
-
- 新字段为null时应保持原有行为
|
|
237
|
-
- 在toString方法中包含所有字段
|
|
238
|
-
|
|
239
|
-
## 快速开始示例
|
|
240
|
-
|
|
241
|
-
假设需要为限额累计金额规则(`PriceLimitSumAmountHandlerExtension`)添加场景类型选择功能:
|
|
242
|
-
|
|
243
|
-
1. **扩展DTO**:在 `PriceLimitSumAmountDTO` 中添加 `sceneTypes` 字段
|
|
244
|
-
2. **创建定制实现**:创建 `CrossScenePriceLimitSumAmountHandlerImpl`,位于 `net.xnzn.yunshitang.marketing.handler`,使用 `@Primary` 注解
|
|
245
|
-
3. **实现逻辑**:在 `handle()` 方法中使用 `sceneTypes` 字段过滤订单
|
|
246
|
-
4. **向后兼容**:`sceneTypes` 为null时查询所有场景
|
|
247
|
-
|
|
248
|
-
```java
|
|
249
|
-
// 1. 扩展DTO(在核心工程包名下覆盖)
|
|
250
|
-
// 位置:net.xnzn.core.marketing.v2.rule.price.handler.limitsum.dto.PriceLimitSumAmountDTO
|
|
251
|
-
@Data
|
|
252
|
-
@ApiModel("限额-累计金额规则详情")
|
|
253
|
-
public class PriceLimitSumAmountDTO {
|
|
254
|
-
@ApiModelProperty("累计金额限额(分)")
|
|
255
|
-
private BigDecimal limitAmount;
|
|
256
|
-
|
|
257
|
-
// 扩展字段:场景类型列表,null时表示所有场景
|
|
258
|
-
@ApiModelProperty("适用场景类型(null=所有)")
|
|
259
|
-
private List<Integer> sceneTypes;
|
|
260
|
-
|
|
261
|
-
@Override
|
|
262
|
-
public String toString() {
|
|
263
|
-
return "limitAmount=" + limitAmount + ", sceneTypes=" + sceneTypes;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// 2. 定制实现(在yunshitang项目包下)
|
|
268
|
-
// 位置:net.xnzn.yunshitang.marketing.handler
|
|
269
|
-
@Slf4j
|
|
270
|
-
@Service
|
|
271
|
-
@Primary
|
|
272
|
-
public class CrossScenePriceLimitSumAmountHandlerImpl
|
|
273
|
-
implements PriceLimitSumAmountHandlerExtension {
|
|
274
|
-
|
|
275
|
-
@Autowired
|
|
276
|
-
private MarketRuleRangeService rangeService;
|
|
277
|
-
|
|
278
|
-
@Override
|
|
279
|
-
public void handle(RulePriceResultOrderDTO order, MarketRuleVO rule,
|
|
280
|
-
List<RulePriceResultOrderDTO> orderResults) {
|
|
281
|
-
PriceLimitSumAmountDTO ruleInfo = JSON.parseObject(
|
|
282
|
-
rule.getRuleInfo(), PriceLimitSumAmountDTO.class);
|
|
283
|
-
|
|
284
|
-
// 场景过滤(向后兼容:null时不过滤)
|
|
285
|
-
if (CollUtil.isNotEmpty(ruleInfo.getSceneTypes())
|
|
286
|
-
&& !ruleInfo.getSceneTypes().contains(order.getSceneType())) {
|
|
287
|
-
return; // 不在适用场景内,跳过
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
// 原有累计金额限额逻辑...
|
|
291
|
-
BigDecimal sumAmount = calcSumAmount(order, orderResults);
|
|
292
|
-
if (sumAmount.compareTo(ruleInfo.getLimitAmount()) >= 0) {
|
|
293
|
-
throw new LeException("已超出累计消费限额");
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
```
|
|
298
|
-
|
|
299
|
-
## 参考文档
|
|
300
|
-
|
|
301
|
-
详见:[leniu-tengyun-core 源码](/Users/xujiajun/Developer/gongsi_proj/core/leniu-tengyun-core)
|