442 lines
19 KiB
JavaScript
442 lines
19 KiB
JavaScript
|
|
'use strict';
|
||
|
|
|
||
|
|
const Service = require('../core/base_service');
|
||
|
|
|
||
|
|
const { Op } = require('sequelize');
|
||
|
|
|
||
|
|
const shunt = (uid) => {
|
||
|
|
return uid[uid.length - 1];
|
||
|
|
};
|
||
|
|
|
||
|
|
const fileName = 'home';
|
||
|
|
|
||
|
|
class HomeService extends Service {
|
||
|
|
// 获取SN号
|
||
|
|
async getSN(uid) {
|
||
|
|
const { ctx } = this;
|
||
|
|
try {
|
||
|
|
const where = { user_uid: uid };
|
||
|
|
const uidLastCode = shunt(uid);
|
||
|
|
const model = {
|
||
|
|
"B": ctx.model.BuserSNBind,
|
||
|
|
"C": ctx.model2.CuserSNBind,
|
||
|
|
"T": ctx.model4.TuserSNBind,
|
||
|
|
};
|
||
|
|
const result = await model[uidLastCode].findOne({ where });
|
||
|
|
return result || false;
|
||
|
|
} catch (error) {
|
||
|
|
const blockName = fileName + "--" + "getSN";
|
||
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
||
|
|
return 'error';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 搜索SN号
|
||
|
|
async searchSN(snNumber) {
|
||
|
|
const { ctx } = this;
|
||
|
|
try {
|
||
|
|
const where = { sn_number: snNumber };
|
||
|
|
const result = await ctx.model3.SNNumber.findOne({ where });
|
||
|
|
return result || false;
|
||
|
|
} catch (error) {
|
||
|
|
const blockName = fileName + "--" + "searchSN";
|
||
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
||
|
|
return 'error';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 搜索可绑定的SN号
|
||
|
|
async searchBindSN(uid) {
|
||
|
|
const { ctx } = this;
|
||
|
|
try {
|
||
|
|
const result = await ctx.model.BSide.findOne({ where: { uid, status: { [Op.not]: 2 }, role: 1 } });
|
||
|
|
if (!result || result.organization == null) return false;
|
||
|
|
const resultAdminSN = await ctx.model.BuserSNAdmin.findOne({ attributes: ['sn_set'], where: { user_uid: uid } });
|
||
|
|
if (!resultAdminSN) return false;
|
||
|
|
const adminSN = [];
|
||
|
|
resultAdminSN.sn_set.split(',').forEach(element => {
|
||
|
|
adminSN.push(element);
|
||
|
|
});
|
||
|
|
const where = { sn_number: { [Op.in]: adminSN }, status: 4 };
|
||
|
|
const resultBindSN = await ctx.model3.SNNumber.findAll({ attributes: ['sn_number'], where });
|
||
|
|
if (resultBindSN.length == 0) return false;
|
||
|
|
const bindSN = [];
|
||
|
|
resultBindSN.forEach(element => {
|
||
|
|
bindSN.push(element.dataValues.sn_number);
|
||
|
|
});
|
||
|
|
const resultData = adminSN.filter(item => bindSN.includes(item));
|
||
|
|
return resultData;
|
||
|
|
} catch (error) {
|
||
|
|
const blockName = fileName + "--" + "searchBindSN";
|
||
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
||
|
|
return 'error';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 搜索可绑定的单位用户
|
||
|
|
async searchBindUser(uid) {
|
||
|
|
const { ctx } = this;
|
||
|
|
try {
|
||
|
|
const result = await ctx.model.BSide.findOne({ where: { uid, status: { [Op.not]: 2 }, role: 1 } });
|
||
|
|
if (!result || result.organization == null) return false;
|
||
|
|
const resultAdminUser = await ctx.model.BSide.findAll({ attributes: ['uid', 'account_name'], where: { status: { [Op.not]: 2 }, organization: result.organization } });
|
||
|
|
const uidArr = [];
|
||
|
|
resultAdminUser.forEach(element => {
|
||
|
|
uidArr.push(element.dataValues.uid);
|
||
|
|
});
|
||
|
|
const where = { user_uid: { [Op.in]: uidArr } };
|
||
|
|
const resultBindUser = await ctx.model.BuserSNBind.findAll({ attributes: ['user_uid'], where });
|
||
|
|
if (resultBindUser.length == 0) return resultAdminUser;
|
||
|
|
const adminUser = [];
|
||
|
|
resultAdminUser.forEach(element => {
|
||
|
|
adminUser.push(element.dataValues);
|
||
|
|
});
|
||
|
|
const bindUser = [];
|
||
|
|
resultBindUser.forEach(element => {
|
||
|
|
bindUser.push(element.dataValues);
|
||
|
|
});
|
||
|
|
const resultData = [];
|
||
|
|
resultAdminUser.forEach(item => {
|
||
|
|
resultBindUser.forEach(element => {
|
||
|
|
if (item.dataValues.uid !== element.dataValues.user_uid) {
|
||
|
|
resultData.push(item);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
return resultData;
|
||
|
|
} catch (error) {
|
||
|
|
const blockName = fileName + "--" + "searchBindUser";
|
||
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
||
|
|
return 'error';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 绑定SN号
|
||
|
|
async bindSN(uid, body) {
|
||
|
|
const { ctx } = this;
|
||
|
|
try {
|
||
|
|
const result = await ctx.model.BSide.findOne({ where: { uid, status: { [Op.not]: 2 }, role: 1 } });
|
||
|
|
if (!result || result.organization == null) return false;
|
||
|
|
const { snNumber, userUid } = body;
|
||
|
|
const where = { sn_number: snNumber };
|
||
|
|
const sn = await ctx.model3.SNNumber.findOne({ where });
|
||
|
|
if (!sn) return false;
|
||
|
|
const model = {
|
||
|
|
"B": ctx.model.BuserSNBind,
|
||
|
|
"C": ctx.model2.CuserSNBind,
|
||
|
|
"T": ctx.model2.TuserSNBind
|
||
|
|
};
|
||
|
|
const uidLastCode = shunt(userUid);
|
||
|
|
const bindCheck = await model[uidLastCode].findOne({ where: { user_uid: userUid } });
|
||
|
|
if (bindCheck) return false;
|
||
|
|
if (shunt(userUid) == 'B' && sn.status !== 4 || shunt(userUid) !== 'B' && sn.status !== 0) {
|
||
|
|
return false;
|
||
|
|
} else {
|
||
|
|
const status = shunt(userUid) == 'B' ? 1 : shunt(userUid) == 'C' ? 2 : 3;
|
||
|
|
const snResult = await ctx.model3.SNNumber.update({ status }, { where });
|
||
|
|
const bindForm = {
|
||
|
|
user_uid: userUid,
|
||
|
|
sn_number: snNumber
|
||
|
|
};
|
||
|
|
const resultForm = {
|
||
|
|
user_uid: userUid,
|
||
|
|
sn_number: snNumber,
|
||
|
|
type: 1,
|
||
|
|
created_at: new Date()
|
||
|
|
};
|
||
|
|
const modelName = {
|
||
|
|
"B": ctx.model.BindBSNrecord,
|
||
|
|
"C": ctx.model2.BindCSNrecord
|
||
|
|
};
|
||
|
|
const bindResult = await model[uidLastCode].create(bindForm);
|
||
|
|
const recordResult = await modelName[uidLastCode].create(resultForm);
|
||
|
|
return snResult[0] && bindResult && recordResult ? true : false;
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
const blockName = fileName + "--" + "bindSN";
|
||
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
||
|
|
return 'error';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 解绑SN号
|
||
|
|
async unBindingSN(uid, body) {
|
||
|
|
const { ctx } = this;
|
||
|
|
try {
|
||
|
|
const result = await ctx.model.BSide.findOne({ where: { uid, status: { [Op.not]: 2 }, role: 1 } });
|
||
|
|
if (!result || result.organization == null) return false;
|
||
|
|
const { snNumber, userUid } = body;
|
||
|
|
const snWhere = { sn_number: snNumber };
|
||
|
|
const snResult = await ctx.model3.SNNumber.update({ status: 0 }, { where: snWhere });
|
||
|
|
if (snResult[0] !== 0) {
|
||
|
|
const where = {
|
||
|
|
user_uid: userUid,
|
||
|
|
sn_number: snNumber
|
||
|
|
};
|
||
|
|
const form = {
|
||
|
|
user_uid: userUid,
|
||
|
|
sn_number: snNumber,
|
||
|
|
type: 0,
|
||
|
|
created_at: new Date()
|
||
|
|
};
|
||
|
|
const uidLastCode = shunt(userUid);
|
||
|
|
const model = {
|
||
|
|
"B": ctx.model.BuserSNBind,
|
||
|
|
"C": ctx.model2.CuserSNBind,
|
||
|
|
"T": ctx.model2.TuserSNBind,
|
||
|
|
};
|
||
|
|
const modelName = {
|
||
|
|
"B": ctx.model.BindBSNrecord,
|
||
|
|
"C": ctx.model2.BindCSNrecord
|
||
|
|
};
|
||
|
|
const bindResult = await model[uidLastCode].destroy({ where });
|
||
|
|
const recordResult = await modelName[uidLastCode].create(form);
|
||
|
|
return bindResult && recordResult ? true : false;
|
||
|
|
} else {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
const blockName = fileName + "--" + "unBindingSN";
|
||
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
||
|
|
return 'error';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取绑定记录
|
||
|
|
async getRecord(uid, body) {
|
||
|
|
const { ctx } = this;
|
||
|
|
try {
|
||
|
|
let { offset, limit, searchOptions, sortOptions } = body;
|
||
|
|
offset = (offset - 1) * limit;
|
||
|
|
let attributes = ['user_uid', 'sn_number', 'type', 'created_at'],
|
||
|
|
where = { user_uid: uid }, order = [], dbOptions, joinSubstring = [],
|
||
|
|
substring = ['user_uid', 'sn_number'], key = ['type'], between = ['created_at'];
|
||
|
|
const uidLastCode = shunt(uid);
|
||
|
|
attributes.forEach(element => {
|
||
|
|
if (searchOptions[element]) {
|
||
|
|
if(searchOptions[element] == '*空') {
|
||
|
|
if(joinSubstring.includes(element)) {
|
||
|
|
where[element] = null;
|
||
|
|
} else {
|
||
|
|
where[element] = [null, ''];
|
||
|
|
}
|
||
|
|
} else if(substring.includes(element)) {
|
||
|
|
where[element] = { [Op.substring]: searchOptions[element] };
|
||
|
|
} else if (key.includes(element)) {
|
||
|
|
where[element] = searchOptions[element] == "1" ? uidLastCode === 'B' ? 1 : 2 : 0;
|
||
|
|
} else if (between.includes(element)) {
|
||
|
|
where[element] = { [Op.between]: [searchOptions[element][0], searchOptions[element][1]] };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (sortOptions[element] == true) {
|
||
|
|
order.push([element, 'ASC']);
|
||
|
|
} else if (sortOptions[element] == false) {
|
||
|
|
order.push([element, 'DESC']);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
dbOptions = { attributes, offset, limit, where, order };
|
||
|
|
const model = {
|
||
|
|
"B": ctx.model.BSide,
|
||
|
|
"C": ctx.model2.CSide,
|
||
|
|
};
|
||
|
|
const modelName = {
|
||
|
|
"B": ctx.model.BindBSNrecord,
|
||
|
|
"C": ctx.model2.BindCSNrecord,
|
||
|
|
};
|
||
|
|
const user = await model[uidLastCode].findAll({ attributes: ['uid', 'account_name'] });
|
||
|
|
const result = await modelName[uidLastCode].findAndCountAll(dbOptions);
|
||
|
|
result.rows.forEach(element => {
|
||
|
|
element.dataValues.account_name = '';
|
||
|
|
user.filter(item => {
|
||
|
|
if (item.dataValues.uid == element.dataValues.user_uid) {
|
||
|
|
element.dataValues.account_name = item.dataValues.account_name;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
return result.rows.length == 0 ? false : result;
|
||
|
|
} catch (error) {
|
||
|
|
const blockName = fileName + "--" + "getRecord";
|
||
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
||
|
|
return 'error';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 存储消息记录
|
||
|
|
async shortMsg(form) {
|
||
|
|
const { ctx } = this;
|
||
|
|
try {
|
||
|
|
const where = { sn_number: form.sn_number };
|
||
|
|
const SN = await ctx.model3.SNNumber.findOne({ where });
|
||
|
|
if (!SN) {
|
||
|
|
ctx.app.middleware.utils.log('../../../unknownSNLog', form);
|
||
|
|
return false;
|
||
|
|
};
|
||
|
|
const model = {
|
||
|
|
1: ctx.model.BuserSNBind,
|
||
|
|
2: ctx.model2.CuserSNBind,
|
||
|
|
3: ctx.model4.TuserSNBind,
|
||
|
|
};
|
||
|
|
const bind = await model[SN.dataValues.status].findOne({ where });
|
||
|
|
if (!bind) return false;
|
||
|
|
form.user_uid = bind.dataValues.user_uid;
|
||
|
|
const uidLastCode = shunt(form.user_uid);
|
||
|
|
const modelName = {
|
||
|
|
"B": ctx.model.BSNMsg,
|
||
|
|
"C": ctx.model2.CSNMsg,
|
||
|
|
"T": ctx.model4.TSNMsg,
|
||
|
|
};
|
||
|
|
const result = await modelName[uidLastCode].create(form);
|
||
|
|
return result || false;
|
||
|
|
} catch (error) {
|
||
|
|
const blockName = fileName + "--" + "shortMsg";
|
||
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
||
|
|
return 'error';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取历史消息
|
||
|
|
async getHistoryMsg(uid, body) {
|
||
|
|
const { ctx } = this;
|
||
|
|
try {
|
||
|
|
let { offset, limit, searchOptions, sortOptions } = body;
|
||
|
|
offset = (offset - 1) * limit;
|
||
|
|
let attributes = ['original_msg', 'sn_number', 'msg_data', 'created_at'],
|
||
|
|
where = { user_uid: uid }, order = [], dbOptions, joinSubstring = [],
|
||
|
|
substring = ['original_msg', 'sn_number', 'msg_data'], key = [], between = ['created_at'];
|
||
|
|
attributes.forEach(element => {
|
||
|
|
if (searchOptions[element]) {
|
||
|
|
if(searchOptions[element] == '*空') {
|
||
|
|
if(joinSubstring.includes(element)) {
|
||
|
|
where[element] = null;
|
||
|
|
} else {
|
||
|
|
where[element] = [null, ''];
|
||
|
|
}
|
||
|
|
} else if (substring.includes(element)) {
|
||
|
|
where[element] = { [Op.substring]: searchOptions[element] };
|
||
|
|
} else if (key.includes(element)) {
|
||
|
|
where[element] = searchOptions[element] == "1" ? true : false;
|
||
|
|
} else if (between.includes(element)) {
|
||
|
|
where[element] = { [Op.between]: [searchOptions[element][0], searchOptions[element][1]] };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (sortOptions[element] == true) {
|
||
|
|
order.push([element, 'ASC']);
|
||
|
|
} else if (sortOptions[element] == false) {
|
||
|
|
order.push([element, 'DESC']);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
dbOptions = { attributes, offset, limit, where, order };
|
||
|
|
const uidLastCode = shunt(uid);
|
||
|
|
const model = {
|
||
|
|
"B": ctx.model.BSNMsg,
|
||
|
|
"C": ctx.model2.CSNMsg,
|
||
|
|
"T": ctx.model4.TSNMsg,
|
||
|
|
};
|
||
|
|
const result = await model[uidLastCode].findAndCountAll(dbOptions);
|
||
|
|
return result.rows.length == 0 ? false : result;
|
||
|
|
} catch (error) {
|
||
|
|
const blockName = fileName + "--" + "getHistoryMsg";
|
||
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
||
|
|
return 'error';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 删除历史消息
|
||
|
|
async delHistoryMsg(uid, modelType, password, requestMsg) {
|
||
|
|
const { ctx } = this;
|
||
|
|
try {
|
||
|
|
const uidLastCode = shunt(uid);
|
||
|
|
const model = {
|
||
|
|
"B": ctx.model.BSide,
|
||
|
|
"C": ctx.model2.CSide,
|
||
|
|
"T": ctx.model4.Account,
|
||
|
|
};
|
||
|
|
const result = await model[uidLastCode].findOne({ where: { uid } });
|
||
|
|
if (!result || result.dataValues.password !== this.shaForm(password, this.decryptRequest(result.dataValues.salt))) return false;
|
||
|
|
let where = {};
|
||
|
|
if (modelType == "single") {
|
||
|
|
const { sn_number, msg_data, origin_time } = requestMsg;
|
||
|
|
where = { user_uid: uid, sn_number, msg_data, created_at: origin_time };
|
||
|
|
} else if (modelType == "multiple") {
|
||
|
|
let sn_number = [], msg_data = [], created_at = [];
|
||
|
|
for (const key in requestMsg) {
|
||
|
|
requestMsg[key].forEach(element => {
|
||
|
|
sn_number.push(element.sn_number),
|
||
|
|
msg_data.push(element.msg_data),
|
||
|
|
created_at.push(element.origin_time)
|
||
|
|
});
|
||
|
|
}
|
||
|
|
where = { user_uid: uid, sn_number, msg_data, created_at };
|
||
|
|
} else if (modelType == "all") {
|
||
|
|
where = { user_uid: uid };
|
||
|
|
}
|
||
|
|
const modelName = {
|
||
|
|
"B": ctx.model.BSNMsg,
|
||
|
|
"C": ctx.model2.CSNMsg,
|
||
|
|
"T": ctx.model4.TSNMsg,
|
||
|
|
};
|
||
|
|
const resetList = await modelName[uidLastCode].destroy({ where });
|
||
|
|
return resetList.length == 0 ? false : resetList;
|
||
|
|
} catch (error) {
|
||
|
|
const blockName = fileName + "--" + "delHistoryMsg";
|
||
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
||
|
|
return 'error';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取单位用户
|
||
|
|
async bsideUserManager(uid, body) {
|
||
|
|
const { ctx } = this;
|
||
|
|
try {
|
||
|
|
const result = await ctx.model.BSide.findOne({ where: { uid, status: { [Op.not]: 2 }, role: 1 } });
|
||
|
|
if (!result || result.organization == null) return false;
|
||
|
|
let { offset, limit, searchOptions } = body;
|
||
|
|
offset = (offset - 1) * limit;
|
||
|
|
let attributes = ['account_name', 'user_name', 'phone'],
|
||
|
|
where = { organization: result.organization, role: { [Op.not]: 1 } }, dbOptions, joinSubstring = [];
|
||
|
|
attributes.forEach(element => {
|
||
|
|
if (searchOptions[element]) {
|
||
|
|
if(searchOptions[element] == '*空') {
|
||
|
|
if(joinSubstring.includes(element)) {
|
||
|
|
where[element] = null;
|
||
|
|
} else {
|
||
|
|
where[element] = [null, ''];
|
||
|
|
}
|
||
|
|
} else{
|
||
|
|
where[element] = { [Op.substring]: searchOptions[element] };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
dbOptions = { attributes, offset, limit, where };
|
||
|
|
const userList = await ctx.model.BSide.findAndCountAll(dbOptions);
|
||
|
|
return userList.rows.length == 0 ? false : userList;
|
||
|
|
} catch (error) {
|
||
|
|
const blockName = fileName + "--" + "bsideUserManager";
|
||
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
||
|
|
return 'error';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取可管理的SN号
|
||
|
|
async bsideSNManager(uid) {
|
||
|
|
const { ctx } = this;
|
||
|
|
try {
|
||
|
|
const result = await ctx.model.BSide.findOne({ where: { uid, status: { [Op.not]: 2 }, role: 1 } });
|
||
|
|
if (!result || result.organization == null) return false;
|
||
|
|
let attributes = ['length', 'sn_set'],
|
||
|
|
where = { user_uid: uid }, dbOptions;
|
||
|
|
dbOptions = { attributes, where };
|
||
|
|
const snMsg = await ctx.model.BuserSNAdmin.findOne(dbOptions);
|
||
|
|
if (!snMsg) return false;
|
||
|
|
const snList = snMsg.dataValues.sn_set.split(',');
|
||
|
|
const snResult = await ctx.model3.SNNumber.findAll({ attributes: ['sn_number', 'status', 'updated_at'], where: { sn_number: { [Op.in]: snList } } });
|
||
|
|
return snResult.length == 0 ? false : snResult;
|
||
|
|
} catch (error) {
|
||
|
|
const blockName = fileName + "--" + "bsideSNManager";
|
||
|
|
ctx.app.middleware.utils.log('../../../serviceLog', blockName + ">>>>" + error);
|
||
|
|
return 'error';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = HomeService;
|