change_audit_list.js 56 KB

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