a919781d4f
配载草稿确认生成正式单并优化详情/列表地址市区展示;运单调度等按承运商联系人所属组织筛选司机车辆;统一地图搜索结果与登录默认账号清理。 Co-authored-by: Cursor <cursoragent@cursor.com>
141 lines
4.5 KiB
JavaScript
141 lines
4.5 KiB
JavaScript
import { getDetail, getList as getCustomerList } from '@/api/vehicle/customer-archive';
|
|
import { getList as getDriverList } from '@/api/transportCapacity/driver';
|
|
import { getList as getVehicleList } from '@/api/transportCapacity/transport-vehicle';
|
|
|
|
const orgNameCache = new Map();
|
|
|
|
const extractRecords = res => {
|
|
const data = res?.data?.data ?? res?.data ?? res;
|
|
if (Array.isArray(data)) return data;
|
|
if (Array.isArray(data?.records)) return data.records;
|
|
if (Array.isArray(data?.data)) return data.data;
|
|
return [];
|
|
};
|
|
|
|
const normalizeOrgName = value => String(value || '').trim();
|
|
|
|
const uniqueOrgNames = (values = []) => {
|
|
const seen = new Set();
|
|
const result = [];
|
|
values.forEach(value => {
|
|
const name = normalizeOrgName(value);
|
|
if (!name || seen.has(name)) return;
|
|
seen.add(name);
|
|
result.push(name);
|
|
});
|
|
return result;
|
|
};
|
|
|
|
/**
|
|
* 解析承运商客商档案上联系人的所属组织(branchName),无联系人时回退客商所属组织。
|
|
*/
|
|
export const resolveCarrierOrganizationNames = async ({ carrierId, carrierName } = {}) => {
|
|
const id = String(carrierId || '').trim();
|
|
const name = normalizeOrgName(carrierName);
|
|
const cacheKey = id || name;
|
|
if (!cacheKey) return [];
|
|
if (orgNameCache.has(cacheKey)) return orgNameCache.get(cacheKey);
|
|
|
|
let detail = null;
|
|
if (id) {
|
|
try {
|
|
const res = await getDetail(id);
|
|
detail = res?.data?.data || res?.data || null;
|
|
} catch (error) {
|
|
detail = null;
|
|
}
|
|
}
|
|
if (!detail && name) {
|
|
try {
|
|
const res = await getCustomerList(1, 20, { fullName: name });
|
|
const records = extractRecords(res);
|
|
const matched =
|
|
records.find(item => normalizeOrgName(item.fullName || item.customerName) === name) ||
|
|
records[0];
|
|
if (matched?.id) {
|
|
const detailRes = await getDetail(matched.id);
|
|
detail = detailRes?.data?.data || detailRes?.data || matched;
|
|
}
|
|
} catch (error) {
|
|
detail = null;
|
|
}
|
|
}
|
|
|
|
const contacts = Array.isArray(detail?.contacts) ? detail.contacts : [];
|
|
const orgNames = uniqueOrgNames([
|
|
...contacts.map(item => item.branchName),
|
|
detail?.deptName,
|
|
detail?.organizationName,
|
|
]);
|
|
orgNameCache.set(cacheKey, orgNames);
|
|
if (id && name) orgNameCache.set(name, orgNames);
|
|
return orgNames;
|
|
};
|
|
|
|
export const clearCarrierOrganizationCache = (carrier = {}) => {
|
|
const id = String(carrier.carrierId || '').trim();
|
|
const name = normalizeOrgName(carrier.carrierName);
|
|
if (id) orgNameCache.delete(id);
|
|
if (name) orgNameCache.delete(name);
|
|
};
|
|
|
|
export const matchOrganizationName = (value, orgNames = []) => {
|
|
const text = normalizeOrgName(value);
|
|
if (!text || !orgNames.length) return false;
|
|
return orgNames.some(org => text === org || text.includes(org) || org.includes(text));
|
|
};
|
|
|
|
const filterByOrganizations = (records = [], orgNames = []) => {
|
|
if (!orgNames.length) return [];
|
|
return records.filter(item => matchOrganizationName(item.organizationName, orgNames));
|
|
};
|
|
|
|
/**
|
|
* 按承运商联系人所属组织筛选司机。
|
|
* 未选择承运商时返回空列表(需先选承运商)。
|
|
*/
|
|
export const fetchDriversByCarrierOrganizations = async (
|
|
query = {},
|
|
{ carrierId, carrierName } = {}
|
|
) => {
|
|
if (!String(carrierId || '').trim() && !normalizeOrgName(carrierName)) {
|
|
return [];
|
|
}
|
|
const orgNames = await resolveCarrierOrganizationNames({ carrierId, carrierName });
|
|
if (!orgNames.length) return [];
|
|
|
|
const size = Math.max(Number(query.size) || 50, 50);
|
|
const params = { ...query };
|
|
delete params.size;
|
|
// 单组织时用后端模糊条件缩小范围;多组织再前端精确过滤
|
|
if (orgNames.length === 1) {
|
|
params.organizationName = orgNames[0];
|
|
}
|
|
const res = await getDriverList(1, Math.min(size * 5, 200), params);
|
|
return filterByOrganizations(extractRecords(res), orgNames).slice(0, size);
|
|
};
|
|
|
|
/**
|
|
* 按承运商联系人所属组织筛选车辆。
|
|
* 未选择承运商时返回空列表。
|
|
*/
|
|
export const fetchVehiclesByCarrierOrganizations = async (
|
|
query = {},
|
|
{ carrierId, carrierName } = {}
|
|
) => {
|
|
if (!String(carrierId || '').trim() && !normalizeOrgName(carrierName)) {
|
|
return [];
|
|
}
|
|
const orgNames = await resolveCarrierOrganizationNames({ carrierId, carrierName });
|
|
if (!orgNames.length) return [];
|
|
|
|
const size = Math.max(Number(query.size) || 50, 50);
|
|
const params = { ...query };
|
|
delete params.size;
|
|
if (orgNames.length === 1) {
|
|
params.organizationName = orgNames[0];
|
|
}
|
|
const res = await getVehicleList(1, Math.min(size * 5, 200), params);
|
|
return filterByOrganizations(extractRecords(res), orgNames).slice(0, size);
|
|
};
|