change_audit_list.js 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const audit = require('../const/audit');
  10. module.exports = app => {
  11. class ChangeAuditList extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'change_audit_list';
  21. }
  22. /**
  23. * 取出变更令清单列表,并按台账清单在前,空白清单在后排序
  24. * @return {void}
  25. */
  26. async getList(cid, order_by = this.ctx.change.order_by) {
  27. if (order_by) {
  28. return await this.getAllDataByCondition({ where: { cid }, orders: [['order', 'asc']] });
  29. }
  30. const sql = 'SELECT * FROM ?? WHERE `cid` = ? ORDER BY `lid` = "0", `id` asc';
  31. const sqlParam = [this.tableName, cid];
  32. return await this.db.query(sql, sqlParam);
  33. }
  34. /**
  35. * 移除清单时,同步其后清单order
  36. * @param transaction - 事务
  37. * @param {Number} cid - 变更cid
  38. * @param {Number} order - order之后的
  39. * @return {Promise<*>}
  40. * @private
  41. */
  42. async _syncOrder(transaction, cid, order, selfOperate = '-', num = 1) {
  43. this.initSqlBuilder();
  44. this.sqlBuilder.setAndWhere('cid', {
  45. value: this.db.escape(cid),
  46. operate: '=',
  47. });
  48. this.sqlBuilder.setAndWhere('order', {
  49. value: order,
  50. operate: '>=',
  51. });
  52. this.sqlBuilder.setUpdateData('order', {
  53. value: num,
  54. selfOperate,
  55. });
  56. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  57. const data = await transaction.query(sql, sqlParam);
  58. return data;
  59. }
  60. /**
  61. * 添加空白变更清单
  62. * @return {void}
  63. */
  64. async add(data) {
  65. if (!this.ctx.tender || !this.ctx.change) {
  66. throw '数据错误';
  67. }
  68. const transaction = await this.db.beginTransaction();
  69. try {
  70. let order = null;
  71. if (this.ctx.change.order_by) {
  72. if (data) {
  73. order = parseInt(data) + 1;
  74. // order以下的清单+1
  75. await this._syncOrder(transaction, this.ctx.change.cid, order, '+');
  76. } else {
  77. order = await this.count({ cid: this.ctx.change.cid });
  78. order = order ? order + 1 : 1;
  79. }
  80. }
  81. const insertData = {
  82. tid: this.ctx.tender.id,
  83. cid: this.ctx.change.cid,
  84. lid: '0',
  85. code: '',
  86. name: '',
  87. bwmx: '',
  88. unit: '',
  89. unit_price: null,
  90. oamount: 0,
  91. camount: 0,
  92. samount: '',
  93. detail: '',
  94. spamount: 0,
  95. xmj_code: null,
  96. xmj_jldy: null,
  97. xmj_dwgc: null,
  98. xmj_fbgc: null,
  99. xmj_fxgc: null,
  100. gcl_id: '',
  101. order,
  102. };
  103. // 新增工料
  104. const result = await transaction.insert(this.tableName, insertData);
  105. if (result.affectedRows === 0) {
  106. throw '新增空白清单数据失败';
  107. }
  108. await transaction.commit();
  109. return await this.getDataById(result.insertId);
  110. } catch (err) {
  111. await transaction.rollback();
  112. throw err;
  113. }
  114. }
  115. /**
  116. * 批量添加空白变更清单
  117. * @return {void}
  118. */
  119. async batchAdd(data) {
  120. if (!this.ctx.tender || !this.ctx.change) {
  121. throw '数据错误';
  122. }
  123. const transaction = await this.db.beginTransaction();
  124. try {
  125. const num = data.num ? parseInt(data.num) : 0;
  126. if (num < 1 || num > 100) {
  127. throw '批量添加的空白清单数目不能小于1或大于100';
  128. }
  129. let order = null;
  130. if (this.ctx.change.order_by) {
  131. if (data) {
  132. order = parseInt(data.postData) + 1;
  133. // order以下的清单+1
  134. await this._syncOrder(transaction, this.ctx.change.cid, order, '+', num);
  135. } else {
  136. order = await this.count({ cid: this.ctx.change.cid });
  137. order = order ? order + 1 : 1;
  138. }
  139. }
  140. const insertArray = [];
  141. for (let i = 0; i < num; i++) {
  142. const insertData = {
  143. tid: this.ctx.tender.id,
  144. cid: this.ctx.change.cid,
  145. lid: '0',
  146. code: '',
  147. name: '',
  148. bwmx: '',
  149. unit: '',
  150. unit_price: null,
  151. oamount: 0,
  152. camount: 0,
  153. samount: '',
  154. detail: '',
  155. spamount: 0,
  156. xmj_code: null,
  157. xmj_jldy: null,
  158. xmj_dwgc: null,
  159. xmj_fbgc: null,
  160. xmj_fxgc: null,
  161. gcl_id: '',
  162. order: order ? order + i : null,
  163. };
  164. insertArray.push(insertData);
  165. }
  166. // 新增工料
  167. const result = await transaction.insert(this.tableName, insertArray);
  168. if (result.affectedRows !== num) {
  169. throw '批量添加空白清单数据失败';
  170. }
  171. await transaction.commit();
  172. // // 获取刚批量添加的所有list
  173. // for (let j = 0; j < num; j++) {
  174. // insertArray[j].id = result.insertId + j;
  175. // }
  176. // return insertArray;
  177. return await this.getList(this.ctx.change.cid);
  178. } catch (err) {
  179. await transaction.rollback();
  180. throw err;
  181. }
  182. }
  183. /**
  184. * 删除变更清单
  185. * @param {int} id 清单id
  186. * @return {void}
  187. */
  188. async del(data) {
  189. if (!this.ctx.tender || !this.ctx.change) {
  190. throw '数据错误';
  191. }
  192. const transaction = await this.db.beginTransaction();
  193. try {
  194. // 判断是否可删
  195. await transaction.delete(this.tableName, { id: data.ids });
  196. // // order以下的清单-1
  197. if (this.ctx.change.order_by) {
  198. await this._syncOrder(transaction, this.ctx.change.cid, data.postData, '-', data.ids.length);
  199. }
  200. // 重新算变更令总额
  201. await this.calcCamountSum(transaction);
  202. await transaction.commit();
  203. return true;
  204. } catch (err) {
  205. await transaction.rollback();
  206. throw err;
  207. }
  208. }
  209. /**
  210. * 修改变更清单
  211. * @param {Object} data 工料内容
  212. * @param {int} order 期数
  213. * @return {void}
  214. */
  215. async save(data, order) {
  216. if (!this.ctx.tender || !this.ctx.change) {
  217. throw '数据错误';
  218. }
  219. const transaction = await this.db.beginTransaction();
  220. try {
  221. // const mb_id = data.mb_id;
  222. // delete data.mb_id;
  223. await transaction.update(this.tableName, data);
  224. // await this.calcQuantityByML(transaction, mb_id);
  225. await this.calcCamountSum(transaction);
  226. await transaction.commit();
  227. return true;
  228. } catch (err) {
  229. await transaction.rollback();
  230. throw err;
  231. }
  232. }
  233. /**
  234. * 修改变更清单 复制粘贴
  235. * @param {Object} datas 修改内容
  236. * @return {void}
  237. */
  238. async saveDatas(datas) {
  239. if (!this.ctx.tender || !this.ctx.change) {
  240. throw '数据错误';
  241. }
  242. // 判断是否可修改
  243. // 判断t_type是否为费用
  244. const transaction = await this.db.beginTransaction();
  245. try {
  246. // for (const data of datas) {
  247. // const mb_id = data.mb_id;
  248. // delete data.mb_id;
  249. // await transaction.update(this.tableName, data);
  250. // await this.calcQuantityByML(transaction, mb_id);
  251. // }
  252. await transaction.updateRows(this.tableName, datas);
  253. await this.calcCamountSum(transaction);
  254. await transaction.commit();
  255. return true;
  256. } catch (err) {
  257. await transaction.rollback();
  258. throw err;
  259. }
  260. }
  261. /**
  262. * 台账数据清单 重新选择
  263. * @param {Object} datas 内容
  264. * @return {void}
  265. */
  266. async saveLedgerListDatas(datas, data = null, order_by = this.ctx.change.order_by) {
  267. if (!this.ctx.tender || !this.ctx.change) {
  268. throw '数据错误';
  269. }
  270. // 判断是否可修改
  271. // 判断t_type是否为费用
  272. const transaction = await this.db.beginTransaction();
  273. try {
  274. let order = null;
  275. if (order_by) {
  276. if (data) {
  277. order = parseInt(data) + 1;
  278. // order以下的清单+1
  279. await this._syncOrder(transaction, this.ctx.change.cid, order, '+', datas.length);
  280. } else {
  281. order = await this.count({ cid: this.ctx.change.cid });
  282. order = order ? order + 1 : 1;
  283. }
  284. } else {
  285. // 先删除原本的台账清单数据
  286. const sql = 'DELETE FROM ?? WHERE cid = ? and lid != "0"';
  287. const sqlParam = [this.tableName, this.ctx.change.cid];
  288. await transaction.query(sql, sqlParam);
  289. }
  290. const insertDatas = [];
  291. for (const data of datas) {
  292. data.tid = this.ctx.tender.id;
  293. data.cid = this.ctx.change.cid;
  294. data.spamount = data.camount;
  295. data.samount = '';
  296. data.order = order ? order : null;
  297. order = order ? order + 1 : null;
  298. insertDatas.push(data);
  299. }
  300. if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
  301. await this.calcCamountSum(transaction);
  302. if (!order_by) {
  303. const sql1 = 'SELECT a.* FROM ?? as b LEFT JOIN ?? as a ON b.cbid = a.id WHERE b.cid = ? GROUP BY b.cbid';
  304. const sqlParam1 = [this.ctx.service.stageChange.tableName, this.tableName, this.ctx.change.cid];
  305. const usedList = await transaction.query(sql1, sqlParam1);
  306. // 更新stage_change和stage_change_final的cbid
  307. if (usedList.length > 0) {
  308. const updateList = [];
  309. const sql2 = 'SELECT * FROM ?? WHERE `cid` = ? AND `lid` != "0"';
  310. const sqlParam2 = [this.tableName, this.ctx.change.cid];
  311. const newList = await transaction.query(sql2, sqlParam2);
  312. // const newList = await transaction.select(this.tableName, { where: { cid: this.ctx.change.cid } });
  313. for (const used of usedList) {
  314. const newone = this._.find(newList, { code: used.code, lid: used.lid, gcl_id: used.gcl_id, bwmx: used.bwmx });
  315. if (newone) {
  316. updateList.push({
  317. row: {
  318. cbid: newone.id,
  319. },
  320. where: {
  321. cid: this.ctx.change.cid,
  322. cbid: used.id,
  323. },
  324. });
  325. }
  326. }
  327. if (updateList.length > 0) {
  328. await transaction.updateRows(this.ctx.service.stageChange.tableName, updateList);
  329. await transaction.updateRows(this.ctx.service.stageChangeFinal.tableName, updateList);
  330. }
  331. }
  332. }
  333. await transaction.commit();
  334. return true;
  335. } catch (err) {
  336. await transaction.rollback();
  337. throw err;
  338. }
  339. }
  340. /**
  341. * 台账数据清单 清除部分并重新算原设计总金额
  342. * @param {Object} datas 内容
  343. * @return {void}
  344. */
  345. async removeLedgerListDatas(datas) {
  346. if (!this.ctx.tender || !this.ctx.change) {
  347. throw '数据错误';
  348. }
  349. // 判断是否可修改
  350. // 判断t_type是否为费用
  351. const transaction = await this.db.beginTransaction();
  352. try {
  353. // 先删除原本的台账清单数据
  354. // const sql = 'DELETE FROM ?? WHERE cid = ? and lid != "0"';
  355. // const sqlParam = [this.tableName, this.ctx.change.cid];
  356. // await transaction.query(sql, sqlParam);
  357. // const insertDatas = [];
  358. for (const data of datas) {
  359. // data.tid = this.ctx.tender.id;
  360. // data.cid = this.ctx.change.cid;
  361. // data.spamount = data.camount;
  362. // data.samount = '';
  363. // insertDatas.push(data);
  364. await transaction.delete(this.tableName, { id: data.id });
  365. }
  366. // if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
  367. await this.calcCamountSum(transaction);
  368. await transaction.commit();
  369. return true;
  370. } catch (err) {
  371. await transaction.rollback();
  372. throw err;
  373. }
  374. }
  375. async calcCamountSum(transaction, updateTpDecimal = false) {
  376. // const sql = 'SELECT SUM(ROUND(`camount`*`unit_price`, )) as total_price FROM ?? WHERE cid = ?';
  377. // const sqlParam = [this.tableName, this.change.cid];
  378. // const tp = await transaction.queryOne(sql, sqlParam);
  379. // 防止小数位不精确,采用取值计算
  380. const sql = 'SELECT unit_price, spamount FROM ?? WHERE cid = ?';
  381. const sqlParam = [this.tableName, this.ctx.change.cid];
  382. const changeList = await transaction.query(sql, sqlParam);
  383. let total_price = 0;
  384. const tp_decimal = this.ctx.change.tp_decimal ? this.ctx.change.tp_decimal : this.ctx.tender.info.decimal.tp;
  385. for (const cl of changeList) {
  386. total_price = this.ctx.helper.accAdd(total_price, this.ctx.helper.mul(cl.unit_price, cl.spamount, tp_decimal));
  387. }
  388. const updateData = {
  389. total_price,
  390. };
  391. if (updateTpDecimal) {
  392. updateData.tp_decimal = tp_decimal;
  393. }
  394. const options = {
  395. where: {
  396. cid: this.ctx.change.cid,
  397. },
  398. };
  399. await transaction.update(this.ctx.service.change.tableName, updateData, options);
  400. }
  401. /**
  402. * 用户数据数量提交
  403. * @param {Object} data 内容
  404. * @return {void}
  405. */
  406. async saveAmountData(data) {
  407. if (!this.ctx.tender || !this.ctx.change) {
  408. throw '数据错误';
  409. }
  410. // 判断是否可修改
  411. // 判断t_type是否为费用
  412. const transaction = await this.db.beginTransaction();
  413. try {
  414. await transaction.update(this.tableName, data);
  415. await this.calcCamountSum(transaction);
  416. await transaction.commit();
  417. return true;
  418. } catch (err) {
  419. await transaction.rollback();
  420. throw err;
  421. }
  422. }
  423. async gatherBgBills(tid) {
  424. const sql = 'SELECT cb.code, cb.name, cb.unit, cb.unit_price, Round(Sum(cb.samount + 0), 6) as quantity' +
  425. ' FROM ' + this.tableName + ' cb' +
  426. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON cb.cid = c.cid' +
  427. ' WHERE cb.tid = ? and c.status = ?' +
  428. ' GROUP BY code, name, unit, unit_price';
  429. const param = [tid, audit.flow.status.checked];
  430. const result = await this.db.query(sql, param);
  431. for (const b of result) {
  432. b.total_price = this.ctx.helper.mul(b.unit_price, b.quantity, this.ctx.tender.info.decimal.tp);
  433. }
  434. return result;
  435. }
  436. /**
  437. * 报表用
  438. * Tony Kang
  439. * @param {tid} tid - 标段id
  440. * @return {void}
  441. */
  442. async getChangeAuditBills(tid, onlyChecked) {
  443. const sql = 'SELECT cb.*' +
  444. ' FROM ' + this.tableName + ' cb' +
  445. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON cb.cid = c.cid' +
  446. ' WHERE c.tid = ? ' + (onlyChecked ? 'and c.status = 3' : '') +
  447. ' ORDER BY cb.cid, cb.code';
  448. const param = [tid];
  449. const result = await this.db.query(sql, param);
  450. return result;
  451. }
  452. /**
  453. * 删除变更清单(form 变更新增部位页)
  454. * Tony Kang
  455. * @param {String} transaction - 队列
  456. * @param {String} tid - 标段id
  457. * @param {Array} ids - id列表
  458. * @param {String} column - id所属字段
  459. * @param {String} mx_id - mx_id为空列删除
  460. * @return {void}
  461. */
  462. async deleteDataByRevise(transaction, tid, ids, column = 'gcl_id', mx_id = 'hello') {
  463. if (ids.length > 0) {
  464. const addSql = mx_id === '' ? ' AND (`mx_id` is NULL OR `mx_id` = "")' : '';
  465. const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ')' + addSql + ' GROUP BY `cid`';
  466. const params = [this.tableName, tid];
  467. const changes = await transaction.query(sql, params);
  468. if (changes.length > 0) {
  469. const delData = {
  470. tid,
  471. };
  472. delData[column] = ids;
  473. await transaction.delete(this.tableName, delData);
  474. for (const c of changes) {
  475. // 重算选了此清单的变更令已变更金额
  476. await this.reCalcTp(transaction, c.cid);
  477. }
  478. }
  479. }
  480. }
  481. /**
  482. * 修改变更清单(form 变更新增部位页台账子节点清单编号编辑)
  483. * Tony Kang
  484. * @param {String} transaction - 队列
  485. * @param {String} tid - 标段id
  486. * @param {Array} datas - 更新列表
  487. * @param {String} column - id所属字段
  488. * @return {void}
  489. */
  490. async updateDataByReviseLedger(transaction, tid, datas, column = 'gcl_id') {
  491. if (datas.length > 0) {
  492. const ids = this._.map(datas, 'id');
  493. const sql = 'SELECT ' + column + ' FROM ?? WHERE `tid` = ? AND ' + column + ' in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ') GROUP BY ' + column;
  494. const params = [this.tableName, tid];
  495. const changeAuditLists = await transaction.query(sql, params);
  496. if (changeAuditLists.length > 0) {
  497. const updateArr = [];
  498. const cidList = [];
  499. for (const ca of changeAuditLists) {
  500. const d = this._.find(datas, { id: ca[column] });
  501. if (d.id) {
  502. const changePosNum = await transaction.count(this.ctx.service.changePos.tableName, { lid: d.id });
  503. const updateCol = {};
  504. if (column === 'gcl_id' && d.b_code) updateCol.code = d.b_code;
  505. if (column === 'gcl_id' && d.quantity !== undefined && changePosNum === 0) updateCol.oamount = d.quantity ? d.quantity : 0;
  506. if (column === 'gcl_id' && d.unit_price !== undefined) updateCol.unit_price = d.unit_price ? d.unit_price : 0;
  507. if (column === 'gcl_id' && d.unit !== undefined) updateCol.unit = d.unit;
  508. if (column === 'gcl_id' && d.name !== undefined) updateCol.name = d.name;
  509. if (d.b_code !== undefined && d.b_code === null) {
  510. // 清单升级成了项目节,故删除变更已有的此清单,并找出需要重新计算的变更令
  511. const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' = ? GROUP BY `cid`';
  512. const params = [this.tableName, tid, d.id];
  513. const changes = await transaction.query(sql, params);
  514. for (const c of changes) {
  515. if (this._.indexOf(cidList, c.cid) === -1) {
  516. cidList.push(c.cid);
  517. }
  518. }
  519. const delData = {
  520. tid,
  521. };
  522. delData[column] = d.id;
  523. await transaction.delete(this.tableName, delData);
  524. } else {
  525. const options = {
  526. row: {},
  527. where: {},
  528. };
  529. options.row = updateCol;
  530. options.where[column] = d.id;
  531. if (!this._.isEmpty(options.row)) updateArr.push(options);
  532. if (updateCol.unit !== undefined || updateCol.unit_price !== undefined) {
  533. const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' = ? GROUP BY `cid`';
  534. const params = [this.tableName, tid, d.id];
  535. const changes = await transaction.query(sql, params);
  536. for (const c of changes) {
  537. if (this._.indexOf(cidList, c.cid) === -1) {
  538. cidList.push(c.cid);
  539. }
  540. }
  541. }
  542. }
  543. }
  544. }
  545. if (updateArr.length > 0) await transaction.updateRows(this.tableName, updateArr);
  546. if (cidList.length > 0) {
  547. for (const c of cidList) {
  548. await this.reCalcTp(transaction, c);
  549. }
  550. }
  551. }
  552. // 针对项目节更新可能对清单影响判断,修正变更清单项目节编号,细目,单位工程,分部分项工程数据
  553. for (const data of datas) {
  554. const select = await transaction.get(this.ctx.service.changeLedger.tableName, { id: data.id });
  555. if (select && select.is_leaf === 0) {
  556. const lists = await this.ctx.service.changeLedger.getDataByFullPath(this.ctx.service.changeLedger.tableName, tid, select.full_path + '%', transaction);
  557. const childLists = this._.filter(lists, { level: select.level + 1 }); // 细目or项目节编号更新
  558. if (childLists.length > 0) {
  559. const d = { xmj_code: '', xmj_jldy: '' };
  560. if (select.code !== null) {
  561. d.xmj_code = select.code;
  562. d.xmj_jldy = select.name;
  563. } else {
  564. // 再找出上一个项目节节点并更新
  565. this.newBills = false;
  566. const parents = await this.ctx.service.changeLedger.getDataByKid(tid, select.ledger_pid);
  567. d.xmj_code = parents.code;
  568. d.xmj_jldy = parents.name;
  569. }
  570. for (const cl of childLists) {
  571. await transaction.update(this.tableName, { xmj_code: d.xmj_code, xmj_jldy: d.xmj_jldy }, { where: { tid, gcl_id: cl.id } });
  572. }
  573. }
  574. if (select.code !== null && data.name !== undefined) { // 名称修改则可能影响几个数据
  575. const secondChildLists = this._.filter(lists, { level: select.level + 2 }); // 分项工程更新
  576. const thirdChildLists = this._.filter(lists, { level: select.level + 3 }); // 分部工程更新
  577. const fourthChildLists = this._.filter(lists, { level: select.level + 4 }); // 单位工程更新
  578. if (secondChildLists.length > 0) {
  579. for (const sl of secondChildLists) {
  580. await transaction.update(this.tableName, { xmj_fxgc: select.name }, { where: { tid, gcl_id: sl.id } });
  581. }
  582. }
  583. if (thirdChildLists.length > 0) {
  584. for (const tl of thirdChildLists) {
  585. await transaction.update(this.tableName, { xmj_fbgc: select.name }, { where: { tid, gcl_id: tl.id } });
  586. }
  587. }
  588. if (fourthChildLists.length > 0 && select.level === 2) {
  589. for (const fl of fourthChildLists) {
  590. await transaction.update(this.tableName, { xmj_dwgc: select.name }, { where: { tid, gcl_id: fl.id } });
  591. }
  592. }
  593. }
  594. }
  595. }
  596. }
  597. }
  598. /**
  599. * 修改变更清单(form 变更新增部位页台账节点清单编号升降级)
  600. * Tony Kang
  601. * @param {String} transaction - 队列
  602. * @param {String} tid - 标段id
  603. * @param {Array} datas - 更新列表
  604. * @param {String} column - id所属字段
  605. * @return {void}
  606. */
  607. async updateDataByReviseLedgerUpDownLevel(transaction, tid, datas, column = 'gcl_id') {
  608. if (datas.length > 0) {
  609. console.log(datas);
  610. // const ids = this._.map(datas, 'id');
  611. // const sql = 'SELECT ' + column + ' FROM ?? WHERE `tid` = ? AND ' + column + ' in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ') GROUP BY ' + column;
  612. // const params = [this.tableName, tid];
  613. // const changeAuditLists = await transaction.query(sql, params);
  614. // if (changeAuditLists.length > 0) {
  615. // const updateArr = [];
  616. // const cidList = [];
  617. // for (const ca of changeAuditLists) {
  618. // const d = this._.find(datas, { id: ca[column] });
  619. // console.log(d);
  620. // if (d.id) {
  621. // const changePosNum = await transaction.count(this.ctx.service.changePos.tableName, { lid: d.id });
  622. // const updateCol = {};
  623. // if (column === 'gcl_id' && d.b_code !== undefined) updateCol.code = d.b_code;
  624. // if (column === 'gcl_id' && d.sgfh_qty !== undefined && changePosNum === 0) updateCol.oamount = d.sgfh_qty ? d.sgfh_qty : 0;
  625. // if (column === 'gcl_id' && d.unit_price !== undefined) updateCol.unit_price = d.unit_price ? d.unit_price : 0;
  626. // if (column === 'gcl_id' && d.unit !== undefined) updateCol.unit = d.unit;
  627. // if (column === 'gcl_id' && d.name !== undefined) updateCol.name = d.name;
  628. // if (d.code !== undefined && d.b_code === null) {
  629. // // 清单升级成了项目节,故删除变更已有的此清单,并找出需要重新计算的变更令
  630. // const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' = ? GROUP BY `cid`';
  631. // const params = [this.tableName, tid, d.id];
  632. // const changes = await transaction.query(sql, params);
  633. // for (const c of changes) {
  634. // if (this._.indexOf(cidList, c.cid) === -1) {
  635. // cidList.push(c.cid);
  636. // }
  637. // }
  638. // const delData = {
  639. // tid,
  640. // };
  641. // delData[column] = d.id;
  642. // console.log(delData);
  643. // await transaction.delete(this.tableName, delData);
  644. // } else {
  645. // const options = {
  646. // row: {},
  647. // where: {},
  648. // };
  649. // options.row = updateCol;
  650. // options.where[column] = d.id;
  651. // if (!this._.isEmpty(options.row)) updateArr.push(options);
  652. // if (updateCol.unit !== undefined || updateCol.unit_price !== undefined) {
  653. // const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' = ? GROUP BY `cid`';
  654. // const params = [this.tableName, tid, d.id];
  655. // const changes = await transaction.query(sql, params);
  656. // for (const c of changes) {
  657. // if (this._.indexOf(cidList, c.cid) === -1) {
  658. // cidList.push(c.cid);
  659. // }
  660. // }
  661. // }
  662. // }
  663. // }
  664. // }
  665. // console.log(updateArr, cidList);
  666. // if (updateArr.length > 0) await transaction.updateRows(this.tableName, updateArr);
  667. // if (cidList.length > 0) {
  668. // for (const c of cidList) {
  669. // await this.reCalcTp(transaction, c);
  670. // }
  671. // }
  672. // }
  673. // 针对项目节更新可能对清单影响判断,修正变更清单项目节编号,细目,单位工程,分部分项工程数据
  674. // for (const data of datas) {
  675. // const select = await transaction.get(this.ctx.service.changeLedger.tableName, { id: data.id });
  676. // console.log(select);
  677. // if (select && select.is_leaf === 0) {
  678. // const lists = await this.ctx.service.changeLedger.getDataByFullPath(this.ctx.service.changeLedger.tableName, tid, select.full_path + '%', transaction);
  679. // const childLists = this._.filter(lists, { level: select.level + 1 }); // 细目or项目节编号更新
  680. // if (childLists.length > 0) {
  681. // const d = { xmj_code: '', xmj_jldy: '' };
  682. // if (select.code !== null) {
  683. // d.xmj_code = select.code;
  684. // d.xmj_jldy = select.name;
  685. // } else {
  686. // // 再找出上一个项目节节点并更新
  687. // this.newBills = false;
  688. // const parents = await this.ctx.service.changeLedger.getDataByKid(tid, select.ledger_pid);
  689. // console.log('hello :', parents);
  690. // d.xmj_code = parents.code;
  691. // d.xmj_jldy = parents.name;
  692. // }
  693. // for (const cl of childLists) {
  694. // await transaction.update(this.tableName, { xmj_code: d.xmj_code, xmj_jldy: d.xmj_jldy }, { where: { tid, gcl_id: cl.id } });
  695. // }
  696. // }
  697. // if (select.code !== null && data.name !== undefined) { // 名称修改则可能影响几个数据
  698. // const secondChildLists = this._.filter(lists, { level: select.level + 2 }); // 分项工程更新
  699. // const thirdChildLists = this._.filter(lists, { level: select.level + 3 }); // 分部工程更新
  700. // const fourthChildLists = this._.filter(lists, { level: select.level + 4 }); // 单位工程更新
  701. // if (secondChildLists.length > 0) {
  702. // for (const sl of secondChildLists) {
  703. // await transaction.update(this.tableName, { xmj_fxgc: select.name }, { where: { tid, gcl_id: sl.id } });
  704. // }
  705. // }
  706. // if (thirdChildLists.length > 0) {
  707. // for (const tl of thirdChildLists) {
  708. // await transaction.update(this.tableName, { xmj_fbgc: select.name }, { where: { tid, gcl_id: tl.id } });
  709. // }
  710. // }
  711. // if (fourthChildLists.length > 0) {
  712. // for (const fl of fourthChildLists) {
  713. // await transaction.update(this.tableName, { xmj_dwgc: select.name }, { where: { tid, gcl_id: fl.id } });
  714. // }
  715. // }
  716. // }
  717. // }
  718. // }
  719. }
  720. }
  721. /**
  722. * 修改变更清单(form 变更新增部位页计量单元编辑)
  723. * Tony Kang
  724. * @param {String} transaction - 队列
  725. * @param {String} tid - 标段id
  726. * @param {Array} datas - 更新列表
  727. * @param {String} column - id所属字段
  728. * @return {void}
  729. */
  730. async updateDataByRevisePos(transaction, tid, datas, column = 'mx_id') {
  731. if (datas.length > 0) {
  732. const ids = this._.map(datas, 'id');
  733. const sql = 'SELECT ' + column + ' FROM ?? WHERE `tid` = ? AND ' + column + ' in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ') GROUP BY ' + column;
  734. const params = [this.tableName, tid];
  735. const changeAuditLists = await transaction.query(sql, params);
  736. if (changeAuditLists.length > 0) {
  737. const updateArr = [];
  738. for (const ca of changeAuditLists) {
  739. const d = this._.find(datas, { id: ca[column] });
  740. if (d.id) {
  741. const updateCol = {};
  742. if (column === 'mx_id' && d.name !== undefined) updateCol.bwmx = d.name;
  743. if (column === 'mx_id' && d.quantity !== undefined) updateCol.oamount = d.quantity ? d.quantity : 0;
  744. if (column === 'mx_id' && d.quantity === undefined &&
  745. ((d.sgfh_expr && d.sgfh_expr === '') || (d.sjcl_expr && d.sjcl_expr === '') || (d.qtcl_expr && d.qtcl_expr === ''))) updateCol.oamount = 0;
  746. const options = {
  747. row: {},
  748. where: {},
  749. };
  750. options.row = updateCol;
  751. options.where[column] = d.id;
  752. // if (!this._.isEmpty(updateCol)) await transaction.update(this.tableName, updateCol, options);
  753. if (!this._.isEmpty(options.row)) updateArr.push(options);
  754. }
  755. }
  756. if (updateArr.length > 0) await transaction.updateRows(this.tableName, updateArr);
  757. }
  758. }
  759. }
  760. /**
  761. * 重算变更令总金额(变更新增部位设置时使用)
  762. * @param {String} transaction - 队列
  763. * @param {String} cid - 变更令id
  764. */
  765. async reCalcTp(transaction, cid) {
  766. const change = await transaction.get(this.ctx.service.change.tableName, { cid });
  767. let count = '';
  768. if (change.status === audit.flow.status.uncheck || change.status === audit.flow.status.back || change.status === audit.flow.status.revise) {
  769. count = '`camount`';
  770. } else if (change.status === audit.flow.status.checking || change.status === audit.flow.status.backnew) {
  771. count = '`spamount`';
  772. }
  773. if (count) {
  774. const sql = 'SELECT `unit_price`, ' + count + ' as `count` FROM ?? WHERE `cid` = ?';
  775. const params = [this.tableName, change.cid];
  776. const caLists = await transaction.query(sql, params);
  777. let tp = 0;
  778. const tpUnit = change.tp_decimal ? change.tp_decimal : this.ctx.tender.info.decimal.tp;
  779. for (const ca of caLists) {
  780. const catp = this.ctx.helper.round(this.ctx.helper.mul(ca.unit_price, ca.count), tpUnit);
  781. tp = this.ctx.helper.add(tp, catp);
  782. }
  783. console.log(tp);
  784. if (tp !== change.total_price) {
  785. const options = {
  786. where: {
  787. cid: change.cid,
  788. },
  789. };
  790. const change_update = {
  791. total_price: tp,
  792. };
  793. await transaction.update(this.ctx.service.change.tableName, change_update, options);
  794. }
  795. }
  796. }
  797. async updateToLedger(transaction, tid, cid) {
  798. // 找出本条变更属于新增部位的数据
  799. const allList = await transaction.select(this.tableName, { where: { tid, cid } });
  800. const result = [];
  801. const result2 = [];
  802. for (const l of allList) {
  803. const changeLedgerInfo = await transaction.get(this.ctx.service.changeLedger.tableName, { id: l.gcl_id });
  804. if (changeLedgerInfo && this._.findIndex(result, { id: l.gcl_id }) === -1) {
  805. result.push(changeLedgerInfo);
  806. }
  807. const changePosInfo = await transaction.get(this.ctx.service.changePos.tableName, { id: l.mx_id });
  808. if (changePosInfo) {
  809. result2.push(changePosInfo);
  810. }
  811. }
  812. // const sql = 'SELECT a.* FROM ?? a LEFT JOIN ?? b ON a.id = b.gcl_id WHERE b.tid = ? AND b.cid = ? GROUP BY a.id';
  813. // const sqlParam = [this.ctx.service.changeLedger.tableName, this.tableName, tid, cid];
  814. // const result = await transaction.query(sql, sqlParam);
  815. // const sql2 = 'SELECT a.* FROM ?? a LEFT JOIN ?? b ON a.id = b.mx_id WHERE b.tid = ? AND b.cid = ?';
  816. // const sqlParam2 = [this.ctx.service.changePos.tableName, this.tableName, tid, cid];
  817. // const result2 = await transaction.query(sql2, sqlParam2);
  818. if (result.length > 0 || result2.length > 0) {
  819. const changeLedgerGclIdList = this._.map(result, 'id');
  820. const changeLedgerIdList = this._.uniq(this._.map(result, 'ledger_pid'));// 父节点集合
  821. const needUpdateLedgerList = [];// 找出需要更新的原台账清单的id
  822. const needUpdateChangeLedgerList = [];// 找出需要更新的新台账清单的id
  823. const tpDecimal = this.ctx.tender.info.decimal.tp;
  824. // 要更新的ledger节点,数量及总数
  825. for (const data of result2) {
  826. if (this._.indexOf(changeLedgerGclIdList, data.lid) === -1) {
  827. const info = this._.find(needUpdateLedgerList, { id: data.lid });
  828. if (info) {
  829. info.sgfh_qty = this.ctx.helper.add(info.sgfh_qty, data.sgfh_qty);
  830. info.sjcl_qty = this.ctx.helper.add(info.sjcl_qty, data.sjcl_qty);
  831. info.qtcl_qty = this.ctx.helper.add(info.qtcl_qty, data.qtcl_qty);
  832. info.quantity = this.ctx.helper.add(info.quantity, data.quantity);
  833. } else {
  834. needUpdateLedgerList.push({ id: data.lid, sgfh_qty: data.sgfh_qty, sjcl_qty: data.sjcl_qty, qtcl_qty: data.qtcl_qty, quantity: data.quantity });
  835. }
  836. } else {
  837. const info = this._.find(needUpdateChangeLedgerList, { id: data.lid });
  838. if (info) {
  839. info.sgfh_qty = this.ctx.helper.add(info.sgfh_qty, data.sgfh_qty);
  840. info.sjcl_qty = this.ctx.helper.add(info.sjcl_qty, data.sjcl_qty);
  841. info.qtcl_qty = this.ctx.helper.add(info.qtcl_qty, data.qtcl_qty);
  842. info.quantity = this.ctx.helper.add(info.quantity, data.quantity);
  843. } else {
  844. needUpdateChangeLedgerList.push({ id: data.lid, sgfh_qty: data.sgfh_qty, sjcl_qty: data.sjcl_qty, qtcl_qty: data.qtcl_qty, quantity: data.quantity });
  845. }
  846. }
  847. }
  848. // 更新到result上
  849. if (needUpdateChangeLedgerList.length > 0) {
  850. for (const nucl of needUpdateChangeLedgerList) {
  851. const now = this._.find(result, { id: nucl.id });
  852. now.sgfh_qty = nucl.sgfh_qty;
  853. now.sjcl_qty = nucl.sjcl_qty;
  854. now.qtcl_qty = nucl.qtcl_qty;
  855. now.quantity = nucl.quantity;
  856. now.sgfh_tp = this.ctx.helper.mul(now.sgfh_qty, now.unit_price, tpDecimal);
  857. now.sjcl_tp = this.ctx.helper.mul(now.sjcl_qty, now.unit_price, tpDecimal);
  858. now.qtcl_tp = this.ctx.helper.mul(now.qtcl_qty, now.unit_price, tpDecimal);
  859. now.total_price = this.ctx.helper.mul(now.quantity, now.unit_price, tpDecimal);
  860. }
  861. }
  862. // 更新到ledger上
  863. if (needUpdateLedgerList.length > 0) {
  864. for (const nul of needUpdateLedgerList) {
  865. const ledgerInfo = await this.ctx.service.ledger.getDataById(nul.id);
  866. ledgerInfo.sgfh_qty = this.ctx.helper.add(ledgerInfo.sgfh_qty, nul.sgfh_qty);
  867. ledgerInfo.sjcl_qty = this.ctx.helper.add(ledgerInfo.sjcl_qty, nul.sjcl_qty);
  868. ledgerInfo.qtcl_qty = this.ctx.helper.add(ledgerInfo.qtcl_qty, nul.qtcl_qty);
  869. ledgerInfo.quantity = this.ctx.helper.add(ledgerInfo.quantity, nul.quantity);
  870. ledgerInfo.sgfh_tp = this.ctx.helper.mul(ledgerInfo.sgfh_qty, ledgerInfo.unit_price, tpDecimal);
  871. ledgerInfo.sjcl_tp = this.ctx.helper.mul(ledgerInfo.sjcl_qty, ledgerInfo.unit_price, tpDecimal);
  872. ledgerInfo.qtcl_tp = this.ctx.helper.mul(ledgerInfo.qtcl_qty, ledgerInfo.unit_price, tpDecimal);
  873. ledgerInfo.total_price = this.ctx.helper.mul(ledgerInfo.quantity, ledgerInfo.unit_price, tpDecimal);
  874. await transaction.update(this.ctx.service.ledger.tableName, ledgerInfo);
  875. }
  876. }
  877. // 找出所有新增的父节点并插入到result中
  878. for (const r of changeLedgerIdList) {
  879. await this._findParents(transaction, tid, r, result);
  880. }
  881. // 插入到计量单元表,并删除变更的计量单元数据, 插入清单表,并删除变更的清单表
  882. await this._insertByChangeRevise(transaction, tid, cid, result, result2);
  883. // 更新标段总金额
  884. const sumSql = 'SELECT Sum(total_price) As total_price, Sum(deal_tp) As deal_tp' +
  885. ' FROM ' + this.ctx.service.ledger.tableName + this.ctx.helper.whereSql({ tender_id: tid });
  886. const sum = await transaction.queryOne(sumSql);
  887. await transaction.update(this.ctx.service.tender.tableName, {
  888. id: tid,
  889. total_price: sum.total_price,
  890. deal_tp: sum.deal_tp,
  891. });
  892. }
  893. }
  894. async _findParents(transaction, tid, id, result) {
  895. const info = await transaction.get(this.ctx.service.changeLedger.tableName, { tender_id: tid, ledger_id: id });
  896. if (info && this._.findIndex(result, { ledger_id: info.ledger_id }) === -1) {
  897. result.push(info);
  898. await this._findParents(transaction, tid, info.ledger_pid, result);
  899. } else {
  900. return;
  901. }
  902. }
  903. async _insertByChangeRevise(transaction, tid, cid, ledgerList, posList) {
  904. if (ledgerList.length > 0) {
  905. const insertLedgerArr = [];
  906. for (const l of ledgerList) {
  907. const insertL = [
  908. l.id, l.code, l.b_code, l.name, l.unit, l.source, l.remark, l.ledger_id,
  909. l.ledger_pid, l.level, l.order, l.full_path, l.is_leaf, l.quantity, l.total_price,
  910. l.unit_price, l.drawing_code, l.memo, l.dgn_qty1, l.dgn_qty2, l.deal_qty, l.deal_tp,
  911. l.sgfh_qty, l.sgfh_tp, l.sjcl_qty, l.sjcl_tp, l.qtcl_qty, l.qtcl_tp, l.node_type, l.crid,
  912. l.tender_id, l.is_tp, l.sgfh_expr, l.sjcl_expr, l.qtcl_expr, l.check_calc, l.gxby_status,
  913. l.dagl_status, l.dagl_url, l.gxby_limit, l.dagl_limit, l.ex_memo1, l.ex_memo2, l.ex_memo3,
  914. ];
  915. insertLedgerArr.push('(' + this.ctx.helper.getInArrStrSqlFilter(insertL) + ')');
  916. await transaction.delete(this.ctx.service.changeLedger.tableName, { id: l.id });
  917. // 日志添加
  918. await transaction.insert(this.ctx.service.changeReviseLog.tableName, { tid, cid, lid: l.id, name: l.name, create_time: new Date() });
  919. }
  920. const bSql = 'Insert Into ' +
  921. this.ctx.service.ledger.tableName +
  922. ' (id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
  923. ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
  924. ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id, is_tp,' +
  925. ' sgfh_expr, sjcl_expr, qtcl_expr, check_calc,' +
  926. ' gxby_status, dagl_status, dagl_url, gxby_limit, dagl_limit,' +
  927. ' ex_memo1, ex_memo2, ex_memo3) VALUES ' + insertLedgerArr.join(',') + ';';
  928. await transaction.query(bSql, []);
  929. }
  930. if (posList.length > 0) {
  931. const insertPosArr = [];
  932. for (const p of posList) {
  933. const insertp = [
  934. p.id, p.tid, p.lid, p.name, p.drawing_code, p.quantity, p.add_stage, p.add_stage_order, p.add_times,
  935. p.add_user, p.sgfh_qty, p.sjcl_qty, p.qtcl_qty, p.crid, p.porder, p.position,
  936. p.sgfh_expr, p.sjcl_expr, p.qtcl_expr, p.real_qty,
  937. p.gxby_status, p.dagl_status, p.dagl_url, p.gxby_limit, p.dagl_limit,
  938. p.ex_memo1, p.ex_memo2, p.ex_memo3,
  939. ];
  940. insertPosArr.push('(' + this.ctx.helper.getInArrStrSqlFilter(insertp) + ')');
  941. await transaction.delete(this.ctx.service.changePos.tableName, { id: p.id });
  942. // 日志添加
  943. await transaction.insert(this.ctx.service.changeReviseLog.tableName, { tid, cid, pid: p.id, name: p.name, create_time: new Date() });
  944. }
  945. const pSql =
  946. 'Insert Into ' +
  947. this.ctx.service.pos.tableName +
  948. ' (id, tid, lid, name, drawing_code, quantity, add_stage, add_stage_order, add_times, add_user,' +
  949. ' sgfh_qty, sjcl_qty, qtcl_qty, crid, porder, position, ' +
  950. ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty,' +
  951. ' gxby_status, dagl_status, dagl_url, gxby_limit, dagl_limit,' +
  952. ' ex_memo1, ex_memo2, ex_memo3) VALUES ' + insertPosArr.join(',') + ';';
  953. await transaction.query(pSql, []);
  954. }
  955. }
  956. async checkedChangeBills(tid) {
  957. const DefaultDecimal = this.ctx.tender.info.decimal.tp;
  958. const sql = 'SELECT cal.*, c.tp_decimal FROM ' + this.ctx.service.changeAuditList.tableName + ' cal LEFT JOIN ' + this.ctx.service.change.tableName + ' c on cal.cid = c.cid where c.tid = ? and c.valid and c.status = ?';
  959. const changeBills = await this.db.query(sql, [tid, audit.flow.status.checked]);
  960. changeBills.forEach(x => {
  961. x.tp_decimal = x.tp_decimal !== 0 ? x.tp_decimal : DefaultDecimal
  962. });
  963. return changeBills;
  964. }
  965. /**
  966. * 交换两个清单的顺序
  967. * @param {Number} id1 - 工料1的id
  968. * @param {Number} id2 - 工料2的id
  969. * @returns {Promise<void>}
  970. */
  971. async changeOrder(datas) {
  972. if (!this.ctx.tender || !this.ctx.change) {
  973. throw '数据错误';
  974. }
  975. // const bill1 = await this.getDataByCondition({ tid: this.ctx.tender.id, id: id1 });
  976. // const bill2 = await this.getDataByCondition({ tid: this.ctx.tender.id, id: id2 });
  977. // if (!bill1 || !bill2) {
  978. // throw '数据错误';
  979. // }
  980. const transaction = await this.db.beginTransaction();
  981. try {
  982. // const order = bill1.order;
  983. // bill1.order = bill2.order;
  984. // bill2.order = order;
  985. // await transaction.update(this.tableName, { id: bill1.id, order: bill1.order });
  986. // await transaction.update(this.tableName, { id: bill2.id, order: bill2.order });
  987. await transaction.updateRows(this.tableName, datas);
  988. await transaction.commit();
  989. return true;
  990. } catch (err) {
  991. await transaction.rollback();
  992. throw err;
  993. }
  994. }
  995. }
  996. return ChangeAuditList;
  997. };