construction_unit.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict';
  2. /**
  3. * Created by EllisRan on 2020/3/3.
  4. */
  5. const BaseService = require('../base/base_service');
  6. module.exports = app => {
  7. class ConstructionUnit extends BaseService {
  8. /**
  9. * 构造函数
  10. *
  11. * @param {Object} ctx - egg全局变量
  12. * @return {void}
  13. */
  14. constructor(ctx) {
  15. super(ctx);
  16. this.tableName = 'construction_unit';
  17. this.dataId = 'id';
  18. }
  19. /**
  20. * 数据验证规则
  21. *
  22. * @param {String} scene - 场景
  23. * @return {Object} - 返回数据
  24. */
  25. rule(scene) {
  26. let rule = {};
  27. switch (scene) {
  28. case 'add':
  29. rule = {
  30. name: { type: 'string', required: true },
  31. type: { type: 'string', required: true },
  32. };
  33. break;
  34. default:
  35. break;
  36. }
  37. return rule;
  38. }
  39. /**
  40. * 修改单位数据
  41. *
  42. * @param {Object} data - post过来的数据
  43. * @return {Boolean} - 返回修改结果
  44. */
  45. async save(data) {
  46. if (data._csrf_j !== undefined) {
  47. delete data._csrf_j;
  48. }
  49. const id = data.id !== undefined ? parseInt(data.id) : 0;
  50. const transaction = await this.db.beginTransaction();
  51. try {
  52. if (id > 0) {
  53. // 修改操作时
  54. delete data.create_time;
  55. data.id = id;
  56. } else {
  57. // 重名检测
  58. const accountData = await this.db.select(this.tableName, {
  59. where: {
  60. name: data.name,
  61. pid: data.pid,
  62. },
  63. });
  64. if (accountData.length > 0) {
  65. throw '已存在对应的单位名';
  66. }
  67. data.create_time = new Date();
  68. }
  69. const operate = id === 0 ? await transaction.insert(this.tableName, data) :
  70. await transaction.update(this.tableName, data);
  71. if (id === 0 && operate.affectedRows === 1) data.id = operate.insertId;
  72. // 并修改用户的用户组及单位名称
  73. const updateRow = {};
  74. if (data.name) {
  75. updateRow.company = data.name;
  76. updateRow.company_id = data.id;
  77. }
  78. if (data.type) updateRow.account_group = data.type;
  79. if (!this._.isEmpty(updateRow)) {
  80. const info = id !== 0 ? await this.getDataById(id) : null;
  81. await this.ctx.service.projectAccount.update(updateRow, {
  82. project_id: data.pid,
  83. company: info ? info.name : data.name,
  84. }, transaction);
  85. // egg 自带的update方法无法使用UPDATE zh_project_account SET company = ? WHERE project_id = ? AND company = ? ,未知原因
  86. // await transaction.update(this.ctx.service.projectAccount.tableName, updateRow,
  87. // {
  88. // where: {
  89. // project_id: data.pid,
  90. // company: info ? info.name : data.name,
  91. // },
  92. // }
  93. // );
  94. }
  95. transaction.commit();
  96. const result = operate.affectedRows > 0;
  97. return result;
  98. } catch (e) {
  99. transaction.rollback();
  100. }
  101. }
  102. async del(id) {
  103. // 先删除签章oss地址
  104. const info = await this.getDataById(id);
  105. if (info.sign_path) {
  106. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + info.sign_path);
  107. }
  108. // 删除单位
  109. return await this.db.delete(this.tableName, { id });
  110. }
  111. }
  112. return ConstructionUnit;
  113. };