contract.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. const updateData = await this.ctx.service.contractTree.getNextsData(options, !node.c_code ? node.contract_id : node[this.setting.pid], node[this.setting.order] + 1);
  74. if (!node.c_code) {
  75. const parent = await this.ctx.service.contractTree.getDataById(node.id);
  76. updateData.push(parent);
  77. }
  78. return { create: createData, update: updateData };
  79. }
  80. /**
  81. * 提交数据 - 响应计算(增量方式计算)
  82. * @param {Number} tenderId
  83. * @param {Object} data
  84. * @return {Promise<*>}
  85. */
  86. async updateCalc(options, data) {
  87. const helper = this.ctx.helper;
  88. if (!data) {
  89. throw '提交数据错误';
  90. }
  91. const datas = data instanceof Array ? data : [data];
  92. const ids = [];
  93. for (const row of datas) {
  94. ids.push(row.id);
  95. }
  96. const transaction = await this.db.beginTransaction();
  97. try {
  98. const updateDatas = [];
  99. for (const row of datas) {
  100. const updateNode = await this.getDataById(row.id);
  101. if (!updateNode) {
  102. throw '提交数据错误';
  103. }
  104. updateDatas.push(row);
  105. }
  106. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  107. await transaction.commit();
  108. } catch (err) {
  109. await transaction.rollback();
  110. throw err;
  111. }
  112. return { update: await this.getDataById(ids) };
  113. }
  114. async getMaxOrder(options, pid, transaction) {
  115. const sql = 'SELECT MAX(`' + this.setting.order + '`) AS max_order FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ?';
  116. const params = [this.tableName, pid];
  117. const result = await transaction.query(sql, params);
  118. const maxOrder = result[0].max_order || 0;
  119. const sql1 = 'SELECT MAX(`' + this.setting.order + '`) AS max_order FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ?';
  120. const params1 = [this.ctx.service.contractTree.tableName, pid];
  121. const result1 = await transaction.query(sql1, params1);
  122. const maxOrder1 = result1[0].max_order || 0;
  123. return Math.max(maxOrder, maxOrder1) + 1;
  124. }
  125. async getListByUsers(options, user) {
  126. const _ = this._;
  127. const list = await this.getAllDataByCondition({ where: options });
  128. if (user.is_admin) {
  129. return list;
  130. }
  131. const userPermission = await this.ctx.service.contractAudit.getDataByCondition({ spid: options.spid || null, tid: options.tid || null, uid: user.accountId });
  132. if (!userPermission) return [];
  133. const cloneOptions = this._.cloneDeep(options);
  134. cloneOptions.uid = user.accountId;
  135. const userTreePermission = await this.ctx.service.contractTreeAudit.getAllDataByCondition({ where: cloneOptions });
  136. if (userTreePermission.length === 0) return list;
  137. const newList = this._.filter(list, { uid: user.accountId });
  138. const userInfo = await this.ctx.service.projectAccount.getDataById(user.accountId);
  139. // const unit = userInfo.company ? await this.ctx.service.constructionUnit.getDataByCondition({ pid: userInfo.project_id, name: userInfo.company }) : null;
  140. const uids = await this.ctx.service.projectAccount.getAllDataByCondition({ columns: ['id'], where: { project_id: userInfo.project_id, company: userInfo.company } });
  141. for (const ut of userTreePermission) {
  142. if (userPermission.permission_show_node) {
  143. const nodes = this._.filter(list, { contract_pid: ut.contract_id });
  144. newList.push(...this._.filter(nodes, function(item) {
  145. return item.uid !== user.accountId;
  146. }));
  147. } else if (userPermission.permission_show_unit) {
  148. const nodes = this._.filter(list, function(item) {
  149. return item.contract_pid === ut.contract_id && _.includes(_.map(uids, 'id'), item.uid);
  150. });
  151. newList.push(...this._.filter(nodes, function(item) {
  152. return item.uid !== user.accountId;
  153. }));
  154. // } else {
  155. // const nodes = this._.filter(list, { contract_pid: ut.contract_id, uid: user.accountId });
  156. // newList.push(...nodes);
  157. }
  158. }
  159. return newList;
  160. }
  161. // async getCountByUser(stid, type, user) {
  162. //
  163. // }
  164. }
  165. return Contract;
  166. };