change_audit_list.js 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. let valuation_tp = 0;
  398. let unvaluation_tp = 0;
  399. const tp_decimal = this.ctx.change.tp_decimal ? this.ctx.change.tp_decimal : this.ctx.tender.info.decimal.tp;
  400. for (const cl of changeList) {
  401. const price = this.ctx.helper.mul(cl.unit_price, cl.spamount, tp_decimal);
  402. total_price = this.ctx.helper.accAdd(total_price, price);
  403. if (price >= 0) {
  404. positive_tp = this.ctx.helper.accAdd(positive_tp, price);
  405. } else {
  406. negative_tp = this.ctx.helper.accAdd(negative_tp, price);
  407. }
  408. if (cl.is_valuation) {
  409. valuation_tp = this.ctx.helper.accAdd(valuation_tp, price);
  410. } else {
  411. unvaluation_tp = this.ctx.helper.accAdd(unvaluation_tp, price);
  412. }
  413. }
  414. const updateData = {
  415. total_price,
  416. positive_tp,
  417. negative_tp,
  418. valuation_tp,
  419. unvaluation_tp,
  420. };
  421. if (updateTpDecimal) {
  422. updateData.tp_decimal = tp_decimal;
  423. }
  424. const options = {
  425. where: {
  426. cid: this.ctx.change.cid,
  427. },
  428. };
  429. await transaction.update(this.ctx.service.change.tableName, updateData, options);
  430. }
  431. /**
  432. * 用户数据数量提交
  433. * @param {Object} data 内容
  434. * @return {void}
  435. */
  436. async saveAmountData(data) {
  437. if (!this.ctx.tender || !this.ctx.change) {
  438. throw '数据错误';
  439. }
  440. // 判断是否可修改
  441. // 判断t_type是否为费用
  442. const transaction = await this.db.beginTransaction();
  443. try {
  444. await transaction.update(this.tableName, data);
  445. await this.calcCamountSum(transaction);
  446. await transaction.commit();
  447. return true;
  448. } catch (err) {
  449. await transaction.rollback();
  450. throw err;
  451. }
  452. }
  453. async gatherBgBills(tid) {
  454. const sql = 'SELECT cb.code, cb.name, cb.unit, cb.unit_price, Round(Sum(cb.samount + 0), 6) as quantity' +
  455. ' FROM ' + this.tableName + ' cb' +
  456. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON cb.cid = c.cid' +
  457. ' WHERE cb.tid = ? and c.status = ?' +
  458. ' GROUP BY code, name, unit, unit_price';
  459. const param = [tid, audit.flow.status.checked];
  460. const result = await this.db.query(sql, param);
  461. for (const b of result) {
  462. b.total_price = this.ctx.helper.mul(b.unit_price, b.quantity, this.ctx.tender.info.decimal.tp);
  463. }
  464. return result;
  465. }
  466. /**
  467. * 报表用
  468. * Tony Kang
  469. * @param {tid} tid - 标段id
  470. * @return {void}
  471. */
  472. async getChangeAuditBills(tid, onlyChecked) {
  473. const sql = 'SELECT cb.*' +
  474. ' FROM ' + this.tableName + ' cb' +
  475. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON cb.cid = c.cid' +
  476. ' WHERE c.tid = ? ' + (onlyChecked ? 'and c.status = 3' : '') +
  477. ' ORDER BY cb.cid, cb.code';
  478. const param = [tid];
  479. const result = await this.db.query(sql, param);
  480. return result;
  481. }
  482. /**
  483. * 删除变更清单(form 变更新增部位页)
  484. * Tony Kang
  485. * @param {String} transaction - 队列
  486. * @param {String} tid - 标段id
  487. * @param {Array} ids - id列表
  488. * @param {String} column - id所属字段
  489. * @param {String} mx_id - mx_id为空列删除
  490. * @return {void}
  491. */
  492. async deleteDataByRevise(transaction, tid, ids, column = 'gcl_id', mx_id = 'hello') {
  493. if (ids.length > 0) {
  494. const addSql = mx_id === '' ? ' AND (`mx_id` is NULL OR `mx_id` = "")' : '';
  495. const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ')' + addSql + ' GROUP BY `cid`';
  496. const params = [this.tableName, tid];
  497. const changes = await transaction.query(sql, params);
  498. if (changes.length > 0) {
  499. const delData = {
  500. tid,
  501. };
  502. delData[column] = ids;
  503. await transaction.delete(this.tableName, delData);
  504. for (const c of changes) {
  505. // 重算选了此清单的变更令已变更金额
  506. await this.reCalcTp(transaction, c.cid);
  507. }
  508. }
  509. }
  510. }
  511. /**
  512. * 修改变更清单(form 变更新增部位页台账子节点清单编号编辑)
  513. * Tony Kang
  514. * @param {String} transaction - 队列
  515. * @param {String} tid - 标段id
  516. * @param {Array} datas - 更新列表
  517. * @param {String} column - id所属字段
  518. * @return {void}
  519. */
  520. async updateDataByReviseLedger(transaction, tid, datas, column = 'gcl_id') {
  521. if (datas.length > 0) {
  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. if (d.id) {
  532. const changePosNum = await transaction.count(this.ctx.service.changePos.tableName, { lid: d.id });
  533. const updateCol = {};
  534. if (column === 'gcl_id' && d.b_code) updateCol.code = d.b_code;
  535. if (column === 'gcl_id' && d.quantity !== undefined && changePosNum === 0) updateCol.oamount = d.quantity ? d.quantity : 0;
  536. if (column === 'gcl_id' && d.unit_price !== undefined) updateCol.unit_price = d.unit_price ? d.unit_price : 0;
  537. if (column === 'gcl_id' && d.unit !== undefined) updateCol.unit = d.unit;
  538. if (column === 'gcl_id' && d.name !== undefined) updateCol.name = d.name;
  539. if (d.b_code !== undefined && d.b_code === null) {
  540. // 清单升级成了项目节,故删除变更已有的此清单,并找出需要重新计算的变更令
  541. const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' = ? GROUP BY `cid`';
  542. const params = [this.tableName, tid, d.id];
  543. const changes = await transaction.query(sql, params);
  544. for (const c of changes) {
  545. if (this._.indexOf(cidList, c.cid) === -1) {
  546. cidList.push(c.cid);
  547. }
  548. }
  549. const delData = {
  550. tid,
  551. };
  552. delData[column] = d.id;
  553. await transaction.delete(this.tableName, delData);
  554. } else {
  555. const options = {
  556. row: {},
  557. where: {},
  558. };
  559. options.row = updateCol;
  560. options.where[column] = d.id;
  561. if (!this._.isEmpty(options.row)) updateArr.push(options);
  562. if (updateCol.unit !== undefined || updateCol.unit_price !== undefined) {
  563. const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' = ? GROUP BY `cid`';
  564. const params = [this.tableName, tid, d.id];
  565. const changes = await transaction.query(sql, params);
  566. for (const c of changes) {
  567. if (this._.indexOf(cidList, c.cid) === -1) {
  568. cidList.push(c.cid);
  569. }
  570. }
  571. }
  572. }
  573. }
  574. }
  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. if (select && select.is_leaf === 0) {
  586. const lists = await this.ctx.service.changeLedger.getDataByFullPath(this.ctx.service.changeLedger.tableName, tid, select.full_path + '%', transaction);
  587. const childLists = this._.filter(lists, { level: select.level + 1 }); // 细目or项目节编号更新
  588. if (childLists.length > 0) {
  589. const d = { xmj_code: '', xmj_jldy: '' };
  590. if (select.code !== null) {
  591. d.xmj_code = select.code;
  592. d.xmj_jldy = select.name;
  593. } else {
  594. // 再找出上一个项目节节点并更新
  595. this.newBills = false;
  596. const parents = await this.ctx.service.changeLedger.getDataByKid(tid, select.ledger_pid);
  597. d.xmj_code = parents.code;
  598. d.xmj_jldy = parents.name;
  599. }
  600. for (const cl of childLists) {
  601. await transaction.update(this.tableName, { xmj_code: d.xmj_code, xmj_jldy: d.xmj_jldy }, { where: { tid, gcl_id: cl.id } });
  602. }
  603. }
  604. if (select.code !== null && data.name !== undefined) { // 名称修改则可能影响几个数据
  605. const secondChildLists = this._.filter(lists, { level: select.level + 2 }); // 分项工程更新
  606. const thirdChildLists = this._.filter(lists, { level: select.level + 3 }); // 分部工程更新
  607. const fourthChildLists = this._.filter(lists, { level: select.level + 4 }); // 单位工程更新
  608. if (secondChildLists.length > 0) {
  609. for (const sl of secondChildLists) {
  610. await transaction.update(this.tableName, { xmj_fxgc: select.name }, { where: { tid, gcl_id: sl.id } });
  611. }
  612. }
  613. if (thirdChildLists.length > 0) {
  614. for (const tl of thirdChildLists) {
  615. await transaction.update(this.tableName, { xmj_fbgc: select.name }, { where: { tid, gcl_id: tl.id } });
  616. }
  617. }
  618. if (fourthChildLists.length > 0 && select.level === 2) {
  619. for (const fl of fourthChildLists) {
  620. await transaction.update(this.tableName, { xmj_dwgc: select.name }, { where: { tid, gcl_id: fl.id } });
  621. }
  622. }
  623. }
  624. }
  625. }
  626. }
  627. }
  628. /**
  629. * 修改变更清单(form 变更新增部位页台账节点清单编号升降级)
  630. * Tony Kang
  631. * @param {String} transaction - 队列
  632. * @param {String} tid - 标段id
  633. * @param {Array} datas - 更新列表
  634. * @param {String} column - id所属字段
  635. * @return {void}
  636. */
  637. async updateDataByReviseLedgerUpDownLevel(transaction, tid, datas, column = 'gcl_id') {
  638. if (datas.length > 0) {
  639. console.log(datas);
  640. // const ids = this._.map(datas, 'id');
  641. // const sql = 'SELECT ' + column + ' FROM ?? WHERE `tid` = ? AND ' + column + ' in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ') GROUP BY ' + column;
  642. // const params = [this.tableName, tid];
  643. // const changeAuditLists = await transaction.query(sql, params);
  644. // if (changeAuditLists.length > 0) {
  645. // const updateArr = [];
  646. // const cidList = [];
  647. // for (const ca of changeAuditLists) {
  648. // const d = this._.find(datas, { id: ca[column] });
  649. // console.log(d);
  650. // if (d.id) {
  651. // const changePosNum = await transaction.count(this.ctx.service.changePos.tableName, { lid: d.id });
  652. // const updateCol = {};
  653. // if (column === 'gcl_id' && d.b_code !== undefined) updateCol.code = d.b_code;
  654. // if (column === 'gcl_id' && d.sgfh_qty !== undefined && changePosNum === 0) updateCol.oamount = d.sgfh_qty ? d.sgfh_qty : 0;
  655. // if (column === 'gcl_id' && d.unit_price !== undefined) updateCol.unit_price = d.unit_price ? d.unit_price : 0;
  656. // if (column === 'gcl_id' && d.unit !== undefined) updateCol.unit = d.unit;
  657. // if (column === 'gcl_id' && d.name !== undefined) updateCol.name = d.name;
  658. // if (d.code !== undefined && d.b_code === null) {
  659. // // 清单升级成了项目节,故删除变更已有的此清单,并找出需要重新计算的变更令
  660. // const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' = ? GROUP BY `cid`';
  661. // const params = [this.tableName, tid, d.id];
  662. // const changes = await transaction.query(sql, params);
  663. // for (const c of changes) {
  664. // if (this._.indexOf(cidList, c.cid) === -1) {
  665. // cidList.push(c.cid);
  666. // }
  667. // }
  668. // const delData = {
  669. // tid,
  670. // };
  671. // delData[column] = d.id;
  672. // console.log(delData);
  673. // await transaction.delete(this.tableName, delData);
  674. // } else {
  675. // const options = {
  676. // row: {},
  677. // where: {},
  678. // };
  679. // options.row = updateCol;
  680. // options.where[column] = d.id;
  681. // if (!this._.isEmpty(options.row)) updateArr.push(options);
  682. // if (updateCol.unit !== undefined || updateCol.unit_price !== undefined) {
  683. // const sql = 'SELECT `cid` FROM ?? WHERE `tid` = ? AND ' + column + ' = ? GROUP BY `cid`';
  684. // const params = [this.tableName, tid, d.id];
  685. // const changes = await transaction.query(sql, params);
  686. // for (const c of changes) {
  687. // if (this._.indexOf(cidList, c.cid) === -1) {
  688. // cidList.push(c.cid);
  689. // }
  690. // }
  691. // }
  692. // }
  693. // }
  694. // }
  695. // console.log(updateArr, cidList);
  696. // if (updateArr.length > 0) await transaction.updateRows(this.tableName, updateArr);
  697. // if (cidList.length > 0) {
  698. // for (const c of cidList) {
  699. // await this.reCalcTp(transaction, c);
  700. // }
  701. // }
  702. // }
  703. // 针对项目节更新可能对清单影响判断,修正变更清单项目节编号,细目,单位工程,分部分项工程数据
  704. // for (const data of datas) {
  705. // const select = await transaction.get(this.ctx.service.changeLedger.tableName, { id: data.id });
  706. // console.log(select);
  707. // if (select && select.is_leaf === 0) {
  708. // const lists = await this.ctx.service.changeLedger.getDataByFullPath(this.ctx.service.changeLedger.tableName, tid, select.full_path + '%', transaction);
  709. // const childLists = this._.filter(lists, { level: select.level + 1 }); // 细目or项目节编号更新
  710. // if (childLists.length > 0) {
  711. // const d = { xmj_code: '', xmj_jldy: '' };
  712. // if (select.code !== null) {
  713. // d.xmj_code = select.code;
  714. // d.xmj_jldy = select.name;
  715. // } else {
  716. // // 再找出上一个项目节节点并更新
  717. // this.newBills = false;
  718. // const parents = await this.ctx.service.changeLedger.getDataByKid(tid, select.ledger_pid);
  719. // console.log('hello :', parents);
  720. // d.xmj_code = parents.code;
  721. // d.xmj_jldy = parents.name;
  722. // }
  723. // for (const cl of childLists) {
  724. // await transaction.update(this.tableName, { xmj_code: d.xmj_code, xmj_jldy: d.xmj_jldy }, { where: { tid, gcl_id: cl.id } });
  725. // }
  726. // }
  727. // if (select.code !== null && data.name !== undefined) { // 名称修改则可能影响几个数据
  728. // const secondChildLists = this._.filter(lists, { level: select.level + 2 }); // 分项工程更新
  729. // const thirdChildLists = this._.filter(lists, { level: select.level + 3 }); // 分部工程更新
  730. // const fourthChildLists = this._.filter(lists, { level: select.level + 4 }); // 单位工程更新
  731. // if (secondChildLists.length > 0) {
  732. // for (const sl of secondChildLists) {
  733. // await transaction.update(this.tableName, { xmj_fxgc: select.name }, { where: { tid, gcl_id: sl.id } });
  734. // }
  735. // }
  736. // if (thirdChildLists.length > 0) {
  737. // for (const tl of thirdChildLists) {
  738. // await transaction.update(this.tableName, { xmj_fbgc: select.name }, { where: { tid, gcl_id: tl.id } });
  739. // }
  740. // }
  741. // if (fourthChildLists.length > 0) {
  742. // for (const fl of fourthChildLists) {
  743. // await transaction.update(this.tableName, { xmj_dwgc: select.name }, { where: { tid, gcl_id: fl.id } });
  744. // }
  745. // }
  746. // }
  747. // }
  748. // }
  749. }
  750. }
  751. /**
  752. * 修改变更清单(form 变更新增部位页计量单元编辑)
  753. * Tony Kang
  754. * @param {String} transaction - 队列
  755. * @param {String} tid - 标段id
  756. * @param {Array} datas - 更新列表
  757. * @param {String} column - id所属字段
  758. * @return {void}
  759. */
  760. async updateDataByRevisePos(transaction, tid, datas, column = 'mx_id') {
  761. if (datas.length > 0) {
  762. const ids = this._.map(datas, 'id');
  763. const sql = 'SELECT ' + column + ' FROM ?? WHERE `tid` = ? AND ' + column + ' in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ') GROUP BY ' + column;
  764. const params = [this.tableName, tid];
  765. const changeAuditLists = await transaction.query(sql, params);
  766. if (changeAuditLists.length > 0) {
  767. const updateArr = [];
  768. for (const ca of changeAuditLists) {
  769. const d = this._.find(datas, { id: ca[column] });
  770. if (d.id) {
  771. const updateCol = {};
  772. if (column === 'mx_id' && d.name !== undefined) updateCol.bwmx = d.name;
  773. if (column === 'mx_id' && d.quantity !== undefined) updateCol.oamount = d.quantity ? d.quantity : 0;
  774. if (column === 'mx_id' && d.quantity === undefined &&
  775. ((d.sgfh_expr && d.sgfh_expr === '') || (d.sjcl_expr && d.sjcl_expr === '') || (d.qtcl_expr && d.qtcl_expr === ''))) updateCol.oamount = 0;
  776. const options = {
  777. row: {},
  778. where: {},
  779. };
  780. options.row = updateCol;
  781. options.where[column] = d.id;
  782. // if (!this._.isEmpty(updateCol)) await transaction.update(this.tableName, updateCol, options);
  783. if (!this._.isEmpty(options.row)) updateArr.push(options);
  784. }
  785. }
  786. if (updateArr.length > 0) await transaction.updateRows(this.tableName, updateArr);
  787. }
  788. }
  789. }
  790. /**
  791. * 重算变更令总金额(变更新增部位设置时使用)
  792. * @param {String} transaction - 队列
  793. * @param {String} cid - 变更令id
  794. */
  795. async reCalcTp(transaction, cid) {
  796. const change = await transaction.get(this.ctx.service.change.tableName, { cid });
  797. let count = '';
  798. if (change.status === audit.flow.status.uncheck || change.status === audit.flow.status.back || change.status === audit.flow.status.revise) {
  799. count = '`camount`';
  800. } else if (change.status === audit.flow.status.checking || change.status === audit.flow.status.backnew) {
  801. count = '`spamount`';
  802. }
  803. if (count) {
  804. const sql = 'SELECT `unit_price`, ' + count + ' as `count` FROM ?? WHERE `cid` = ?';
  805. const params = [this.tableName, change.cid];
  806. const caLists = await transaction.query(sql, params);
  807. let tp = 0;
  808. const tpUnit = change.tp_decimal ? change.tp_decimal : this.ctx.tender.info.decimal.tp;
  809. for (const ca of caLists) {
  810. const catp = this.ctx.helper.round(this.ctx.helper.mul(ca.unit_price, ca.count), tpUnit);
  811. tp = this.ctx.helper.add(tp, catp);
  812. }
  813. console.log(tp);
  814. if (tp !== change.total_price) {
  815. const options = {
  816. where: {
  817. cid: change.cid,
  818. },
  819. };
  820. const change_update = {
  821. total_price: tp,
  822. };
  823. await transaction.update(this.ctx.service.change.tableName, change_update, options);
  824. }
  825. }
  826. }
  827. async updateToLedger(transaction, tid, cid) {
  828. // 找出本条变更属于新增部位的数据
  829. const allList = await transaction.select(this.tableName, { where: { tid, cid } });
  830. const result = [];
  831. const result2 = [];
  832. for (const l of allList) {
  833. const changeLedgerInfo = await transaction.get(this.ctx.service.changeLedger.tableName, { id: l.gcl_id });
  834. if (changeLedgerInfo && this._.findIndex(result, { id: l.gcl_id }) === -1) {
  835. result.push(changeLedgerInfo);
  836. }
  837. const changePosInfo = await transaction.get(this.ctx.service.changePos.tableName, { id: l.mx_id });
  838. if (changePosInfo) {
  839. result2.push(changePosInfo);
  840. }
  841. }
  842. // 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';
  843. // const sqlParam = [this.ctx.service.changeLedger.tableName, this.tableName, tid, cid];
  844. // const result = await transaction.query(sql, sqlParam);
  845. // const sql2 = 'SELECT a.* FROM ?? a LEFT JOIN ?? b ON a.id = b.mx_id WHERE b.tid = ? AND b.cid = ?';
  846. // const sqlParam2 = [this.ctx.service.changePos.tableName, this.tableName, tid, cid];
  847. // const result2 = await transaction.query(sql2, sqlParam2);
  848. if (result.length > 0 || result2.length > 0) {
  849. const changeLedgerGclIdList = this._.map(result, 'id');
  850. const changeLedgerIdList = this._.uniq(this._.map(result, 'ledger_pid'));// 父节点集合
  851. const needUpdateLedgerList = [];// 找出需要更新的原台账清单的id
  852. const needUpdateChangeLedgerList = [];// 找出需要更新的新台账清单的id
  853. const tpDecimal = this.ctx.tender.info.decimal.tp;
  854. // 要更新的ledger节点,数量及总数
  855. for (const data of result2) {
  856. if (this._.indexOf(changeLedgerGclIdList, data.lid) === -1) {
  857. const info = this._.find(needUpdateLedgerList, { id: data.lid });
  858. if (info) {
  859. info.sgfh_qty = this.ctx.helper.add(info.sgfh_qty, data.sgfh_qty);
  860. info.sjcl_qty = this.ctx.helper.add(info.sjcl_qty, data.sjcl_qty);
  861. info.qtcl_qty = this.ctx.helper.add(info.qtcl_qty, data.qtcl_qty);
  862. info.quantity = this.ctx.helper.add(info.quantity, data.quantity);
  863. } else {
  864. needUpdateLedgerList.push({ id: data.lid, sgfh_qty: data.sgfh_qty, sjcl_qty: data.sjcl_qty, qtcl_qty: data.qtcl_qty, quantity: data.quantity });
  865. }
  866. } else {
  867. const info = this._.find(needUpdateChangeLedgerList, { id: data.lid });
  868. if (info) {
  869. info.sgfh_qty = this.ctx.helper.add(info.sgfh_qty, data.sgfh_qty);
  870. info.sjcl_qty = this.ctx.helper.add(info.sjcl_qty, data.sjcl_qty);
  871. info.qtcl_qty = this.ctx.helper.add(info.qtcl_qty, data.qtcl_qty);
  872. info.quantity = this.ctx.helper.add(info.quantity, data.quantity);
  873. } else {
  874. needUpdateChangeLedgerList.push({ id: data.lid, sgfh_qty: data.sgfh_qty, sjcl_qty: data.sjcl_qty, qtcl_qty: data.qtcl_qty, quantity: data.quantity });
  875. }
  876. }
  877. }
  878. // 更新到result上
  879. if (needUpdateChangeLedgerList.length > 0) {
  880. for (const nucl of needUpdateChangeLedgerList) {
  881. const now = this._.find(result, { id: nucl.id });
  882. now.sgfh_qty = nucl.sgfh_qty;
  883. now.sjcl_qty = nucl.sjcl_qty;
  884. now.qtcl_qty = nucl.qtcl_qty;
  885. now.quantity = nucl.quantity;
  886. now.sgfh_tp = this.ctx.helper.mul(now.sgfh_qty, now.unit_price, tpDecimal);
  887. now.sjcl_tp = this.ctx.helper.mul(now.sjcl_qty, now.unit_price, tpDecimal);
  888. now.qtcl_tp = this.ctx.helper.mul(now.qtcl_qty, now.unit_price, tpDecimal);
  889. now.total_price = this.ctx.helper.mul(now.quantity, now.unit_price, tpDecimal);
  890. }
  891. }
  892. // 更新到ledger上
  893. if (needUpdateLedgerList.length > 0) {
  894. for (const nul of needUpdateLedgerList) {
  895. const ledgerInfo = await this.ctx.service.ledger.getDataById(nul.id);
  896. ledgerInfo.sgfh_qty = this.ctx.helper.add(ledgerInfo.sgfh_qty, nul.sgfh_qty);
  897. ledgerInfo.sjcl_qty = this.ctx.helper.add(ledgerInfo.sjcl_qty, nul.sjcl_qty);
  898. ledgerInfo.qtcl_qty = this.ctx.helper.add(ledgerInfo.qtcl_qty, nul.qtcl_qty);
  899. ledgerInfo.quantity = this.ctx.helper.add(ledgerInfo.quantity, nul.quantity);
  900. ledgerInfo.sgfh_tp = this.ctx.helper.mul(ledgerInfo.sgfh_qty, ledgerInfo.unit_price, tpDecimal);
  901. ledgerInfo.sjcl_tp = this.ctx.helper.mul(ledgerInfo.sjcl_qty, ledgerInfo.unit_price, tpDecimal);
  902. ledgerInfo.qtcl_tp = this.ctx.helper.mul(ledgerInfo.qtcl_qty, ledgerInfo.unit_price, tpDecimal);
  903. ledgerInfo.total_price = this.ctx.helper.mul(ledgerInfo.quantity, ledgerInfo.unit_price, tpDecimal);
  904. await transaction.update(this.ctx.service.ledger.tableName, ledgerInfo);
  905. }
  906. }
  907. // 找出所有新增的父节点并插入到result中
  908. for (const r of changeLedgerIdList) {
  909. await this._findParents(transaction, tid, r, result);
  910. }
  911. // 插入到计量单元表,并删除变更的计量单元数据, 插入清单表,并删除变更的清单表
  912. await this._insertByChangeRevise(transaction, tid, cid, result, result2);
  913. // 更新标段总金额
  914. const sumSql = 'SELECT Sum(total_price) As total_price, Sum(deal_tp) As deal_tp' +
  915. ' FROM ' + this.ctx.service.ledger.tableName + this.ctx.helper.whereSql({ tender_id: tid });
  916. const sum = await transaction.queryOne(sumSql);
  917. await transaction.update(this.ctx.service.tender.tableName, {
  918. id: tid,
  919. total_price: sum.total_price,
  920. deal_tp: sum.deal_tp,
  921. });
  922. // 清除修订及台账的maxLid缓存,防止树结构混乱
  923. await this.ctx.service.reviseBills._removeCacheMaxLid(tid);
  924. await this.ctx.service.ledger._removeCacheMaxLid(tid);
  925. }
  926. }
  927. async _findParents(transaction, tid, id, result) {
  928. const info = await transaction.get(this.ctx.service.changeLedger.tableName, { tender_id: tid, ledger_id: id });
  929. if (info && this._.findIndex(result, { ledger_id: info.ledger_id }) === -1) {
  930. result.push(info);
  931. await this._findParents(transaction, tid, info.ledger_pid, result);
  932. } else {
  933. return;
  934. }
  935. }
  936. async _insertByChangeRevise(transaction, tid, cid, ledgerList, posList) {
  937. if (ledgerList.length > 0) {
  938. const insertLedgerArr = [];
  939. for (const l of ledgerList) {
  940. const insertL = [
  941. l.id, l.code, l.b_code, l.name, l.unit, l.source, l.remark, l.ledger_id,
  942. l.ledger_pid, l.level, l.order, l.full_path, l.is_leaf, l.quantity, l.total_price,
  943. l.unit_price, l.drawing_code, l.memo, l.dgn_qty1, l.dgn_qty2, l.deal_qty, l.deal_tp,
  944. 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,
  945. l.tender_id, l.sgfh_expr, l.sjcl_expr, l.qtcl_expr, l.check_calc,
  946. l.ex_memo1, l.ex_memo2, l.ex_memo3,
  947. ];
  948. insertLedgerArr.push('(' + this.ctx.helper.getInArrStrSqlFilter(insertL) + ')');
  949. await transaction.delete(this.ctx.service.changeLedger.tableName, { id: l.id });
  950. // 日志添加
  951. 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() });
  952. }
  953. const bSql = 'Insert Into ' +
  954. this.ctx.service.ledger.tableName +
  955. ' (id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
  956. ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
  957. ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, ccid, tender_id,' +
  958. ' sgfh_expr, sjcl_expr, qtcl_expr, check_calc,' +
  959. ' ex_memo1, ex_memo2, ex_memo3) VALUES ' + insertLedgerArr.join(',') + ';';
  960. await transaction.query(bSql, []);
  961. }
  962. if (posList.length > 0) {
  963. const insertPosArr = [];
  964. for (const p of posList) {
  965. const insertp = [
  966. p.id, p.tid, p.lid, p.name, p.drawing_code, p.quantity, p.add_stage, p.add_stage_order, p.add_times,
  967. p.add_user, p.sgfh_qty, p.sjcl_qty, p.qtcl_qty, p.crid, p.ccid, p.porder, p.position,
  968. p.sgfh_expr, p.sjcl_expr, p.qtcl_expr, p.real_qty,
  969. p.ex_memo1, p.ex_memo2, p.ex_memo3,
  970. ];
  971. insertPosArr.push('(' + this.ctx.helper.getInArrStrSqlFilter(insertp) + ')');
  972. await transaction.delete(this.ctx.service.changePos.tableName, { id: p.id });
  973. // 日志添加
  974. await transaction.insert(this.ctx.service.changeReviseLog.tableName, { tid, cid, pid: p.id, name: p.name, create_time: new Date() });
  975. }
  976. const pSql =
  977. 'Insert Into ' +
  978. this.ctx.service.pos.tableName +
  979. ' (id, tid, lid, name, drawing_code, quantity, add_stage, add_stage_order, add_times, add_user,' +
  980. ' sgfh_qty, sjcl_qty, qtcl_qty, crid, ccid, porder, position, ' +
  981. ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty,' +
  982. ' ex_memo1, ex_memo2, ex_memo3) VALUES ' + insertPosArr.join(',') + ';';
  983. await transaction.query(pSql, []);
  984. }
  985. }
  986. async checkedChangeBills(tid) {
  987. const DefaultDecimal = this.ctx.tender.info.decimal.tp;
  988. 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 = ?';
  989. const changeBills = await this.db.query(sql, [tid, audit.flow.status.checked]);
  990. changeBills.forEach(x => {
  991. x.tp_decimal = x.tp_decimal !== 0 ? x.tp_decimal : DefaultDecimal
  992. });
  993. return changeBills;
  994. }
  995. /**
  996. * 交换两个清单的顺序
  997. * @param {Number} id1 - 工料1的id
  998. * @param {Number} id2 - 工料2的id
  999. * @returns {Promise<void>}
  1000. */
  1001. async changeOrder(datas) {
  1002. if (!this.ctx.tender || !this.ctx.change) {
  1003. throw '数据错误';
  1004. }
  1005. // const bill1 = await this.getDataByCondition({ tid: this.ctx.tender.id, id: id1 });
  1006. // const bill2 = await this.getDataByCondition({ tid: this.ctx.tender.id, id: id2 });
  1007. // if (!bill1 || !bill2) {
  1008. // throw '数据错误';
  1009. // }
  1010. const transaction = await this.db.beginTransaction();
  1011. try {
  1012. // const order = bill1.order;
  1013. // bill1.order = bill2.order;
  1014. // bill2.order = order;
  1015. // await transaction.update(this.tableName, { id: bill1.id, order: bill1.order });
  1016. // await transaction.update(this.tableName, { id: bill2.id, order: bill2.order });
  1017. await transaction.updateRows(this.tableName, datas);
  1018. await transaction.commit();
  1019. return true;
  1020. } catch (err) {
  1021. await transaction.rollback();
  1022. throw err;
  1023. }
  1024. }
  1025. async setAllValuation(cid, ids, is_valuation) {
  1026. return await this.db.update(this.tableName, { is_valuation }, {
  1027. where: {
  1028. cid,
  1029. id: ids,
  1030. },
  1031. });
  1032. }
  1033. async getBillsSum(tid) {
  1034. 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' +
  1035. ' FROM(' +
  1036. ' SELECT cal.gcl_id, Sum(cal.checked_amount) AS qc_qty, Sum(cal.checked_price) AS qc_tp, 0 As qc_minus_qty' +
  1037. ` FROM ${this.tableName} cal LEFT JOIN ${this.ctx.service.change.tableName} c ON cal.cid = c.cid` +
  1038. ' WHERE c.tid = ? AND c.valid AND c.status = ? AND cal.is_valuation' +
  1039. ' GROUP BY cal.gcl_id' +
  1040. ' UNION ALL ' +
  1041. ' SELECT cal.gcl_id, 0 As qc_qty, 0 As qc_tp, Sum(cal.checked_amount) AS qc_minus_qty' +
  1042. ` FROM ${this.tableName} cal LEFT JOIN ${this.ctx.service.change.tableName} c ON cal.cid = c.cid` +
  1043. ' WHERE c.tid = ? AND c.valid AND c.status = ? AND not cal.is_valuation' +
  1044. ' GROUP BY cal.gcl_id) As TEMP' +
  1045. ' GROUP BY gcl_id';
  1046. return await this.db.query(sql, [tid, audit.flow.status.checked, tid, audit.flow.status.checked]);
  1047. }
  1048. async getPosSum(tid) {
  1049. 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' +
  1050. ' FROM(' +
  1051. ' SELECT cal.mx_id, Sum(cal.checked_amount) AS qc_qty, Sum(cal.checked_price) AS qc_tp, 0 As qc_minus_qty' +
  1052. ` FROM ${this.tableName} cal LEFT JOIN ${this.ctx.service.change.tableName} c ON cal.cid = c.cid` +
  1053. ' WHERE c.tid = ? AND c.valid AND c.status = ? AND cal.is_valuation' +
  1054. ' GROUP BY cal.mx_id' +
  1055. ' UNION ALL ' +
  1056. ' SELECT cal.mx_id, 0 As qc_qty, 0 As qc_tp, Sum(cal.checked_amount) AS qc_minus_qty' +
  1057. ` FROM ${this.tableName} cal LEFT JOIN ${this.ctx.service.change.tableName} c ON cal.cid = c.cid` +
  1058. ' WHERE c.tid = ? AND c.valid AND c.status = ? AND not cal.is_valuation' +
  1059. ' GROUP BY cal.mx_id) As TEMP' +
  1060. ' GROUP BY mx_id';
  1061. return await this.db.query(sql, [tid, audit.flow.status.checked, tid, audit.flow.status.checked]);
  1062. }
  1063. }
  1064. return ChangeAuditList;
  1065. };