feat(qa-ops): 支持自定义区域与分级部门批量归属
build / build (push) Successful in 1m8s

This commit is contained in:
姚章浩
2026-10-18 21:50:29 +08:00
parent 8835a473dd
commit 76671e0784
33 changed files with 865 additions and 100 deletions
+216 -39
View File
@@ -1,11 +1,24 @@
import { useEffect, useRef, useState } from 'react'
import { changeRegionRule, fetchOrganization, previewOrganization, regionNames, saveOrganization } from './qaOrg'
import type { Organization, RegionRule } from './qaOrg'
import {
changeRegionRule,
fetchOrganization,
previewOrganization,
organizationRegions,
parseRegionDefinitions,
saveOrganization,
} from './qaOrg'
import type { Organization, RegionRule, RegionDefinition } from './qaOrg'
import './qaRegions.css'
import { applyBulkRegionRules, departmentLevels } from './qaRegionSelection'
import { completedSyncAction, directorySyncActive, fetchDirectorySync, startDirectorySync } from './qaOrgSync'
import type { DirectorySyncStatus } from './qaOrgSync'
import { newOperationID } from './operationKey'
interface Props {
regions?: RegionDefinition[]
selectedIDs?: string[]
onCatalog?: (regions: RegionDefinition[]) => void
onSelect?: (ids: string[]) => void
onApply?: () => void
syncStatus?: DirectorySyncStatus | null
syncError?: string
syncSubmitting?: boolean
@@ -28,10 +41,35 @@ interface Props {
onReload: () => void
}
export function RegionMappingView(p: Props) {
const display = p.preview ?? p.state
const available = p.state?.state === 'ready'
const departments =
display?.view.departments.filter((d) => d.path.toLowerCase().includes(p.search.toLowerCase())) ?? []
const [newName, setNewName] = useState(''),
[level, setLevel] = useState(''),
[catalogError, setCatalogError] = useState('')
const display = p.preview ?? p.state,
available = p.state?.state === 'ready'
const regions = p.regions ?? organizationRegions(p.state),
selected = p.selectedIDs ?? []
const all = display?.view.departments ?? [],
nodes = new Map(all.map((d) => [d.id, d]))
const rows = all.map((d) => ({ department: d, levels: departmentLevels(all, d.id, nodes) }))
const departments = rows.filter(
({ department: d, levels }) =>
d.path.toLowerCase().includes(p.search.toLowerCase()) && (!level || levels.length === Number(level))
)
const maximum = rows.reduce((n, r) => Math.max(n, r.levels.length), 0)
const label = (id: string, catalog = regions) => catalog.find((r) => r.id === id)?.name ?? id
const choose = (id: string, checked: boolean) =>
p.onSelect?.(checked ? [...new Set([...selected, id])] : selected.filter((x) => x !== id))
const add = () => {
if (!newName.trim()) return
const next = [...regions, { id: 'region-' + newOperationID(), name: newName.trim() }]
try {
p.onCatalog?.(parseRegionDefinitions(next))
setNewName('')
setCatalogError('')
} catch {
setCatalogError('区域名称无效,请核对长度、重名或保留名称')
}
}
return (
<section className="qa-regions" aria-label="区域归属配置">
<div className="qa-region-heading">
@@ -85,7 +123,6 @@ export function RegionMappingView(p: Props) {
{p.state.revision_id || '尚未保存'}
</p>
) : null}
{p.error ? (
<p role="alert" className="error-banner">
{p.error}
@@ -98,13 +135,66 @@ export function RegionMappingView(p: Props) {
<p className="qa-ops-empty">暂无通讯录,请先同步。</p>
) : (
<>
<section className="qa-region-catalog" aria-label="区域设置">
<h4>区域设置</h4>
{catalogError ? (
<p role="alert" className="error-banner">
{catalogError}
</p>
) : null}
<div className="qa-region-names">
{regions.map((r) => (
<label key={r.id}>
区域名称
<input
aria-label={`区域名称 ${r.id}`}
value={r.name}
maxLength={100}
disabled={p.busy}
onChange={(e) =>
p.onCatalog?.(regions.map((x) => (x.id === r.id ? { ...x, name: e.target.value } : x)))
}
/>
</label>
))}
</div>
<form
onSubmit={(e) => {
e.preventDefault()
add()
}}
className="qa-region-add"
>
<input
aria-label="新区域名称"
value={newName}
maxLength={100}
disabled={p.busy}
placeholder="输入业务区域名称"
onChange={(e) => setNewName(e.target.value)}
/>
<button
disabled={
p.busy || !newName.trim() || regions.length >= 100 || regions.some((r) => r.name === newName.trim())
}
>
添加区域
</button>
</form>
</section>
<div className="qa-region-filters">
<label>
选择区域
<select value={p.selectedRegion} disabled={p.busy} onChange={(e) => p.onRegion(e.target.value)}>
{regionNames.map((r) => (
<option key={r}>{r}</option>
))}
{regions.length ? (
regions.map((r) => (
<option key={r.id} value={r.id}>
{r.name}
</option>
))
) : (
<option value="">请先添加区域</option>
)}
</select>
</label>
<label>
@@ -116,6 +206,47 @@ export function RegionMappingView(p: Props) {
placeholder="输入部门名称或完整路径"
/>
</label>
<label>
部门层级
<select aria-label="部门层级" value={level} onChange={(e) => setLevel(e.target.value)}>
<option value="">全部层级</option>
{Array.from({ length: maximum }, (_, i) => (
<option key={i} value={i + 1}>
第 {i + 1} 级
</option>
))}
</select>
</label>
</div>
<div className="qa-region-bulk">
<label>
<input
type="checkbox"
aria-label="全选当前列表"
disabled={p.busy || !departments.length}
checked={departments.length > 0 && departments.every((r) => selected.includes(r.department.id))}
onChange={(e) => {
const visible = new Set(departments.map((r) => r.department.id))
p.onSelect?.(
e.target.checked
? [...new Set([...selected, ...visible])]
: selected.filter((id) => !visible.has(id))
)
}}
/>
全选当前列表
</label>
<span>已勾选 {selected.length} 个部门</span>
<button
type="button"
disabled={p.busy || !selected.length || !regions.some((r) => r.id === p.selectedRegion)}
onClick={p.onApply}
>
批量归属
</button>
<button type="button" disabled={p.busy || !selected.length} onClick={() => p.onSelect?.([])}>
取消本次勾选
</button>
<button type="button" disabled={p.busy || !p.dirty} onClick={p.onPreview}>
预览影响
</button>
@@ -127,18 +258,16 @@ export function RegionMappingView(p: Props) {
<div className="qa-region-preview" aria-label="归属人数预览">
<strong>{p.preview ? '预览结果(未保存)' : '已保存归属'}</strong>
<span>通讯录人数:{display.view.total_people}</span>
{regionNames.map((r) => (
<span key={r}>
{r}:{display.view.regions[r]}
{organizationRegions(display).map((r) => (
<span key={r.id}>
{r.name}:{display.view.regions[r.id]}
</span>
))}
<span>未归属:{display.view.unassigned_people}</span>
<span>跨区域待核对:{display.view.conflicting_people}</span>
</div>
) : null}
{p.dirty && !p.preview ? (
<p className="qa-ops-muted">勾选已修改,人数仍为已保存结果;请预览后保存。</p>
) : null}
{p.dirty && !p.preview ? <p className="qa-ops-muted">人数仍为已保存结果;请预览后保存。</p> : null}
{display?.view.missing_departments.length ? (
<div role="alert" className="qa-ops-warning">
部分已配置部门不在当前通讯录中,原规则保留,需核对:
@@ -156,29 +285,60 @@ export function RegionMappingView(p: Props) {
<table className="qa-region-table">
<thead>
<tr>
<th>直接配置</th>
<th>选择</th>
<th>一级部门</th>
<th>二级部门</th>
<th>三级部门</th>
<th>四级及更深</th>
<th>完整部门路径</th>
<th>直接配置</th>
<th>{p.dirty && !p.preview ? '已保存归属' : '生效归属'}</th>
<th>依据</th>
</tr>
</thead>
<tbody>
{departments.map((d) => (
<tr key={d.id}>
<td>
<input
type="checkbox"
aria-label={`将 ${d.path} 直接归入 ${p.selectedRegion}`}
checked={p.rules.some((r) => r.department_id === d.id && r.region === p.selectedRegion)}
disabled={p.busy}
onChange={(e) => p.onToggle(d.id, e.target.checked)}
/>
</td>
<td>{d.path}</td>
<td>{d.region || '未归属'}</td>
<td>{d.explicit ? '直接配置' : d.inherited_from ? '继承上级' : '未配置'}</td>
</tr>
))}
{departments.map(({ department: d, levels }) => {
const direct = p.rules.find((r) => r.department_id === d.id)
return (
<tr key={d.id} data-department-id={d.id}>
<td>
<input
type="checkbox"
aria-label={`选择部门 ${d.path}`}
checked={selected.includes(d.id)}
disabled={p.busy}
onChange={(e) => choose(d.id, e.target.checked)}
/>
</td>
<td>{levels[0] || '—'}</td>
<td>{levels[1] || '—'}</td>
<td>{levels[2] || '—'}</td>
<td>{levels.slice(3).join(' / ') || '—'}</td>
<td>{d.path}</td>
<td>
{direct ? (
<>
<span>{label(direct.region)}</span>
<button
type="button"
aria-label={`清除直接配置 ${d.path}`}
disabled={p.busy}
onClick={() => p.onToggle(d.id, false)}
>
清除
</button>
</>
) : (
'—'
)}
</td>
<td>{d.region ? label(d.region, organizationRegions(display)) : '未归属'}</td>
<td className="qa-region-basis">
{d.explicit ? '直接配置' : d.inherited_from ? '继承上级' : '未配置'}
</td>
</tr>
)
})}
</tbody>
</table>
{!departments.length ? <p>没有匹配的部门路径</p> : null}
@@ -188,8 +348,8 @@ export function RegionMappingView(p: Props) {
</section>
)
}
function signature(rules: RegionRule[]) {
return JSON.stringify([...rules].sort((a, b) => a.department_id.localeCompare(b.department_id)))
function signature(rules: RegionRule[], regions: RegionDefinition[]) {
return JSON.stringify({ rules: [...rules].sort((a, b) => a.department_id.localeCompare(b.department_id)), regions })
}
export function QARegions() {
const [syncStatus, setSyncStatus] = useState<DirectorySyncStatus | null>(null),
@@ -203,7 +363,9 @@ export function QARegions() {
const [state, setState] = useState<Organization | null>(null),
[preview, setPreview] = useState<Organization | null>(null),
[rules, setRules] = useState<RegionRule[]>([])
const [region, setRegion] = useState<string>(regionNames[0]),
const [regions, setRegions] = useState<RegionDefinition[]>([]),
[selected, setSelected] = useState<string[]>([])
const [region, setRegion] = useState<string>(''),
[search, setSearch] = useState(''),
[busy, setBusy] = useState(false),
[error, setError] = useState(''),
@@ -212,6 +374,11 @@ export function QARegions() {
const accept = (value: Organization) => {
setState(value)
setRules(value.rules)
setRegions(organizationRegions(value))
setRegion((current) =>
organizationRegions(value).some((r) => r.id === current) ? current : (organizationRegions(value)[0]?.id ?? '')
)
setSelected([])
setPreview(null)
}
const load = async () => {
@@ -238,7 +405,7 @@ export function QARegions() {
void load()
return () => request.current?.abort()
}, [])
const dirty = Boolean(state && signature(rules) !== signature(state.rules))
const dirty = Boolean(state && signature(rules, regions) !== signature(state.rules, organizationRegions(state)))
dirtyRef.current = dirty
const loadRef = useRef(load)
loadRef.current = load
@@ -320,8 +487,8 @@ export function QARegions() {
setNotice('')
try {
const value = await (save
? saveOrganization(state, rules, controller.signal)
: previewOrganization(state, rules, controller.signal))
? saveOrganization({ ...state, regions: parseRegionDefinitions(regions) }, rules, controller.signal)
: previewOrganization({ ...state, regions: parseRegionDefinitions(regions) }, rules, controller.signal))
if (!controller.signal.aborted) {
if (save) {
const readback = await fetchOrganization(controller.signal)
@@ -342,6 +509,16 @@ export function QARegions() {
}
return (
<RegionMappingView
regions={regions}
selectedIDs={selected}
onCatalog={(next) => {
setRegions(next)
setPreview(null)
setNotice('')
setError('')
}}
onSelect={setSelected}
onApply={() => edit(applyBulkRegionRules(rules, selected, region))}
syncStatus={syncStatus}
syncError={syncError}
syncSubmitting={syncSubmitting}