171 lines
6.5 KiB
JavaScript
171 lines
6.5 KiB
JavaScript
'use strict';
|
|
|
|
const Service = require('../core/base_service');
|
|
|
|
const { Op } = require('sequelize');
|
|
|
|
const fileName = "Organization";
|
|
|
|
class OrganizationService extends Service {
|
|
// 注册B端用户单位
|
|
async addOrganization(form) {
|
|
const { ctx } = this;
|
|
try {
|
|
const result = await ctx.model3.Organization.bulkCreate(form);
|
|
return result.length == 0 ? false : true;
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "addOrganization";
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
|
return 'error';
|
|
}
|
|
}
|
|
|
|
// 查看B端用户单位
|
|
async showOrganization(body) {
|
|
const { ctx } = this;
|
|
try {
|
|
let { offset, limit, searchOptions, sortOptions } = body;
|
|
offset = (offset - 1) * limit;
|
|
let attributes = ['uid', 'organization_name', 'address', 'liaison_name', 'phone', 'email', 'other_information', 'post_code', 'created_at', 'updated_at'],
|
|
where = {}, order = [], dbOptions, joinSubstring = [],
|
|
substring = ['uid', 'organization_name', 'address', 'liaison_name', 'phone', 'email', 'other_information', 'post_code'],
|
|
key = [], between = ['created_at', 'updated_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']);
|
|
}
|
|
});
|
|
dbOptions = { attributes, offset, limit, where, order };
|
|
const result = await ctx.model3.Organization.findAndCountAll(dbOptions);
|
|
return result.rows.length == 0 ? false : result;
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "showOrganization";
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
|
return 'error';
|
|
}
|
|
}
|
|
|
|
//远程搜索B端用户单位
|
|
async SearchOrganization(body) {
|
|
const { ctx } = this;
|
|
try {
|
|
let { query, key } = body;
|
|
let attributes = ['uid', 'organization_name', 'created_at'], where = {}, dbOptions;
|
|
if (query == '*空') {
|
|
delete where[key]
|
|
} else {
|
|
where[key] = { [Op.substring]: query };
|
|
}
|
|
dbOptions = { attributes, where };
|
|
const Organization = await ctx.model3.Organization.findAll(dbOptions);
|
|
return Organization.length == 0 ? false : Organization;
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "SearchOrganization";
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
|
return 'error';
|
|
}
|
|
}
|
|
|
|
// 编辑B端用户单位
|
|
async editOrganization(uid, modelType, updateMsg, requestMsg) {
|
|
const { ctx } = this;
|
|
try {
|
|
const result = await ctx.model3.Insiders.findOne({ where: { uid } });
|
|
if (!result || result.dataValues.role < 6) {
|
|
if (!result) systemMonitoring.status = 0;
|
|
else if (result.dataValues.role < 6) systemMonitoring.status = 2;
|
|
await ctx.model3.Monitoring.create(systemMonitoring);
|
|
return false;
|
|
};
|
|
let where = {};
|
|
if (modelType == "single") {
|
|
const { uid, organization_name } = requestMsg;
|
|
where = { uid, organization_name };
|
|
} else if (modelType == "multiple") {
|
|
let uid = [], organization_name = [];
|
|
for (const key in requestMsg) {
|
|
requestMsg[key].forEach(element => {
|
|
uid.push(element.uid),
|
|
organization_name.push(element.organization_name)
|
|
});
|
|
}
|
|
where = { uid, organization_name };
|
|
}
|
|
updateMsg.updated_at = new Date();
|
|
const resultList = await ctx.model3.Organization.update(updateMsg, { where });
|
|
return resultList[0] == 0 ? false : resultList;
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "editOrganization";
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
|
return 'error';
|
|
}
|
|
}
|
|
|
|
// 删除B端用户单位
|
|
async deleteOrganization(uid, modelType, password, requestMsg) {
|
|
const { ctx } = this;
|
|
const systemMonitoring = {
|
|
operate: 2,
|
|
operate_msg: '删除B端用户单位',
|
|
type: 0,
|
|
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 < 6) {
|
|
if (!result) systemMonitoring.status = 0;
|
|
else if (result.dataValues.password !== this.shaForm(password, this.decryptRequest(result.dataValues.salt))) systemMonitoring.status = 1;
|
|
else if (result.dataValues.role < 6) systemMonitoring.status = 2;
|
|
await ctx.model3.Monitoring.create(systemMonitoring);
|
|
return false;
|
|
};
|
|
let where = {};
|
|
if (modelType == "single") {
|
|
const { uid, organization_name } = requestMsg;
|
|
where = { uid, organization_name };
|
|
} else if (modelType == "multiple") {
|
|
let uid = [], organization_name = [];
|
|
for (const key in requestMsg) {
|
|
requestMsg[key].forEach(element => {
|
|
uid.push(element.uid),
|
|
organization_name.push(element.organization_name)
|
|
});
|
|
}
|
|
where = { uid, organization_name };
|
|
}
|
|
const resultList = await ctx.model3.Organization.destroy({ where });
|
|
systemMonitoring.status = resultList == 0 ? 3 : 4;
|
|
systemMonitoring.affect_entries = resultList;
|
|
await ctx.model3.Monitoring.create(systemMonitoring);
|
|
return resultList == 0 ? false : true;
|
|
} catch (error) {
|
|
systemMonitoring.status = 5;
|
|
await ctx.model3.Monitoring.create(systemMonitoring);
|
|
const blockName = fileName + "--" + "deleteOrganization";
|
|
ctx.app.middleware.log('../../serviceLog', blockName + ">>>>" + error);
|
|
return 'error';
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = OrganizationService; |