225 lines
7.1 KiB
JavaScript
225 lines
7.1 KiB
JavaScript
const { ObjectId } = require('mongodb');
|
|
const validator = require('../../utils/validator');
|
|
const Sm4Util = require('../../utils/sm4-util');
|
|
|
|
const COLLECTION_NAME = 'hlw-patient';
|
|
|
|
exports.main = async ({ type, ...item }, db) => {
|
|
switch (type) {
|
|
case 'addHlwPatient':
|
|
return addHlwPatient(item, db);
|
|
case 'autoAddHlwPatient':
|
|
return autoAddHlwPatient(item, db);
|
|
case 'getHlwPatientList':
|
|
return getHlwPatientList(item, db);
|
|
case 'getHlwPatient':
|
|
return getHlwPatient(item, db);
|
|
case 'updateHlwPatient':
|
|
return updateHlwPatient(item, db);
|
|
case 'deleteHlwPatient':
|
|
return deleteHlwPatient(item, db);
|
|
default:
|
|
return { success: false, message: '未找到就诊人接口' };
|
|
}
|
|
};
|
|
|
|
function validateAccountId(accountId) {
|
|
if (typeof accountId !== 'string' || accountId.trim() === '') {
|
|
return { success: false, message: 'accountId不能为空' };
|
|
}
|
|
return { success: true, accountId: accountId.trim() };
|
|
}
|
|
|
|
function validatePatient(item) {
|
|
const accountResult = validateAccountId(item && item.accountId);
|
|
if (!accountResult.success) return accountResult;
|
|
|
|
const name = typeof item.name === 'string' ? item.name.trim() : '';
|
|
const certNo = typeof item.certNo === 'string' ? item.certNo.trim() : '';
|
|
const mobile = typeof item.mobile === 'string' ? item.mobile.trim() : '';
|
|
|
|
if (!name) return { success: false, message: '姓名不能为空' };
|
|
if (!certNo) return { success: false, message: '证件号不能为空' };
|
|
if (!mobile) return { success: false, message: '手机号不能为空' };
|
|
if (!validator.isMobile(mobile)) {
|
|
return { success: false, message: '手机号格式不正确' };
|
|
}
|
|
|
|
let address = '';
|
|
if (item.address !== undefined && item.address !== null) {
|
|
if (typeof item.address !== 'string') {
|
|
return { success: false, message: '地址格式不正确' };
|
|
}
|
|
address = item.address.trim();
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
data: {
|
|
accountId: accountResult.accountId,
|
|
anotherIdNo: Sm4Util.encryptDataForSm3(certNo),
|
|
anotherName: Sm4Util.encryptDataForSm3(name),
|
|
anotherMobile: Sm4Util.encryptDataForSm3(mobile),
|
|
address,
|
|
},
|
|
};
|
|
}
|
|
|
|
async function addHlwPatient(item, db) {
|
|
try {
|
|
const validation = validatePatient(item);
|
|
if (!validation.success) return validation;
|
|
|
|
const exists = await db.collection(COLLECTION_NAME).countDocuments({
|
|
accountId: validation.data.accountId,
|
|
anotherIdNo: validation.data.anotherIdNo,
|
|
disabled: false,
|
|
});
|
|
if (exists > 0) {
|
|
return { success: false, message: '当前账户下已存在相同证件号的就诊人' };
|
|
}
|
|
|
|
const result = await db.collection(COLLECTION_NAME).insertOne({
|
|
...validation.data,
|
|
createTime: new Date(),
|
|
disabled: false,
|
|
});
|
|
|
|
return {
|
|
success: true,
|
|
message: '新增就诊人成功',
|
|
id: result.insertedId.toString(),
|
|
};
|
|
} catch (error) {
|
|
return { success: false, message: error.message || '新增就诊人失败' };
|
|
}
|
|
}
|
|
|
|
async function autoAddHlwPatient(item, db) {
|
|
try {
|
|
const validation = validatePatient(item);
|
|
if (!validation.success) return validation;
|
|
|
|
const exists = await db.collection(COLLECTION_NAME).countDocuments({
|
|
accountId: validation.data.accountId,
|
|
anotherIdNo: validation.data.anotherIdNo,
|
|
disabled: false,
|
|
});
|
|
|
|
if (exists > 0) {
|
|
return { success: true, data: true, message: '就诊人已存在' };
|
|
}
|
|
|
|
const result = await addHlwPatient(item, db);
|
|
if (!result.success) return result;
|
|
return { success: true, data: true, message: '自动新增就诊人成功' };
|
|
} catch (error) {
|
|
return { success: false, message: error.message || '自动新增就诊人失败' };
|
|
}
|
|
}
|
|
|
|
async function getHlwPatientList(item, db) {
|
|
try {
|
|
const validation = validateAccountId(item && item.accountId);
|
|
if (!validation.success) return validation;
|
|
|
|
const patients = await db.collection(COLLECTION_NAME)
|
|
.find({ accountId: validation.accountId, disabled: false })
|
|
.toArray();
|
|
|
|
const data = patients.map(toPatientData);
|
|
|
|
return { success: true, message: '查询就诊人列表成功', data };
|
|
} catch (error) {
|
|
return { success: false, message: error.message || '查询就诊人列表失败' };
|
|
}
|
|
}
|
|
|
|
async function getHlwPatient(item, db) {
|
|
try {
|
|
const validation = validateAccountId(item && item.accountId);
|
|
if (!validation.success) return validation;
|
|
if (!item || typeof item.id !== 'string' || !ObjectId.isValid(item.id.trim())) {
|
|
return { success: false, message: '就诊人ID格式不正确' };
|
|
}
|
|
|
|
const patient = await db.collection(COLLECTION_NAME).findOne({
|
|
_id: new ObjectId(item.id.trim()),
|
|
accountId: validation.accountId,
|
|
disabled: false,
|
|
});
|
|
if (!patient) {
|
|
return { success: false, message: '就诊人不存在或accountId不匹配' };
|
|
}
|
|
|
|
return { success: true, message: '查询就诊人成功', data: toPatientData(patient) };
|
|
} catch (error) {
|
|
return { success: false, message: error.message || '查询就诊人失败' };
|
|
}
|
|
}
|
|
|
|
function toPatientData(patient) {
|
|
return {
|
|
id: patient._id.toString(),
|
|
accountId: patient.accountId,
|
|
name: Sm4Util.decryptDataForSm3(patient.anotherName),
|
|
certNo: Sm4Util.decryptDataForSm3(patient.anotherIdNo),
|
|
mobile: Sm4Util.decryptDataForSm3(patient.anotherMobile),
|
|
address: patient.address || '',
|
|
createTime: patient.createTime,
|
|
};
|
|
}
|
|
|
|
async function updateHlwPatient(item, db) {
|
|
try {
|
|
const validation = validatePatient(item);
|
|
if (!validation.success) return validation;
|
|
|
|
if (typeof item.id !== 'string' || !ObjectId.isValid(item.id.trim())) {
|
|
return { success: false, message: '就诊人ID格式不正确' };
|
|
}
|
|
|
|
const result = await db.collection(COLLECTION_NAME).updateOne(
|
|
{
|
|
_id: new ObjectId(item.id.trim()),
|
|
accountId: validation.data.accountId,
|
|
disabled: false,
|
|
},
|
|
{ $set: validation.data },
|
|
);
|
|
|
|
if (result.matchedCount === 0) {
|
|
return { success: false, message: '就诊人不存在或accountId不匹配' };
|
|
}
|
|
return { success: true, message: '更新就诊人成功' };
|
|
} catch (error) {
|
|
return { success: false, message: error.message || '更新就诊人失败' };
|
|
}
|
|
}
|
|
|
|
async function deleteHlwPatient(item, db) {
|
|
try {
|
|
const validation = validateAccountId(item && item.accountId);
|
|
if (!validation.success) return validation;
|
|
if (!item || typeof item.id !== 'string' || !ObjectId.isValid(item.id.trim())) {
|
|
return { success: false, message: '就诊人ID格式不正确' };
|
|
}
|
|
|
|
const result = await db.collection(COLLECTION_NAME).updateOne(
|
|
{
|
|
_id: new ObjectId(item.id.trim()),
|
|
accountId: validation.accountId,
|
|
disabled: false,
|
|
},
|
|
{ $set: { disabled: true } },
|
|
);
|
|
|
|
if (result.matchedCount === 0) {
|
|
return { success: false, message: '就诊人不存在或accountId不匹配' };
|
|
}
|
|
return { success: true, message: '删除就诊人成功' };
|
|
} catch (error) {
|
|
return { success: false, message: error.message || '删除就诊人失败' };
|
|
}
|
|
}
|