/** 企业信息管理:前后端字段与分页格式适配 */ import { formatDate, formatDateTime, toApiDate, toApiDateTime } from "../../utils/dateFormat"; const GENDER_NAME = { 1: "男", 2: "女" }; const RESIGN_AUDIT_STATUS_NAME = { 0: "未审核", 1: "已审核", 2: "已退回" }; export function toPageQuery(params = {}, fieldMap = {}) { const query = { current: params.pageIndex ?? params.current ?? 1, size: params.pageSize ?? params.size ?? 10, }; Object.entries(fieldMap).forEach(([from, to]) => { const value = params[from]; if (value !== undefined && value !== null && value !== "") { query[to] = value; } }); return query; } export function fromPageResponse(res, mapItem) { if (!res) { return { success: false, data: [], totalCount: 0 }; } const list = Array.isArray(res.data) ? res.data : []; return { ...res, data: mapItem ? list.map(mapItem) : list, totalCount: res.totalCount ?? res.total ?? list.length, }; } export function fromSingleResponse(res, mapItem) { if (!res) { return { success: false, data: null }; } return { ...res, data: res.data && mapItem ? mapItem(res.data) : res.data, }; } function pick(obj, keys) { const next = {}; keys.forEach((key) => { if (obj[key] !== undefined) { next[key] = obj[key]; } }); return next; } // ─── 机构信息 ─── export function toOrgInfoForm(data = {}) { return { id: data.id, orgName: data.unitName, creditCode: data.creditCode, safetyIndustryCategory: data.safetyIndustryCategoryName, regionCountyName: data.districtName, regionStreetName: data.townStreet, regionCommunityName: data.villageCommunity, longitude: data.longitude, latitude: data.latitude, registerAddress: data.registerAddress, businessAddress: data.businessAddress, ownershipType: data.ownershipTypeName, gbIndustryCode: data.economyIndustryCode, legalRepresentative: data.legalRepresentative, legalRepPhone: data.legalRepresentativePhone, principal: data.principalName, principalPhone: data.principalPhone, safetyDeptHead: data.safetyDeptManager, safetyDeptPhone: data.safetyDeptManagerPhone, safetyVpPhone: data.safetyDeputyPhone, productionDate: data.productionDate, businessStatus: data.businessStatusName, disclosureUrl: data.infoDisclosureUrl, workplaceArea: data.workplaceArea, archiveRoomArea: data.archiveRoomArea, fullTimeEvaluatorCount: data.fulltimeEvaluatorCount, registeredSafetyEngineerCount: data.registeredEngineerCount, authStatusCode: data.authStatusCode, }; } export function fromOrgInfoForm(values = {}, options = {}) { const { isDraft = false } = options; return { ...pick(values, ["id", "tenantId"]), unitName: values.orgName, creditCode: values.creditCode, safetyIndustryCategoryName: values.safetyIndustryCategory, districtName: values.regionCountyName, townStreet: values.regionStreetName, villageCommunity: values.regionCommunityName, longitude: values.longitude, latitude: values.latitude, registerAddress: values.registerAddress, businessAddress: values.businessAddress, ownershipTypeName: values.ownershipType, economyIndustryCode: values.gbIndustryCode, legalRepresentative: values.legalRepresentative, legalRepresentativePhone: values.legalRepPhone, principalName: values.principal, principalPhone: values.principalPhone, safetyDeptManager: values.safetyDeptHead, safetyDeptManagerPhone: values.safetyDeptPhone, safetyDeputyPhone: values.safetyVpPhone, productionDate: values.productionDate, businessStatusName: values.businessStatus, infoDisclosureUrl: values.disclosureUrl, workplaceArea: values.workplaceArea, archiveRoomArea: values.archiveRoomArea, fulltimeEvaluatorCount: values.fullTimeEvaluatorCount, registeredEngineerCount: values.registeredSafetyEngineerCount, authStatusCode: isDraft ? 0 : 1, authStatusName: isDraft ? "草稿" : "已提交", }; } // ─── 部门 / 岗位 ─── export function toDepartmentForm(data = {}) { return { id: data.id, parentId: data.parentId, deptName: data.deptName, leaderName: data.managerName, leaderAccount: data.managerAccount, deptLevel: data.deptLevelName ?? data.deptLevelCode, }; } export function fromDepartmentForm(values = {}) { return { ...pick(values, ["id", "parentId", "tenantId"]), deptName: values.deptName, managerName: values.leaderName, managerAccount: values.leaderAccount, deptLevelName: values.deptLevel, deptLevelCode: values.deptLevel, }; } export function buildDepartmentTree(departments = []) { const map = new Map(); departments.forEach((item) => { map.set(item.id, { ...toDepartmentForm(item), children: [] }); }); const roots = []; departments.forEach((item) => { const node = map.get(item.id); if (item.parentId != null && item.parentId !== 0 && map.has(item.parentId)) { map.get(item.parentId).children.push(node); } else { roots.push(node); } }); return roots; } export function toPositionForm(data = {}) { return { id: data.id, deptId: data.deptId, deptName: data.deptName, positionName: data.positionName, dutyDesc: data.dutyDesc, remark: data.remark ?? data.remarks, }; } export function fromPositionForm(values = {}) { const deptId = values.deptId != null && values.deptId !== "" ? Number(values.deptId) : undefined; return { ...pick(values, ["id", "tenantId"]), deptId, positionName: values.positionName, dutyDesc: values.dutyDesc, remark: values.remark, }; } // ─── 人员 ─── export function toStaffForm(data = {}) { return { id: data.id, staffName: data.userName, account: data.account, gender: data.genderCode, birthDate: data.birthDate, deptId: data.deptId, positionId: data.postId, idCardNo: data.idCardNo, homeAddress: data.currentAddress, officeAddress: data.officeAddress, education: data.educationName ?? data.educationCode, graduateSchool: data.graduateSchool, major: data.major, employmentStatus: data.employmentStatusCode, deptName: data.deptName, positionName: data.postName ?? data.positionName, certNames: data.certNames, }; } export function fromStaffForm(values = {}) { const genderCode = values.gender; return { ...pick(values, ["id", "tenantId", "deptId", "postId"]), userName: values.staffName, account: values.account, genderCode, genderName: GENDER_NAME[genderCode] ?? values.genderName, birthDate: toApiDate(values.birthDate), postId: values.positionId ?? values.postId, idCardNo: values.idCardNo, currentAddress: values.homeAddress, officeAddress: values.officeAddress, educationName: values.education, educationCode: values.education, graduateSchool: values.graduateSchool, major: values.major, employmentStatusCode: values.employmentStatus ?? 1, employmentStatusName: values.employmentStatus === 2 ? "离职" : "在职", }; } // ─── 人员证书 ─── export function toStaffCertForm(data = {}) { const certImgs = data.certAttachmentUrl ? [{ url: data.certAttachmentUrl, fileName: data.certName }] : data.certImgFiles || []; return { id: data.id, staffId: data.personnelId, certName: data.certName, certCategory: data.certCategoryName ?? data.certCategoryCode, certWorkCategory: data.operationCategoryName ?? data.operationCategoryCode, certNo: data.certNo, issueOrg: data.issueOrg, validStartDate: data.validStartDate, validEndDate: data.validEndDate, reviewDate: data.reviewDate, certImgFiles: certImgs, certImgs, }; } export function fromStaffCertForm(values = {}) { const certAttachmentUrl = values.certAttachmentUrl || values.certImgs?.[0]?.url || values.certImgFiles?.[0]?.url; return { ...pick(values, ["id", "tenantId"]), personnelId: values.staffId, certName: values.certName, certCategoryName: values.certCategory, certCategoryCode: values.certCategory, operationCategoryName: values.certWorkCategory, operationCategoryCode: values.certWorkCategory, certNo: values.certNo, issueOrg: values.issueOrg, validStartDate: values.validStartDate, validEndDate: values.validEndDate, reviewDate: values.reviewDate, certAttachmentUrl, }; } // ─── 机构资质证书 ─── export function toQualificationForm(data = {}) { const certImgs = data.certImageUrl ? [{ url: data.certImageUrl, fileName: data.certName }] : data.certImgFiles || []; return { id: data.id, certType: data.licenseTypeName ?? data.licenseTypeCode, certName: data.certName, certNo: data.certNo, issueOrg: data.issueOrg, issueDate: data.issueDate, validStartDate: data.validStartDate, validEndDate: data.validEndDate, remark: data.remark ?? data.remarks, enableStatus: data.enableFlag, issueDate: formatDate(data.issueDate), validStartDate: formatDate(data.validStartDate), validEndDate: formatDate(data.validEndDate), certImgFiles: certImgs, certImgs, }; } export function fromQualificationForm(values = {}) { const certImageUrl = values.certImageUrl || values.certImgs?.[0]?.url || values.certImgFiles?.[0]?.url; return { ...pick(values, ["id", "tenantId"]), licenseTypeName: values.certType, licenseTypeCode: values.certType, certName: values.certName, certNo: values.certNo, issueOrg: values.issueOrg, issueDate: toApiDate(values.issueDate), validStartDate: toApiDate(values.validStartDate ?? values.validDate?.[0]), validEndDate: toApiDate(values.validEndDate ?? values.validDate?.[1]), certImageUrl, remark: values.remark, enableFlag: values.enableStatus ?? 1, }; } // ─── 装备 ─── export function toEquipForm(data = {}) { const instrumentType = data.instrumentTypeName ?? data.instrumentTypeCode; return { id: data.id, instrumentCategory: instrumentType, instrumentType, deviceType: data.deviceTypeName ?? data.deviceTypeCode, deviceName: data.deviceName, model: data.deviceModel, flowDesc: data.flowDesc, manufacturer: data.manufacturer, minFlow: data.minFlow, maxFlow: data.maxFlow, calibrationUnit: data.calibrationUnit, calibrationInitialValue: data.calibrationInitValue, onSiteCalibrationType: data.fieldCalibrationTypeName ?? data.fieldCalibrationTypeCode, isDualChannel: data.dualChannelFlag, enableStatus: data.enableFlag, }; } export function fromEquipForm(values = {}) { return { ...pick(values, ["id", "tenantId"]), deviceName: values.deviceName, deviceModel: values.model, instrumentTypeName: values.instrumentCategory, instrumentTypeCode: values.instrumentCategory, deviceTypeName: values.deviceType, deviceTypeCode: values.deviceType, manufacturer: values.manufacturer, flowDesc: values.flowDesc, minFlow: values.minFlow, maxFlow: values.maxFlow, calibrationUnit: values.calibrationUnit, calibrationInitValue: values.calibrationInitialValue, fieldCalibrationTypeName: values.onSiteCalibrationType, fieldCalibrationTypeCode: values.onSiteCalibrationType, dualChannelFlag: values.isDualChannel, enableFlag: values.enableStatus ?? 1, }; } // ─── 人员变更 / 离职 ─── export function toChangeLogForm(data = {}) { return { id: data.id, staffId: data.personnelId, changeItem: data.changeItem, changeTime: formatDateTime(data.changeTime), createBy: data.operatorName, }; } export function toResignApplyForm(data = {}) { const auditStatus = data.auditStatusCode ?? data.auditStatus ?? 0; return { id: data.id, personnelId: data.personnelId, applicantName: data.applicantName, applyTime: formatDateTime(data.applyTime), expectedLeaveDate: formatDate(data.expectedResignDate), leaveReason: data.resignReason, remark: data.remark ?? data.remarks, rejectReason: data.rejectReason, auditStatus, auditStatusName: data.auditStatusName ?? RESIGN_AUDIT_STATUS_NAME[auditStatus] ?? "未审核", attachmentId: data.reportFileUrl, account: data.account, deptName: data.deptName, }; } export function fromResignApplyForm(values = {}) { const personnelId = values.personnelId; return { ...pick(values, ["id", "tenantId"]), personnelId: personnelId != null && personnelId !== "" ? Number(personnelId) : undefined, applicantName: values.applicantName, applyTime: toApiDateTime(values.applyTime), expectedResignDate: toApiDate(values.expectedLeaveDate), resignReason: values.leaveReason, remark: values.remark, reportFileUrl: values.attachmentId ?? values.reportFileUrl, auditStatusCode: values.auditStatus ?? 0, auditStatusName: values.auditStatus === 1 ? "已审核" : values.auditStatus === 2 ? "已退回" : "未审核", }; } export function fromResignAuditForm(values = {}) { const auditStatus = values.auditStatus; return { id: values.id, personnelId: values.personnelId, auditStatusCode: auditStatus, auditStatusName: auditStatus === 1 ? "已审核" : auditStatus === 2 ? "已退回" : "未审核", rejectReason: values.rejectReason, }; } export function toStaffChangeSummary(personnel = {}) { const employmentCode = Number(personnel.employmentStatusCode ?? personnel.employmentStatus ?? 1); const hasResignApply = personnel.resignApplyId != null && personnel.resignApplyId !== ""; const resignStatusRaw = personnel.resignAuditStatus; let resignStatus = null; if (hasResignApply) { resignStatus = resignStatusRaw === null || resignStatusRaw === undefined ? 0 : Number(resignStatusRaw); } return { id: personnel.id, staffId: personnel.id, account: personnel.account, staffName: personnel.userName ?? personnel.staffName, deptName: personnel.deptName || "", employmentStatus: employmentCode, employmentStatusCode: employmentCode, employmentStatusName: personnel.employmentStatusName || (employmentCode === 2 ? "离职" : "在职"), changeCount: Number(personnel.changeCount ?? 0), resignApplyId: personnel.resignApplyId, resignAuditStatus: resignStatus, resignAuditStatusName: personnel.resignAuditStatusName ?? (resignStatus !== null ? RESIGN_AUDIT_STATUS_NAME[resignStatus] : undefined), }; }