Files
cz-web/src/stores/modules/cases.ts
2026-10-01 17:24:57 +08:00

206 lines
5.9 KiB
TypeScript

import { defineStore } from 'pinia'
import { ref } from 'vue'
import type {
BusinessIndustryOption,
Case,
CaseQuery,
CaseTypeOption,
ProjectCaseStatisticsVo,
ProjectCaseUserBo,
ScaleLimit,
} from '@/types/business/cases'
import { collectCase, getCaseCategories, getCaseDetail, getCaseList } from '@/api/cases'
import { toCase } from '@/utils/caseVo'
import type { CaseTypeNameMap } from '@/utils/caseVo'
const PAGE_SIZE = 9
const PROBE_DELAY = 300
function createDefaultQuery(): CaseQuery {
return {
categories: [],
industries: [],
scales: [],
dateRange: null,
favoriteOnly: false,
keyword: '',
page: 1,
pageSize: PAGE_SIZE,
}
}
function withTime(date: string, time: string): string {
return date.length === 10 ? `${date} ${time}` : date
}
function trimmed(raw: string | null | undefined): string {
return (raw ?? '').trim()
}
function toTypeOptions(stats: ProjectCaseStatisticsVo | null): CaseTypeOption[] {
return (stats?.projectCaseCounts ?? [])
.map((item) => ({
value: String(item.id ?? '').trim(),
label: trimmed(item.businessType),
count: item.caseCount ?? 0,
}))
.filter((item) => item.value.length > 0 && item.label.length > 0)
}
function toTypeNameMap(stats: ProjectCaseStatisticsVo | null): CaseTypeNameMap {
const map: CaseTypeNameMap = {}
for (const item of stats?.projectCaseCounts ?? []) {
const id = String(item.id ?? '').trim()
const name = trimmed(item.businessType)
if (id.length > 0 && name.length > 0) map[id] = name
}
return map
}
export const useCasesStore = defineStore('cases', () => {
const query = ref<CaseQuery>(createDefaultQuery())
const list = ref<Case[]>([])
const total = ref(0)
const previewTotal = ref(0)
const typeOptions = ref<CaseTypeOption[]>([])
const typeNameById = ref<CaseTypeNameMap>({})
const industryOptions = ref<BusinessIndustryOption[]>([])
const scaleOptions = ref<ScaleLimit[]>([])
const caseTotal = ref(0)
const currentCase = ref<Case | null>(null)
let probeTimer: ReturnType<typeof setTimeout> | null = null
function buildQuery(input: CaseQuery): {
bo: ProjectCaseUserBo
pageNum: number
pageSize: number
} {
const bo: ProjectCaseUserBo = {}
if (input.categories.length > 0) {
bo.businessTypes = [...input.categories]
}
if (input.industries.length > 0) {
bo.businessIndustries = [...input.industries]
}
if (input.scales.length > 0) {
bo.scales = input.scales.map((item) => ({ ...item }))
}
if (input.dateRange) {
bo.beginTime = withTime(input.dateRange[0], '00:00:00')
bo.endTime = withTime(input.dateRange[1], '23:59:59')
}
if (input.favoriteOnly) bo.isFavorite = 1
const keyword = input.keyword.trim()
if (keyword) bo.key = keyword
return { bo, pageNum: input.page, pageSize: input.pageSize }
}
async function fetchCategories(): Promise<void> {
const result = await getCaseCategories()
const stats = result.projectCaseStatisticsVo
typeOptions.value = toTypeOptions(stats)
typeNameById.value = toTypeNameMap(stats)
caseTotal.value = stats?.total ?? 0
industryOptions.value = (result.businessIndustry ?? []).filter((item) => trimmed(item.name).length > 0)
scaleOptions.value = result.upperAndLowerLimits ?? []
}
async function ensureCategories(): Promise<void> {
if (typeOptions.value.length === 0) await fetchCategories()
}
async function fetchCases(): Promise<void> {
const { bo, pageNum, pageSize } = buildQuery(query.value)
const result = await getCaseList(bo, pageNum, pageSize)
list.value = (result.projectCasePage?.rows ?? []).map((vo) => toCase(vo, typeNameById.value))
total.value = result.projectCasePage?.total ?? 0
previewTotal.value = total.value
}
async function probeTotal(): Promise<void> {
const { bo } = buildQuery(query.value)
const result = await getCaseList(bo, 1, 1)
previewTotal.value = result.projectCasePage?.total ?? 0
}
function scheduleProbe(): void {
if (probeTimer) clearTimeout(probeTimer)
probeTimer = setTimeout(() => void probeTotal(), PROBE_DELAY)
}
function setQuery(next: CaseQuery): void {
query.value = next
scheduleProbe()
}
async function search(): Promise<void> {
query.value = { ...query.value, page: 1 }
await fetchCases()
}
async function resetQuery(): Promise<void> {
query.value = createDefaultQuery()
await fetchCases()
}
async function changePage(page: number): Promise<void> {
query.value = { ...query.value, page }
await fetchCases()
}
async function toggleFavoriteOnly(): Promise<void> {
query.value = { ...query.value, favoriteOnly: !query.value.favoriteOnly, page: 1 }
await fetchCases()
}
async function fetchCaseDetail(id: string): Promise<void> {
const vo = await getCaseDetail(id)
currentCase.value = vo ? toCase(vo, typeNameById.value) : null
}
async function toggleFavorite(id: string): Promise<void> {
await collectCase(id)
const index = list.value.findIndex((item) => item.id === id)
if (index >= 0) {
const target = list.value[index]
const delta = target.isFavorite ? -1 : 1
list.value[index] = { ...target, isFavorite: !target.isFavorite, favoriteCount: target.favoriteCount + delta }
}
if (currentCase.value?.id === id) {
const delta = currentCase.value.isFavorite ? -1 : 1
currentCase.value = {
...currentCase.value,
isFavorite: !currentCase.value.isFavorite,
favoriteCount: currentCase.value.favoriteCount + delta,
}
}
}
return {
query,
list,
total,
previewTotal,
typeOptions,
typeNameById,
industryOptions,
scaleOptions,
caseTotal,
currentCase,
fetchCases,
fetchCategories,
ensureCategories,
setQuery,
search,
resetQuery,
changePage,
toggleFavoriteOnly,
fetchCaseDetail,
toggleFavorite,
}
})