change_audit_list.js 59 KB

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