import type { WatchStatus, StatusChange } from '../types' /** 后端权威契约:万元与缺失值保持原样;浏览器不重新计算匹配分或收费。 */ export interface SearchQuery { keywords: string[] beginDate: string endDate: string stages: number[] preference: 'COMPREHENSIVE' | 'REGION' | 'BUSINESS' | 'AMOUNT' preferredRegions: string[] budgetMinWan: number | null budgetMaxWan: number | null mode: 'SEARCH' | 'LEADS' | 'MARKET' keywordMode: 'ANY' | 'ALL' hardRegions: string[] hardMinWan: number | null hardMaxWan: number | null } export interface Candidate { id: string; sourceId: string; title: string; purchaser: string; region: string budgetWan: number | null; publishDate: string; stage: number | null; url: string | null userFileId?: string | null; products: string[]; evidenceId: string | null; gaps: string[] } export interface RankedItem { facts: Candidate; relevance: number; preferenceBonus: number; reasoning: string[] analysis: null | { projectNumber: string summary: { construction: string; region: string; schedule: string[]; qualifications: string[]; gaps: string[]; evidenceId: string; locator: string; method: string } match: { score: number | null; completeness: number; profileVersion: number; version: string; limitation: string weights: Record; dimensions: Record } } } export interface SearchReport { conditions: SearchQuery; rankingVersion: string scope: { platformTotal: number | null; fetchedCount: number; uniqueCount: number; displayCount: number; pagesRead: number; stopReason: string; complete: boolean; budgetFilteredLocally?: boolean } stageChains: { projectNumber: string; purchaser: string; notices: Candidate[]; basis: string }[] items: RankedItem[]; limitations: string[]; referencesVerified: boolean summaryProgress: { completed: number; total: number; method: string } market?: { basis: string; buyers: Record; suppliers: Record; monthlyNotices: Record; platformAggregations: { tool: string; evidenceId: string; scope: string; rows: { name: string; count: number | null; amountWan: number | null; url: string | null }[] }[] } } export interface SearchHistory { id: string; status: string; createdAt: string; schemeId: string | null; schemeName: string | null; schemeVersion: number | null } export interface SearchScheme { id: string; name: string; conditions: SearchQuery; version: number } export interface WatchItem { fileId?: string | null; summary?: NonNullable['summary'] projectId: string; status: WatchStatus; note: string; version: number; tags: string[] createdAt: string; history: StatusChange[]; group: string; project: Candidate } export interface Tag { id: string; name: string } export const inputClass = 'w-full rounded-ctrl border border-line-soft bg-white px-3 py-2 text-sm outline-none focus:border-brand-500' export const stageNames: Record = { 1: '采购意向', 2: '预招标', 4: '招标', 5: '更正', 6: '终止', 7: '中标', 8: '合同', 9: '其他', 10: '其他' } export const preferenceNames = { COMPREHENSIVE: '综合推荐', REGION: '地域优先', BUSINESS: '业务优先', AMOUNT: '金额优先' } /** 时间快捷选项统一使用北京时间,避免午夜时浏览器时区改变查询日期。 */ export function beijingDate(offset = 0) { return new Date(Date.now() + (8 * 60 * 60 * 1000) - offset * 86400000).toISOString().slice(0, 10) } export function initialQuery(): SearchQuery { return { keywords: [], beginDate: beijingDate(89), endDate: beijingDate(), stages: [1, 2, 4, 7, 8], preference: 'COMPREHENSIVE', preferredRegions: [], budgetMinWan: null, budgetMaxWan: null, mode: 'LEADS', keywordMode: 'ANY', hardRegions: [], hardMinWan: null, hardMaxWan: null } } /** * 发现商机表单中的地区和预算是用户明确填写的筛选条件。 * 旧版页面误用了获客偏好字段;载入后转换为普通检索的硬条件,并原样保留日期和关键词逻辑。 * 返回新对象,避免编辑表单时改写已保存方案或历史任务;市场查询仍保留自身模式。 */ export function asSearchQuery(query: SearchQuery): SearchQuery { if (query.mode !== 'LEADS') return { ...query } return { ...query, mode: 'SEARCH', preference: 'COMPREHENSIVE', hardRegions: [...query.preferredRegions], hardMinWan: query.budgetMinWan, hardMaxWan: query.budgetMaxWan, preferredRegions: [], budgetMinWan: null, budgetMaxWan: null, } } /** 快捷范围包含今天;先固定北京时间的结束日期,避免午夜跨天造成起止日期相差一天。 */ export function publicationRange(days: number) { const endDate = beijingDate() const begin = new Date(`${endDate}T00:00:00Z`) begin.setUTCDate(begin.getUTCDate() - days + 1) return { beginDate: begin.toISOString().slice(0, 10), endDate } } export function words(value: string) { return value.split(/[、,,;;\n]+/).map(s => s.trim()).filter(Boolean) } export function message(error: unknown) { return error instanceof Error ? error.message : '请求失败,请重试' } export function time(value: string) { return new Date(value).toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai', hour12: false }) } const registration = /报名(?:及获取文件)?|(?:获取|领取|下载|购买)(?:招标|采购|磋商|询价|比选)文件/g const calendarDate = /^(\d{4})\s*[-年/.]\s*(\d{1,2})\s*[-月/.]\s*(\d{1,2})/ /** 仅在报名或获取采购文件的条款内读取截止日。日期区间取尾端,投标、开标和服务结束日期不能补位。 */ export function registrationDate(schedule: string[] = []): string | null { for (const clause of schedule.flatMap(line => line.split(/[,,;;。\n]/))) { for (const marker of clause.matchAll(registration)) { const related = clause.slice(marker.index + marker[0].length).split(/(?:递交|提交)(?:投标|响应)|投标截止|响应截止|开标|服务(?:截止|期)|工期/)[0] const end = related.match(/(?:截止|截至|结束)(?:时间|日期)?\s*[::为至到于]?\s*(.+)/) ?? related.match(/(?:至|到|[~~—–])\s*(.+)/) const match = end?.[1].match(calendarDate) if (!match) continue const value = `${match[1]}-${match[2].padStart(2, '0')}-${match[3].padStart(2, '0')}` const date = new Date(`${value}T00:00:00Z`) if (Number.isFinite(date.getTime()) && date.toISOString().slice(0, 10) === value) return value } } return null } /** 用北京时间的两个日历日计算剩余天数,避免临近午夜时用小时差造成前后一天的偏差。 */ export function registrationReminder(schedule?: string[], today = beijingDate()) { const date = registrationDate(schedule) if (!date) return null const days = (Date.parse(date) - Date.parse(today)) / 86400000 return { date, days, label: days < 0 ? '报名已截止' : days === 0 ? '报名今天截止' : `报名仅剩 ${days} 天` } } export function budgetLabel(conditions: SearchQuery) { const preference = conditions.mode === 'LEADS' const min = preference ? conditions.budgetMinWan : conditions.hardMinWan const max = preference ? conditions.budgetMaxWan : conditions.hardMaxWan if (min == null && max == null) return '' const bounds = min != null && max != null ? `${min.toLocaleString('zh-CN')}–${max.toLocaleString('zh-CN')}` : min != null ? `≥ ${min.toLocaleString('zh-CN')}` : `≤ ${max!.toLocaleString('zh-CN')}` return `${preference ? '预算偏好' : '预算'} ${bounds} 万元` }