change_audit_list.js 60 KB

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