ancillary_gcl.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. 'use strict';
  2. /**
  3. * 附属工程量(ancillary gcl)
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class AncillaryGcl extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'ancillary_gcl';
  20. }
  21. async _addDatas(data) {
  22. const qtyDecimal = this.ctx.tender.info.decimal.qty;
  23. const user_id = this.ctx.session.sessionUser.accountId;
  24. const datas = data instanceof Array ? data : [data];
  25. const insertData = [];
  26. for (const d of datas) {
  27. if (!d.lid || !d.pid || !d.g_order) throw '新增附属工程量数据,提交的数据错误';
  28. const nd = {
  29. id: this.uuid.v4(), tid: this.ctx.tender.id,
  30. add_user_id: user_id, update_user_id: user_id,
  31. lid: d.lid, pid: d.pid, g_order: d.g_order,
  32. };
  33. if (d.is_aux !== undefined) nd.is_aux = d.is_aux;
  34. if (d.name) nd.name = d.name || '';
  35. if (d.unit) nd.unit = d.unit || '';
  36. if (d.quantity) nd.quantity = this.ctx.helper.round(d.quantity || 0, qtyDecimal);
  37. if (d.expr) nd.expr = d.expr || '';
  38. if (d.drawing_code !== undefined) nd.drawing_code = d.drawing_code;
  39. if (d.memo) nd.memo = d.memo || '';
  40. insertData.push(nd);
  41. }
  42. await this.db.insert(this.tableName, insertData);
  43. return await this.getAllDataByCondition({
  44. where: { id: this.ctx.helper._.map(insertData, 'id') }
  45. });
  46. }
  47. async _delDatas (data) {
  48. if (!data || data.length === 0) throw '提交数据错误';
  49. const orgDatas = await this.getAllDataByCondition({ where: { id: data } });
  50. if (!orgDatas || orgDatas.length === 0) throw '删除的辅助工程量不存在';
  51. const pos = await this.ctx.service.pos.getDataById(orgDatas[0].pid);
  52. let posAnc = await this.getAllDataByCondition({ where: { tid: pos.tid, pid: pos.id } });
  53. posAnc = posAnc.filter(ba => {
  54. return data.indexOf(ba.id) < 0;
  55. });
  56. posAnc.sort((x, y) => { return x.g_order - y.g_order; });
  57. const updateData = [];
  58. posAnc.forEach((x, i) => {
  59. if (x.g_order !== i + 1) updateData.push({ id: x.id, g_order: i + 1});
  60. });
  61. const conn = await this.db.beginTransaction();
  62. try {
  63. await conn.delete(this.tableName, { id: data });
  64. if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
  65. await this.ctx.service.ancillaryGclDetail.deleteAncGclPartData(conn, orgDatas[0].tid, data);
  66. await conn.commit();
  67. return [data, updateData];
  68. } catch (err) {
  69. await conn.rollback();
  70. throw err;
  71. }
  72. }
  73. async _updateDatas (data) {
  74. if (!data || data.length === 0) throw '提交数据错误';
  75. const qtyDecimal = this.ctx.tender.info.decimal.qty;
  76. const user_id = this.ctx.session.sessionUser.accountId;
  77. const datas = data instanceof Array ? data : [data];
  78. const orgDatas = await this.getAllDataByCondition({
  79. where: { id: this.ctx.helper._.map(datas, 'id') }
  80. });
  81. if (!orgDatas || orgDatas.length === 0) throw '修改的辅助工程量不存在';
  82. const uDatas = [];
  83. for (const d of datas) {
  84. const od = orgDatas.find(x => { return x.id === d.id; });
  85. if (!od) continue;
  86. const nd = { id: od.id, update_user_id: user_id };
  87. if (d.is_aux !== undefined) nd.is_aux = d.is_aux;
  88. if (d.name !== undefined) nd.name = d.name;
  89. if (d.g_order !== undefined) nd.g_order = d.g_order;
  90. if (d.unit !== undefined) nd.unit = d.unit;
  91. if (!od.calc_template) {
  92. if (d.quantity !== undefined) nd.quantity = this.ctx.helper.round(d.quantity, qtyDecimal);
  93. if (d.expr !== undefined) nd.expr = d.expr || '';
  94. }
  95. if (d.drawing_code !== undefined) nd.drawing_code = d.drawing_code;
  96. if (d.memo !== undefined) nd.memo = d.memo || '';
  97. uDatas.push(nd);
  98. }
  99. if (uDatas.length > 0) {
  100. await this.db.updateRows(this.tableName, uDatas);
  101. return uDatas;
  102. } else {
  103. return [];
  104. }
  105. }
  106. async _setCalcTemplate(data) {
  107. if (!data || data.length === 0) throw '提交数据错误';
  108. const qtyDecimal = this.ctx.tender.info.decimal.qty;
  109. const user_id = this.ctx.session.sessionUser.accountId;
  110. const datas = data instanceof Array ? data : [data];
  111. const orgDatas = await this.getAllDataByCondition({
  112. where: { id: this.ctx.helper._.map(datas, 'id') }
  113. });
  114. if (!orgDatas || orgDatas.length === 0) throw '修改的辅助工程量不存在';
  115. const uDatas = [], detailUpdate = [];
  116. for (const d of datas) {
  117. const od = orgDatas.find(x => { return x.id === d.id; });
  118. if (!od) continue;
  119. const nd = { id: od.id, update_user_id: user_id, calc_template: d.calc_template, expr: '', quantity: 0 };
  120. uDatas.push(nd);
  121. }
  122. const conn = await this.db.beginTransaction();
  123. try {
  124. if (datas[0].calc_template) {
  125. for (const ud of uDatas) {
  126. const [du, quantity] = await this.ctx.service.ancillaryGclDetail.resetAncGclCalcTemplate(conn, ud.id, ud.calc_template);
  127. detailUpdate.push(...du);
  128. ud.quantity = this.ctx.helper.round(quantity, qtyDecimal);
  129. }
  130. } else {
  131. await this.ctx.service.ancillaryGclDetail.deleteAncGclPartData(conn, orgDatas[0].tid, uDatas.map(x => { return x.id; }));
  132. }
  133. await conn.updateRows(this.tableName, uDatas);
  134. await conn.commit();
  135. return [uDatas, detailUpdate];
  136. } catch (err) {
  137. this.ctx.log(err);
  138. await conn.rollback();
  139. throw '修改计算模板失败';
  140. }
  141. }
  142. async _pasteBlock(data) {
  143. if (!(data.block instanceof Array)) throw '提交数据错误';
  144. const qtyDecimal = this.ctx.tender.info.decimal.qty;
  145. const user_id = this.ctx.session.sessionUser.accountId;
  146. const pos = await this.ctx.service.pos.getDataById(data.pid);
  147. const existAncGcl = await this.getAllDataByCondition({ where : { pid: data.pid }, orders: [['g_order', 'DESC']]});
  148. const maxOrder = existAncGcl.length > 0 ? existAncGcl[0].g_order : 0;
  149. const insertAncGcl = [], insertDetail = [];
  150. for (const [i, b] of data.block.entries()) {
  151. const nig = {
  152. id: this.uuid.v4(), tid: this.ctx.tender.id,
  153. add_user_id: user_id, update_user_id: user_id,
  154. lid: pos.lid, pid: pos.id, g_order: maxOrder + i + 1,
  155. is_aux: b.is_aux || 0, name: b.name || '', unit: b.unit || '',
  156. drawing_code: b.drawing_code || '', memo: b.memo || '',
  157. quantity: b.quantity || 0, expr: b.expr || '', calc_template: b.calc_template || ''
  158. };
  159. insertAncGcl.push(nig);
  160. if (!b.calc_template) continue;
  161. let sumQty = 0;
  162. for (const cd of b.calcDetail) {
  163. const newDetail = {
  164. id: this.uuid.v4(), tid: this.ctx.tender.id, lid: pos.lid, pid: pos.id, ag_id: nig.id,
  165. create_user_id: user_id, update_user_id: user_id,
  166. agd_order: cd.agd_order,
  167. str1 : cd.str1 || '', str2 : cd.str1 || '', str3 : cd.str1 || '', str4 : cd.str1 || '',
  168. num_a : cd.num_a || 0, num_b : cd.num_b || 0, num_c : cd.num_c || 0, num_d : cd.num_d || 0,
  169. num_e : cd.num_e || 0, num_f : cd.num_f || 0, num_g : cd.num_g || 0, num_h : cd.num_h || 0,
  170. num_i : cd.num_i || 0, num_j : cd.num_j || 0, num_k : cd.num_k || 0, num_l : cd.num_l || 0,
  171. num_m : cd.num_m || 0, num_n : cd.num_n || 0, num_o : cd.num_o || 0, num_p : cd.num_p || 0,
  172. num_q : cd.num_q || 0, num_r : cd.num_r || 0, num_s : cd.num_s || 0, num_t : cd.num_t || 0,
  173. num_u : cd.num_u || 0,
  174. qty: cd.qty || 0, expr: cd.expr || 0, spec: cd.spec || ''
  175. };
  176. sumQty = this.ctx.helper.add(sumQty, newDetail.qty);
  177. insertDetail.push(newDetail);
  178. }
  179. nig.quantity = this.ctx.helper.round(sumQty, qtyDecimal);
  180. }
  181. const transaction = await this.db.beginTransaction();
  182. try {
  183. await transaction.insert(this.tableName, insertAncGcl);
  184. if (insertDetail.length > 0) await transaction.insert(this.ctx.service.ancillaryGclDetail.tableName, insertDetail);
  185. await transaction.commit();
  186. } catch (err) {
  187. await transaction.rollback();
  188. throw err;
  189. }
  190. return [ insertAncGcl, { add: insertDetail }];
  191. }
  192. async updateDatas(data) {
  193. const result = { add: [], del: [], update: [], details: null };
  194. try {
  195. if (data.add) {
  196. result.add = await this._addDatas(data.add);
  197. }
  198. if (data.update) {
  199. result.update = await this._updateDatas(data.update);
  200. }
  201. if (data.del) {
  202. [result.del, result.update] = await this._delDatas(data.del);
  203. }
  204. if (data.calcTmpl) {
  205. [result.update, result.details] = await this._setCalcTemplate(data.calcTmpl);
  206. }
  207. if (data.pasteBlock) {
  208. [result.add, result.details] = await this._pasteBlock(data.pasteBlock);
  209. }
  210. return result;
  211. } catch (err) {
  212. if (err.stack) {
  213. throw err;
  214. } else {
  215. result.err = err.toString();
  216. return result;
  217. }
  218. }
  219. }
  220. async deleteBillsPartData(transaction, tid, lid) {
  221. await transaction.delete(this.tableName, { tid: tid, lid: lid });
  222. }
  223. async deletePosPartData(transaction, tid, pid) {
  224. await transaction.delete(this.tableName, { tid: tid, pid: pid });
  225. }
  226. async getUsedCalcTemplate(tid) {
  227. const sql = `SELECT calc_template, count(id) as count FROM ${this.tableName} WHERE tid = ? and calc_template <> '' GROUP BY calc_template`;
  228. const result = await this.db.query(sql, [tid]);
  229. return result;
  230. }
  231. }
  232. return AncillaryGcl;
  233. };