179 lines
8.6 KiB
JavaScript
179 lines
8.6 KiB
JavaScript
|
|
'use strict';
|
||
|
|
|
||
|
|
const Service = require('../core/base_service');
|
||
|
|
|
||
|
|
const { Op } = require('sequelize');
|
||
|
|
|
||
|
|
const fileName = "BindSNRecord";
|
||
|
|
|
||
|
|
class BindSNRecordService extends Service {
|
||
|
|
// 查看SN号绑定记录
|
||
|
|
async showBindSNRecord(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 = {}, order = [], dbOptions, joinSubstring = [],
|
||
|
|
substring = ['user_uid', 'sn_number'],
|
||
|
|
key = ['type'],
|
||
|
|
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];
|
||
|
|
} 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, cside, bsideRecord, csideRecord] = await Promise.all([
|
||
|
|
ctx.model.BSide.findAll({ attributes: ['uid', 'account_name'] }),
|
||
|
|
ctx.model2.CSide.findAll({ attributes: ['uid', 'account_name'] }),
|
||
|
|
ctx.model.BindBSNrecord.findAll({ attributes: ['user_uid', 'sn_number', 'created_at'] }),
|
||
|
|
ctx.model2.BindCSNrecord.findAll({ attributes: ['user_uid', 'sn_number', 'created_at'] })
|
||
|
|
]);
|
||
|
|
if (searchOptions.account_name) {
|
||
|
|
if (searchOptions.account_name == '*空') {
|
||
|
|
where.user_uid = [null, ''];
|
||
|
|
} else {
|
||
|
|
let userArr = [], SNArr = [], createdArr = [];
|
||
|
|
bside.filter(item => {
|
||
|
|
if (item.dataValues.account_name.includes(searchOptions.account_name)) {
|
||
|
|
bsideRecord.filter(item2 => {
|
||
|
|
if (item2.dataValues.user_uid == item.dataValues.uid) {
|
||
|
|
userArr.push(item2.dataValues.user_uid);
|
||
|
|
SNArr.push(item2.dataValues.sn_number);
|
||
|
|
createdArr.push(item2.dataValues.created_at);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
cside.filter(item => {
|
||
|
|
if (item.dataValues.account_name.includes(searchOptions.account_name)) {
|
||
|
|
csideRecord.filter(item2 => {
|
||
|
|
if (item2.dataValues.user_uid == item.dataValues.uid) {
|
||
|
|
userArr.push(item2.dataValues.user_uid);
|
||
|
|
SNArr.push(item2.dataValues.sn_number);
|
||
|
|
createdArr.push(item2.dataValues.created_at);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
where.user_uid = [...userArr];
|
||
|
|
where.sn_number = [...SNArr];
|
||
|
|
where.created_at = [...createdArr];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
dbOptions = { attributes, offset, limit, where, order };
|
||
|
|
let result;
|
||
|
|
if (searchOptions.side !== undefined && searchOptions.side !== '') {
|
||
|
|
if (searchOptions.side == '0') {
|
||
|
|
result = await ctx.model.BindBSNrecord.findAndCountAll(dbOptions);
|
||
|
|
} else if (searchOptions.side == '1') {
|
||
|
|
result = await ctx.model2.BindCSNrecord.findAndCountAll(dbOptions);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
dbOptions.limit = Math.floor(limit / 2);
|
||
|
|
dbOptions.offset = Math.floor(offset / 2);
|
||
|
|
const [bresult, cresult] = await Promise.all([
|
||
|
|
ctx.model.BindBSNrecord.findAndCountAll(dbOptions),
|
||
|
|
ctx.model2.BindCSNrecord.findAndCountAll(dbOptions)
|
||
|
|
]);
|
||
|
|
result = {
|
||
|
|
count: bresult.count + cresult.count,
|
||
|
|
rows: bresult.rows.concat(cresult.rows)
|
||
|
|
};
|
||
|
|
result.rows.sort((a, b) => b.dataValues.created_at - a.dataValues.created_at);
|
||
|
|
}
|
||
|
|
result.rows.forEach(element => {
|
||
|
|
element.dataValues.account_name = '';
|
||
|
|
element.dataValues.side = element.dataValues.user_uid[element.dataValues.user_uid.length - 1] == 'B' ? '0' : '1';
|
||
|
|
bside.filter(item => {
|
||
|
|
if (item.dataValues.uid == element.dataValues.user_uid) {
|
||
|
|
element.dataValues.account_name = item.dataValues.account_name;
|
||
|
|
} else {
|
||
|
|
cside.filter(item2 => {
|
||
|
|
if (item2.dataValues.uid == element.dataValues.user_uid) {
|
||
|
|
element.dataValues.account_name = item2.dataValues.account_name;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
return result.rows.length == 0 ? false : result;
|
||
|
|
} catch (error) {
|
||
|
|
const blockName = fileName + "--" + "showBindSNRecord";
|
||
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||
|
|
return 'error';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 删除SN号绑定记录
|
||
|
|
async deleteBindSNRecord(uid, modelType, password, requestMsg) {
|
||
|
|
const { ctx } = this;
|
||
|
|
const systemBindSNRecord = {
|
||
|
|
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) systemBindSNRecord.status = 0;
|
||
|
|
else if (result.dataValues.password !== this.shaForm(password, this.decryptRequest(result.dataValues.salt))) systemBindSNRecord.status = 1;
|
||
|
|
else if (result.dataValues.role < 9) systemBindSNRecord.status = 2;
|
||
|
|
await ctx.model3.Monitoring.create(systemBindSNRecord);
|
||
|
|
return false;
|
||
|
|
};
|
||
|
|
let where = {};
|
||
|
|
if (modelType == "single") {
|
||
|
|
const { sn_number, type, created_at } = requestMsg;
|
||
|
|
where = { sn_number, type, created_at };
|
||
|
|
} else if (modelType == "multiple") {
|
||
|
|
let sn_number = [], type = [], created_at = [];
|
||
|
|
for (const key in requestMsg) {
|
||
|
|
requestMsg[key].forEach(element => {
|
||
|
|
sn_number.push(element.sn_number),
|
||
|
|
type.push(element.type),
|
||
|
|
created_at.push(element.created_at)
|
||
|
|
});
|
||
|
|
}
|
||
|
|
where = { sn_number, type, created_at };
|
||
|
|
}
|
||
|
|
const [bresultList, cresultList] = await Promise.all([
|
||
|
|
ctx.model.BindBSNrecord.destroy({ where }),
|
||
|
|
ctx.model2.BindCSNrecord.destroy({ where })
|
||
|
|
]);
|
||
|
|
systemBindSNRecord.status = bresultList + cresultList == 0 ? 3 : 4;
|
||
|
|
systemBindSNRecord.affect_entries = bresultList + cresultList;
|
||
|
|
await ctx.model3.Monitoring.create(systemBindSNRecord);
|
||
|
|
return bresultList + cresultList == 0 ? false : true;
|
||
|
|
} catch (error) {
|
||
|
|
systemBindSNRecord.status = 5;
|
||
|
|
await ctx.model3.Monitoring.create(systemBindSNRecord);
|
||
|
|
const blockName = fileName + "--" + "deleteBindSNRecord";
|
||
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
||
|
|
return 'error';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = BindSNRecordService;
|