shenpi_group.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. 'use strict';
  2. /**
  3. * 版本数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/25
  7. * @version
  8. */
  9. const shenpiConst = require('../const/shenpi');
  10. module.exports = app => {
  11. class ShenpiGroup extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'shenpi_group';
  21. }
  22. async getGroupList(tid, type, transaction = null) {
  23. return transaction ? await transaction.select(this.tableName, { where: { tid, sp_type: type } }) : await this.getAllDataByCondition({ where: { tid, sp_type: type } });
  24. }
  25. async getOneGroup(id) {
  26. const group = await this.getDataById(id);
  27. if (group.change_type) group.change_type = JSON.parse(group.change_type);
  28. group.auditGroupList = await this.ctx.service.shenpiAudit.getAuditGroupList(group.tid, group.sp_type, shenpiConst.sp_status.gdspl, group.id);
  29. return group;
  30. }
  31. async saveGroup(tid, data) {
  32. const transaction = await this.db.beginTransaction();
  33. try {
  34. const sp_type = shenpiConst.sp_type[data.code];
  35. const groupList = await this.getGroupList(tid, sp_type);
  36. if (data.groupId) {
  37. const info = await this.getDataById(data.groupId);
  38. if (!info) {
  39. throw '该审批组不存在';
  40. }
  41. if (this._.findIndex(groupList, function(item) {
  42. return item.id !== data.groupId && item.name === data.name;
  43. }) !== -1) {
  44. throw '该审批组名称已存在,请更改其它名称';
  45. }
  46. const updateData = {
  47. id: info.id,
  48. name: data.name,
  49. };
  50. if (data.code === 'change' && data.change_type) {
  51. updateData.change_type = JSON.stringify(data.change_type);
  52. }
  53. await transaction.update(this.tableName, updateData);
  54. } else {
  55. const insertData = {
  56. tid, sp_type, name: data.name,
  57. is_select: 1, change_type: data.code === 'change' && data.change_type ? JSON.stringify(data.change_type) : null,
  58. create_time: new Date(),
  59. };
  60. const updateGroupData = this._.map(groupList, function(item) {
  61. return {
  62. id: item.id,
  63. is_select: 0,
  64. };
  65. });
  66. if (updateGroupData.length > 0) await transaction.updateRows(this.tableName, updateGroupData);
  67. const result = await transaction.insert(this.tableName, insertData);
  68. data.groupId = result.insertId;
  69. // 判断是否存在group_id 为 0的,则自动填写
  70. const auditList = await this.ctx.service.shenpiAudit.getAuditList(tid, sp_type, shenpiConst.sp_status.gdspl);
  71. if (auditList && auditList.length > 0) {
  72. const updateData = this._.map(auditList, function(item) {
  73. return {
  74. id: item.id,
  75. sp_group: data.groupId,
  76. };
  77. });
  78. await transaction.updateRows(this.ctx.service.shenpiAudit.tableName, updateData);
  79. }
  80. }
  81. await transaction.commit();
  82. return { group: await this.getOneGroup(data.groupId) };
  83. } catch (err) {
  84. await transaction.rollback();
  85. throw err;
  86. }
  87. }
  88. async changeGroup(data) {
  89. const info = await this.getDataById(data.groupId);
  90. if (!info) {
  91. throw '该审批组不存在';
  92. }
  93. const groupList = await this.getGroupList(info.tid, info.sp_type);
  94. const updateGroupData = this._.map(groupList, function(item) {
  95. return {
  96. id: item.id,
  97. is_select: item.id === info.id ? 1 : 0,
  98. };
  99. });
  100. if (updateGroupData.length > 0) return await this.db.updateRows(this.tableName, updateGroupData);
  101. }
  102. async deleteGroup(data) {
  103. const transaction = await this.db.beginTransaction();
  104. try {
  105. const group = await this.getDataById(data.groupId);
  106. if (!group) throw '该审批组不存在';
  107. // 移除审批人
  108. await transaction.delete(this.ctx.service.shenpiAudit.tableName, { sp_group: group.id });
  109. await transaction.delete(this.tableName, { id: group.id });
  110. // 第一个审批组设置为is_select=1
  111. const groupList = await this.getGroupList(group.tid, group.sp_type, transaction);
  112. if (groupList && groupList.length > 0) {
  113. const updateGroupData = this._.map(groupList, function(item) {
  114. return {
  115. id: item.id,
  116. is_select: item.id === groupList[0].id ? 1 : 0,
  117. };
  118. });
  119. if (updateGroupData.length > 0) await transaction.updateRows(this.tableName, updateGroupData);
  120. }
  121. await transaction.commit();
  122. return true;
  123. } catch (err) {
  124. await transaction.rollback();
  125. throw err;
  126. }
  127. }
  128. async getGroupBySelect(tid, sp_type) {
  129. return await this.getDataByCondition({ tid, sp_type, is_select: 1 });
  130. }
  131. async getGroupListByChangeType(tid, sp_type, change_type) {
  132. const sql = 'select * from ?? where tid= ? and sp_type= ? and JSON_CONTAINS(change_type, json_object(?, true)) order by id asc';
  133. const sqlParam = [this.tableName, tid, sp_type, change_type];
  134. return await this.db.query(sql, sqlParam);
  135. }
  136. async getSelectGroupByChangeType(tid, sp_type, change_type, group_id = 0) {
  137. if (group_id !== 0) {
  138. const group = await this.getDataById(group_id);
  139. if (group) return group;
  140. }
  141. // 查找变更选中的审批组id,没有这为默认审批组或null
  142. const result = await this.getGroupListByChangeType(tid, sp_type, change_type);
  143. if (result && result.length > 0) {
  144. const hadSelect = this._.find(result, function(item) {
  145. return item.is_select === 1;
  146. });
  147. if (hadSelect) {
  148. return hadSelect;
  149. }
  150. return result[0];
  151. }
  152. return null;
  153. }
  154. }
  155. return ShenpiGroup;
  156. };