123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- 'use strict';
- const Service = require('../service/baseService');
- class SysDeptService extends Service {
- tag() {
- return this.ctx.model.SysDeptModel;
- }
- async addDeptAndPerson(data) {
- const { ctx, service } = this;
- const listByCode = await service.sysDeptService.list({ code: data.code });
- if (listByCode.length > 0) {
- return '地区编码已存在';
- }
- const order = await service.sysDeptService.getMaxOrder();
- data.order = order;
- if (data.fid == '0') {
- data.level = '1';
- } else {
- const dept = await service.sysDeptService.one(data.fid);
- data.level = (parseInt(dept.level) + 1) + '';
- }
- const result = await ctx.model.SysDeptModel.create(data);
- const arr = await this.findDept(result._id, data.fid);
- const dept1 = arr[0];
- const dept2 = arr[1];
- const dept3 = arr[2];
- const dept4 = arr[3];
- const dept5 = arr[4];
- const userData = { type: 'admin', deptId: result._id,
- dept1, dept2, dept3, dept4, dept5 };
- await ctx.service.sysUserService.batchAdd(userData);
- return null;
- }
- async updateDeptAndPerson(id, data) {
- const { ctx, service } = this;
- const { model } = ctx;
- const dept = await this.one(id);
- const code = dept ? dept.code : data.code;
- delete data.fid;
- const listByCode = await service.sysDeptService.list({ code: data.code });
- if (listByCode.length > 0 && listByCode[0]._id != id && listByCode[0].code == data.code) {
- return '地区编码已存在';
- }
- await model.SysDeptModel.updateOne({ _id: id }, data);
- if (code != data.code) {
- const codes = await model.SysDeptModel.find({ code: { $regex: '^' + code } });
- for (const c of codes) {
- const replaceName = c.code.replace(code, data.code);
- await model.SysDeptModel.updateOne(
- { _id: c._id },
- { code: replaceName });
- }
- const users = await model.SysUserModel.find({ loginName: { $regex: '^' + code } });
- for (const user of users) {
- const replaceName = user.loginName.replace(code, data.code);
- await model.SysUserModel.updateOne(
- { _id: user._id },
- { loginName: replaceName });
- }
- }
- return null;
- }
- async dataMigration(id, data) {
- const { ctx, service } = this;
- const { model } = ctx;
- const dept = await this.one(id);
- const code = dept ? dept.code : data.code;
- const listByCode = await service.sysDeptService.list({ code: data.code });
- let progress = '';
- if (listByCode.length > 0 && listByCode[0]._id != id && listByCode[0].code == data.code) {
- return '地区编码已存在';
- }
- await model.SysDeptModel.updateOne({ _id: id }, data);
- if (code != data.code) {
- const codes = await model.SysDeptModel.find({ code: { $regex: '^' + code } });
- for (const c of codes) {
- const replaceName = c.code.replace(code, data.code);
- await model.SysDeptModel.updateOne({ _id: c._id }, { code: replaceName });
- }
- const users = await model.SysUserModel.find({ loginName: { $regex: '^' + code } });
- for (const user of users) {
- const replaceName = user.loginName.replace(code, data.code);
- await model.SysUserModel.updateOne({ _id: user._id }, { loginName: replaceName });
- }
- }
- if (data.level) {
- const depts = await model.SysUserModel.find({ ['dept' + data.level]: id });
- const infos = await model.InfoModel.find({ ['dept' + data.level]: id });
- const values = await model.ValueModel.find({ ['dept' + data.level]: id });
- const visits = await model.VisitModel.find({ ['dept' + data.level]: id });
- //
- const urgencys = await model.UrgencyModel.find({ ['dept' + data.level]: id });
- const overRanges = await model.OverRangeModel.find({ ['dept' + data.level]: id });
- const notices = await model.NoticeModel.find({ ['dept' + data.level]: id });
- const familys = await model.FamilyModel.find({ ['dept' + data.level]: id });
- //
- if (Object.keys(depts).length > 0) {
- for (const dept of depts) {
- const dd = await model.SysUserModel.updateOne({ _id: dept._id }, { ['dept' + (parseInt(data.level) - 1)]: data.fid });
- console.log('dd', dd);
- }
- progress += '地区表已修改';
- } else {
- progress += '地区表未变动';
- }
- if (Object.keys(infos).length > 0) {
- for (const info of infos) {
- const cc = await model.InfoModel.updateOne({ _id: info._id }, { ['dept' + (parseInt(data.level) - 1)]: data.fid });
- console.log('cc', cc);
- }
- progress += '采集表已修改';
- } else {
- progress += '采集表未变动';
- }
- if (Object.keys(values).length > 0) {
- for (const value of values) {
- const dd = await model.ValueModel.updateOne({ _id: value._id }, { ['dept' + (parseInt(data.level) - 1)]: data.fid });
- console.log('dd', dd);
- }
- progress += '积分表已修改';
- } else {
- progress += '积分表未变动';
- }
- if (Object.keys(visits).length > 0) {
- for (const visit of visits) {
- const ee = await model.VisitModel.updateOne({ _id: visit._id }, { ['dept' + (parseInt(data.level) - 1)]: data.fid });
- console.log('ee', ee);
- }
- progress += '积分表已修改';
- } else {
- progress += '积分表未变动';
- }
- //
- if (Object.keys(urgencys).length > 0) {
- for (const urgency of urgencys) {
- const hh = await model.UrgencyModel.updateOne({ _id: urgency._id }, { ['dept' + (parseInt(data.level) - 1)]: data.fid });
- console.log('hh', hh);
- }
- progress += '紧急通知表已修改';
- } else {
- progress += '紧急通知表未变动';
- }
- if (Object.keys(overRanges).length > 0) {
- for (const overRange of overRanges) {
- const ii = await model.OverRangeModel.updateOne({ _id: overRange._id }, { ['dept' + (parseInt(data.level) - 1)]: data.fid });
- console.log('ii', ii);
- }
- progress += 'overRange表已修改';
- } else {
- progress += 'overRange表未变动';
- }
- if (Object.keys(familys).length > 0) {
- for (const family of familys) {
- const gg = await model.FamilyModel.updateOne({ _id: family._id }, { ['dept' + (parseInt(data.level) - 1)]: data.fid });
- console.log('gg', gg);
- }
- progress += '家庭表已修改';
- } else {
- progress += '家庭表未变动';
- }
- if (Object.keys(notices).length > 0) {
- for (const notice of notices) {
- const hh = await model.NoticeModel.updateOne({ _id: notice._id }, { ['dept' + (parseInt(data.level) - 1)]: data.fid });
- console.log('hh', hh);
- }
- progress += '通知表已修改';
- } else {
- progress += '通知表未变动';
- }
- //
- }
- console.log(progress, 'progress');
- return null;
- }
- async listDept(fid) {
- const result = await this.one(fid);
- const list = await this.tag().find({ fid: result.fid });
- console.log(list, 'list');
- return list;
- }
- async orderChange(id, type) {
- const { model } = this.ctx;
- const dept = await model.SysDeptModel.findById(id);
- let where = {};
- let sort = {};
- if (type === 'down') {
- where = { order: { $gte: dept.order }, fid: dept.fid };
- sort = { order: 1 };
- }
- if (type === 'up') {
- where = { order: { $lte: dept.order }, fid: dept.fid };
- sort = { order: -1 };
- }
- const result = await model.SysDeptModel.find(where)
- .sort(sort)
- .skip(0)
- .limit(2);
- if (result.length === 2) {
- await model.SysDeptModel.updateOne({ _id: result[0]._id }, { order: result[1].order });
- await model.SysDeptModel.updateOne({ _id: result[1]._id }, { order: result[0].order });
- return null;
- }
- return '更新排序异常';
- }
- async getMaxOrder() {
- const { model } = this.ctx;
- const result = await model.SysDeptModel.find()
- .sort({ order: -1 })
- .skip(0)
- .limit(1);
- let max_order = 0;
- if (result.length > 0 && result[0].order) {
- max_order = result[0].order;
- }
- max_order = max_order + 1;
- return max_order;
- }
- async deleteWithSub(id) {
- const { model } = this.ctx;
- const dbDeptArr = await model.SysDeptModel.find({ fid: id });
- if (dbDeptArr.length > 0) {
- return '删除失败,该地区存在下级地区';
- }
- const dbUserArr = await model.SysUserModel.find({ dept: id });
- if (dbUserArr.length > 0) {
- if (dbUserArr.length === 1) {
- if (dbUserArr[0].role == this.app.config.defaultOwnerManagerRoleId) {
- await model.SysUserModel.deleteOne({ _id: dbUserArr[0]._id });
- } else {
- return '删除失败,该地区下存在人员';
- }
- } else {
- return '删除失败,该地区下存在人员';
- }
- }
- await model.SysDeptModel.deleteOne({ _id: id });
- return null;
- }
- async listForPageSortWithUser(data) {
- const deptId = data.deptId;
- const roleId = data.roleId;
- const fid = data.fid;
- delete data.deptId;
- delete data.roleId;
- delete data.level;
- // type 代表前端是否需要显示添加按钮 true需要 false 不需要
- let type = true;
- if (fid === '0') {
- if (roleId == this.app.config.defaultUserRoleId) { // 采集员
- data.fid = '-1';
- type = false;
- } else if (roleId == this.app.config.defaultManagerRoleId) { // 地区管理员
- delete data.fid;
- data._id = deptId;
- type = false;
- }
- } else if (fid === '') { // 第五级管理员登陆
- type = false;
- } else {
- const fidResult = await this.tag().find({ _id: fid });
- if (fidResult[0].level === '5') {
- type = false;
- }
- }
- const result = await super.listForPage(data);
- result.type = type;
- return result;
- }
- async findDept(id, fid) {
- const arr = [ id ];
- while (fid != '0') { // 最多3次 比如是最底层的5级,那么知道5.4,只需要找3次
- const result = await this.one(fid);
- arr.unshift(fid);
- fid = result.fid;
- }
- return arr;
- }
- async findDeptDetail(dept, fid) {
- const arr = [ dept ];
- while (fid != '0') { // 最多3次 比如是最底层的5级,那么知道5.4,只需要找3次
- const result = await this.one(fid);
- arr.unshift(result);
- fid = result.fid;
- }
- return arr;
- }
- async findLink() {
- const result = await this.list({ name: { $regex: this.ctx.query.name } });
- const arr = [];
- for (let i = 0; i < result.length; i++) {
- const deptArr = await this.findDeptDetail(result[i], result[i].fid);
- arr.push(deptArr);
- }
- return arr;
- }
- }
- module.exports = SysDeptService;
|