删除门店的多银行账户列表
This commit is contained in:
@@ -6,55 +6,10 @@ export type StoreBankAccountSnapshot = {
|
||||
bankBranch: string | null;
|
||||
};
|
||||
|
||||
export type StoreBankAccountRow = StoreBankAccountSnapshot & {
|
||||
id: bigint;
|
||||
storeId: bigint;
|
||||
isDefault: boolean;
|
||||
status: string;
|
||||
sortOrder: number;
|
||||
};
|
||||
|
||||
/** 门店收款账户列表(默认账户优先,其次 sortOrder/创建顺序) */
|
||||
export async function loadStoreBankAccounts(
|
||||
prisma: PrismaClient,
|
||||
storeId: bigint,
|
||||
): Promise<StoreBankAccountRow[]> {
|
||||
const rows = await prisma.storeBankAccount.findMany({
|
||||
where: { storeId, status: 'ACTIVE' },
|
||||
orderBy: [{ isDefault: 'desc' }, { sortOrder: 'asc' }, { id: 'asc' }],
|
||||
});
|
||||
return rows.map((r) => ({
|
||||
id: r.id,
|
||||
storeId: r.storeId,
|
||||
bankAccountName: r.bankAccountName,
|
||||
bankAccountNo: r.bankAccountNo,
|
||||
bankBranch: r.bankBranch,
|
||||
isDefault: r.isDefault === 1,
|
||||
status: r.status,
|
||||
sortOrder: r.sortOrder,
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* 门店默认收款账户:优先 is_default=1;否则第一个 ACTIVE;再退回旧主账号字段(兼容未迁移数据)。
|
||||
* 返回 null 表示无收款账户。
|
||||
* 门店结算资质银行账户:主账号(is_primary=1)字段;无主账号则取最早绑定账号。
|
||||
* 来源为门店详情「结算资质」(StoreAccount.bank_account_*),实时查库。
|
||||
*/
|
||||
export async function loadStoreDefaultBank(
|
||||
prisma: PrismaClient,
|
||||
storeId: bigint,
|
||||
): Promise<StoreBankAccountSnapshot | null> {
|
||||
const accounts = await loadStoreBankAccounts(prisma, storeId);
|
||||
if (accounts.length) {
|
||||
const def = accounts.find((a) => a.isDefault) ?? accounts[0];
|
||||
return {
|
||||
bankAccountName: def.bankAccountName,
|
||||
bankAccountNo: def.bankAccountNo,
|
||||
bankBranch: def.bankBranch,
|
||||
};
|
||||
}
|
||||
return loadStorePrimaryBank(prisma, storeId);
|
||||
}
|
||||
|
||||
export async function loadStorePrimaryBank(
|
||||
prisma: PrismaClient,
|
||||
storeId: bigint,
|
||||
@@ -80,6 +35,14 @@ export async function loadStorePrimaryBank(
|
||||
return fallback?.storeAccount ?? null;
|
||||
}
|
||||
|
||||
/** 打款/提现读结算资质(主账号银行字段) */
|
||||
export async function loadStoreDefaultBank(
|
||||
prisma: PrismaClient,
|
||||
storeId: bigint,
|
||||
): Promise<StoreBankAccountSnapshot | null> {
|
||||
return loadStorePrimaryBank(prisma, storeId);
|
||||
}
|
||||
|
||||
export async function loadStorePrimaryBanksMap(
|
||||
prisma: PrismaClient,
|
||||
storeIds: bigint[],
|
||||
@@ -87,49 +50,28 @@ export async function loadStorePrimaryBanksMap(
|
||||
const map = new Map<string, StoreBankAccountSnapshot>();
|
||||
if (!storeIds.length) return map;
|
||||
|
||||
// 优先从门店多账户表读取默认账户
|
||||
const accounts = await prisma.storeBankAccount.findMany({
|
||||
where: { storeId: { in: storeIds }, status: 'ACTIVE' },
|
||||
orderBy: [{ storeId: 'asc' }, { isDefault: 'desc' }, { sortOrder: 'asc' }, { id: 'asc' }],
|
||||
});
|
||||
const seenStoreIds = new Set<string>();
|
||||
for (const a of accounts) {
|
||||
const key = String(a.storeId);
|
||||
if (map.has(key)) continue;
|
||||
map.set(key, {
|
||||
bankAccountName: a.bankAccountName,
|
||||
bankAccountNo: a.bankAccountNo,
|
||||
bankBranch: a.bankBranch,
|
||||
});
|
||||
seenStoreIds.add(key);
|
||||
}
|
||||
|
||||
// 未迁移/无多账户的门店,回退旧主账号字段
|
||||
const missingIds = storeIds.filter((id) => !seenStoreIds.has(String(id)));
|
||||
if (missingIds.length) {
|
||||
const bindings = await prisma.storeAccountStore.findMany({
|
||||
where: { storeId: { in: missingIds } },
|
||||
include: {
|
||||
storeAccount: {
|
||||
select: {
|
||||
isPrimary: true,
|
||||
bankAccountName: true,
|
||||
bankAccountNo: true,
|
||||
bankBranch: true,
|
||||
},
|
||||
const bindings = await prisma.storeAccountStore.findMany({
|
||||
where: { storeId: { in: storeIds } },
|
||||
include: {
|
||||
storeAccount: {
|
||||
select: {
|
||||
isPrimary: true,
|
||||
bankAccountName: true,
|
||||
bankAccountNo: true,
|
||||
bankBranch: true,
|
||||
},
|
||||
},
|
||||
orderBy: [{ storeAccount: { isPrimary: 'desc' } }, { storeAccountId: 'asc' }],
|
||||
},
|
||||
orderBy: [{ storeAccount: { isPrimary: 'desc' } }, { storeAccountId: 'asc' }],
|
||||
});
|
||||
for (const binding of bindings) {
|
||||
const key = String(binding.storeId);
|
||||
if (map.has(key)) continue;
|
||||
map.set(key, {
|
||||
bankAccountName: binding.storeAccount.bankAccountName,
|
||||
bankAccountNo: binding.storeAccount.bankAccountNo,
|
||||
bankBranch: binding.storeAccount.bankBranch,
|
||||
});
|
||||
for (const binding of bindings) {
|
||||
const key = String(binding.storeId);
|
||||
if (map.has(key)) continue;
|
||||
map.set(key, {
|
||||
bankAccountName: binding.storeAccount.bankAccountName,
|
||||
bankAccountNo: binding.storeAccount.bankAccountNo,
|
||||
bankBranch: binding.storeAccount.bankBranch,
|
||||
});
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user