change_audit_list.js 59 KB

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