190 lines
7.2 KiB
JavaScript
190 lines
7.2 KiB
JavaScript
const validator = require('../../utils/validator.js')
|
|
|
|
let db = "";
|
|
module.exports = async (item, mongodb) => {
|
|
db = mongodb;
|
|
switch (item.type) {
|
|
case "getHlwOrderConfig":
|
|
return await getHlwOrderConfig(item);
|
|
case "updateHlwOrderConfig":
|
|
return await updateHlwOrderConfig(item);
|
|
case "getHlwConfig":
|
|
return await getHlwConfig(item);
|
|
case "changeHlwConfig":
|
|
return await changeHlwConfig(item);
|
|
case "getHlwConfigGroup":
|
|
return await getHlwConfigGroup(item);
|
|
case "getHlwConfigByCorpIds":
|
|
return await getHlwConfigByCorpIds(item);
|
|
}
|
|
};
|
|
|
|
async function getHlwOrderConfig({ corpId }) {
|
|
try {
|
|
const res = await db.collection("hlw-config").findOne(
|
|
{ corpId },
|
|
{
|
|
projection: {
|
|
payDuration: 1,
|
|
acceptDuration: 1,
|
|
consultationDuration: 1,
|
|
medicinePurchaseRxDuration: 1,
|
|
emsExpressDiscountFee: 1,
|
|
emsExpressFee: 1,
|
|
fengNiaoExpressDiscountFee: 1,
|
|
fengNiaoExpressFee: 1,
|
|
firstSubmitRxIntervel: 1,
|
|
submitRxInterval: 1
|
|
}
|
|
});
|
|
if (!res) {
|
|
return { success: false, message: "未找到配置" };
|
|
}
|
|
return { success: true, data: res, message: "获取配置成功" };
|
|
} catch (e) {
|
|
return { success: false, message: e.message }
|
|
}
|
|
}
|
|
|
|
async function updateHlwOrderConfig(ctx) {
|
|
try {
|
|
const { payDuration, acceptDuration, consultationDuration, medicinePurchaseRxDuration, emsExpressDiscountFee, emsExpressFee, fengNiaoExpressDiscountFee, fengNiaoExpressFee } = ctx;
|
|
const data = {}
|
|
if (typeof payDuration == 'number') {
|
|
const [valid, message] = validator.verifyNumber(payDuration, "支付时长", 0, 5, 60, true, true);
|
|
if (!valid) {
|
|
return { success: false, message };
|
|
}
|
|
data.payDuration = payDuration;
|
|
}
|
|
if (typeof acceptDuration == 'number') {
|
|
const [valid, message] = validator.verifyNumber(acceptDuration, "接诊时长", 0, 5, 120, true, true);
|
|
if (!valid) {
|
|
return { success: false, message };
|
|
}
|
|
data.acceptDuration = acceptDuration;
|
|
}
|
|
if (typeof consultationDuration == 'number') {
|
|
const [valid, message] = validator.verifyNumber(consultationDuration, "订单持续时长", 0, 10, 60 * 8, true, true);
|
|
if (!valid) {
|
|
return { success: false, message };
|
|
}
|
|
data.consultationDuration = consultationDuration;
|
|
}
|
|
if (typeof medicinePurchaseRxDuration == 'number') {
|
|
const [valid, message] = validator.verifyNumber(medicinePurchaseRxDuration, "配药处方持续时长", 0, 10, 60 * 8, true, true);
|
|
if (!valid) {
|
|
return { success: false, message };
|
|
}
|
|
data.medicinePurchaseRxDuration = medicinePurchaseRxDuration;
|
|
}
|
|
if (typeof presOpen == 'boolean') {
|
|
data.presOpen = presOpen;
|
|
}
|
|
|
|
if (typeof emsExpressDiscountFee == 'number' || typeof emsExpressFee == 'number') {
|
|
const [valid, message] = validator.verifyNumber(emsExpressDiscountFee, "邮政快递优惠费用", 2, 0, 9999, true, true);
|
|
if (!valid) {
|
|
return { success: false, message };
|
|
}
|
|
const [valid1, message1] = validator.verifyNumber(emsExpressFee, "邮政快递费用", 2, 0, 9999, true, true);
|
|
if (!valid1) {
|
|
return { success: false, message: message1 };
|
|
}
|
|
data.emsExpressDiscountFee = Number(emsExpressDiscountFee);
|
|
data.emsExpressFee = Number(emsExpressFee);
|
|
if (!(data.emsExpressFee >= data.emsExpressDiscountFee)) {
|
|
return { success: false, message: "邮政快递优惠费用不能大于邮政快递费用" };
|
|
}
|
|
}
|
|
if (typeof fengNiaoExpressDiscountFee == 'number' || typeof fengNiaoExpressFee == 'number') {
|
|
const [valid, message] = validator.verifyNumber(fengNiaoExpressDiscountFee, "蜂鸟配送优惠费用", 2, 0, 9999, true, true);
|
|
if (!valid) {
|
|
return { success: false, message };
|
|
}
|
|
const [valid1, message1] = validator.verifyNumber(fengNiaoExpressFee, "蜂鸟配送费用", 2, 0, 9999, true, true);
|
|
if (!valid1) {
|
|
return { success: false, message: message1 };
|
|
}
|
|
data.fengNiaoExpressDiscountFee = Number(fengNiaoExpressDiscountFee);
|
|
data.fengNiaoExpressFee = Number(fengNiaoExpressFee);
|
|
if (!(data.fengNiaoExpressFee >= data.fengNiaoExpressDiscountFee)) {
|
|
return { success: false, message: "蜂鸟配送优惠费用不能大于蜂鸟配送费用" };
|
|
}
|
|
}
|
|
if (Object.keys(data).length === 0) {
|
|
return { success: false, message: "没有需要更新的配置" };
|
|
}
|
|
const res = await db.collection("hlw-config").updateOne({ corpId: ctx.corpId }, { $set: data });
|
|
if (res.modifiedCount === 0) {
|
|
return { success: false, message: "更新配置失败" };
|
|
}
|
|
return { success: true, message: "更新配置成功" };
|
|
} catch (e) {
|
|
return { success: false, message: e.message }
|
|
}
|
|
}
|
|
|
|
|
|
async function getHlwConfig({ corpId }) {
|
|
try {
|
|
if (typeof corpId !== 'string' || corpId.trim() === '') {
|
|
return { success: false, message: 'corpId不能为空' }
|
|
}
|
|
const res = await db.collection("hlw-config").findOne({ corpId: corpId.trim() });
|
|
return { success: true, data: res, message: '获取配置成功' }
|
|
} catch (e) {
|
|
return { success: false, message: e.message }
|
|
}
|
|
}
|
|
|
|
async function changeHlwConfig({ corpId, data }) {
|
|
try {
|
|
if (typeof corpId !== 'string' || corpId.trim() === '') {
|
|
return { success: false, message: 'corpId不能为空' }
|
|
}
|
|
if (Object.prototype.toString.call(data) !== '[object Object]' || Object.keys(data).length === 0) {
|
|
return { success: false, message: 'data不能为空' }
|
|
}
|
|
const trimmedCorpId = corpId.trim();
|
|
const now = Date.now();
|
|
const res = await db.collection("hlw-config").updateOne(
|
|
{ corpId: trimmedCorpId },
|
|
{
|
|
$set: { ...data, updateTime: now },
|
|
$setOnInsert: { createTime: now }
|
|
},
|
|
{ upsert: true }
|
|
);
|
|
const message = res.upsertedCount > 0 ? '新增配置成功' : '更新配置成功';
|
|
return { success: true, data: res, message, id: res.upsertedId || res.modifiedCount }
|
|
} catch (e) {
|
|
return { success: false, message: e.message }
|
|
}
|
|
}
|
|
|
|
async function getHlwConfigGroup({ group }) {
|
|
try {
|
|
if (typeof group !== 'string' || group.trim() === '') {
|
|
return { success: false, message: 'group不能为空' }
|
|
}
|
|
const res = await db.collection("hlw-config").find({ group }, { projection: { _id: 0, key: 1, description: 1, list: 1 } }).toArray();
|
|
return { success: true, data: res, message: "获取配置成功" }
|
|
} catch (e) {
|
|
return { success: false, message: e.message }
|
|
}
|
|
}
|
|
|
|
async function getHlwConfigByCorpIds(ctx) {
|
|
try {
|
|
const corpIds = Array.isArray(ctx.corpIds) ? ctx.corpIds.filter(i => typeof i == 'string' && i.trim() !== '') : [];
|
|
if (corpIds.length === 0) {
|
|
return { success: false, message: 'corpIds不能为空' }
|
|
}
|
|
const trimmedCorpIds = corpIds.map(i => i.trim());
|
|
const res = await db.collection("hlw-config").find({ corpId: { $in: trimmedCorpIds } }, { projection: { _id: 0, key: 1, description: 1, list: 1 } }).toArray();
|
|
return { success: true, data: res, message: "获取配置成功" }
|
|
} catch (e) {
|
|
return { success: false, message: e.message }
|
|
}
|
|
} |