contract.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. 'use strict';
  2. /**
  3. * Created by EllisRan on 2020/3/3.
  4. */
  5. const auditConst = require('../const/audit').contract;
  6. const auditType = require('../const/audit').auditType;
  7. const BaseService = require('../base/base_service');
  8. module.exports = app => {
  9. class Contract extends BaseService {
  10. /**
  11. * 构造函数
  12. *
  13. * @param {Object} ctx - egg全局变量
  14. * @return {void}
  15. */
  16. constructor(ctx) {
  17. const setting = {
  18. spid: 'spid',
  19. mid: 'tid',
  20. type: 'contract_type',
  21. kid: 'contract_id',
  22. pid: 'contract_pid',
  23. order: 'order',
  24. level: 'level',
  25. isLeaf: 'is_leaf',
  26. fullPath: 'full_path',
  27. keyPre: 'contract_maxLid:', // 换个名称,防止缓存导致旧数据出问题
  28. uuid: true,
  29. };
  30. super(ctx);
  31. this.setting = setting;
  32. this.tableName = 'contract';
  33. this.dataId = 'id';
  34. }
  35. async add(options, node, data) {
  36. if (!options[this.setting.type] || !node || !data) throw '参数有误';
  37. const insertId = this.uuid.v4();
  38. const transaction = await this.db.beginTransaction();
  39. try {
  40. const maxId = await this.ctx.service.contractTree._getMaxLid(options);
  41. const insertData = {
  42. id: insertId,
  43. spid: options.spid || null,
  44. tid: options.tid || null,
  45. contract_type: options[this.setting.type],
  46. uid: this.ctx.session.sessionUser.accountId,
  47. contract_id: maxId + 1,
  48. contract_pid: !node.c_code ? node.contract_id : node.contract_pid,
  49. level: !node.c_code ? node.level + 1 : node.level,
  50. is_leaf: 1,
  51. c_code: data.code,
  52. name: data.name,
  53. total_price: data.total_price ? parseFloat(data.total_price) : 0,
  54. party_a: data.party_a,
  55. party_b: data.party_b,
  56. sign_date: data.sign_date,
  57. remark: data.remark,
  58. type: data.type,
  59. create_time: new Date(),
  60. need_shenpi: options.spid ? this.ctx.subProject.page_show.openContractSubProjectShenpi : this.ctx.subProject.page_show.openContractTenderShenpi,
  61. };
  62. insertData[this.setting.fullPath] = !node.c_code
  63. ? node[this.setting.fullPath] + '-' + insertData[this.setting.kid]
  64. : node[this.setting.fullPath].replace('-' + node[this.setting.kid], '-' + insertData[this.setting.kid]);
  65. const order = !node.c_code ? (!node.is_leaf ? await this.getMaxOrder(options, node.contract_id, transaction) : 1) : node.order + 1;
  66. insertData[this.setting.order] = order;
  67. await this.ctx.service.contractTree._updateChildrenOrder(options, insertData[this.setting.pid], insertData[this.setting.order], 1, transaction);
  68. await transaction.insert(this.tableName, insertData);
  69. if (!node.c_code && node.is_leaf) {
  70. await transaction.update(this.ctx.service.contractTree.tableName, { id: node.id, is_leaf: 0 });
  71. }
  72. this.ctx.service.contractTree._cacheMaxLid(options, maxId + 1);
  73. await this.ctx.service.contractSpAudit.makeAudits(transaction, options, insertId, null, this.ctx.session.sessionUser.accountId);
  74. await transaction.commit();
  75. } catch (err) {
  76. await transaction.rollback();
  77. throw err;
  78. }
  79. const createData = await this.ctx.service.contract.getDataById(insertId);
  80. createData.username = this.ctx.session.sessionUser.name;
  81. const updateData = await this.ctx.service.contractTree.getNextsData(options, !node.c_code ? node.contract_id : node[this.setting.pid], node[this.setting.order] + 1);
  82. if (!node.c_code) {
  83. const parent = await this.ctx.service.contractTree.getDataById(node.id);
  84. updateData.push(parent);
  85. }
  86. return { create: createData, update: updateData };
  87. }
  88. /**
  89. * 提交数据 - 响应计算(增量方式计算)
  90. * @param {Number} tenderId
  91. * @param {Object} data
  92. * @return {Promise<*>}
  93. */
  94. async updateCalc(options, data) {
  95. const helper = this.ctx.helper;
  96. if (!data) {
  97. throw '提交数据错误';
  98. }
  99. const datas = data instanceof Array ? data : [data];
  100. const ids = [];
  101. for (const row of datas) {
  102. ids.push(row.id);
  103. }
  104. const transaction = await this.db.beginTransaction();
  105. try {
  106. const updateDatas = [];
  107. for (const row of datas) {
  108. const updateNode = await this.getDataById(row.id);
  109. if (!updateNode) {
  110. throw '提交数据错误';
  111. }
  112. updateDatas.push(row);
  113. }
  114. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  115. await transaction.commit();
  116. } catch (err) {
  117. await transaction.rollback();
  118. throw err;
  119. }
  120. return { update: await this.getDataById(ids) };
  121. }
  122. async getMaxOrder(options, pid, transaction) {
  123. const sql = 'SELECT MAX(`' + this.setting.order + '`) AS max_order FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ?';
  124. const params = [this.tableName, pid];
  125. const result = await transaction.query(sql, params);
  126. const maxOrder = result[0].max_order || 0;
  127. const sql1 = 'SELECT MAX(`' + this.setting.order + '`) AS max_order FROM ?? WHERE ' + this.ctx.helper._getOptionsSql(options) + ' AND ' + this.setting.pid + ' = ?';
  128. const params1 = [this.ctx.service.contractTree.tableName, pid];
  129. const result1 = await transaction.query(sql1, params1);
  130. const maxOrder1 = result1[0].max_order || 0;
  131. return Math.max(maxOrder, maxOrder1) + 1;
  132. }
  133. async getListByUsers(options, user) {
  134. const _ = this._;
  135. const list = await this.getAllDataByCondition({ where: options });
  136. const userList = list.length > 0 ? await this.ctx.service.projectAccount.getAllDataByCondition({ where: { id: this._.map(list, 'uid') } }) : [];
  137. const shenpi_status = this.ctx.contract_tender ? this.ctx.subProject.page_show.openContractTenderShenpi : this.ctx.subProject.page_show.openContractSubProjectShenpi;
  138. for (const l of list) {
  139. const userInfo = userList.find(item => item.id === l.uid);
  140. l.username = userInfo ? userInfo.name : '';
  141. if (shenpi_status) {
  142. // await this.ctx.service.contractSpAudit.loadUser(l);
  143. // await this.ctx.service.contractSpAudit.loadAuditViewData(l);
  144. // await this.ctx.service.contractSpAudit.checkShenpi(l);
  145. if (l.status !== auditConst.status.checked || !l.final_auditor_str) {
  146. l.curAuditors2 = await this.ctx.service.contractSpAudit.getAuditorsByStatus(l.id, null, l.status, l.times);
  147. if (l.status === auditConst.status.checked && !l.final_auditor_str) {
  148. const final_auditor_str = l.curAuditors2[0].audit_type === auditType.key.common
  149. ? l.curAuditors2[0].name + (l.curAuditors2[0].role ? '-' + l.curAuditors2[0].role : '')
  150. : this.ctx.helper.transFormToChinese(l.curAuditors2[0].audit_order) + '审';
  151. await this.defaultUpdate({ final_auditor_str, id: l.id });
  152. l.final_auditor_str = final_auditor_str;
  153. }
  154. }
  155. }
  156. }
  157. if (user.is_admin) {
  158. return list;
  159. }
  160. 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);
  161. if (!userPermission) return [];
  162. const cloneOptions = this._.cloneDeep(options);
  163. cloneOptions.uid = user.accountId;
  164. const userTreePermission = await this.ctx.service.contractTreeAudit.getAllDataByCondition({ where: cloneOptions });
  165. if (userTreePermission.length === 0) return list;
  166. const newList = this._.filter(list, { uid: user.accountId });
  167. // 审批的列表也要可显示
  168. const checkList = await this.ctx.service.contractSpAudit.getAuditList(options, user.accountId);
  169. // 用 checkList里的数组和list的id对比,存在则合并到newList中
  170. newList.push(...this._.filter(list, function(item) {
  171. return checkList.some(checkItem => checkItem.id === item.id && item.uid !== user.accountId);
  172. }));
  173. const userInfo = await this.ctx.service.projectAccount.getDataById(user.accountId);
  174. // const unit = userInfo.company ? await this.ctx.service.constructionUnit.getDataByCondition({ pid: userInfo.project_id, name: userInfo.company }) : null;
  175. const uids = await this.ctx.service.projectAccount.getAllDataByCondition({ columns: ['id'], where: { project_id: userInfo.project_id, company: userInfo.company } });
  176. for (const ut of userTreePermission) {
  177. if (userPermission.permission_show_node) {
  178. const nodes = this._.filter(list, { contract_pid: ut.contract_id });
  179. newList.push(...this._.filter(nodes, function(item) {
  180. return item.uid !== user.accountId;
  181. }));
  182. } else if (userPermission.permission_show_unit) {
  183. const nodes = this._.filter(list, function(item) {
  184. return item.contract_pid === ut.contract_id && _.includes(_.map(uids, 'id'), item.uid);
  185. });
  186. newList.push(...this._.filter(nodes, function(item) {
  187. return item.uid !== user.accountId;
  188. }));
  189. // } else {
  190. // const nodes = this._.filter(list, { contract_pid: ut.contract_id, uid: user.accountId });
  191. // newList.push(...nodes);
  192. }
  193. }
  194. return newList;
  195. }
  196. async savePageShow(spid, page_show) {
  197. const transaction = await this.db.beginTransaction();
  198. try {
  199. await this.ctx.service.subProject.updatePageshow(spid, page_show, transaction);
  200. // 同步更新所有pay和contract状态
  201. await transaction.update(this.tableName, { need_shenpi: page_show.openContractSubProjectShenpi }, { where: { spid } });
  202. await transaction.update(this.ctx.service.contractPay.tableName, { need_shenpi: page_show.openContractPaySubProjectShenpi }, { where: { spid } });
  203. const tenders = await this.ctx.service.tender.getAllDataByCondition({ columns: ['id'], where: { spid } });
  204. if (tenders.length > 0) {
  205. await transaction.update(this.tableName, { need_shenpi: page_show.openContractTenderShenpi }, { where: { tid: this._.map(tenders, 'id') } });
  206. await transaction.update(this.ctx.service.contractPay.tableName, { need_shenpi: page_show.openContractPayTenderShenpi }, { where: { tid: this._.map(tenders, 'id') } });
  207. }
  208. await transaction.commit();
  209. return true;
  210. } catch (err) {
  211. await transaction.rollback();
  212. throw err;
  213. }
  214. return false;
  215. }
  216. // async getCountByUser(stid, type, user) {
  217. //
  218. // }
  219. }
  220. return Contract;
  221. };