新增功能
This commit is contained in:
@@ -4,202 +4,299 @@ const Service = require('../core/base_service');
|
||||
|
||||
const { Op } = require('sequelize');
|
||||
|
||||
const moment = require('moment');
|
||||
|
||||
const fileName = "SNAdmin";
|
||||
|
||||
class SNAdminService extends Service {
|
||||
// 搜索可分配的SN号
|
||||
async searchAllocationSN() {
|
||||
const { ctx } = this;
|
||||
try {
|
||||
const where = { status: 0 };
|
||||
const result = await ctx.model3.SNNumber.findAll({ attributes: ['sn_number'], where });
|
||||
return result.length == 0 ? false : result;
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "searchAllocationSN";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
// 搜索可分配的SN号
|
||||
async searchAllocationSN() {
|
||||
const { ctx } = this;
|
||||
try {
|
||||
const where = { status: 0 };
|
||||
const result = await ctx.model3.SNNumber.findAll({ attributes: ['sn_number'], where });
|
||||
return result.length == 0 ? false : result;
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "searchAllocationSN";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
// 搜索可分配的单位用户
|
||||
async searchAllocationUser() {
|
||||
const { ctx } = this;
|
||||
try {
|
||||
const where = { status: { [Op.not]: 2 }, role: 1 };
|
||||
const bside = await ctx.model.BSide.findAll({ attributes: ['uid', 'account_name'], where });
|
||||
const buser = await ctx.model.BuserSNAdmin.findAll({ attributes: ['user_uid'] });
|
||||
let result = [];
|
||||
bside.forEach(element => {
|
||||
buser.filter(item => {
|
||||
if (item.dataValues.user_uid !== element.dataValues.uid) {
|
||||
result.push(element);
|
||||
}
|
||||
});
|
||||
});
|
||||
return result.length == 0 ? false : result;
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "searchAllocationUser";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
// 搜索可分配的单位用户
|
||||
async searchAllocationUser(body) {
|
||||
const { ctx } = this;
|
||||
try {
|
||||
const { mode } = body;
|
||||
const where = { status: { [Op.not]: 2 }, role: 1 };
|
||||
const bside = await ctx.model.BSide.findAll({ attributes: ['uid', 'account_name'], where });
|
||||
const buser = await ctx.model.BuserSNAdmin.findAll({ attributes: ['user_uid', 'length', 'sn_set'] });
|
||||
if (buser.length == 0) return false;
|
||||
const buserUidArr = [];
|
||||
let result = [];
|
||||
bside.forEach(element => {
|
||||
buser.filter(item => {
|
||||
buserUidArr.push(item.dataValues.user_uid);
|
||||
if (mode && item.dataValues.user_uid == element.dataValues.uid && item.dataValues.length > item.dataValues.sn_set.split(',').length) {
|
||||
element.dataValues.length = item.dataValues.length;
|
||||
element.dataValues.sn_set = item.dataValues.sn_set;
|
||||
result.push(element);
|
||||
}else if (!mode && item.dataValues.user_uid == element.dataValues.uid) {
|
||||
element.dataValues.length = item.dataValues.length;
|
||||
element.dataValues.sn_set = item.dataValues.sn_set;
|
||||
result.push(element);
|
||||
}
|
||||
});
|
||||
});
|
||||
if (mode) {
|
||||
const buserUidSet = new Set(buserUidArr);
|
||||
bside.filter(element => !buserUidSet.has(element.dataValues.uid)).forEach(item => {
|
||||
item.dataValues.length = 0;
|
||||
item.dataValues.sn_set = '';
|
||||
result.push(item);
|
||||
});
|
||||
}
|
||||
return result.length == 0 ? false : result;
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "searchAllocationUser";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
// B端分配SN号
|
||||
async allocationSN(uid, body) {
|
||||
const { ctx } = this;
|
||||
try {
|
||||
const result = await ctx.model3.Insiders.findOne({ where: { uid } });
|
||||
if (!result || result.dataValues.role < 6) return false;
|
||||
const { user_uid, length, sn_set } = body;
|
||||
const adminResult = await ctx.model.BuserSNAdmin.create({ user_uid, length, sn_set });
|
||||
if (adminResult == 0) return false;
|
||||
const where = { sn_number: sn_set.split(',') };
|
||||
const updateMsg = { status: 4, updated_at: new Date() };
|
||||
const resultList = await ctx.model3.SNNumber.update(updateMsg, { where });
|
||||
return resultList[0] == 0 ? false : resultList;
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "allocationSN";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
// B端分配SN号
|
||||
async allocationSN(uid, body) {
|
||||
const { ctx } = this;
|
||||
try {
|
||||
const result = await ctx.model3.Insiders.findOne({ where: { uid } });
|
||||
if (!result || result.dataValues.role < 6) return false;
|
||||
const { user_uid, length, sn_set } = body;
|
||||
let status, sn_number;
|
||||
const existence = await ctx.model.BuserSNAdmin.findOne({ attributes: ['sn_set'], where: { user_uid: user_uid } });
|
||||
if (existence) {
|
||||
const adminResult = await ctx.model.BuserSNAdmin.update({ length, sn_set }, { where: { user_uid: user_uid } });
|
||||
if (adminResult[0] == 0) return false;
|
||||
const newSNArr = sn_set.split(',');
|
||||
const oldSNArr = existence.dataValues.sn_set.split(',');
|
||||
if (newSNArr.length > oldSNArr.length) {
|
||||
status = 4;
|
||||
sn_number = newSNArr.filter(item => !oldSNArr.includes(item));
|
||||
} else if (newSNArr.length < oldSNArr.length) {
|
||||
status = 0;
|
||||
sn_number = oldSNArr.filter(item => !newSNArr.includes(item));
|
||||
}
|
||||
} else {
|
||||
const adminResult = await ctx.model.BuserSNAdmin.create({ user_uid, length, sn_set });
|
||||
if (adminResult == 0) return false;
|
||||
status = 4;
|
||||
sn_number = sn_set.split(',');
|
||||
}
|
||||
const where = { sn_number };
|
||||
const updateMsg = { status, updated_at: new Date() };
|
||||
const resultList = await ctx.model3.SNNumber.update(updateMsg, { where });
|
||||
return resultList[0] == 0 ? false : resultList;
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "allocationSN";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
// 查看B端SN号分配记录
|
||||
async showAllocationSNRecord(body) {
|
||||
const { ctx } = this;
|
||||
try {
|
||||
let { offset, limit, searchOptions, sortOptions } = body;
|
||||
offset = (offset - 1) * limit;
|
||||
let attributes = ['user_uid', 'length', 'sn_set'],
|
||||
where = {}, order = [], dbOptions, joinSubstring = [],
|
||||
substring = ['user_uid', 'sn_set'],
|
||||
key = ['length'],
|
||||
between = [];
|
||||
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];
|
||||
} 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']);
|
||||
}
|
||||
});
|
||||
// const [bside, bsideSNRecord] = await Promise.all([
|
||||
// ctx.model.BSide.findAll({ attributes: ['uid', 'account_name'] }),
|
||||
// ctx.model.BindBSNrecord.findAll({ attributes: ['user_uid', 'sn_number', 'type'] })
|
||||
// ]);
|
||||
// if (searchOptions.account_name) {
|
||||
// if (searchOptions.account_name == '*空') {
|
||||
// where.user_uid = [null, ''];
|
||||
// } else {
|
||||
// let userArr = [], lengthArr = [], SNArr = [];
|
||||
// bside.filter(item => {
|
||||
// if (item.dataValues.account_name.includes(searchOptions.account_name)) {
|
||||
// bsideSNRecord.filter(item2 => {
|
||||
// if (item2.dataValues.user_uid == item.dataValues.uid) {
|
||||
// userArr.push(item2.dataValues.user_uid);
|
||||
// lengthArr.push(item2.dataValues.length);
|
||||
// SNArr.push(item2.dataValues.sn_set);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// });
|
||||
// where.user_uid = [...userArr];
|
||||
// where.length = [...lengthArr];
|
||||
// where.sn_set = [...SNArr];
|
||||
// }
|
||||
// }
|
||||
dbOptions = { attributes, offset, limit, where, order };
|
||||
// let result;
|
||||
dbOptions.limit = Math.floor(limit / 2);
|
||||
dbOptions.offset = Math.floor(offset / 2);
|
||||
// const [bresult] = await Promise.all([
|
||||
// ctx.model.BuserSNAdmin.findAndCountAll(dbOptions)
|
||||
// ]);
|
||||
// result = {
|
||||
// count: bresult.count,
|
||||
// rows: bresult.rows
|
||||
// };
|
||||
const bside = await ctx.model.BSide.findAll({ attributes: ['uid', 'account_name'] });
|
||||
const result = await ctx.model.BuserSNAdmin.findAndCountAll(dbOptions);
|
||||
result.rows.forEach(element => {
|
||||
element.dataValues.account_name = '';
|
||||
bside.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 + "--" + "showAllocationSNRecord";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
// 删除B端SN号分配记录
|
||||
async deleteAllocationSNRecord(uid, modelType, password, requestMsg) {
|
||||
const { ctx } = this;
|
||||
const systemSNAdmin = {
|
||||
operate: 2,
|
||||
operate_msg: '删除SN号分配记录',
|
||||
type: 1,
|
||||
status: 0,
|
||||
affect_entries: 0,
|
||||
user: uid,
|
||||
created_at: new Date()
|
||||
};
|
||||
try {
|
||||
const result = await ctx.model3.Insiders.findOne({ where: { uid } });
|
||||
if (!result || result.dataValues.password !== this.shaForm(password, this.decryptRequest(result.dataValues.salt)) || result.dataValues.role < 9) {
|
||||
if (!result) systemSNAdmin.status = 0;
|
||||
else if (result.dataValues.password !== this.shaForm(password, this.decryptRequest(result.dataValues.salt))) systemSNAdmin.status = 1;
|
||||
else if (result.dataValues.role < 9) systemSNAdmin.status = 2;
|
||||
await ctx.model3.Monitoring.create(systemSNAdmin);
|
||||
return false;
|
||||
};
|
||||
let where = {};
|
||||
if (modelType == "single") {
|
||||
const { sn_set } = requestMsg;
|
||||
where = { sn_set };
|
||||
} else if (modelType == "multiple") {
|
||||
let sn_set = [];
|
||||
for (const key in requestMsg) {
|
||||
requestMsg[key].forEach(element => {
|
||||
sn_set.push(element.sn_set)
|
||||
});
|
||||
}
|
||||
where = { sn_set };
|
||||
// 查看B端SN号分配记录
|
||||
async showAllocationSNRecord(body) {
|
||||
const { ctx } = this;
|
||||
try {
|
||||
let { offset, limit, searchOptions, sortOptions } = body;
|
||||
offset = (offset - 1) * limit;
|
||||
let attributes = ['user_uid', 'length', 'sn_set'],
|
||||
where = {}, order = [], dbOptions, joinSubstring = [],
|
||||
substring = ['user_uid', 'sn_set'],
|
||||
key = ['length'],
|
||||
between = [];
|
||||
attributes.forEach(element => {
|
||||
if (searchOptions[element]) {
|
||||
if (searchOptions[element] == '*空') {
|
||||
if (joinSubstring.includes(element)) {
|
||||
where[element] = null;
|
||||
} else {
|
||||
where[element] = [null, ''];
|
||||
}
|
||||
const [bresultList] = await Promise.all([
|
||||
ctx.model.BuserSNAdmin.destroy({ where })
|
||||
]);
|
||||
systemSNAdmin.status = bresultList == 0 ? 3 : 4;
|
||||
systemSNAdmin.affect_entries = bresultList;
|
||||
await ctx.model3.Monitoring.create(systemSNAdmin);
|
||||
return bresultList == 0 ? false : true;
|
||||
} catch (error) {
|
||||
systemSNAdmin.status = 5;
|
||||
await ctx.model3.Monitoring.create(systemSNAdmin);
|
||||
const blockName = fileName + "--" + "deleteAllocationSNRecord";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
} else if (substring.includes(element)) {
|
||||
where[element] = { [Op.substring]: searchOptions[element] };
|
||||
} else if (key.includes(element)) {
|
||||
where[element] = searchOptions[element];
|
||||
} 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']);
|
||||
}
|
||||
});
|
||||
// const [bside, bsideSNRecord] = await Promise.all([
|
||||
// ctx.model.BSide.findAll({ attributes: ['uid', 'account_name'] }),
|
||||
// ctx.model.BindBSNrecord.findAll({ attributes: ['user_uid', 'sn_number', 'type'] })
|
||||
// ]);
|
||||
// if (searchOptions.account_name) {
|
||||
// if (searchOptions.account_name == '*空') {
|
||||
// where.user_uid = [null, ''];
|
||||
// } else {
|
||||
// let userArr = [], lengthArr = [], SNArr = [];
|
||||
// bside.filter(item => {
|
||||
// if (item.dataValues.account_name.includes(searchOptions.account_name)) {
|
||||
// bsideSNRecord.filter(item2 => {
|
||||
// if (item2.dataValues.user_uid == item.dataValues.uid) {
|
||||
// userArr.push(item2.dataValues.user_uid);
|
||||
// lengthArr.push(item2.dataValues.length);
|
||||
// SNArr.push(item2.dataValues.sn_set);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// });
|
||||
// where.user_uid = [...userArr];
|
||||
// where.length = [...lengthArr];
|
||||
// where.sn_set = [...SNArr];
|
||||
// }
|
||||
// }
|
||||
dbOptions = { attributes, offset, limit, where, order };
|
||||
// let result;
|
||||
dbOptions.limit = Math.floor(limit / 2);
|
||||
dbOptions.offset = Math.floor(offset / 2);
|
||||
// const [bresult] = await Promise.all([
|
||||
// ctx.model.BuserSNAdmin.findAndCountAll(dbOptions)
|
||||
// ]);
|
||||
// result = {
|
||||
// count: bresult.count,
|
||||
// rows: bresult.rows
|
||||
// };
|
||||
const bside = await ctx.model.BSide.findAll({ attributes: ['uid', 'account_name'] });
|
||||
const result = await ctx.model.BuserSNAdmin.findAndCountAll(dbOptions);
|
||||
result.rows.forEach(element => {
|
||||
element.dataValues.account_name = '';
|
||||
bside.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 + "--" + "showAllocationSNRecord";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
// 删除B端SN号分配记录
|
||||
async deleteAllocationSNRecord(uid, modelType, password, requestMsg) {
|
||||
const { ctx } = this;
|
||||
const systemSNAdmin = {
|
||||
operate: 2,
|
||||
operate_msg: '删除SN号分配记录',
|
||||
type: 1,
|
||||
status: 0,
|
||||
affect_entries: 0,
|
||||
user: uid,
|
||||
created_at: new Date()
|
||||
};
|
||||
try {
|
||||
const result = await ctx.model3.Insiders.findOne({ where: { uid } });
|
||||
if (!result || result.dataValues.password !== this.shaForm(password, this.decryptRequest(result.dataValues.salt)) || result.dataValues.role < 9) {
|
||||
if (!result) systemSNAdmin.status = 0;
|
||||
else if (result.dataValues.password !== this.shaForm(password, this.decryptRequest(result.dataValues.salt))) systemSNAdmin.status = 1;
|
||||
else if (result.dataValues.role < 9) systemSNAdmin.status = 2;
|
||||
await ctx.model3.Monitoring.create(systemSNAdmin);
|
||||
return false;
|
||||
};
|
||||
let where = {};
|
||||
if (modelType == "single") {
|
||||
const { sn_set } = requestMsg;
|
||||
where = { sn_set };
|
||||
} else if (modelType == "multiple") {
|
||||
let sn_set = [];
|
||||
for (const key in requestMsg) {
|
||||
requestMsg[key].forEach(element => {
|
||||
sn_set.push(element.sn_set)
|
||||
});
|
||||
}
|
||||
where = { sn_set };
|
||||
}
|
||||
const [bresultList] = await Promise.all([
|
||||
ctx.model.BuserSNAdmin.destroy({ where })
|
||||
]);
|
||||
systemSNAdmin.status = bresultList == 0 ? 3 : 4;
|
||||
systemSNAdmin.affect_entries = bresultList;
|
||||
await ctx.model3.Monitoring.create(systemSNAdmin);
|
||||
return bresultList == 0 ? false : true;
|
||||
} catch (error) {
|
||||
systemSNAdmin.status = 5;
|
||||
await ctx.model3.Monitoring.create(systemSNAdmin);
|
||||
const blockName = fileName + "--" + "deleteAllocationSNRecord";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
|
||||
// 管理系统导出B端SN号分配记录
|
||||
async downloadAllocationSNRecord(uid, searchMsg) {
|
||||
const { ctx } = this;
|
||||
try {
|
||||
const user = await ctx.model3.Insiders.findOne({ where: { uid } });
|
||||
if (!user || user.dataValues.role < 3) return false;
|
||||
let { searchOptions, sortOptions } = searchMsg;
|
||||
let attributes = ['user_uid', 'length', 'sn_set'],
|
||||
where = {}, order = [], dbOptions,
|
||||
joinSubstring = [],
|
||||
substring = ['user_uid', 'length', 'sn_set'],
|
||||
key = [],
|
||||
between = [];
|
||||
attributes.forEach(element => {
|
||||
if (searchOptions[element]) {
|
||||
if (searchOptions[element] == '*空') {
|
||||
if (joinSubstring.includes(element)) {
|
||||
where[element] = null;
|
||||
} else {
|
||||
where[element] = [null, ''];
|
||||
}
|
||||
} else if (joinSubstring.includes(element)) {
|
||||
let targetKeyArr = [];
|
||||
where[element] = targetKeyArr.length > 0 ? targetKeyArr : 'null';
|
||||
} else if (substring.includes(element)) {
|
||||
where[element] = { [Op.substring]: searchOptions[element] };
|
||||
} else if (key.includes(element)) {
|
||||
where[element] = searchOptions[element];
|
||||
} 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, where, order };
|
||||
const [bresult, cresult] = await Promise.all([
|
||||
ctx.model.BuserSNAdmin.findAll(dbOptions),
|
||||
ctx.model2.CuserSNAdmin.findAll(dbOptions)
|
||||
]);
|
||||
if (bresult.length == 0 && cresult.length == 0) return { code: false };
|
||||
let data = [];
|
||||
bresult.forEach(element => {
|
||||
data.push(element.dataValues);
|
||||
});
|
||||
cresult.forEach(element => {
|
||||
data.push(element.dataValues);
|
||||
});
|
||||
return { code: true, data };
|
||||
} catch (error) {
|
||||
const blockName = fileName + "--" + "downloadAllocationSNRecord";
|
||||
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SNAdminService;
|
||||
Reference in New Issue
Block a user