0.0.5.1
This commit is contained in:
@@ -88,6 +88,175 @@ export async function PATCH(
|
||||
}
|
||||
})
|
||||
|
||||
// 维护配偶双向关系
|
||||
if (body.spouseIds !== undefined) {
|
||||
const newSpouseIds = body.spouseIds || []
|
||||
const oldSpouseIds = existingMember.spouseIds || []
|
||||
|
||||
// 添加新配偶的反向关系
|
||||
for (const spouseId of newSpouseIds) {
|
||||
if (!oldSpouseIds.includes(spouseId)) {
|
||||
const spouse = await prisma.familyMember.findUnique({
|
||||
where: { id: spouseId }
|
||||
})
|
||||
|
||||
if (spouse && !spouse.spouseIds.includes(memberId)) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: spouseId },
|
||||
data: {
|
||||
spouseIds: [...spouse.spouseIds, memberId]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 移除旧配偶的反向关系
|
||||
for (const oldSpouseId of oldSpouseIds) {
|
||||
if (!newSpouseIds.includes(oldSpouseId)) {
|
||||
const spouse = await prisma.familyMember.findUnique({
|
||||
where: { id: oldSpouseId }
|
||||
})
|
||||
|
||||
if (spouse && spouse.spouseIds.includes(memberId)) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: oldSpouseId },
|
||||
data: {
|
||||
spouseIds: spouse.spouseIds.filter(id => id !== memberId)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 维护父母-子女双向关系
|
||||
// 1. 处理父亲关系变化
|
||||
if (body.fatherId !== undefined) {
|
||||
const oldFatherId = existingMember.fatherId
|
||||
const newFatherId = body.fatherId
|
||||
|
||||
// 从旧父亲的子女列表中移除
|
||||
if (oldFatherId && oldFatherId !== newFatherId) {
|
||||
const oldFather = await prisma.familyMember.findUnique({
|
||||
where: { id: oldFatherId }
|
||||
})
|
||||
if (oldFather && oldFather.childrenIds.includes(memberId)) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: oldFatherId },
|
||||
data: {
|
||||
childrenIds: oldFather.childrenIds.filter(id => id !== memberId)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 添加到新父亲的子女列表
|
||||
if (newFatherId && newFatherId !== oldFatherId) {
|
||||
const newFather = await prisma.familyMember.findUnique({
|
||||
where: { id: newFatherId }
|
||||
})
|
||||
if (newFather && !newFather.childrenIds.includes(memberId)) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: newFatherId },
|
||||
data: {
|
||||
childrenIds: [...newFather.childrenIds, memberId]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 处理母亲关系变化
|
||||
if (body.motherId !== undefined) {
|
||||
const oldMotherId = existingMember.motherId
|
||||
const newMotherId = body.motherId
|
||||
|
||||
// 从旧母亲的子女列表中移除
|
||||
if (oldMotherId && oldMotherId !== newMotherId) {
|
||||
const oldMother = await prisma.familyMember.findUnique({
|
||||
where: { id: oldMotherId }
|
||||
})
|
||||
if (oldMother && oldMother.childrenIds.includes(memberId)) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: oldMotherId },
|
||||
data: {
|
||||
childrenIds: oldMother.childrenIds.filter(id => id !== memberId)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 添加到新母亲的子女列表
|
||||
if (newMotherId && newMotherId !== oldMotherId) {
|
||||
const newMother = await prisma.familyMember.findUnique({
|
||||
where: { id: newMotherId }
|
||||
})
|
||||
if (newMother && !newMother.childrenIds.includes(memberId)) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: newMotherId },
|
||||
data: {
|
||||
childrenIds: [...newMother.childrenIds, memberId]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 处理子女关系变化
|
||||
if (body.childrenIds !== undefined) {
|
||||
const newChildrenIds = body.childrenIds || []
|
||||
const oldChildrenIds = existingMember.childrenIds || []
|
||||
|
||||
// 添加新子女的父母关系
|
||||
for (const childId of newChildrenIds) {
|
||||
if (!oldChildrenIds.includes(childId)) {
|
||||
const child = await prisma.familyMember.findUnique({
|
||||
where: { id: childId }
|
||||
})
|
||||
|
||||
if (child) {
|
||||
// 根据当前成员的性别设置为父亲或母亲
|
||||
if (member.gender === 'MALE' && !child.fatherId) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: childId },
|
||||
data: { fatherId: memberId }
|
||||
})
|
||||
} else if (member.gender === 'FEMALE' && !child.motherId) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: childId },
|
||||
data: { motherId: memberId }
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 移除旧子女的父母关系
|
||||
for (const oldChildId of oldChildrenIds) {
|
||||
if (!newChildrenIds.includes(oldChildId)) {
|
||||
const child = await prisma.familyMember.findUnique({
|
||||
where: { id: oldChildId }
|
||||
})
|
||||
|
||||
if (child) {
|
||||
// 根据当前成员的性别移除父亲或母亲
|
||||
if (member.gender === 'MALE' && child.fatherId === memberId) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: oldChildId },
|
||||
data: { fatherId: null }
|
||||
})
|
||||
} else if (member.gender === 'FEMALE' && child.motherId === memberId) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: oldChildId },
|
||||
data: { motherId: null }
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 记录操作日志
|
||||
await prisma.activityLog.create({
|
||||
data: {
|
||||
|
||||
@@ -12,12 +12,32 @@ const createMemberSchema = z.object({
|
||||
fullName: z.string().min(1),
|
||||
gender: z.nativeEnum(Gender),
|
||||
generation: z.number().int().positive(),
|
||||
generationName: z.string().optional(),
|
||||
courtesyName: z.string().optional(),
|
||||
artName: z.string().optional(),
|
||||
posthumousName: z.string().optional(),
|
||||
rank: z.number().optional(),
|
||||
birthDate: z.string().optional(),
|
||||
deathDate: z.string().optional(),
|
||||
isLunarDate: z.boolean().optional(),
|
||||
ancestralHome: z.string().optional(),
|
||||
birthPlace: z.string().optional(),
|
||||
burialPlace: z.string().optional(),
|
||||
phone: z.string().optional(),
|
||||
telephone: z.string().optional(),
|
||||
email: z.string().optional(),
|
||||
address: z.string().optional(),
|
||||
fatherId: z.string().optional(),
|
||||
motherId: z.string().optional(),
|
||||
spouseIds: z.array(z.string()).optional(),
|
||||
// ... 其他字段
|
||||
childrenIds: z.array(z.string()).optional(),
|
||||
spouseFatherName: z.string().optional(),
|
||||
spouseMotherName: z.string().optional(),
|
||||
bio: z.string().optional(),
|
||||
tags: z.array(z.string()).optional(),
|
||||
avatarUrl: z.string().optional(),
|
||||
avatarImageId: z.string().optional(),
|
||||
photoIds: z.array(z.string()).optional(),
|
||||
})
|
||||
|
||||
// 获取家族树的所有成员
|
||||
@@ -116,6 +136,81 @@ export async function POST(
|
||||
}
|
||||
})
|
||||
|
||||
// 维护配偶双向关系
|
||||
if (data.spouseIds && data.spouseIds.length > 0) {
|
||||
for (const spouseId of data.spouseIds) {
|
||||
const spouse = await prisma.familyMember.findUnique({
|
||||
where: { id: spouseId }
|
||||
})
|
||||
|
||||
if (spouse && !spouse.spouseIds.includes(member.id)) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: spouseId },
|
||||
data: {
|
||||
spouseIds: [...spouse.spouseIds, member.id]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 维护父母-子女双向关系
|
||||
// 1. 如果设置了父亲,将当前成员添加到父亲的子女列表
|
||||
if (data.fatherId) {
|
||||
const father = await prisma.familyMember.findUnique({
|
||||
where: { id: data.fatherId }
|
||||
})
|
||||
|
||||
if (father && !father.childrenIds.includes(member.id)) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: data.fatherId },
|
||||
data: {
|
||||
childrenIds: [...father.childrenIds, member.id]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 如果设置了母亲,将当前成员添加到母亲的子女列表
|
||||
if (data.motherId) {
|
||||
const mother = await prisma.familyMember.findUnique({
|
||||
where: { id: data.motherId }
|
||||
})
|
||||
|
||||
if (mother && !mother.childrenIds.includes(member.id)) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: data.motherId },
|
||||
data: {
|
||||
childrenIds: [...mother.childrenIds, member.id]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 如果设置了子女,将当前成员设置为子女的父亲或母亲
|
||||
if (data.childrenIds && data.childrenIds.length > 0) {
|
||||
for (const childId of data.childrenIds) {
|
||||
const child = await prisma.familyMember.findUnique({
|
||||
where: { id: childId }
|
||||
})
|
||||
|
||||
if (child) {
|
||||
// 根据当前成员的性别设置为父亲或母亲
|
||||
if (data.gender === 'MALE' && !child.fatherId) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: childId },
|
||||
data: { fatherId: member.id }
|
||||
})
|
||||
} else if (data.gender === 'FEMALE' && !child.motherId) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: childId },
|
||||
data: { motherId: member.id }
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 记录操作日志
|
||||
await prisma.activityLog.create({
|
||||
data: {
|
||||
|
||||
@@ -17,13 +17,14 @@ import { useDialog } from "@/components/ui/alert-dialog-custom"
|
||||
|
||||
export default function MemberProfilePage() {
|
||||
const { id } = useParams()
|
||||
const { getMember, updateMember, deleteMember } = useFamily()
|
||||
const { getMember, updateMember, deleteMember, treeData } = useFamily()
|
||||
const { showConfirm } = useDialog()
|
||||
const router = useRouter()
|
||||
const [isEditing, setIsEditing] = useState(false)
|
||||
const [avatarBlobUrl, setAvatarBlobUrl] = useState<string | undefined>(undefined)
|
||||
|
||||
const member = getMember(id as string)
|
||||
const allMembers = Object.values(treeData.members)
|
||||
|
||||
// Load avatar blob if exists
|
||||
useEffect(() => {
|
||||
@@ -76,7 +77,12 @@ export default function MemberProfilePage() {
|
||||
</Button>
|
||||
<h1 className="text-2xl font-bold font-serif">编辑成员: {member.fullName}</h1>
|
||||
</div>
|
||||
<MemberForm initialData={member} onSubmit={handleSave} onCancel={() => setIsEditing(false)} />
|
||||
<MemberForm
|
||||
initialData={member}
|
||||
existingMembers={allMembers}
|
||||
onSubmit={handleSave}
|
||||
onCancel={() => setIsEditing(false)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -21,7 +21,8 @@ export default function NewMemberPage() {
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
})
|
||||
router.push(`/members/${newMember.id}`)
|
||||
// 跳转到成员列表页
|
||||
router.push('/members')
|
||||
} catch (error) {
|
||||
console.error('添加成员失败:', error)
|
||||
alert('添加成员失败: ' + (error as Error).message)
|
||||
|
||||
+41
-5
@@ -112,7 +112,7 @@ export default function MembersPage() {
|
||||
return result.sort((a, b) => a.generation - b.generation || (a.birthDate || "").localeCompare(b.birthDate || ""))
|
||||
}, [allMembers, query, selectedSurname, selectedHome, statusFilter, generationRange])
|
||||
|
||||
// 按世代分组
|
||||
// 按世代分组并排序
|
||||
const membersByGeneration = useMemo(() => {
|
||||
const groups = new Map<number, typeof filteredMembers>()
|
||||
|
||||
@@ -127,10 +127,46 @@ export default function MembersPage() {
|
||||
// 转换为数组并排序
|
||||
return Array.from(groups.entries())
|
||||
.sort((a, b) => a[0] - b[0])
|
||||
.map(([generation, members]) => ({
|
||||
generation,
|
||||
members
|
||||
}))
|
||||
.map(([generation, members]) => {
|
||||
// 对每个世代的成员进行排序
|
||||
const sortedMembers = [...members].sort((a, b) => {
|
||||
// 1. 始祖永远排第一
|
||||
const aIsFounder = a.tags?.includes('始祖') || a.generation === 1
|
||||
const bIsFounder = b.tags?.includes('始祖') || b.generation === 1
|
||||
if (aIsFounder && !bIsFounder) return -1
|
||||
if (!aIsFounder && bIsFounder) return 1
|
||||
|
||||
// 2. 按出生日期排序
|
||||
return (a.birthDate || "").localeCompare(b.birthDate || "")
|
||||
})
|
||||
|
||||
// 3. 将配偶插入到主成员后面
|
||||
const result: typeof members = []
|
||||
const processedSpouses = new Set<string>()
|
||||
|
||||
sortedMembers.forEach(member => {
|
||||
// 跳过已经作为配偶处理过的成员
|
||||
if (processedSpouses.has(member.id)) return
|
||||
|
||||
result.push(member)
|
||||
|
||||
// 如果有配偶,将配偶紧跟在后面
|
||||
if (member.spouseIds && member.spouseIds.length > 0) {
|
||||
member.spouseIds.forEach(spouseId => {
|
||||
const spouse = members.find(m => m.id === spouseId)
|
||||
if (spouse && !processedSpouses.has(spouseId)) {
|
||||
result.push(spouse)
|
||||
processedSpouses.add(spouseId)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
generation,
|
||||
members: result
|
||||
}
|
||||
})
|
||||
}, [filteredMembers])
|
||||
|
||||
const clearFilters = () => {
|
||||
|
||||
Reference in New Issue
Block a user