- {selectedBrand} · {statusLabel(selectedStatus)}
+ {selectedBrand} · {statusLabel(selectedStatus, period)}
明细字段与“进行中项目”一致,完整展示表单信息、当前节点与业务时间。
@@ -107,8 +114,8 @@ export default function MonthlyLaunchesInsights({ insights }: { insights: Monthl
>
diff --git a/components/department-dashboard/core-metrics.ts b/components/department-dashboard/core-metrics.ts
index f1691ba7..90962f1a 100644
--- a/components/department-dashboard/core-metrics.ts
+++ b/components/department-dashboard/core-metrics.ts
@@ -43,7 +43,7 @@ export const CORE_METRICS: CoreMetric[] = [
exampleValue: '18/24',
description: '本月已完结的新品美术设计流程与当前进行中新品美术设计流程的对照。',
icon: PackagePlus,
- detailDescription: '查看 2026 年 4 月 1 日起发起的新品美术设计流程;分子按本月整条流程完结时间统计,分母为本月已完结与当前进行中流程之和。',
+ detailDescription: '可按月份或自定义日期范围查看 2026 年 4 月 1 日起发起的新品美术设计流程;已完结按所选范围统计,进行中沿用当前流程快照。',
plannedFields: ['流程编码', '品牌', '发起人', '所在部门', '当前节点', '业务时间'],
},
{
diff --git a/components/department-dashboard/monthly-launch-period.ts b/components/department-dashboard/monthly-launch-period.ts
new file mode 100644
index 00000000..42b4a87f
--- /dev/null
+++ b/components/department-dashboard/monthly-launch-period.ts
@@ -0,0 +1,29 @@
+const DAY_MS = 24 * 60 * 60 * 1000
+const SHANGHAI_OFFSET_MS = 8 * 60 * 60 * 1000
+
+export function toShanghaiDateInput(value: string, endExclusive = false) {
+ const instant = new Date(value).getTime() - (endExclusive ? DAY_MS : 0)
+ if (!Number.isFinite(instant)) return ''
+ return new Date(instant + SHANGHAI_OFFSET_MS).toISOString().slice(0, 10)
+}
+
+function monthValue(value: Date) {
+ const shifted = new Date(value.getTime() + SHANGHAI_OFFSET_MS)
+ return shifted.toISOString().slice(0, 7)
+}
+
+/** 完整自然月显示“本月/2026年8月”,自定义区间显示起止日期。 */
+export function formatMonthlyLaunchPeriod(monthStart: string, monthEnd: string) {
+ const startDate = toShanghaiDateInput(monthStart)
+ const endDate = toShanghaiDateInput(monthEnd, true)
+ if (!startDate || !endDate) return '所选期间'
+ const [year, month, day] = startDate.split('-').map(Number)
+ const [, , endDay] = endDate.split('-').map(Number)
+ const lastDay = new Date(Date.UTC(year, month, 0)).getUTCDate()
+ if (day === 1 && endDay === lastDay && endDate.slice(0, 7) === startDate.slice(0, 7)) {
+ const current = monthValue(new Date())
+ return startDate.slice(0, 7) === current ? '本月' : `${year}年${month}月`
+ }
+ return `${startDate.replaceAll('-', '/')} - ${endDate.replaceAll('-', '/')}`
+}
+
diff --git a/lib/__tests__/ding-workflow-sync.test.ts b/lib/__tests__/ding-workflow-sync.test.ts
index c21f4708..50352494 100644
--- a/lib/__tests__/ding-workflow-sync.test.ts
+++ b/lib/__tests__/ding-workflow-sync.test.ts
@@ -8,6 +8,9 @@ import {
buildMonthlyLaunchOngoingWorkflowWhere,
buildMonthlyLaunchWorkflowWhere,
getShanghaiMonthRange,
+ getShanghaiMonthRangeForValue,
+ getMonthlyLaunchMonthOptions,
+ resolveMonthlyLaunchDateRange,
getDingWorkflowActivityNodeName,
getDingWorkflowProjectType,
isDingWorkflowTaskOngoing,
@@ -82,6 +85,39 @@ describe('ding workflow snapshot sync semantics', () => {
expect(isDingWorkflowCompletedInRange('COMPLETED', null, range)).toBe(false)
})
+ it('resolves a requested Shanghai month and inclusive custom date range', () => {
+ expect(getShanghaiMonthRangeForValue('2026-08')).toEqual({
+ start: new Date('2026-07-31T16:00:00.000Z'),
+ end: new Date('2026-08-31T16:00:00.000Z'),
+ })
+ expect(resolveMonthlyLaunchDateRange({ startDate: '2026-08-01', endDate: '2026-08-31' })).toEqual({
+ start: new Date('2026-07-31T16:00:00.000Z'),
+ end: new Date('2026-08-31T16:00:00.000Z'),
+ })
+ })
+
+ it('offers month presets from the April 2026 cutoff through the current month', () => {
+ const options = getMonthlyLaunchMonthOptions(new Date('2026-09-15T00:00:00.000Z'))
+ expect(options.map((option) => option.value)).toEqual([
+ '2026-09',
+ '2026-08',
+ '2026-07',
+ '2026-06',
+ '2026-05',
+ '2026-04',
+ ])
+ expect(options[0]).toMatchObject({ label: '本月', startDate: '2026-09-01', endDate: '2026-09-30' })
+ expect(options[1]).toMatchObject({ label: '上月', startDate: '2026-08-01', endDate: '2026-08-31' })
+ })
+
+ it('rejects invalid or ambiguous monthly launch date filters', () => {
+ expect(() => getShanghaiMonthRangeForValue('2026-13')).toThrow('month不是有效月份')
+ expect(() => getShanghaiMonthRangeForValue('2026/08')).toThrow('month格式应为YYYY-MM')
+ expect(() => resolveMonthlyLaunchDateRange({ startDate: '2026-02-30', endDate: '2026-03-01' })).toThrow('startDate不是有效日期')
+ expect(() => resolveMonthlyLaunchDateRange({ month: '2026-08', startDate: '2026-08-01' })).toThrow('month不能与startDate/endDate同时使用')
+ expect(() => resolveMonthlyLaunchDateRange({ startDate: '2026-09-01', endDate: '2026-08-31' })).toThrow('endDate不能早于startDate')
+ })
+
it('uses the delivery assessment finish node for monthly inclusion and excludes pending', () => {
const range = getShanghaiMonthRange(new Date('2026-08-26T03:00:00.000Z'))
expect(isDingWorkflowDeliveryAssessmentInRange({
diff --git a/lib/ding-workflow-sync.ts b/lib/ding-workflow-sync.ts
index 16e61fcb..391530f1 100644
--- a/lib/ding-workflow-sync.ts
+++ b/lib/ding-workflow-sync.ts
@@ -437,10 +437,31 @@ export type MonthlyLaunchSummary = {
syncStatus: DingWorkflowSyncStatus
}
+/** 查询本月上新指标时可选的日期筛选参数。月份使用 YYYY-MM,
+ * 自定义区间使用上海本地日期(YYYY-MM-DD,结束日期含当天)。 */
+export type MonthlyLaunchRangeInput = {
+ month?: string | null
+ startDate?: string | null
+ endDate?: string | null
+}
+
+export type MonthlyLaunchDateRange = {
+ start: Date
+ end: Date
+}
+
+export type MonthlyLaunchMonthOption = {
+ value: string
+ label: string
+ startDate: string
+ endDate: string
+}
+
export type MonthlyLaunchInsights = {
items: MonthlyLaunchWorkflowItem[]
brands: MonthlyLaunchBrandSummary[]
summary: MonthlyLaunchSummary
+ availableMonths?: MonthlyLaunchMonthOption[]
}
export type OnTimeDeliveryWorkflowItem = OngoingDingWorkflowListItem & {
@@ -1037,6 +1058,115 @@ export function getShanghaiMonthRange(now = new Date()) {
}
}
+const SHANGHAI_OFFSET_MS = 8 * 60 * 60 * 1000
+const SHANGHAI_MONTH_PATTERN = /^(\d{4})-(\d{2})$/
+const SHANGHAI_DATE_PATTERN = /^(\d{4})-(\d{2})-(\d{2})$/
+
+function parseShanghaiCalendarDate(value: unknown, label: string): { year: number; month: number; day: number } {
+ const normalized = clean(value)
+ const match = normalized?.match(SHANGHAI_DATE_PATTERN)
+ if (!match) throw new DingWorkflowMetricApiError(`${label}格式应为YYYY-MM-DD`, 400)
+ const year = Number(match[1])
+ const month = Number(match[2])
+ const day = Number(match[3])
+ const candidate = new Date(Date.UTC(year, month - 1, day))
+ if (
+ !Number.isFinite(candidate.getTime())
+ || candidate.getUTCFullYear() !== year
+ || candidate.getUTCMonth() !== month - 1
+ || candidate.getUTCDate() !== day
+ ) {
+ throw new DingWorkflowMetricApiError(`${label}不是有效日期`, 400)
+ }
+ return { year, month, day }
+}
+
+/** 将上海本地月份转换为 UTC Date 区间,区间结束点为下月一日(不含)。 */
+export function getShanghaiMonthRangeForValue(value: unknown): MonthlyLaunchDateRange {
+ const normalized = clean(value)
+ const match = normalized?.match(SHANGHAI_MONTH_PATTERN)
+ if (!match) throw new DingWorkflowMetricApiError('month格式应为YYYY-MM', 400)
+ const year = Number(match[1])
+ const month = Number(match[2])
+ if (month < 1 || month > 12) throw new DingWorkflowMetricApiError('month不是有效月份', 400)
+ return {
+ start: new Date(Date.UTC(year, month - 1, 1) - SHANGHAI_OFFSET_MS),
+ end: new Date(Date.UTC(year, month, 1) - SHANGHAI_OFFSET_MS),
+ }
+}
+
+/** 解析月份或上海本地日期区间。自定义 endDate 含当天。 */
+export function resolveMonthlyLaunchDateRange(
+ input: MonthlyLaunchRangeInput = {},
+ now = new Date(),
+): MonthlyLaunchDateRange {
+ const month = clean(input.month)
+ const startDate = clean(input.startDate)
+ const endDate = clean(input.endDate)
+ if (month && (startDate || endDate)) {
+ throw new DingWorkflowMetricApiError('month不能与startDate/endDate同时使用', 400)
+ }
+ if (month) return getShanghaiMonthRangeForValue(month)
+ if (!startDate && !endDate) return getShanghaiMonthRange(now)
+ if (!startDate || !endDate) {
+ throw new DingWorkflowMetricApiError('startDate和endDate必须同时提供', 400)
+ }
+ const start = parseShanghaiCalendarDate(startDate, 'startDate')
+ const end = parseShanghaiCalendarDate(endDate, 'endDate')
+ const startInstant = new Date(Date.UTC(start.year, start.month - 1, start.day) - SHANGHAI_OFFSET_MS)
+ const endExclusive = new Date(Date.UTC(end.year, end.month - 1, end.day + 1) - SHANGHAI_OFFSET_MS)
+ if (endExclusive <= startInstant) {
+ throw new DingWorkflowMetricApiError('endDate不能早于startDate', 400)
+ }
+ return { start: startInstant, end: endExclusive }
+}
+
+function shanghaiMonthValue(value: Date) {
+ const parts = new Intl.DateTimeFormat('en-US', {
+ timeZone: 'Asia/Shanghai',
+ year: 'numeric',
+ month: '2-digit',
+ }).formatToParts(value)
+ const year = parts.find((part) => part.type === 'year')?.value || '0000'
+ const month = parts.find((part) => part.type === 'month')?.value || '01'
+ return `${year}-${month}`
+}
+
+function formatShanghaiDateInput(value: Date) {
+ const shifted = new Date(value.getTime() + SHANGHAI_OFFSET_MS)
+ return shifted.toISOString().slice(0, 10)
+}
+
+/** 返回从 2026 年 4 月起到当前月份的筛选选项,供详情页下拉框使用。 */
+export function getMonthlyLaunchMonthOptions(now = new Date()): MonthlyLaunchMonthOption[] {
+ const currentValue = shanghaiMonthValue(now)
+ const [currentYear, currentMonth] = currentValue.split('-').map(Number)
+ const cutoffValue = shanghaiMonthValue(new Date(DING_WORKFLOW_STARTED_AT_CUTOFF_ISO))
+ const [cutoffYear, cutoffMonth] = cutoffValue.split('-').map(Number)
+ const options: MonthlyLaunchMonthOption[] = []
+ const previousMonthValue = currentMonth === 1
+ ? `${currentYear - 1}-12`
+ : `${currentYear}-${String(currentMonth - 1).padStart(2, '0')}`
+ let year = currentYear
+ let month = currentMonth
+ while (year > cutoffYear || (year === cutoffYear && month >= cutoffMonth)) {
+ const value = `${year}-${String(month).padStart(2, '0')}`
+ const range = getShanghaiMonthRangeForValue(value)
+ options.push({
+ value,
+ label: value === currentValue ? '本月' : value === previousMonthValue ? '上月' : `${year}年${month}月`,
+ startDate: formatShanghaiDateInput(range.start),
+ endDate: formatShanghaiDateInput(new Date(range.end.getTime() - 24 * 60 * 60 * 1000)),
+ })
+ month -= 1
+ if (month === 0) {
+ month = 12
+ year -= 1
+ }
+ }
+ return options
+}
+
export function isDingWorkflowCompletedInRange(
status: unknown,
finishedAt: unknown,
@@ -2168,8 +2298,11 @@ export function buildMonthlyLaunchOngoingWorkflowWhere() {
}
}
-async function readMonthlyLaunchInsightsWithoutAccessCheck(now = new Date()): Promise
{
- const range = getShanghaiMonthRange(now)
+async function readMonthlyLaunchInsightsWithoutAccessCheck(
+ input: MonthlyLaunchRangeInput = {},
+ now = new Date(),
+): Promise {
+ const range = resolveMonthlyLaunchDateRange(input, now)
const baseWhere = buildMonthlyLaunchWorkflowWhere()
const [completedCandidates, ongoingRows, state] = await Promise.all([
prisma.dingWorkflowSnapshot.findMany({
@@ -2184,6 +2317,7 @@ async function readMonthlyLaunchInsightsWithoutAccessCheck(now = new Date()): Pr
select: MONTHLY_LAUNCH_WORKFLOW_SELECT,
}),
prisma.dingWorkflowSnapshot.findMany({
+ // “当前进行中”沿用进行中项目的全量在办口径,与所选完结月份无关。
where: buildMonthlyLaunchOngoingWorkflowWhere(),
select: MONTHLY_LAUNCH_WORKFLOW_SELECT,
}),
@@ -2240,17 +2374,24 @@ async function readMonthlyLaunchInsightsWithoutAccessCheck(now = new Date()): Pr
lastSyncedAt: state?.lastCompletedAt?.toISOString() || null,
syncStatus: normalizeSyncStatus(state),
},
+ availableMonths: getMonthlyLaunchMonthOptions(now),
}
}
-export async function getMonthlyLaunchSummary(session: Session | null): Promise {
+export async function getMonthlyLaunchSummary(
+ session: Session | null,
+ input: MonthlyLaunchRangeInput = {},
+): Promise {
await assertDashboardAccess(session)
- return (await readMonthlyLaunchInsightsWithoutAccessCheck()).summary
+ return (await readMonthlyLaunchInsightsWithoutAccessCheck(input)).summary
}
-export async function getMonthlyLaunchInsights(session: Session | null): Promise {
+export async function getMonthlyLaunchInsights(
+ session: Session | null,
+ input: MonthlyLaunchRangeInput = {},
+): Promise {
await assertDashboardAccess(session)
- return readMonthlyLaunchInsightsWithoutAccessCheck()
+ return readMonthlyLaunchInsightsWithoutAccessCheck(input)
}
async function readOnTimeDeliveryInsightsWithoutAccessCheck(now = new Date()): Promise {