Files
account-manager/app/service/SNAdmin.js

205 lines
8.9 KiB
JavaScript
Raw Normal View History

'use strict';
const Service = require('../core/base_service');
const { Op } = require('sequelize');
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';
}
}
// 搜索可分配的单位用户
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';
}
}
// 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 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 };
}
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';
}
}
}
module.exports = SNAdminService;