官网开发 0810
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Router, type Request, type Response } from 'express';
|
||||
import { eq, and } from 'drizzle-orm';
|
||||
import { eq, and, max } from 'drizzle-orm';
|
||||
import { db } from '../../config/database.js';
|
||||
import { wwwSectionBlocks, wwwSpecGroups, wwwSpecItems } from '../../schemas/index.js';
|
||||
import logger from '../../config/logger.js';
|
||||
@@ -77,6 +77,15 @@ router.post('/api/www/sections', async (req: Request, res: Response) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// 未指定排序时,自动追加到同 page_type(及同产品)已有区块的最后,避免默认 0 导致乱序
|
||||
let sortOrder = Number(b.sort_order) || 0;
|
||||
if (b.sort_order === undefined || b.sort_order === null) {
|
||||
const scope = [eq(wwwSectionBlocks.pageType, b.page_type)];
|
||||
if (b.product_id) scope.push(eq(wwwSectionBlocks.productId, b.product_id));
|
||||
const [agg] = await db.select({ max: max(wwwSectionBlocks.sortOrder) }).from(wwwSectionBlocks).where(and(...scope));
|
||||
sortOrder = (agg?.max ?? -1) + 1;
|
||||
}
|
||||
|
||||
const result = await db.insert(wwwSectionBlocks).values({
|
||||
pageType: b.page_type,
|
||||
productId: b.product_id || null,
|
||||
@@ -108,7 +117,7 @@ router.post('/api/www/sections', async (req: Request, res: Response) => {
|
||||
videoUrl: b.video_url || '',
|
||||
config: b.config || null,
|
||||
isVisible: b.is_visible !== undefined ? (b.is_visible ? 1 : 0) : 1,
|
||||
sortOrder: b.sort_order || 0,
|
||||
sortOrder,
|
||||
});
|
||||
|
||||
const newId = result[0].insertId;
|
||||
|
||||
Reference in New Issue
Block a user