contract.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 Contract extends BaseService {
  8. /**
  9. * 构造函数
  10. *
  11. * @param {Object} ctx - egg全局变量
  12. * @return {void}
  13. */
  14. constructor(ctx) {
  15. const setting = {
  16. spid: 'spid',
  17. mid: 'tid',
  18. type: 'contract_type',
  19. kid: 'contract_id',
  20. pid: 'contract_pid',
  21. order: 'order',
  22. level: 'level',
  23. isLeaf: 'is_leaf',
  24. fullPath: 'full_path',
  25. keyPre: 'contract_maxLid:', // 换个名称,防止缓存导致旧数据出问题
  26. uuid: true,
  27. };
  28. super(ctx);
  29. this.setting = setting;
  30. this.tableName = 'contract';
  31. this.dataId = 'id';
  32. }
  33. async add(options, node, data) {
  34. if (!options[this.setting.type] || !node || !data) throw '参数有误';
  35. const insertId = this.uuid.v4();
  36. const transaction = await this.db.beginTransaction();
  37. try {
  38. const maxId = await this.ctx.service.contractTree._getMaxLid(options);
  39. const insertData = {
  40. id: insertId,
  41. spid: options.spid || null,
  42. tid: options.tid || null,
  43. contract_type: options[this.setting.type],
  44. uid: this.ctx.session.sessionUser.accountId,
  45. contract_id: maxId + 1,
  46. contract_pid: !node.c_code ? node.contract_id : node.contract_pid,
  47. level: !node.c_code ? node.level + 1 : node.level,
  48. is_leaf: 1,
  49. c_code: data.code,
  50. name: data.name,
  51. total_price: data.total_price ? parseFloat(data.total_price) : 0,
  52. party_b: data.party_b,
  53. remark: data.remark,
  54. create_time: new Date(),
  55. };
  56. insertData[this.setting.fullPath] = !node.c_code
  57. ? node[this.setting.fullPath] + '-' + insertData[this.setting.kid]
  58. : node[this.setting.fullPath].replace('-' + node[this.setting.kid], '-' + insertData[this.setting.kid]);
  59. const order = !node.c_code ? (!node.is_leaf ? await this.getMaxOrder(options, node.contract_id, transaction) : 1) : node.order + 1;
  60. insertData[this.setting.order] = order;
  61. await this.ctx.service.contractTree._updateChildrenOrder(options, insertData[this.setting.pid], insertData[this.setting.order], 1, transaction);
  62. await transaction.insert(this.tableName, insertData);
  63. if (!node.c_code && node.is_leaf) {
  64. await transaction.update(this.ctx.service.contractTree.tableName, { id: node.id, is_leaf: 0 });
  65. }
  66. this.ctx.service.contractTree._cacheMaxLid(options, maxId + 1);
  67. await transaction.commit();
  68. } catch (err) {
  69. await transaction.rollback();
  70. throw err;
  71. }
  72. const createData = await this.ctx.service.contract.getDataById(insertId);
  73. createData.username = this.ctx.session.sessionUser.name;
  74. const updateData = await this.ctx.service.contractTree.getNextsData(options, !node.c_code ? node.contract_id : node[this.setting.pid], node[this.setting.order] + 1);
  75. if (!node.c_code) {
  76. const parent = await this.ctx.service.contractTree.getDataById(node.id);
  77. updateData.push(parent);
  78. }
  79. return { create: createData, update: updateData };
  80. }
  81. /**
  82. * 提交数据 - 响应计算(增量方式计算)
  83. * @param {Number} tenderId
  84. * @param {Object} data
  85. * @return {Promise<*>}
  86. */
  87. async updateCalc(options, data) {
  88. const helper = this.ctx.helper;
  89. if (!data) {
  90. throw '提交数据错误';
  91. }
  92. const datas = data instanceof Array ? data : [data];
  93. const ids = [];
  94. for (const row of datas) {
  95. ids.push(row.id);
  96. }
  97. const transaction = await this.db.beginTransaction();
  98. try {
  99. const updateDatas = [];
  100. for (const row of datas) {
  101. const updateNode = await this.getDataById(row.id);
  102. if (!updateNode) {
  103. throw '提交数据错误';
  104. }
  105. updateDatas.push(row);
  106. }
  107. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  108. await transaction.commit();
  109. } catch (err) {
  110. await transaction.rollback();
  111. throw err;
  112. }
  113. return { update: await this.getDataById(ids) };
  114. }
  115. async getMaxOrder(options, pid, transaction) {
  116. const sql = 'SELECT MAX(`' + this.setting.order + '`) AS max_order FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ?';
  117. const params = [this.tableName, pid];
  118. const result = await transaction.query(sql, params);
  119. const maxOrder = result[0].max_order || 0;
  120. const sql1 = 'SELECT MAX(`' + this.setting.order + '`) AS max_order FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ?';
  121. const params1 = [this.ctx.service.contractTree.tableName, pid];
  122. const result1 = await transaction.query(sql1, params1);
  123. const maxOrder1 = result1[0].max_order || 0;
  124. return Math.max(maxOrder, maxOrder1) + 1;
  125. }
  126. async getListByUsers(options, user) {
  127. const _ = this._;
  128. const list = await this.getAllDataByCondition({ where: options });
  129. for (const l of list) {
  130. l.username = (await this.ctx.service.projectAccount.getAccountInfoById(l.uid)).name;
  131. }
  132. if (user.is_admin) {
  133. return list;
  134. }
  135. const userPermission = options.tid ? await this.ctx.service.contractAudit.getDataByCondition({ spid: options.spid || null, tid: options.tid || null, uid: user.accountId }) : await this.ctx.service.subProjPermission.getContractPermission(this.ctx.subProject.permission.contract_permission);
  136. if (!userPermission) return [];
  137. const cloneOptions = this._.cloneDeep(options);
  138. cloneOptions.uid = user.accountId;
  139. const userTreePermission = await this.ctx.service.contractTreeAudit.getAllDataByCondition({ where: cloneOptions });
  140. if (userTreePermission.length === 0) return list;
  141. const newList = this._.filter(list, { uid: user.accountId });
  142. const userInfo = await this.ctx.service.projectAccount.getDataById(user.accountId);
  143. // const unit = userInfo.company ? await this.ctx.service.constructionUnit.getDataByCondition({ pid: userInfo.project_id, name: userInfo.company }) : null;
  144. const uids = await this.ctx.service.projectAccount.getAllDataByCondition({ columns: ['id'], where: { project_id: userInfo.project_id, company: userInfo.company } });
  145. for (const ut of userTreePermission) {
  146. if (userPermission.permission_show_node) {
  147. const nodes = this._.filter(list, { contract_pid: ut.contract_id });
  148. newList.push(...this._.filter(nodes, function(item) {
  149. return item.uid !== user.accountId;
  150. }));
  151. } else if (userPermission.permission_show_unit) {
  152. const nodes = this._.filter(list, function(item) {
  153. return item.contract_pid === ut.contract_id && _.includes(_.map(uids, 'id'), item.uid);
  154. });
  155. newList.push(...this._.filter(nodes, function(item) {
  156. return item.uid !== user.accountId;
  157. }));
  158. // } else {
  159. // const nodes = this._.filter(list, { contract_pid: ut.contract_id, uid: user.accountId });
  160. // newList.push(...nodes);
  161. }
  162. }
  163. return newList;
  164. }
  165. // async getCountByUser(stid, type, user) {
  166. //
  167. // }
  168. }
  169. return Contract;
  170. };