safetyCultureActivityManagement.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. <template>
  2. <div class="safety-platform-container">
  3. <header class="safety-platform-container__header">
  4. <div class="breadcrumb-title"> 安全文化活动管理 </div>
  5. </header>
  6. <main class="safety-platform-container__main">
  7. <div class="search-table-container">
  8. <header>
  9. <div style="position: relative">
  10. <el-button type="primary" :icon="Plus" class="search-table-container--button" @click="handleCreate">
  11. 新增
  12. </el-button>
  13. <el-button plain class="search-table-container--button" @click="handleImport"> 导入 </el-button>
  14. </div>
  15. <div class="act-search">
  16. <section class="select-box">
  17. <div class="select-box--item">
  18. <span>行动项内容/计划名称:</span>
  19. <el-input
  20. v-model="queryParams.keyword"
  21. placeholder="搜索行动项内容/计划名称"
  22. class="act-search-input"
  23. />
  24. </div>
  25. <div class="select-box--item">
  26. <span>状态:</span>
  27. <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
  28. <el-option label="未下发" :value="1" />
  29. <el-option label="待反馈" :value="2" />
  30. <el-option label="已完成" :value="3" />
  31. </el-select>
  32. </div>
  33. <div class="select-box--item">
  34. <span>分类名称:</span>
  35. <el-select v-model="queryParams.classifyName" placeholder="请选择分类" clearable>
  36. <el-option label="安全综合工作" value="安全综合工作" />
  37. <el-option label="生产安全工作" value="生产安全工作" />
  38. </el-select>
  39. </div>
  40. <div class="select-box--item">
  41. <span>计划日期范围:</span>
  42. <el-date-picker
  43. v-model="uploadDateRange"
  44. type="daterange"
  45. range-separator="至"
  46. start-placeholder="开始日期"
  47. end-placeholder="结束日期"
  48. value-format="YYYY-MM-DD"
  49. format="YYYY-MM-DD"
  50. />
  51. </div>
  52. </section>
  53. <section class="search-btn">
  54. <el-button type="primary" @click="handleSearch">查询</el-button>
  55. <el-button @click="handleReset">重置</el-button>
  56. <el-button plain @click="handleDownload"> 导出 </el-button>
  57. </section>
  58. </div>
  59. </header>
  60. <div class="batch-table">
  61. <BasicTable
  62. ref="basicTableRef"
  63. :tableData="tableData"
  64. :tableConfig="tableConfig"
  65. @update:pageSize="handleSizeChange"
  66. @update:pageNumber="handleCurrentChange"
  67. >
  68. <!-- <template #actionContent="scope">
  69. <span class="action-content" @click="activityRegistration(scope.row.id)">
  70. {{ scope.row.actionContent || '-'}}
  71. </span>
  72. </template> -->
  73. <template #action="scope">
  74. <div class="action-container--div" style="justify-content: left">
  75. <ActionButton text="编辑" v-if="scope.row.status === 1" @click="handleEdit(scope.row.id)" />
  76. <ActionButton
  77. text="删除"
  78. v-if="scope.row.status === 1"
  79. :popconfirm="{
  80. title: '确定要删除?',
  81. }"
  82. @confirm="handleDelete(scope.row.id)"
  83. />
  84. <ActionButton text="查看" @click="handleView(scope.row.id)" />
  85. <ActionButton text="下发" @click="handleDispatch(scope.row.id)" v-if="scope.row.status === 1" />
  86. <ActionButton
  87. text="活动报名"
  88. @click="activityRegistration(scope.row.id)"
  89. v-if="scope.row.status !== 1"
  90. />
  91. </div>
  92. </template>
  93. </BasicTable>
  94. </div>
  95. </div>
  96. <el-dialog
  97. v-model="showIssueDialog"
  98. title="安全文化活动下发"
  99. width="500px"
  100. destroy-on-close
  101. @open="onIssueDialogOpen"
  102. >
  103. <el-form ref="issueFormRef" :model="issueForm" :rules="issueRules" label-width="140px">
  104. <el-form-item label="具体负责人部门" prop="rectificationDepartmentId">
  105. <el-cascader
  106. v-model="issueForm.rectificationDepartmentId"
  107. :options="issueDeptTree"
  108. :props="cascaderDeptProp"
  109. :show-all-levels="false"
  110. placeholder="请选择具体负责人部门"
  111. filterable
  112. clearable
  113. style="width: 100%"
  114. @change="onIssueDeptChange"
  115. />
  116. </el-form-item>
  117. <el-form-item label="具体负责人" prop="rectificationResponsibleUserId">
  118. <el-select
  119. v-model="issueForm.rectificationResponsibleUserId"
  120. placeholder="选择具体负责人"
  121. filterable
  122. clearable
  123. style="width: 100%"
  124. >
  125. <el-option
  126. v-for="user in issueUserList"
  127. :key="user.id"
  128. :label="user.realname ?? user.username"
  129. :value="user.id"
  130. />
  131. </el-select>
  132. </el-form-item>
  133. <el-form-item label="计划开始日期:" prop="startDate">
  134. <el-date-picker
  135. v-model="issueForm.startDate"
  136. type="date"
  137. placeholder="请选择计划开始日期"
  138. value-format="YYYY-MM-DD"
  139. format="YYYY-MM-DD"
  140. style="width: 100%"
  141. :disabled-date="(date: Date) => {
  142. if (issueForm.endDate) {
  143. return date > new Date(issueForm.endDate);
  144. }
  145. return false;
  146. }"
  147. />
  148. </el-form-item>
  149. <el-form-item label="计划结束日期:" prop="endDate">
  150. <el-date-picker
  151. v-model="issueForm.endDate"
  152. type="date"
  153. placeholder="请选择计划结束日期"
  154. value-format="YYYY-MM-DD"
  155. format="YYYY-MM-DD"
  156. style="width: 100%"
  157. :disabled-date="(date: Date) => {
  158. if (issueForm.startDate) {
  159. return date < new Date(issueForm.startDate);
  160. }
  161. return false;
  162. }"
  163. />
  164. </el-form-item>
  165. </el-form>
  166. <template #footer>
  167. <el-button @click="showIssueDialog = false">取消</el-button>
  168. <el-button type="primary" @click="handleIssueSave">保存</el-button>
  169. </template>
  170. </el-dialog>
  171. </main>
  172. <BatchImport
  173. v-if="batchImportVisible"
  174. :visible="batchImportVisible"
  175. :import-api-url="importApiUrl"
  176. :template-url="templateUrl"
  177. template-name="安全文化活动管理导入模版"
  178. :show-template="true"
  179. @close="batchImportVisible = false"
  180. @update="handleUpdate"
  181. />
  182. </div>
  183. </template>
  184. <script setup lang="ts">
  185. import { onMounted, reactive, ref } from 'vue';
  186. import { ElMessage } from 'element-plus';
  187. import { Plus } from '@element-plus/icons-vue';
  188. import BasicTable from '@/components/BasicTable.vue';
  189. import useTableConfig from '@/hooks/useTableConfigHook';
  190. import ActionButton from '@/components/ActionButton.vue';
  191. import { TABLE_OPTIONS, INVENTORY_TABLE_COLUMNS } from './configs/tables';
  192. import { useRouter } from 'vue-router';
  193. import {
  194. safetyCultureActivityManagementFilePage,
  195. deleteSafetyCultureActivityManagement,
  196. getAllDepartments,
  197. activityDistribution,
  198. safetyCultureActivityExport,
  199. type safetyCultureFileQuery,
  200. type safetyCultureFilePageQuery,
  201. } from '@/api/safety-culture';
  202. import type { DeptTree } from '@/types/dept/type';
  203. import { downloadByData } from '@/utils/file/download';
  204. import BatchImport from '@/components/batch-import/BatchImport.vue';
  205. import { useGlobSetting } from '@/hooks/setting';
  206. import urlJoin from 'url-join';
  207. import { queryAvailableUserList } from '@/api/production-safety/responsibility-implementation';
  208. const router = useRouter();
  209. /** 下发弹窗:点击下发打开弹窗,弹窗内部门用 queryAllDeptTree、负责人用 queryAvailableUserList,点击保存才触发下发接口 */
  210. const showIssueDialog = ref(false);
  211. const distributionId = ref<number>(0);
  212. const issueFormRef = ref();
  213. const issueForm = ref({
  214. rectificationDepartmentId: undefined as number | undefined,
  215. rectificationResponsibleUserId: undefined as number | undefined,
  216. rectificationResponsiblePersonName: '' as string,
  217. startDate: '' as string,
  218. endDate: '' as string,
  219. });
  220. const issueRules = {
  221. rectificationDepartmentId: [{ required: true, message: '请选择整改责任部门', trigger: 'change' }],
  222. rectificationResponsibleUserId: [{ required: true, message: '请选择整改负责人', trigger: 'change' }],
  223. startDate: [{ required: true, message: '请选择计划开始日期', trigger: 'change' }],
  224. endDate: [{ required: true, message: '请选择计划结束日期', trigger: 'change' }],
  225. };
  226. /** 下发弹窗部门树,与新增隐患台账复查人员所属部门一致(getAllDepartments 取第一级 children) */
  227. const cascaderDeptProp = {
  228. checkStrictly: true,
  229. expandTrigger: 'hover' as const,
  230. value: 'id',
  231. label: 'deptName',
  232. emitPath: false,
  233. };
  234. const issueDeptTree = ref<DeptTree[]>([]);
  235. const issueUserList = ref<Array<{ id: number; realname?: string; username?: string }>>([]);
  236. const rectificationResponsibleDeptName = ref('');
  237. // 表格
  238. const basicTableRef = ref<InstanceType<typeof BasicTable>>();
  239. const { tableConfig, pagination } = useTableConfig(INVENTORY_TABLE_COLUMNS, TABLE_OPTIONS);
  240. const tableData = ref<any[]>([]);
  241. const deptNameMap = ref<Record<string, string>>({});
  242. const CATEGORY_NAME_MAP: Record<string, string> = {
  243. '0': '外部院级文件',
  244. '1': '内部院级文件',
  245. '2': '内部院级文件',
  246. };
  247. const normalizeCategoryName = (row: any): string => {
  248. const raw = row?.categoryName ?? row?.classifyName ?? row?.category;
  249. if (raw == null || raw === '') {
  250. return '-';
  251. }
  252. const stringValue = String(raw);
  253. if (stringValue.includes('外部') || stringValue.includes('内部')) {
  254. return stringValue;
  255. }
  256. return CATEGORY_NAME_MAP[stringValue] || stringValue;
  257. };
  258. const normalizeListText = (value: unknown): string => {
  259. if (Array.isArray(value)) {
  260. const list = value.map((item) => String(item).trim()).filter((item) => item);
  261. return list.length ? list.join('、') : '-';
  262. }
  263. if (value == null || value === '') {
  264. return '-';
  265. }
  266. const text = String(value).trim();
  267. if (!text) {
  268. return '-';
  269. }
  270. return text.includes(',')
  271. ? text
  272. .split(',')
  273. .map((item) => item.trim())
  274. .filter((item) => item)
  275. .join('、') || '-'
  276. : text;
  277. };
  278. const parseIdList = (value: unknown): string[] => {
  279. if (Array.isArray(value)) {
  280. return value.map((item) => String(item).trim()).filter((item) => item);
  281. }
  282. if (value == null || value === '') {
  283. return [];
  284. }
  285. const text = String(value).trim();
  286. if (!text) {
  287. return [];
  288. }
  289. return text
  290. .split(',')
  291. .map((item) => item.trim())
  292. .filter((item) => item);
  293. };
  294. const flattenDeptTree = (tree: DeptTree[]): DeptTree[] => {
  295. const result: DeptTree[] = [];
  296. const walk = (nodes: DeptTree[]) => {
  297. nodes.forEach((node) => {
  298. result.push(node);
  299. if (node.children?.length) {
  300. walk(node.children);
  301. }
  302. });
  303. };
  304. walk(tree || []);
  305. return result;
  306. };
  307. const loadDeptNameMap = async () => {
  308. try {
  309. const deptTree = await getAllDepartments();
  310. const flatList = flattenDeptTree(deptTree || []);
  311. const map: Record<string, string> = {};
  312. flatList.forEach((dept) => {
  313. if (dept.id != null) {
  314. map[String(dept.id)] = dept.deptName;
  315. }
  316. });
  317. deptNameMap.value = map;
  318. } catch (error) {
  319. console.error('加载部门字典失败:', error);
  320. deptNameMap.value = {};
  321. }
  322. };
  323. const normalizeCooperateDeptName = (row: any): string => {
  324. const rawName = row?.cooperateDeptNames;
  325. if (rawName) {
  326. return normalizeListText(rawName);
  327. }
  328. const ids = parseIdList(row?.cooperateDeptIds);
  329. if (!ids.length) {
  330. return '-';
  331. }
  332. const names = ids.map((id) => deptNameMap.value[id] || id).filter((item) => item);
  333. return names.length ? names.join('、') : '-';
  334. };
  335. const normalizeResponsibleDeptName = (row: any): string => {
  336. const rawName = row?.responsibleDeptName;
  337. if (rawName) {
  338. return normalizeListText(rawName);
  339. }
  340. const ids = parseIdList(row?.responsibleDeptId);
  341. if (!ids.length) {
  342. return '-';
  343. }
  344. const names = ids.map((id) => deptNameMap.value[id] || id).filter((item) => item);
  345. return names.length ? names.join('、') : '-';
  346. };
  347. const queryParams = reactive<safetyCultureFileQuery>({
  348. keyword: '', // 文件名称/编号(模糊查询)
  349. status: undefined, // 状态:1-启用,0-禁用
  350. classifyName: '', // 分类名称:外部院级文件/内部院级文件
  351. startDate: '', // 上传日期范围-开始日期
  352. endDate: '', // 上传日期范围-结束日期
  353. });
  354. // 上传日期范围(用于日期选择器)
  355. const uploadDateRange = ref<[string, string] | null>(null);
  356. const handleSizeChange = (value: number) => {
  357. pagination.pageSize = value;
  358. getTableData();
  359. };
  360. const handleCurrentChange = (value: number) => {
  361. pagination.pageNumber = value;
  362. getTableData();
  363. };
  364. async function getTableData() {
  365. tableConfig.loading = true;
  366. try {
  367. const pageQuery: safetyCultureFilePageQuery = {
  368. pageNumber: pagination.pageNumber,
  369. pageSize: pagination.pageSize,
  370. queryParam: {
  371. keyword: queryParams.keyword || undefined,
  372. status: queryParams.status,
  373. classifyName: queryParams.classifyName || undefined,
  374. startDate: queryParams.startDate || undefined,
  375. endDate: queryParams.endDate || undefined,
  376. },
  377. };
  378. const res = await safetyCultureActivityManagementFilePage(pageQuery);
  379. if (res) {
  380. tableData.value = (res.records || []).map((item: any) => ({
  381. ...item,
  382. categoryNameDisplay: normalizeCategoryName(item),
  383. responsibleDeptNameDisplay: normalizeResponsibleDeptName(item),
  384. responsiblePersonNameDisplay: normalizeListText(
  385. item.responsiblePersonName || item.specificPersonName || item.responsiblePersonId,
  386. ),
  387. cooperateDeptNameDisplay: normalizeCooperateDeptName(item),
  388. }));
  389. pagination.total = res.totalRow || 0;
  390. }
  391. } catch (e) {
  392. console.error('获取院级文件列表失败:', e);
  393. tableData.value = [];
  394. pagination.total = 0;
  395. } finally {
  396. tableConfig.loading = false;
  397. }
  398. }
  399. const handleSearch = () => {
  400. // 处理日期范围
  401. if (uploadDateRange.value && uploadDateRange.value.length === 2) {
  402. queryParams.startDate = uploadDateRange.value[0];
  403. queryParams.endDate = uploadDateRange.value[1];
  404. } else {
  405. queryParams.startDate = '';
  406. queryParams.endDate = '';
  407. }
  408. pagination.pageNumber = 1;
  409. getTableData();
  410. };
  411. const handleReset = () => {
  412. queryParams.keyword = '';
  413. queryParams.status = undefined;
  414. queryParams.classifyName = '';
  415. queryParams.startDate = '';
  416. queryParams.endDate = '';
  417. uploadDateRange.value = null;
  418. handleSearch();
  419. };
  420. // 批量导入
  421. const batchImportVisible = ref(false);
  422. const { urlPrefix } = useGlobSetting();
  423. const importApiUrl = ref(urlJoin(urlPrefix, '/safetyCulture/activity/import'));
  424. const templateUrl = ref('./skyeye-file-upload/sfysecurity/TEMPLATE/安全文化活动导入模版.xlsx');
  425. const handleImport = () => {
  426. batchImportVisible.value = true;
  427. };
  428. const handleUpdate = () => {
  429. batchImportVisible.value = false;
  430. getTableData();
  431. };
  432. const handleDownload = async () => {
  433. try {
  434. const exportParams: safetyCultureFileQuery = {
  435. keyword: queryParams.keyword || undefined,
  436. status: queryParams.status,
  437. classifyName: queryParams.classifyName || undefined,
  438. startDate: queryParams.startDate || undefined,
  439. endDate: queryParams.endDate || undefined,
  440. };
  441. const response = await safetyCultureActivityExport(exportParams);
  442. if (response) {
  443. const fileName = `安全文化活动管理_${new Date().toISOString().split('T')[0]}.xlsx`;
  444. downloadByData(response, fileName);
  445. ElMessage.success('导出成功');
  446. }
  447. } catch (e) {
  448. console.error('导出安全文化活动文件失败:', e);
  449. ElMessage.error('导出失败,请重试');
  450. }
  451. };
  452. const handleCreate = () => {
  453. router.push({
  454. name: 'safetyCultureActivityManagementItem',
  455. query: {
  456. operate: 'safety-culture-material-create',
  457. },
  458. });
  459. };
  460. const handleEdit = (id: number) => {
  461. router.push({
  462. name: 'safetyCultureActivityManagementItem',
  463. query: {
  464. id,
  465. operate: 'safety-culture-material-edit',
  466. },
  467. });
  468. };
  469. const handleDelete = async (id: number) => {
  470. try {
  471. await deleteSafetyCultureActivityManagement(id);
  472. ElMessage.success('删除成功');
  473. getTableData();
  474. } catch (e) {
  475. console.error('删除安全文化活动失败:', e);
  476. ElMessage.error('删除失败,请重试');
  477. }
  478. };
  479. const handleView = (id: number) => {
  480. router.push({
  481. name: 'safetyCultureActivityManagementItem',
  482. query: {
  483. id,
  484. operate: 'safety-culture-material-view',
  485. },
  486. });
  487. };
  488. const activityRegistration = async (id: number) => {
  489. router.push({
  490. name: 'safetyCultureActivityManagementActivityRegistration',
  491. query: {
  492. id,
  493. operate: 'safety-culture-material-view',
  494. },
  495. });
  496. };
  497. const onIssueDialogOpen = async () => {
  498. try {
  499. const [deptRes, userRes] = await Promise.all([
  500. getAllDepartments(),
  501. queryAvailableUserList({ pageNumber: 1, pageSize: 9999, queryParam: {
  502. deptName: rectificationResponsibleDeptName.value,
  503. } }),
  504. ]);
  505. const fullTree = (deptRes as DeptTree[]) ?? [];
  506. issueDeptTree.value = Array.isArray(fullTree) && fullTree[0]?.children ? fullTree[0].children : [];
  507. issueUserList.value = (userRes as any)?.records ?? [];
  508. } catch (e) {
  509. console.error('获取部门/用户列表失败:', e);
  510. ElMessage.error(e?.message || e?.data || '加载部门或负责人列表失败');
  511. issueDeptTree.value = [];
  512. issueUserList.value = [];
  513. }
  514. };
  515. const onIssueDeptChange = (val: number | undefined) => {
  516. if (val) {
  517. issueForm.value.rectificationResponsibleUserId = undefined;
  518. const findDeptName = (tree: any[], id: number): string => {
  519. for (const node of tree) {
  520. if (node.id === id) {
  521. return node.deptName || node.label || '';
  522. }
  523. if (node.children) {
  524. const found = findDeptName(node.children, id);
  525. if (found) return found;
  526. }
  527. }
  528. return '';
  529. };
  530. const deptName = findDeptName(issueDeptTree.value, val);
  531. rectificationResponsibleDeptName.value = deptName;
  532. onIssueDialogOpen();
  533. }
  534. };
  535. const handleDispatch = (id: number) => {
  536. distributionId.value = id;
  537. showIssueDialog.value = true;
  538. };
  539. /** 从部门树中根据 id 查找部门名称 */
  540. function findDeptNameById(nodes: DeptTree[] | undefined, id: number): string {
  541. if (!nodes?.length) return '';
  542. for (const n of nodes) {
  543. if (n.id === id) return n.deptName ?? '';
  544. if (n.children?.length) {
  545. const found = findDeptNameById(n.children, id);
  546. if (found) return found;
  547. }
  548. }
  549. return '';
  550. }
  551. const handleIssueSave = async () => {
  552. await issueFormRef.value?.validate?.().catch(() => {});
  553. const { rectificationDepartmentId, rectificationResponsibleUserId } = issueForm.value;
  554. if (rectificationDepartmentId == null || rectificationResponsibleUserId == null) {
  555. ElMessage.warning('请选择整改责任部门和整改负责人');
  556. return;
  557. }
  558. if (!issueForm.value.startDate || !issueForm.value.endDate) {
  559. ElMessage.warning('请选择计划开始日期和计划结束日期');
  560. return;
  561. }
  562. const selectedUser = issueUserList.value.find((u) => u.id === rectificationResponsibleUserId);
  563. const personName = selectedUser?.realname ?? selectedUser?.username ?? '';
  564. const deptName = findDeptNameById(issueDeptTree.value, rectificationDepartmentId);
  565. try {
  566. await activityDistribution({
  567. id: distributionId.value,
  568. specificDeptId: String(rectificationDepartmentId),
  569. specificPersonId: String(rectificationResponsibleUserId),
  570. startTime: issueForm.value.startDate,
  571. endTime: issueForm.value.endDate,
  572. });
  573. ElMessage.success('下发成功');
  574. showIssueDialog.value = false;
  575. getTableData();
  576. } catch (e) {
  577. console.error('下发失败:', e);
  578. ElMessage.error(e?.message || e?.data || '下发失败,请重试');
  579. }
  580. };
  581. onMounted(() => {
  582. loadDeptNameMap().finally(() => {
  583. getTableData();
  584. });
  585. });
  586. </script>
  587. <style scoped lang="scss">
  588. @use '@/styles/page-details-layout.scss' as *;
  589. @use '@/styles/page-main-layout.scss' as *;
  590. @use '@/styles/basic-table-action.scss' as *;
  591. @use '@/views/traffic/violation/style/act-search-table.scss' as *;
  592. .action-content {
  593. color: #409eff;
  594. cursor: pointer;
  595. }
  596. </style>