sysDeptService.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. 'use strict';
  2. const Service = require('../service/baseService');
  3. class SysDeptService extends Service {
  4. tag() {
  5. return this.ctx.model.SysDeptModel;
  6. }
  7. async addDeptAndPerson(data) {
  8. const { ctx, service } = this;
  9. const listByCode = await service.sysDeptService.list({ code: data.code });
  10. if (listByCode.length > 0) {
  11. return '地区编码已存在';
  12. }
  13. const order = await service.sysDeptService.getMaxOrder();
  14. data.order = order;
  15. if (data.fid == '0') {
  16. data.level = '1';
  17. } else {
  18. const dept = await service.sysDeptService.one(data.fid);
  19. data.level = (parseInt(dept.level) + 1) + '';
  20. }
  21. const result = await ctx.model.SysDeptModel.create(data);
  22. const arr = await this.findDept(result._id, data.fid);
  23. const dept1 = arr[0];
  24. const dept2 = arr[1];
  25. const dept3 = arr[2];
  26. const dept4 = arr[3];
  27. const dept5 = arr[4];
  28. const userData = { type: 'admin', deptId: result._id,
  29. dept1, dept2, dept3, dept4, dept5 };
  30. await ctx.service.sysUserService.batchAdd(userData);
  31. return null;
  32. }
  33. async updateDeptAndPerson(id, data) {
  34. const { ctx, service } = this;
  35. const { model } = ctx;
  36. const dept = await this.one(id);
  37. const code = dept ? dept.code : data.code;
  38. delete data.fid;
  39. const listByCode = await service.sysDeptService.list({ code: data.code });
  40. if (listByCode.length > 0 && listByCode[0]._id != id && listByCode[0].code == data.code) {
  41. return '地区编码已存在';
  42. }
  43. await model.SysDeptModel.updateOne({ _id: id }, data);
  44. if (code != data.code) {
  45. const codes = await model.SysDeptModel.find({ code: { $regex: '^' + code } });
  46. for (const c of codes) {
  47. const replaceName = c.code.replace(code, data.code);
  48. await model.SysDeptModel.updateOne(
  49. { _id: c._id },
  50. { code: replaceName });
  51. }
  52. const users = await model.SysUserModel.find({ loginName: { $regex: '^' + code } });
  53. for (const user of users) {
  54. const replaceName = user.loginName.replace(code, data.code);
  55. await model.SysUserModel.updateOne(
  56. { _id: user._id },
  57. { loginName: replaceName });
  58. }
  59. }
  60. return null;
  61. }
  62. async dataMigration(id, data) {
  63. const { ctx, service } = this;
  64. const { model } = ctx;
  65. const dept = await this.one(id);
  66. const code = dept ? dept.code : data.code;
  67. const listByCode = await service.sysDeptService.list({ code: data.code });
  68. let progress = '';
  69. if (listByCode.length > 0 && listByCode[0]._id != id && listByCode[0].code == data.code) {
  70. return '地区编码已存在';
  71. }
  72. await model.SysDeptModel.updateOne({ _id: id }, data);
  73. if (code != data.code) {
  74. const codes = await model.SysDeptModel.find({ code: { $regex: '^' + code } });
  75. for (const c of codes) {
  76. const replaceName = c.code.replace(code, data.code);
  77. await model.SysDeptModel.updateOne({ _id: c._id }, { code: replaceName });
  78. }
  79. const users = await model.SysUserModel.find({ loginName: { $regex: '^' + code } });
  80. for (const user of users) {
  81. const replaceName = user.loginName.replace(code, data.code);
  82. await model.SysUserModel.updateOne({ _id: user._id }, { loginName: replaceName });
  83. }
  84. }
  85. if (data.level) {
  86. const depts = await model.SysUserModel.find({ ['dept' + data.level]: id });
  87. const infos = await model.InfoModel.find({ ['dept' + data.level]: id });
  88. const values = await model.ValueModel.find({ ['dept' + data.level]: id });
  89. const visits = await model.VisitModel.find({ ['dept' + data.level]: id });
  90. //
  91. const urgencys = await model.UrgencyModel.find({ ['dept' + data.level]: id });
  92. const overRanges = await model.OverRangeModel.find({ ['dept' + data.level]: id });
  93. const notices = await model.NoticeModel.find({ ['dept' + data.level]: id });
  94. const familys = await model.FamilyModel.find({ ['dept' + data.level]: id });
  95. //
  96. if (Object.keys(depts).length > 0) {
  97. for (const dept of depts) {
  98. const dd = await model.SysUserModel.updateOne({ _id: dept._id }, { ['dept' + (parseInt(data.level) - 1)]: data.fid });
  99. console.log('dd', dd);
  100. }
  101. progress += '地区表已修改';
  102. } else {
  103. progress += '地区表未变动';
  104. }
  105. if (Object.keys(infos).length > 0) {
  106. for (const info of infos) {
  107. const cc = await model.InfoModel.updateOne({ _id: info._id }, { ['dept' + (parseInt(data.level) - 1)]: data.fid });
  108. console.log('cc', cc);
  109. }
  110. progress += '采集表已修改';
  111. } else {
  112. progress += '采集表未变动';
  113. }
  114. if (Object.keys(values).length > 0) {
  115. for (const value of values) {
  116. const dd = await model.ValueModel.updateOne({ _id: value._id }, { ['dept' + (parseInt(data.level) - 1)]: data.fid });
  117. console.log('dd', dd);
  118. }
  119. progress += '积分表已修改';
  120. } else {
  121. progress += '积分表未变动';
  122. }
  123. if (Object.keys(visits).length > 0) {
  124. for (const visit of visits) {
  125. const ee = await model.VisitModel.updateOne({ _id: visit._id }, { ['dept' + (parseInt(data.level) - 1)]: data.fid });
  126. console.log('ee', ee);
  127. }
  128. progress += '积分表已修改';
  129. } else {
  130. progress += '积分表未变动';
  131. }
  132. //
  133. if (Object.keys(urgencys).length > 0) {
  134. for (const urgency of urgencys) {
  135. const hh = await model.UrgencyModel.updateOne({ _id: urgency._id }, { ['dept' + (parseInt(data.level) - 1)]: data.fid });
  136. console.log('hh', hh);
  137. }
  138. progress += '紧急通知表已修改';
  139. } else {
  140. progress += '紧急通知表未变动';
  141. }
  142. if (Object.keys(overRanges).length > 0) {
  143. for (const overRange of overRanges) {
  144. const ii = await model.OverRangeModel.updateOne({ _id: overRange._id }, { ['dept' + (parseInt(data.level) - 1)]: data.fid });
  145. console.log('ii', ii);
  146. }
  147. progress += 'overRange表已修改';
  148. } else {
  149. progress += 'overRange表未变动';
  150. }
  151. if (Object.keys(familys).length > 0) {
  152. for (const family of familys) {
  153. const gg = await model.FamilyModel.updateOne({ _id: family._id }, { ['dept' + (parseInt(data.level) - 1)]: data.fid });
  154. console.log('gg', gg);
  155. }
  156. progress += '家庭表已修改';
  157. } else {
  158. progress += '家庭表未变动';
  159. }
  160. if (Object.keys(notices).length > 0) {
  161. for (const notice of notices) {
  162. const hh = await model.NoticeModel.updateOne({ _id: notice._id }, { ['dept' + (parseInt(data.level) - 1)]: data.fid });
  163. console.log('hh', hh);
  164. }
  165. progress += '通知表已修改';
  166. } else {
  167. progress += '通知表未变动';
  168. }
  169. //
  170. }
  171. console.log(progress, 'progress');
  172. return null;
  173. }
  174. async listDept(fid) {
  175. const result = await this.one(fid);
  176. const list = await this.tag().find({ fid: result.fid });
  177. console.log(list, 'list');
  178. return list;
  179. }
  180. async orderChange(id, type) {
  181. const { model } = this.ctx;
  182. const dept = await model.SysDeptModel.findById(id);
  183. let where = {};
  184. let sort = {};
  185. if (type === 'down') {
  186. where = { order: { $gte: dept.order }, fid: dept.fid };
  187. sort = { order: 1 };
  188. }
  189. if (type === 'up') {
  190. where = { order: { $lte: dept.order }, fid: dept.fid };
  191. sort = { order: -1 };
  192. }
  193. const result = await model.SysDeptModel.find(where)
  194. .sort(sort)
  195. .skip(0)
  196. .limit(2);
  197. if (result.length === 2) {
  198. await model.SysDeptModel.updateOne({ _id: result[0]._id }, { order: result[1].order });
  199. await model.SysDeptModel.updateOne({ _id: result[1]._id }, { order: result[0].order });
  200. return null;
  201. }
  202. return '更新排序异常';
  203. }
  204. async getMaxOrder() {
  205. const { model } = this.ctx;
  206. const result = await model.SysDeptModel.find()
  207. .sort({ order: -1 })
  208. .skip(0)
  209. .limit(1);
  210. let max_order = 0;
  211. if (result.length > 0 && result[0].order) {
  212. max_order = result[0].order;
  213. }
  214. max_order = max_order + 1;
  215. return max_order;
  216. }
  217. async deleteWithSub(id) {
  218. const { model } = this.ctx;
  219. const dbDeptArr = await model.SysDeptModel.find({ fid: id });
  220. if (dbDeptArr.length > 0) {
  221. return '删除失败,该地区存在下级地区';
  222. }
  223. const dbUserArr = await model.SysUserModel.find({ dept: id });
  224. if (dbUserArr.length > 0) {
  225. if (dbUserArr.length === 1) {
  226. if (dbUserArr[0].role == this.app.config.defaultOwnerManagerRoleId) {
  227. await model.SysUserModel.deleteOne({ _id: dbUserArr[0]._id });
  228. } else {
  229. return '删除失败,该地区下存在人员';
  230. }
  231. } else {
  232. return '删除失败,该地区下存在人员';
  233. }
  234. }
  235. await model.SysDeptModel.deleteOne({ _id: id });
  236. return null;
  237. }
  238. async listForPageSortWithUser(data) {
  239. const deptId = data.deptId;
  240. const roleId = data.roleId;
  241. const fid = data.fid;
  242. delete data.deptId;
  243. delete data.roleId;
  244. delete data.level;
  245. // type 代表前端是否需要显示添加按钮 true需要 false 不需要
  246. let type = true;
  247. if (fid === '0') {
  248. if (roleId == this.app.config.defaultUserRoleId) { // 采集员
  249. data.fid = '-1';
  250. type = false;
  251. } else if (roleId == this.app.config.defaultManagerRoleId) { // 地区管理员
  252. delete data.fid;
  253. data._id = deptId;
  254. type = false;
  255. }
  256. } else if (fid === '') { // 第五级管理员登陆
  257. type = false;
  258. } else {
  259. const fidResult = await this.tag().find({ _id: fid });
  260. if (fidResult[0].level === '5') {
  261. type = false;
  262. }
  263. }
  264. const result = await super.listForPage(data);
  265. result.type = type;
  266. return result;
  267. }
  268. async findDept(id, fid) {
  269. const arr = [ id ];
  270. while (fid != '0') { // 最多3次 比如是最底层的5级,那么知道5.4,只需要找3次
  271. const result = await this.one(fid);
  272. arr.unshift(fid);
  273. fid = result.fid;
  274. }
  275. return arr;
  276. }
  277. async findDeptDetail(dept, fid) {
  278. const arr = [ dept ];
  279. while (fid != '0') { // 最多3次 比如是最底层的5级,那么知道5.4,只需要找3次
  280. const result = await this.one(fid);
  281. arr.unshift(result);
  282. fid = result.fid;
  283. }
  284. return arr;
  285. }
  286. async findLink() {
  287. const result = await this.list({ name: { $regex: this.ctx.query.name } });
  288. const arr = [];
  289. for (let i = 0; i < result.length; i++) {
  290. const deptArr = await this.findDeptDetail(result[i], result[i].fid);
  291. arr.push(deptArr);
  292. }
  293. return arr;
  294. }
  295. }
  296. module.exports = SysDeptService;