change_audit_list.js 46 KB

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