shenpi_group.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // 判断tid是字符还是数字,如果是数字就是tid,不是则是spid
  24. const condition = isNaN(tid) ? { spid: tid, sp_type: type } : { tid, sp_type: type };
  25. return transaction ? await transaction.select(this.tableName, { where: condition }) : await this.getAllDataByCondition({ where: condition });
  26. }
  27. async getOneGroup(id) {
  28. const group = await this.getDataById(id);
  29. if (group.change_type) group.change_type = JSON.parse(group.change_type);
  30. group.auditGroupList = await this.ctx.service.shenpiAudit.getAuditGroupList(group.tid === null ? group.spid : group.tid, group.sp_type, shenpiConst.sp_status.gdspl, group.id);
  31. return group;
  32. }
  33. async saveGroup(tid, data) {
  34. const condition = isNaN(tid) ? { spid: tid } : { tid };
  35. const transaction = await this.db.beginTransaction();
  36. try {
  37. const sp_type = shenpiConst.sp_type[data.code];
  38. const groupList = await this.getGroupList(tid, sp_type);
  39. if (data.groupId) {
  40. const info = await this.getDataById(data.groupId);
  41. if (!info) {
  42. throw '该审批组不存在';
  43. }
  44. if (this._.findIndex(groupList, function(item) {
  45. return item.id !== data.groupId && item.name === data.name;
  46. }) !== -1) {
  47. throw '该审批组名称已存在,请更改其它名称';
  48. }
  49. const updateData = {
  50. id: info.id,
  51. name: data.name,
  52. };
  53. if ((data.code === 'change' || data.code === 'contract') && data.change_type) {
  54. updateData.change_type = JSON.stringify(data.change_type);
  55. }
  56. await transaction.update(this.tableName, updateData);
  57. } else {
  58. const insertData = { sp_type, name: data.name,
  59. is_select: 1, change_type: (data.code === 'change' || data.code === 'contract') && data.change_type ? JSON.stringify(data.change_type) : null,
  60. create_time: new Date(),
  61. };
  62. this._.assign(insertData, condition);
  63. const updateGroupData = this._.map(groupList, function(item) {
  64. return {
  65. id: item.id,
  66. is_select: 0,
  67. };
  68. });
  69. if (updateGroupData.length > 0) await transaction.updateRows(this.tableName, updateGroupData);
  70. const result = await transaction.insert(this.tableName, insertData);
  71. data.groupId = result.insertId;
  72. // 判断是否存在group_id 为 0的,则自动填写
  73. const auditList = await this.ctx.service.shenpiAudit.getAuditList(tid, sp_type, shenpiConst.sp_status.gdspl);
  74. if (auditList && auditList.length > 0) {
  75. const updateData = this._.map(auditList, function(item) {
  76. return {
  77. id: item.id,
  78. sp_group: data.groupId,
  79. };
  80. });
  81. await transaction.updateRows(this.ctx.service.shenpiAudit.tableName, updateData);
  82. }
  83. }
  84. await transaction.commit();
  85. return { group: await this.getOneGroup(data.groupId) };
  86. } catch (err) {
  87. await transaction.rollback();
  88. throw err;
  89. }
  90. }
  91. async changeGroup(data) {
  92. const info = await this.getDataById(data.groupId);
  93. if (!info) {
  94. throw '该审批组不存在';
  95. }
  96. const groupList = await this.getGroupList(info.tid === null ? info.spid : info.tid, info.sp_type);
  97. const updateGroupData = this._.map(groupList, function(item) {
  98. return {
  99. id: item.id,
  100. is_select: item.id === info.id ? 1 : 0,
  101. };
  102. });
  103. if (updateGroupData.length > 0) return await this.db.updateRows(this.tableName, updateGroupData);
  104. }
  105. async deleteGroup(data) {
  106. const transaction = await this.db.beginTransaction();
  107. try {
  108. const group = await this.getDataById(data.groupId);
  109. if (!group) throw '该审批组不存在';
  110. // 移除审批人
  111. await transaction.delete(this.ctx.service.shenpiAudit.tableName, { sp_group: group.id });
  112. await transaction.delete(this.tableName, { id: group.id });
  113. // 第一个审批组设置为is_select=1
  114. const groupList = await this.getGroupList(group.tid === null ? group.spid : group.tid, group.sp_type, transaction);
  115. if (groupList && groupList.length > 0) {
  116. const updateGroupData = this._.map(groupList, function(item) {
  117. return {
  118. id: item.id,
  119. is_select: item.id === groupList[0].id ? 1 : 0,
  120. };
  121. });
  122. if (updateGroupData.length > 0) await transaction.updateRows(this.tableName, updateGroupData);
  123. }
  124. await transaction.commit();
  125. return true;
  126. } catch (err) {
  127. await transaction.rollback();
  128. throw err;
  129. }
  130. }
  131. async getGroupBySelect(tid, sp_type) {
  132. const condition = isNaN(tid) ? { spid: tid, sp_type, is_select: 1 } : { tid, sp_type, is_select: 1 };
  133. return await this.getDataByCondition(condition);
  134. }
  135. async getGroupListByStage(tid, sp_type) {
  136. const condition = isNaN(tid) ? { spid: tid, sp_type } : { tid, sp_type };
  137. return await this.getAllDataByCondition({ where: condition, order: [['id', 'asc']] });
  138. // const sql = 'select * from ?? where tid= ? and sp_type= ? order by id asc';
  139. // const sqlParam = [this.tableName, tid, sp_type];
  140. // return await this.db.query(sql, sqlParam);
  141. }
  142. async getSelectGroupByStageType(tid, sp_type, group_id = 0) {
  143. const result = await this.getGroupListByStage(tid, sp_type);
  144. if (result && result.length > 0) {
  145. if (group_id !== 0) {
  146. const group = await this._.find(result, function(item) {
  147. return item.id === group_id;
  148. });
  149. if (group) return group;
  150. }
  151. const hadSelect = this._.find(result, function(item) {
  152. return item.is_select === 1;
  153. });
  154. if (hadSelect) {
  155. return hadSelect;
  156. }
  157. return result[0];
  158. }
  159. return null;
  160. }
  161. async getGroupListByChangeType(tid, sp_type, change_type) {
  162. const idSql = isNaN(tid) ? 'spid= ?' : 'tid= ?';
  163. const sql = `select * from ?? where ${idSql} and sp_type= ? and JSON_CONTAINS(change_type, json_object(?, true)) order by id asc`;
  164. const sqlParam = [this.tableName, tid, sp_type, change_type];
  165. return await this.db.query(sql, sqlParam);
  166. }
  167. async getSelectGroupByChangeType(tid, sp_type, change_type, group_id = 0) {
  168. // 查找变更选中的审批组id,没有这为默认审批组或null
  169. const result = await this.getGroupListByChangeType(tid, sp_type, change_type);
  170. if (result && result.length > 0) {
  171. if (group_id !== 0) {
  172. const group = await this._.find(result, function(item) {
  173. return item.id === group_id;
  174. });
  175. if (group) return group;
  176. }
  177. const hadSelect = this._.find(result, function(item) {
  178. return item.is_select === 1;
  179. });
  180. if (hadSelect) {
  181. return hadSelect;
  182. }
  183. return result[0];
  184. }
  185. return null;
  186. }
  187. }
  188. return ShenpiGroup;
  189. };