refactor: 按甲方确认重构为一款多码(档案编号标识款、批次码、按款统计)

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Nixevol
2026-06-30 12:02:44 +08:00
co-authored by Cursor
parent 67c5887f8f
commit a16e6f1165
34 changed files with 445 additions and 353 deletions
+72 -45
View File
@@ -98,7 +98,10 @@ async function ensureSampleData(): Promise<void> {
{ title: '影青刻花碗', artist: '陈一川', bg: '#2f6b5e', craft: '刻花 / 影青釉', material: '瓷石', size: '口径 18cm', year: '2024' },
];
let codeSeq = 0;
const base = (process.env.BASE_URL || 'http://localhost:3000').replace(/\/$/, '');
const CODES_PER_ARTWORK = 3; // 每款示例生码数(演示一款多码)
let artworkSeq = 0;
let totalCodes = 0;
for (const s of samples) {
const imgKey = writeStorage(`seed/${shortId()}.jpg`, await placeholder(s.title, s.bg));
const img2 = writeStorage(`seed/${shortId()}.jpg`, await placeholder('细节图', s.bg));
@@ -127,6 +130,11 @@ async function ensureSampleData(): Promise<void> {
},
},
});
// 档案编号(款级,按 id 生成)
await prisma.artwork.update({
where: { id: artwork.id },
data: { archiveNo: `ARTVERIS-${String(artwork.id).padStart(6, '0')}` },
});
// 证书 + 水印
const certOriginKey = writeStorage(`seed/${shortId()}.jpg`, await placeholder(`${s.title} 鉴定证书`, '#444c5a'));
@@ -143,62 +151,81 @@ async function ensureSampleData(): Promise<void> {
},
});
// 追溯码(一物一码)
const shortCode = shortId();
const isActive = codeSeq < 2; // 前两件设为已激活并带查询记录
const code = await prisma.traceCode.create({
// 一款多码:建一个批次 + 多套独立编码
const batch = await prisma.batch.create({
data: {
code: codeId(),
shortCode,
batchNo: `B-SEED-${String(artwork.id).padStart(4, '0')}`,
artworkId: artwork.id,
url: `${(process.env.BASE_URL || 'http://localhost:3000').replace(/\/$/, '')}/t/${shortCode}`,
status: isActive ? 'ACTIVE' : 'INACTIVE',
queryCount: isActive ? 3 + codeSeq * 2 : 0,
firstActivatedAt: isActive ? new Date(Date.now() - 86400000) : null,
lastQueriedAt: isActive ? new Date() : null,
sku: `SKU-${1000 + codeSeq}`,
archiveNo: `AW-${1000 + codeSeq}`,
quantity: CODES_PER_ARTWORK,
generatedCount: CODES_PER_ARTWORK,
status: 'DONE',
createdBy: admin?.id,
},
});
if (isActive) {
// 查询日志
for (let i = 0; i < 3; i++) {
await prisma.queryLog.create({
const artworkActive = artworkSeq < 2; // 前两款带激活码与查询记录
for (let k = 0; k < CODES_PER_ARTWORK; k++) {
const shortCode = shortId();
const isActive = artworkActive && k < 2; // 该款前两套已激活
const code = await prisma.traceCode.create({
data: {
code: codeId(),
shortCode,
artworkId: artwork.id,
batchId: batch.id,
url: `${base}/t/${shortCode}`,
status: isActive ? 'ACTIVE' : 'INACTIVE',
queryCount: isActive ? 3 + k : 0,
firstActivatedAt: isActive ? new Date(Date.now() - 86400000) : null,
lastQueriedAt: isActive ? new Date() : null,
},
});
totalCodes++;
if (isActive) {
for (let i = 0; i < 3; i++) {
await prisma.queryLog.create({
data: {
traceCodeId: code.id,
artworkId: artwork.id,
shortCode,
code: code.code,
archiveNo: `ARTVERIS-${String(artwork.id).padStart(6, '0')}`,
batchNo: batch.batchNo,
queryCount: 3 + k,
firstActivatedAt: new Date(Date.now() - 86400000),
queryResult: i === 0 ? 'FIRST_ACTIVATE' : 'REPEAT',
ip: `120.0.0.${10 + i}`,
region: '未知地区',
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X)',
deviceType: 'Mobile',
browser: 'Safari',
os: 'iOS',
queriedAt: new Date(Date.now() - (3 - i) * 3600000),
},
});
}
const dataHash = '0x' + createHash('sha256').update(JSON.stringify({ c: code.code })).digest('hex');
const txHash = '0x' + randomBytes(32).toString('hex');
await prisma.blockchainProof.create({
data: {
traceCodeId: code.id,
artworkId: artwork.id,
shortCode,
queryResult: i === 0 ? 'FIRST_ACTIVATE' : 'REPEAT',
ip: `120.0.0.${10 + i}`,
region: '未知地区',
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X)',
deviceType: 'Mobile',
queriedAt: new Date(Date.now() - (3 - i) * 3600000),
dataHash,
txHash,
blockHash: '0x' + createHash('sha256').update(txHash).digest('hex'),
blockHeight: BigInt(8_500_000 + totalCodes),
chainName: 'MockChain',
explorerUrl: `https://mock-explorer.local/tx/${txHash}`,
onchainAt: new Date(Date.now() - 86400000),
status: 'ONCHAIN',
rawResponse: { provider: 'mock-chain', code: 0, message: 'success' },
},
});
}
// 区块链存证
const dataHash = '0x' + createHash('sha256').update(JSON.stringify({ t: s.title })).digest('hex');
const txHash = '0x' + randomBytes(32).toString('hex');
await prisma.blockchainProof.create({
data: {
traceCodeId: code.id,
dataHash,
txHash,
blockHash: '0x' + createHash('sha256').update(txHash).digest('hex'),
blockHeight: BigInt(8_500_000 + codeSeq),
chainName: 'MockChain',
explorerUrl: `https://mock-explorer.local/tx/${txHash}`,
onchainAt: new Date(Date.now() - 86400000),
status: 'ONCHAIN',
rawResponse: { provider: 'mock-chain', code: 0, message: 'success' },
},
});
}
codeSeq++;
artworkSeq++;
}
console.log(`已创建示例:模板 1、作品 ${samples.length}、证书与追溯码、查询日志与存证。`);
console.log(`已创建示例:模板 1、作品 ${samples.length}(每款 ${CODES_PER_ARTWORK} 码,共 ${totalCodes} 码)、证书、查询日志与存证。`);
}
async function main(): Promise<void> {