import dayjs from "dayjs";
|
import { APPROVAL_MODULE_KEYS } from "./approvalModuleRegistry.js";
|
|
/** 支持审批单号查询/主表展示的审批申请模块 */
|
export const INSTANCE_NO_SEARCH_MODULE_KEYS = new Set([
|
APPROVAL_MODULE_KEYS.REGULAR,
|
APPROVAL_MODULE_KEYS.TRANSFER,
|
APPROVAL_MODULE_KEYS.WORK_HANDOVER,
|
APPROVAL_MODULE_KEYS.LEAVE,
|
APPROVAL_MODULE_KEYS.OVERTIME,
|
]);
|
|
export const INSTANCE_NO_TABLE_COLUMN = {
|
label: "审批单号",
|
prop: "instanceNo",
|
width: 170,
|
showOverflowTooltip: true,
|
};
|
|
/** 扁平化为 Spring GET 可绑定的 query(approvalInstanceDto.xxx,勿用方括号) */
|
export function appendDotNotationQuery(target, prefix, fields) {
|
if (!fields || typeof fields !== "object") return;
|
for (const [key, value] of Object.entries(fields)) {
|
if (value == null || value === "") continue;
|
target[`${prefix}.${key}`] = value;
|
}
|
}
|
|
function pickApplicantFromSearchForm(searchForm = {}) {
|
const out = {};
|
const sf = searchForm || {};
|
const name = (sf.applicantName || "").trim();
|
const kw = (sf.applicantKeyword || "").trim();
|
const id = sf.applicantId;
|
|
if (name) out.applicantName = name;
|
if (kw) {
|
out.applicantName = kw;
|
if (/^\d+$/.test(kw)) out.applicantId = Number(kw);
|
}
|
if (id != null && id !== "") {
|
out.applicantId = typeof id === "number" ? id : Number(id) || id;
|
}
|
return out;
|
}
|
|
function pickInstanceNoFromSearchForm(searchForm = {}) {
|
const no = (searchForm?.instanceNo || "").trim();
|
return no ? { instanceNo: no } : {};
|
}
|
|
/** 组装 approvalInstanceDto 查询片段(申请人 + 审批单号 + 状态 + 时间范围) */
|
export function buildApprovalInstanceSearchDto(searchForm = {}, extraParams = {}) {
|
const dto = {
|
...(extraParams && typeof extraParams === "object" ? extraParams : {}),
|
};
|
Object.assign(dto, pickApplicantFromSearchForm(searchForm));
|
Object.assign(dto, pickInstanceNoFromSearchForm(searchForm));
|
|
// 审批状态
|
if (searchForm?.status) {
|
dto.status = searchForm.status;
|
}
|
|
// 创建时间范围
|
const range = searchForm?.createTimeRange;
|
if (Array.isArray(range) && range[0]) {
|
dto.createTimeStart = range[0] + (range[0].includes(":") ? "" : " 00:00:00");
|
}
|
if (Array.isArray(range) && range[1]) {
|
dto.createTimeEnd = range[1] + (range[1].includes(":") ? "" : " 23:59:59");
|
}
|
|
delete dto.createTime;
|
return dto;
|
}
|
|
function getRowPayloadValue(row, keys) {
|
const keyList = Array.isArray(keys) ? keys : [keys];
|
const payload = row?.formPayload || {};
|
for (const k of keyList) {
|
if (row?.[k] != null && row[k] !== "") return row[k];
|
if (payload[k] != null && payload[k] !== "") return payload[k];
|
}
|
return "";
|
}
|
|
function matchApplicantKeyword(row, keyword) {
|
const kw = (keyword || "").trim().toLowerCase();
|
if (!kw) return true;
|
const parts = [row?.applicantName, row?.applicantNo, row?.applicantId, getRowPayloadValue(row, ["applicant", "applicantName", "applicantId"])]
|
.filter(v => v != null && v !== "")
|
.map(v => String(v).toLowerCase());
|
return parts.some(p => p.includes(kw));
|
}
|
|
function matchApplicantId(row, applicantId) {
|
if (applicantId == null || applicantId === "") return true;
|
const id = String(applicantId);
|
if (row?.applicantId != null && String(row.applicantId) === id) return true;
|
const payloadApplicant = getRowPayloadValue(row, ["applicant", "applicantId", "applicantUserId"]);
|
return String(payloadApplicant) === id;
|
}
|
|
function matchSelectValue(row, keys, expected) {
|
if (!expected) return true;
|
const raw = getRowPayloadValue(row, keys);
|
return String(raw) === String(expected);
|
}
|
|
function matchInstanceNo(row, instanceNo) {
|
const kw = (instanceNo || "").trim().toLowerCase();
|
if (!kw) return true;
|
const parts = [row?.instanceNo, row?.bizId].filter(v => v != null && v !== "").map(v => String(v).toLowerCase());
|
return parts.some(p => p.includes(kw));
|
}
|
|
/** 是否存在列表筛选条件(申请人 / 审批单号 / 状态 / 时间范围) */
|
export function hasActiveModuleSearch(moduleKey, searchForm = {}) {
|
const sf = searchForm || {};
|
if ((sf.instanceNo || "").trim()) return true;
|
if ((sf.applicantKeyword || "").trim()) return true;
|
if ((sf.applicantName || "").trim()) return true;
|
if (sf.applicantId != null && sf.applicantId !== "") return true;
|
if (sf.status) return true;
|
if (Array.isArray(sf.createTimeRange) && sf.createTimeRange.length === 2) return true;
|
return false;
|
}
|
|
/** 按申请人、审批单号、状态、时间范围做前端兜底筛选 */
|
export function filterInstanceRowsByModuleSearch(moduleKey, rows, searchForm = {}) {
|
const sf = searchForm || {};
|
const list = Array.isArray(rows) ? rows : [];
|
if (!hasActiveModuleSearch(moduleKey, sf)) return list;
|
|
return list.filter(row => {
|
// 审批单号
|
if (!matchInstanceNo(row, sf.instanceNo)) return false;
|
// 申请人
|
if (!matchApplicantId(row, sf.applicantId)) return false;
|
if (!matchApplicantKeyword(row, sf.applicantKeyword || sf.applicantName)) return false;
|
// 状态
|
if (sf.status && String(row.statusRaw || row.status).toUpperCase() !== String(sf.status).toUpperCase()) {
|
return false;
|
}
|
// 时间范围
|
if (Array.isArray(sf.createTimeRange) && sf.createTimeRange.length === 2) {
|
const rowTime = row.createTime || row.applyTime;
|
if (rowTime) {
|
const t = dayjs(rowTime);
|
const start = dayjs(sf.createTimeRange[0] + " 00:00:00");
|
const end = dayjs(sf.createTimeRange[1] + " 23:59:59");
|
if (t.isBefore(start) || t.isAfter(end)) return false;
|
}
|
}
|
return true;
|
});
|
}
|