construction_unit.js 4.5 KB

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