rapid-ztx
Version:
Rapid ZTX module
86 lines (73 loc) • 2.45 kB
JavaScript
/**
* Copyright (c) 2017 Lucky Byte, Inc.
*/
const debug = require('debug')('ztx');
/**
* 查询代理商的平台商户
*
* 商户选择可以有 2 种策略,第一种是每个代理商配置固定的商户,第二种是配置一组商户,
* 每次交易时从中选择一个合适的商户(当前的策略是选择交易额最低的商户)
*
* 参数:
* agent: 代理商的 UUID
* amount: 交易金额,以分为单位
*
* 返回:
* 失败抛出异常
*/
const get_for_agent = async (agent, amount) => {
if (!agent || !amount) {
throw new Error('get_for_agent 参数无效');
}
// 查询自套现参数配置中的商户使用策略
const params = await unique_db.one(`
select merch_policy, merchs from rapid_ctod_params limit 1
`);
// 使用代理商配置的交易商户
if (params.merch_policy == 1) {
return await get_by_agent(agent);
}
// 查询交易额最少的商户
return await get_by_amount(params, amount);
}
/**
* 查询代理商配置的平台商户,查询结果用箭头指示:
*
* +-------+ +---------+ +--------+
* | 代理商 | -> | 平台商户 | -> | 银联商户 |
* +-------+ +---------+ +--------+
* ^
* ^
*/
const get_by_agent = async (agent) => {
return await unique_db.one(`
select * from rapid_merchs where uuid = (
select merch from rapid_ctod_agents where uuid = $1
)
`, [
agent
]);
}
/**
* 从可用的一组商户中查询当天交易额最少的商户
* 选择时,会考虑商户的单笔最低金额和最高金额,仅在此区间的商户会被选择
*/
const get_by_amount = async (params, amount) => {
if (!params.merchs || params.merchs.length == 0) {
throw new Error('未配置自套现交易商户,请检查参数配置');
}
return await unique_db.one(`
select m.*, coalesce((
select coalesce(amount, 0) as amount from rapid_merch_limit
where merch = m.uuid and date = CURRENT_DATE
), 0) as amt_day
from rapid_merchs as m
where m.disabled = false and m.uuid in ($1:csv) and
(m.trctrl->>'amt_per_min')::int < $2 and
(m.trctrl->>'amt_per_max')::int >= $2
order by amt_day limit 1
`, [
params.merchs, amount / 100
]);
}
module.exports = get_for_agent;