change_audit_list.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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, updateTpDecimal = false) {
  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. if (updateTpDecimal) {
  304. updateData.tp_decimal = tp_decimal;
  305. }
  306. const options = {
  307. where: {
  308. cid: this.ctx.change.cid,
  309. },
  310. };
  311. await transaction.update(this.ctx.service.change.tableName, updateData, options);
  312. }
  313. /**
  314. * 用户数据数量提交
  315. * @param {Object} data 内容
  316. * @return {void}
  317. */
  318. async saveAmountData(data) {
  319. if (!this.ctx.tender || !this.ctx.change) {
  320. throw '数据错误';
  321. }
  322. // 判断是否可修改
  323. // 判断t_type是否为费用
  324. const transaction = await this.db.beginTransaction();
  325. try {
  326. await transaction.update(this.tableName, data);
  327. await this.calcCamountSum(transaction);
  328. await transaction.commit();
  329. return true;
  330. } catch (err) {
  331. await transaction.rollback();
  332. throw err;
  333. }
  334. }
  335. async gatherBgBills(tid) {
  336. const sql = 'SELECT cb.code, cb.name, cb.unit, cb.unit_price, Round(Sum(cb.samount + 0), 6) as quantity' +
  337. ' FROM ' + this.tableName + ' cb' +
  338. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON cb.cid = c.cid' +
  339. ' WHERE cb.tid = ? and c.status = ?' +
  340. ' GROUP BY code, name, unit, unit_price';
  341. const param = [tid, audit.flow.status.checked];
  342. const result = await this.db.query(sql, param);
  343. for (const b of result) {
  344. b.total_price = this.ctx.helper.mul(b.unit_price, b.quantity, this.ctx.tender.info.decimal.tp);
  345. }
  346. return result;
  347. }
  348. /**
  349. * 报表用
  350. * Tony Kang
  351. * @param {tid} tid - 标段id
  352. * @return {void}
  353. */
  354. async getChangeAuditBills(tid, onlyChecked) {
  355. const sql = 'SELECT cb.*' +
  356. ' FROM ' + this.tableName + ' cb' +
  357. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON cb.cid = c.cid' +
  358. ' WHERE c.tid = ? ' + (onlyChecked ? 'and c.status = 3' : '') +
  359. ' ORDER BY cb.cid, cb.code';
  360. const param = [tid];
  361. const result = await this.db.query(sql, param);
  362. return result;
  363. }
  364. /**
  365. * 删除变更清单(form 变更新增部位页)
  366. * Tony Kang
  367. * @param {String} transaction - 队列
  368. * @param {String} tid - 标段id
  369. * @param {Array} ids - id列表
  370. * @param {String} column - id所属字段
  371. * @param {String} mx_id - mx_id为空列删除
  372. * @return {void}
  373. */
  374. async deleteDataByRevise(transaction, tid, ids, column = 'gcl_id', mx_id = 'hello') {
  375. if (ids.length > 0) {
  376. const addSql = mx_id === '' ? ' AND (`mx_id` is NULL OR `mx_id` = "")' : '';
  377. const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ')' + addSql + ' GROUP BY `cid`';
  378. const params = [this.tableName, tid];
  379. const changes = await transaction.query(sql, params);
  380. if (changes.length > 0) {
  381. const delData = {
  382. tid,
  383. };
  384. delData[column] = ids;
  385. await transaction.delete(this.tableName, delData);
  386. for (const c of changes) {
  387. // 重算选了此清单的变更令已变更金额
  388. await this.reCalcTp(transaction, c.cid);
  389. }
  390. }
  391. }
  392. }
  393. /**
  394. * 修改变更清单(form 变更新增部位页台账子节点清单编号编辑)
  395. * Tony Kang
  396. * @param {String} transaction - 队列
  397. * @param {String} tid - 标段id
  398. * @param {Array} datas - 更新列表
  399. * @param {String} column - id所属字段
  400. * @return {void}
  401. */
  402. async updateDataByReviseLedger(transaction, tid, datas, column = 'gcl_id') {
  403. if (datas.length > 0) {
  404. const ids = this._.map(datas, 'id');
  405. const sql = 'SELECT ' + column + ' FROM ?? WHERE `tid` = ? AND ' + column + ' in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ') GROUP BY ' + column;
  406. const params = [this.tableName, tid];
  407. const changeAuditLists = await transaction.query(sql, params);
  408. if (changeAuditLists.length > 0) {
  409. const updateArr = [];
  410. const cidList = [];
  411. for (const ca of changeAuditLists) {
  412. const d = this._.find(datas, { id: ca[column] });
  413. if (d.id) {
  414. const changePosNum = await transaction.count(this.ctx.service.changePos.tableName, { lid: d.id });
  415. const updateCol = {};
  416. if (column === 'gcl_id' && d.b_code) updateCol.code = d.b_code;
  417. if (column === 'gcl_id' && d.quantity !== undefined && changePosNum === 0) updateCol.oamount = d.quantity ? d.quantity : 0;
  418. if (column === 'gcl_id' && d.unit_price !== undefined) updateCol.unit_price = d.unit_price ? d.unit_price : 0;
  419. if (column === 'gcl_id' && d.unit !== undefined) updateCol.unit = d.unit;
  420. if (column === 'gcl_id' && d.name !== undefined) updateCol.name = d.name;
  421. if (d.b_code !== undefined && d.b_code === null) {
  422. // 清单升级成了项目节,故删除变更已有的此清单,并找出需要重新计算的变更令
  423. const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' = ? GROUP BY `cid`';
  424. const params = [this.tableName, tid, d.id];
  425. const changes = await transaction.query(sql, params);
  426. for (const c of changes) {
  427. if (this._.indexOf(cidList, c.cid) === -1) {
  428. cidList.push(c.cid);
  429. }
  430. }
  431. const delData = {
  432. tid,
  433. };
  434. delData[column] = d.id;
  435. await transaction.delete(this.tableName, delData);
  436. } else {
  437. const options = {
  438. row: {},
  439. where: {},
  440. };
  441. options.row = updateCol;
  442. options.where[column] = d.id;
  443. if (!this._.isEmpty(options.row)) updateArr.push(options);
  444. if (updateCol.unit !== undefined || updateCol.unit_price !== undefined) {
  445. const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' = ? GROUP BY `cid`';
  446. const params = [this.tableName, tid, d.id];
  447. const changes = await transaction.query(sql, params);
  448. for (const c of changes) {
  449. if (this._.indexOf(cidList, c.cid) === -1) {
  450. cidList.push(c.cid);
  451. }
  452. }
  453. }
  454. }
  455. }
  456. }
  457. if (updateArr.length > 0) await transaction.updateRows(this.tableName, updateArr);
  458. if (cidList.length > 0) {
  459. for (const c of cidList) {
  460. await this.reCalcTp(transaction, c);
  461. }
  462. }
  463. }
  464. // 针对项目节更新可能对清单影响判断,修正变更清单项目节编号,细目,单位工程,分部分项工程数据
  465. for (const data of datas) {
  466. const select = await transaction.get(this.ctx.service.changeLedger.tableName, { id: data.id });
  467. if (select && select.is_leaf === 0) {
  468. const lists = await this.ctx.service.changeLedger.getDataByFullPath(this.ctx.service.changeLedger.tableName, tid, select.full_path + '%', transaction);
  469. const childLists = this._.filter(lists, { level: select.level + 1 }); // 细目or项目节编号更新
  470. if (childLists.length > 0) {
  471. const d = { xmj_code: '', xmj_jldy: '' };
  472. if (select.code !== null) {
  473. d.xmj_code = select.code;
  474. d.xmj_jldy = select.name;
  475. } else {
  476. // 再找出上一个项目节节点并更新
  477. this.newBills = false;
  478. const parents = await this.ctx.service.changeLedger.getDataByKid(tid, select.ledger_pid);
  479. d.xmj_code = parents.code;
  480. d.xmj_jldy = parents.name;
  481. }
  482. for (const cl of childLists) {
  483. await transaction.update(this.tableName, { xmj_code: d.xmj_code, xmj_jldy: d.xmj_jldy }, { where: { tid, gcl_id: cl.id } });
  484. }
  485. }
  486. if (select.code !== null && data.name !== undefined) { // 名称修改则可能影响几个数据
  487. const secondChildLists = this._.filter(lists, { level: select.level + 2 }); // 分项工程更新
  488. const thirdChildLists = this._.filter(lists, { level: select.level + 3 }); // 分部工程更新
  489. const fourthChildLists = this._.filter(lists, { level: select.level + 4 }); // 单位工程更新
  490. if (secondChildLists.length > 0) {
  491. for (const sl of secondChildLists) {
  492. await transaction.update(this.tableName, { xmj_fxgc: select.name }, { where: { tid, gcl_id: sl.id } });
  493. }
  494. }
  495. if (thirdChildLists.length > 0) {
  496. for (const tl of thirdChildLists) {
  497. await transaction.update(this.tableName, { xmj_fbgc: select.name }, { where: { tid, gcl_id: tl.id } });
  498. }
  499. }
  500. if (fourthChildLists.length > 0 && select.level === 2) {
  501. for (const fl of fourthChildLists) {
  502. await transaction.update(this.tableName, { xmj_dwgc: select.name }, { where: { tid, gcl_id: fl.id } });
  503. }
  504. }
  505. }
  506. }
  507. }
  508. }
  509. }
  510. /**
  511. * 修改变更清单(form 变更新增部位页台账节点清单编号升降级)
  512. * Tony Kang
  513. * @param {String} transaction - 队列
  514. * @param {String} tid - 标段id
  515. * @param {Array} datas - 更新列表
  516. * @param {String} column - id所属字段
  517. * @return {void}
  518. */
  519. async updateDataByReviseLedgerUpDownLevel(transaction, tid, datas, column = 'gcl_id') {
  520. if (datas.length > 0) {
  521. console.log(datas);
  522. // const ids = this._.map(datas, 'id');
  523. // const sql = 'SELECT ' + column + ' FROM ?? WHERE `tid` = ? AND ' + column + ' in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ') GROUP BY ' + column;
  524. // const params = [this.tableName, tid];
  525. // const changeAuditLists = await transaction.query(sql, params);
  526. // if (changeAuditLists.length > 0) {
  527. // const updateArr = [];
  528. // const cidList = [];
  529. // for (const ca of changeAuditLists) {
  530. // const d = this._.find(datas, { id: ca[column] });
  531. // console.log(d);
  532. // if (d.id) {
  533. // const changePosNum = await transaction.count(this.ctx.service.changePos.tableName, { lid: d.id });
  534. // const updateCol = {};
  535. // if (column === 'gcl_id' && d.b_code !== undefined) updateCol.code = d.b_code;
  536. // if (column === 'gcl_id' && d.sgfh_qty !== undefined && changePosNum === 0) updateCol.oamount = d.sgfh_qty ? d.sgfh_qty : 0;
  537. // if (column === 'gcl_id' && d.unit_price !== undefined) updateCol.unit_price = d.unit_price ? d.unit_price : 0;
  538. // if (column === 'gcl_id' && d.unit !== undefined) updateCol.unit = d.unit;
  539. // if (column === 'gcl_id' && d.name !== undefined) updateCol.name = d.name;
  540. // if (d.code !== undefined && d.b_code === null) {
  541. // // 清单升级成了项目节,故删除变更已有的此清单,并找出需要重新计算的变更令
  542. // const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' = ? GROUP BY `cid`';
  543. // const params = [this.tableName, tid, d.id];
  544. // const changes = await transaction.query(sql, params);
  545. // for (const c of changes) {
  546. // if (this._.indexOf(cidList, c.cid) === -1) {
  547. // cidList.push(c.cid);
  548. // }
  549. // }
  550. // const delData = {
  551. // tid,
  552. // };
  553. // delData[column] = d.id;
  554. // console.log(delData);
  555. // await transaction.delete(this.tableName, delData);
  556. // } else {
  557. // const options = {
  558. // row: {},
  559. // where: {},
  560. // };
  561. // options.row = updateCol;
  562. // options.where[column] = d.id;
  563. // if (!this._.isEmpty(options.row)) updateArr.push(options);
  564. // if (updateCol.unit !== undefined || updateCol.unit_price !== undefined) {
  565. // const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' = ? GROUP BY `cid`';
  566. // const params = [this.tableName, tid, d.id];
  567. // const changes = await transaction.query(sql, params);
  568. // for (const c of changes) {
  569. // if (this._.indexOf(cidList, c.cid) === -1) {
  570. // cidList.push(c.cid);
  571. // }
  572. // }
  573. // }
  574. // }
  575. // }
  576. // }
  577. // console.log(updateArr, cidList);
  578. // if (updateArr.length > 0) await transaction.updateRows(this.tableName, updateArr);
  579. // if (cidList.length > 0) {
  580. // for (const c of cidList) {
  581. // await this.reCalcTp(transaction, c);
  582. // }
  583. // }
  584. // }
  585. // 针对项目节更新可能对清单影响判断,修正变更清单项目节编号,细目,单位工程,分部分项工程数据
  586. // for (const data of datas) {
  587. // const select = await transaction.get(this.ctx.service.changeLedger.tableName, { id: data.id });
  588. // console.log(select);
  589. // if (select && select.is_leaf === 0) {
  590. // const lists = await this.ctx.service.changeLedger.getDataByFullPath(this.ctx.service.changeLedger.tableName, tid, select.full_path + '%', transaction);
  591. // const childLists = this._.filter(lists, { level: select.level + 1 }); // 细目or项目节编号更新
  592. // if (childLists.length > 0) {
  593. // const d = { xmj_code: '', xmj_jldy: '' };
  594. // if (select.code !== null) {
  595. // d.xmj_code = select.code;
  596. // d.xmj_jldy = select.name;
  597. // } else {
  598. // // 再找出上一个项目节节点并更新
  599. // this.newBills = false;
  600. // const parents = await this.ctx.service.changeLedger.getDataByKid(tid, select.ledger_pid);
  601. // console.log('hello :', parents);
  602. // d.xmj_code = parents.code;
  603. // d.xmj_jldy = parents.name;
  604. // }
  605. // for (const cl of childLists) {
  606. // await transaction.update(this.tableName, { xmj_code: d.xmj_code, xmj_jldy: d.xmj_jldy }, { where: { tid, gcl_id: cl.id } });
  607. // }
  608. // }
  609. // if (select.code !== null && data.name !== undefined) { // 名称修改则可能影响几个数据
  610. // const secondChildLists = this._.filter(lists, { level: select.level + 2 }); // 分项工程更新
  611. // const thirdChildLists = this._.filter(lists, { level: select.level + 3 }); // 分部工程更新
  612. // const fourthChildLists = this._.filter(lists, { level: select.level + 4 }); // 单位工程更新
  613. // if (secondChildLists.length > 0) {
  614. // for (const sl of secondChildLists) {
  615. // await transaction.update(this.tableName, { xmj_fxgc: select.name }, { where: { tid, gcl_id: sl.id } });
  616. // }
  617. // }
  618. // if (thirdChildLists.length > 0) {
  619. // for (const tl of thirdChildLists) {
  620. // await transaction.update(this.tableName, { xmj_fbgc: select.name }, { where: { tid, gcl_id: tl.id } });
  621. // }
  622. // }
  623. // if (fourthChildLists.length > 0) {
  624. // for (const fl of fourthChildLists) {
  625. // await transaction.update(this.tableName, { xmj_dwgc: select.name }, { where: { tid, gcl_id: fl.id } });
  626. // }
  627. // }
  628. // }
  629. // }
  630. // }
  631. }
  632. }
  633. /**
  634. * 修改变更清单(form 变更新增部位页计量单元编辑)
  635. * Tony Kang
  636. * @param {String} transaction - 队列
  637. * @param {String} tid - 标段id
  638. * @param {Array} datas - 更新列表
  639. * @param {String} column - id所属字段
  640. * @return {void}
  641. */
  642. async updateDataByRevisePos(transaction, tid, datas, column = 'mx_id') {
  643. if (datas.length > 0) {
  644. const ids = this._.map(datas, 'id');
  645. const sql = 'SELECT ' + column + ' FROM ?? WHERE `tid` = ? AND ' + column + ' in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ') GROUP BY ' + column;
  646. const params = [this.tableName, tid];
  647. const changeAuditLists = await transaction.query(sql, params);
  648. if (changeAuditLists.length > 0) {
  649. const updateArr = [];
  650. for (const ca of changeAuditLists) {
  651. const d = this._.find(datas, { id: ca[column] });
  652. if (d.id) {
  653. const updateCol = {};
  654. if (column === 'mx_id' && d.name !== undefined) updateCol.bwmx = d.name;
  655. if (column === 'mx_id' && d.quantity !== undefined) updateCol.oamount = d.quantity ? d.quantity : 0;
  656. if (column === 'mx_id' && d.quantity === undefined &&
  657. ((d.sgfh_expr && d.sgfh_expr === '') || (d.sjcl_expr && d.sjcl_expr === '') || (d.qtcl_expr && d.qtcl_expr === ''))) updateCol.oamount = 0;
  658. const options = {
  659. row: {},
  660. where: {},
  661. };
  662. options.row = updateCol;
  663. options.where[column] = d.id;
  664. // if (!this._.isEmpty(updateCol)) await transaction.update(this.tableName, updateCol, options);
  665. if (!this._.isEmpty(options.row)) updateArr.push(options);
  666. }
  667. }
  668. if (updateArr.length > 0) await transaction.updateRows(this.tableName, updateArr);
  669. }
  670. }
  671. }
  672. /**
  673. * 重算变更令总金额(变更新增部位设置时使用)
  674. * @param {String} transaction - 队列
  675. * @param {String} cid - 变更令id
  676. */
  677. async reCalcTp(transaction, cid) {
  678. const change = await transaction.get(this.ctx.service.change.tableName, { cid });
  679. let count = '';
  680. if (change.status === audit.flow.status.uncheck || change.status === audit.flow.status.back || change.status === audit.flow.status.revise) {
  681. count = '`camount`';
  682. } else if (change.status === audit.flow.status.checking || change.status === audit.flow.status.backnew) {
  683. count = '`spamount`';
  684. }
  685. if (count) {
  686. const sql = 'SELECT `unit_price`, ' + count + ' as `count` FROM ?? WHERE `cid` = ?';
  687. const params = [this.tableName, change.cid];
  688. const caLists = await transaction.query(sql, params);
  689. let tp = 0;
  690. const tpUnit = change.tp_decimal ? change.tp_decimal : this.ctx.tender.info.decimal.tp;
  691. for (const ca of caLists) {
  692. const catp = this.ctx.helper.round(this.ctx.helper.mul(ca.unit_price, ca.count), tpUnit);
  693. tp = this.ctx.helper.add(tp, catp);
  694. }
  695. console.log(tp);
  696. if (tp !== change.total_price) {
  697. const options = {
  698. where: {
  699. cid: change.cid,
  700. },
  701. };
  702. const change_update = {
  703. total_price: tp,
  704. };
  705. await transaction.update(this.ctx.service.change.tableName, change_update, options);
  706. }
  707. }
  708. }
  709. async updateToLedger(transaction, tid, cid) {
  710. // 找出本条变更属于新增部位的数据
  711. const allList = await transaction.select(this.tableName, { where: { tid, cid } });
  712. const result = [];
  713. const result2 = [];
  714. for (const l of allList) {
  715. const changeLedgerInfo = await transaction.get(this.ctx.service.changeLedger.tableName, { id: l.gcl_id });
  716. if (changeLedgerInfo && this._.findIndex(result, { id: l.gcl_id }) === -1) {
  717. result.push(changeLedgerInfo);
  718. }
  719. const changePosInfo = await transaction.get(this.ctx.service.changePos.tableName, { id: l.mx_id });
  720. if (changePosInfo) {
  721. result2.push(changePosInfo);
  722. }
  723. }
  724. // 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';
  725. // const sqlParam = [this.ctx.service.changeLedger.tableName, this.tableName, tid, cid];
  726. // const result = await transaction.query(sql, sqlParam);
  727. // const sql2 = 'SELECT a.* FROM ?? a LEFT JOIN ?? b ON a.id = b.mx_id WHERE b.tid = ? AND b.cid = ?';
  728. // const sqlParam2 = [this.ctx.service.changePos.tableName, this.tableName, tid, cid];
  729. // const result2 = await transaction.query(sql2, sqlParam2);
  730. if (result.length > 0 || result2.length > 0) {
  731. const changeLedgerGclIdList = this._.map(result, 'id');
  732. const changeLedgerIdList = this._.uniq(this._.map(result, 'ledger_pid'));// 父节点集合
  733. const needUpdateLedgerList = [];// 找出需要更新的原台账清单的id
  734. const needUpdateChangeLedgerList = [];// 找出需要更新的新台账清单的id
  735. const tpDecimal = this.ctx.tender.info.decimal.tp;
  736. // 要更新的ledger节点,数量及总数
  737. for (const data of result2) {
  738. if (this._.indexOf(changeLedgerGclIdList, data.lid) === -1) {
  739. const info = this._.find(needUpdateLedgerList, { id: data.lid });
  740. if (info) {
  741. info.sgfh_qty = this.ctx.helper.add(info.sgfh_qty, data.sgfh_qty);
  742. info.sjcl_qty = this.ctx.helper.add(info.sjcl_qty, data.sjcl_qty);
  743. info.qtcl_qty = this.ctx.helper.add(info.qtcl_qty, data.qtcl_qty);
  744. info.quantity = this.ctx.helper.add(info.quantity, data.quantity);
  745. } else {
  746. needUpdateLedgerList.push({ id: data.lid, sgfh_qty: data.sgfh_qty, sjcl_qty: data.sjcl_qty, qtcl_qty: data.qtcl_qty, quantity: data.quantity });
  747. }
  748. } else {
  749. const info = this._.find(needUpdateChangeLedgerList, { id: data.lid });
  750. if (info) {
  751. info.sgfh_qty = this.ctx.helper.add(info.sgfh_qty, data.sgfh_qty);
  752. info.sjcl_qty = this.ctx.helper.add(info.sjcl_qty, data.sjcl_qty);
  753. info.qtcl_qty = this.ctx.helper.add(info.qtcl_qty, data.qtcl_qty);
  754. info.quantity = this.ctx.helper.add(info.quantity, data.quantity);
  755. } else {
  756. needUpdateChangeLedgerList.push({ id: data.lid, sgfh_qty: data.sgfh_qty, sjcl_qty: data.sjcl_qty, qtcl_qty: data.qtcl_qty, quantity: data.quantity });
  757. }
  758. }
  759. }
  760. // 更新到result上
  761. if (needUpdateChangeLedgerList.length > 0) {
  762. for (const nucl of needUpdateChangeLedgerList) {
  763. const now = this._.find(result, { id: nucl.id });
  764. now.sgfh_qty = nucl.sgfh_qty;
  765. now.sjcl_qty = nucl.sjcl_qty;
  766. now.qtcl_qty = nucl.qtcl_qty;
  767. now.quantity = nucl.quantity;
  768. now.sgfh_tp = this.ctx.helper.mul(now.sgfh_qty, now.unit_price, tpDecimal);
  769. now.sjcl_tp = this.ctx.helper.mul(now.sjcl_qty, now.unit_price, tpDecimal);
  770. now.qtcl_tp = this.ctx.helper.mul(now.qtcl_qty, now.unit_price, tpDecimal);
  771. now.total_price = this.ctx.helper.mul(now.quantity, now.unit_price, tpDecimal);
  772. }
  773. }
  774. // 更新到ledger上
  775. if (needUpdateLedgerList.length > 0) {
  776. for (const nul of needUpdateLedgerList) {
  777. const ledgerInfo = await this.ctx.service.ledger.getDataById(nul.id);
  778. ledgerInfo.sgfh_qty = this.ctx.helper.add(ledgerInfo.sgfh_qty, nul.sgfh_qty);
  779. ledgerInfo.sjcl_qty = this.ctx.helper.add(ledgerInfo.sjcl_qty, nul.sjcl_qty);
  780. ledgerInfo.qtcl_qty = this.ctx.helper.add(ledgerInfo.qtcl_qty, nul.qtcl_qty);
  781. ledgerInfo.quantity = this.ctx.helper.add(ledgerInfo.quantity, nul.quantity);
  782. ledgerInfo.sgfh_tp = this.ctx.helper.mul(ledgerInfo.sgfh_qty, ledgerInfo.unit_price, tpDecimal);
  783. ledgerInfo.sjcl_tp = this.ctx.helper.mul(ledgerInfo.sjcl_qty, ledgerInfo.unit_price, tpDecimal);
  784. ledgerInfo.qtcl_tp = this.ctx.helper.mul(ledgerInfo.qtcl_qty, ledgerInfo.unit_price, tpDecimal);
  785. ledgerInfo.total_price = this.ctx.helper.mul(ledgerInfo.quantity, ledgerInfo.unit_price, tpDecimal);
  786. await transaction.update(this.ctx.service.ledger.tableName, ledgerInfo);
  787. }
  788. }
  789. // 找出所有新增的父节点并插入到result中
  790. for (const r of changeLedgerIdList) {
  791. await this._findParents(transaction, tid, r, result);
  792. }
  793. // 插入到计量单元表,并删除变更的计量单元数据, 插入清单表,并删除变更的清单表
  794. await this._insertByChangeRevise(transaction, tid, cid, result, result2);
  795. // 更新标段总金额
  796. const sumSql = 'SELECT Sum(total_price) As total_price, Sum(deal_tp) As deal_tp' +
  797. ' FROM ' + this.ctx.service.ledger.tableName + this.ctx.helper.whereSql({ tender_id: tid });
  798. const sum = await transaction.queryOne(sumSql);
  799. await transaction.update(this.ctx.service.tender.tableName, {
  800. id: tid,
  801. total_price: sum.total_price,
  802. deal_tp: sum.deal_tp,
  803. });
  804. }
  805. }
  806. async _findParents(transaction, tid, id, result) {
  807. const info = await transaction.get(this.ctx.service.changeLedger.tableName, { tender_id: tid, ledger_id: id });
  808. if (info && this._.findIndex(result, { ledger_id: info.ledger_id }) === -1) {
  809. result.push(info);
  810. await this._findParents(transaction, tid, info.ledger_pid, result);
  811. } else {
  812. return;
  813. }
  814. }
  815. async _insertByChangeRevise(transaction, tid, cid, ledgerList, posList) {
  816. if (ledgerList.length > 0) {
  817. const insertLedgerArr = [];
  818. for (const l of ledgerList) {
  819. const insertL = [
  820. l.id, l.code, l.b_code, l.name, l.unit, l.source, l.remark, l.ledger_id,
  821. l.ledger_pid, l.level, l.order, l.full_path, l.is_leaf, l.quantity, l.total_price,
  822. l.unit_price, l.drawing_code, l.memo, l.dgn_qty1, l.dgn_qty2, l.deal_qty, l.deal_tp,
  823. l.sgfh_qty, l.sgfh_tp, l.sjcl_qty, l.sjcl_tp, l.qtcl_qty, l.qtcl_tp, l.node_type, l.crid,
  824. l.tender_id, l.is_tp, l.sgfh_expr, l.sjcl_expr, l.qtcl_expr, l.check_calc, l.gxby_status,
  825. l.dagl_status, l.dagl_url, l.gxby_limit, l.dagl_limit, l.ex_memo1, l.ex_memo2, l.ex_memo3,
  826. ];
  827. insertLedgerArr.push('(' + this.ctx.helper.getInArrStrSqlFilter(insertL) + ')');
  828. await transaction.delete(this.ctx.service.changeLedger.tableName, { id: l.id });
  829. // 日志添加
  830. await transaction.insert(this.ctx.service.changeReviseLog.tableName, { tid, cid, lid: l.id, name: l.name, create_time: new Date() });
  831. }
  832. const bSql = 'Insert Into ' +
  833. this.ctx.service.ledger.tableName +
  834. ' (id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
  835. ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
  836. ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id, is_tp,' +
  837. ' sgfh_expr, sjcl_expr, qtcl_expr, check_calc,' +
  838. ' gxby_status, dagl_status, dagl_url, gxby_limit, dagl_limit,' +
  839. ' ex_memo1, ex_memo2, ex_memo3) VALUES ' + insertLedgerArr.join(',') + ';';
  840. await transaction.query(bSql, []);
  841. }
  842. if (posList.length > 0) {
  843. const insertPosArr = [];
  844. for (const p of posList) {
  845. const insertp = [
  846. p.id, p.tid, p.lid, p.name, p.drawing_code, p.quantity, p.add_stage, p.add_stage_order, p.add_times,
  847. p.add_user, p.sgfh_qty, p.sjcl_qty, p.qtcl_qty, p.crid, p.porder, p.position,
  848. p.sgfh_expr, p.sjcl_expr, p.qtcl_expr, p.real_qty,
  849. p.gxby_status, p.dagl_status, p.dagl_url, p.gxby_limit, p.dagl_limit,
  850. p.ex_memo1, p.ex_memo2, p.ex_memo3,
  851. ];
  852. insertPosArr.push('(' + this.ctx.helper.getInArrStrSqlFilter(insertp) + ')');
  853. await transaction.delete(this.ctx.service.changePos.tableName, { id: p.id });
  854. // 日志添加
  855. await transaction.insert(this.ctx.service.changeReviseLog.tableName, { tid, cid, pid: p.id, name: p.name, create_time: new Date() });
  856. }
  857. const pSql =
  858. 'Insert Into ' +
  859. this.ctx.service.pos.tableName +
  860. ' (id, tid, lid, name, drawing_code, quantity, add_stage, add_stage_order, add_times, add_user,' +
  861. ' sgfh_qty, sjcl_qty, qtcl_qty, crid, porder, position, ' +
  862. ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty,' +
  863. ' gxby_status, dagl_status, dagl_url, gxby_limit, dagl_limit,' +
  864. ' ex_memo1, ex_memo2, ex_memo3) VALUES ' + insertPosArr.join(',') + ';';
  865. await transaction.query(pSql, []);
  866. }
  867. }
  868. }
  869. return ChangeAuditList;
  870. };