payment_safe_bills.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. 'use strict';
  2. /**
  3. *
  4. * 支付审批-安全生产
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const defaultBills = require('../const/payment').defaultSafeBills;
  10. const billsUtils = require('../lib/bills_utils');
  11. const SafeBillsFields = {
  12. textFields: ['b_code', 'name', 'unit', 'spec', 'invoice_code', 'memo'],
  13. calcFields: ['unit_price', 'cur_qty', 'cur_tp', 'end_qty', 'end_tp'],
  14. fixedFields: ['safe_id', 'tender_id', 'pre_qty', 'pre_tp', 'cur_read_qty', 'cur_read_tp', 'cur_his', 'add_user_id', 'add_time'],
  15. treeFields: ['id', 'detail_id', 'safe_id', 'tree_id', 'tree_pid', 'tree_level', 'tree_order', 'tree_full_path', 'tree_is_leaf'],
  16. };
  17. SafeBillsFields.editQueryFields = [...SafeBillsFields.treeFields, ...SafeBillsFields.textFields, ...SafeBillsFields.calcFields, 'pre_qty', 'pre_tp'];
  18. SafeBillsFields.readQueryFields = [...SafeBillsFields.treeFields, ...SafeBillsFields.textFields, 'unit_price', 'pre_qty', 'pre_tp', 'cur_read_qty', 'cur_read_tp'];
  19. SafeBillsFields.compareQueryFields = [...SafeBillsFields.treeFields, ...SafeBillsFields.textFields, 'unit_price', 'cur_his', 'cur_qty', 'cur_tp'];
  20. SafeBillsFields.preQueryFields = [...SafeBillsFields.treeFields, ...SafeBillsFields.textFields, 'tender_id', 'unit_price', 'end_qty', 'end_tp', 'add_user_id', 'add_time', 'update_user_id', 'update_time'];
  21. module.exports = app => {
  22. class PaymentSafeBills extends app.BaseTreeService {
  23. /**
  24. * 构造函数
  25. *
  26. * @param {Object} ctx - egg全局变量
  27. * @return {void}
  28. */
  29. constructor(ctx) {
  30. super(ctx, {
  31. mid: 'detail_id',
  32. kid: 'tree_id',
  33. pid: 'tree_pid',
  34. order: 'tree_order',
  35. level: 'tree_level',
  36. isLeaf: 'tree_is_leaf',
  37. fullPath: 'tree_full_path',
  38. keyPre: 'safe_bills_maxLid:',
  39. uuid: true,
  40. });
  41. // this.depart = 10;
  42. this.tableName = 'payment_safe_bills';
  43. this.decimal = { tp: 2, up: 2, qty: 3 };
  44. }
  45. // 继承方法
  46. clearParentingData(data) {
  47. for (const f of SafeBillsFields.calcFields) {
  48. data[f] = 0;
  49. }
  50. }
  51. _getDefaultData(data, detail) {
  52. data.id = this.uuid.v4();
  53. data.safe_id = data.id;
  54. data.tender_id = detail.tender_id;
  55. data.detail_id = detail.id;
  56. data.add_user_id = this.ctx.session.sessionUser.accountId;
  57. }
  58. async getEditData(detail) {
  59. return await this.getAllDataByCondition({
  60. where: { detail_id: detail.id },
  61. columns: SafeBillsFields.editQueryFields
  62. });
  63. }
  64. async getReadData(detail) {
  65. const helper = this.ctx.helper;
  66. const result = await this.getAllDataByCondition({
  67. where: { detail_id: detail.id },
  68. columns: SafeBillsFields.readQueryFields
  69. });
  70. result.forEach(x => {
  71. x.cur_qty = x.cur_read_qty;
  72. x.cur_tp = x.cur_read_tp;
  73. x.end_qty = helper.add(x.pre_qty, x.cur_qty);
  74. x.end_tp = helper.add(x.pre_tp, x.cur_tp);
  75. });
  76. return result;
  77. }
  78. async getCompareData(detail) {
  79. const result = await this.getAllDataByCondition({
  80. where: { detail_id: detail.id },
  81. columns: SafeBillsFields.compareQueryFields
  82. });
  83. result.forEach(x => {
  84. x.cur_his = x.cur_his ? JSON.parse(x.cur_his) : [];
  85. });
  86. return result;
  87. }
  88. async init(detail, transaction) {
  89. if (!detail || !transaction) throw '安全生产费数据错误';
  90. const insertData = [];
  91. for (const b of defaultBills) {
  92. const bills = JSON.parse(JSON.stringify(b));
  93. this._getDefaultData(bills, detail);
  94. insertData.push(bills);
  95. }
  96. const operate = await transaction.insert(this.tableName, insertData);
  97. return operate.affectedRows === insertData.length;
  98. }
  99. async initByPre(detail, preDetail, transaction) {
  100. if (!detail || !preDetail || !transaction) throw '安全生产费数据错误';
  101. const preBills = await this.getAllDataByCondition({
  102. where: { detail_id: preDetail.id },
  103. columns: SafeBillsFields.preQueryFields
  104. });
  105. const insertData = [];
  106. for (const bills of preBills) {
  107. //const bills = JSON.parse(JSON.stringify(b));
  108. bills.id = this.uuid.v4();
  109. bills.detail_id = detail.id;
  110. delete bills.invoice_code;
  111. bills.pre_qty = bills.end_qty;
  112. bills.pre_tp = bills.end_tp;
  113. insertData.push(bills);
  114. }
  115. const operate = await transaction.insert(this.tableName, insertData);
  116. return operate.affectedRows === insertData.length;
  117. }
  118. /**
  119. * 新增数据(供内部或其他service类调用, controller不可直接使用)
  120. * @param {Array|Object} data - 新增数据
  121. * @param {Number} detailId - 标段id
  122. * @param {Object} transaction - 新增事务
  123. * @return {Promise<boolean>} - {Promise<是否正确新增成功>}
  124. */
  125. async innerAdd(data, detailId, transaction) {
  126. const datas = data instanceof Array ? data : [data];
  127. if (detailId <= 0) {
  128. throw '标段id错误';
  129. }
  130. if (datas.length <= 0) {
  131. throw '插入数据为空';
  132. }
  133. if (!transaction) {
  134. throw '内部错误';
  135. }
  136. // 整理数据
  137. const insertData = [];
  138. for (const tmp of datas) {
  139. tmp[this.setting.id] = tmp.template_id;
  140. tmp[this.setting.pid] = tmp.pid;
  141. tmp[this.setting.mid] = detailId;
  142. delete tmp.template_id;
  143. delete tmp.pid;
  144. tmp.id = this.uuid.v4();
  145. insertData.push(tmp);
  146. }
  147. const operate = await transaction.insert(this.tableName, insertData);
  148. return operate.affectedRows === datas.length;
  149. }
  150. /**
  151. * 新增数据
  152. *
  153. * @param {Object} data - 新增的数据(可批量)
  154. * @param {Number} detailId - 支付审批期id
  155. * @return {Boolean} - 返回新增的结果
  156. */
  157. async add(data, detailId) {
  158. this.transaction = await this.db.beginTransaction();
  159. let result = false;
  160. try {
  161. result = await this.innerAdd(data, detailId, this.transaction);
  162. if (!result) {
  163. throw '新增数据错误';
  164. }
  165. await this.transaction.commit();
  166. } catch (error) {
  167. await this.transaction.rollback();
  168. result = false;
  169. }
  170. return result;
  171. }
  172. /**
  173. * 根据节点Id获取数据
  174. *
  175. * @param {Number} detailId - 标段id
  176. * @param {Number} nodeId - 项目节/工程量清单节点id
  177. * @return {Object} - 返回查询到的节点数据
  178. */
  179. async getDataByNodeId(detailId, nodeId) {
  180. if ((nodeId <= 0) || (detailId <= 0)) {
  181. return undefined;
  182. }
  183. const where = {};
  184. where[this.setting.mid] = detailId;
  185. where[this.setting.id] = nodeId;
  186. const data = await this.db.getDataByCondition(where);
  187. return data;
  188. }
  189. /**
  190. * 根据节点Id获取数据
  191. * @param {Number} detailId - 期Id
  192. * @param {Array} nodesIds - 节点Id
  193. * @return {Array}
  194. */
  195. async getDataByNodeIds(detailId, nodesIds) {
  196. if (detailId <= 0) {
  197. return [];
  198. }
  199. const where = {};
  200. where[this.setting.mid] = detailId;
  201. where[this.setting.id] = nodesIds;
  202. const data = await this.db.getAllDataByCondition({ where });
  203. return this._.sortBy(data, function(d) {
  204. return nodesIds.indexOf(d.ledger_id);
  205. });
  206. }
  207. /**
  208. * 根据主键id获取数据
  209. * @param {Array|Number} id - 主键id
  210. * @return {Promise<*>}
  211. */
  212. async getDataByIds(id) {
  213. if (!id) {
  214. return [];
  215. }
  216. const ids = id instanceof Array ? id : [id];
  217. if (ids.length === 0) {
  218. return [];
  219. }
  220. const data = await this.db.getAllDataByCondition({ where: { id: ids } });
  221. return data;
  222. }
  223. /**
  224. * 根据 父节点id 获取子节点
  225. * @param detailId
  226. * @param nodeId
  227. * @return {Promise<*>}
  228. */
  229. async getChildrenByParentId(detailId, nodeId) {
  230. if (detailId <= 0 || !nodeId) {
  231. return undefined;
  232. }
  233. const nodeIds = nodeId instanceof Array ? nodeId : [nodeId];
  234. if (nodeIds.length === 0) {
  235. return [];
  236. }
  237. const where = {};
  238. where[this.setting.mid] = detailId;
  239. where[this.setting.pid] = nodeIds;
  240. const data = await this.getAllDataByCondition({ where, orders: [[this.setting.order, 'ASC']] });
  241. return data;
  242. }
  243. async pasteBlockData(detailId, targetId, pasteData, defaultData) {
  244. const setting = this.setting;
  245. if ((detailId <= 0) || (sid <= 0)) return [];
  246. if (!pasteData || pasteData.length <= 0) throw '复制数据错误';
  247. for (const pd of pasteData) {
  248. if (!pd || pd.length <= 0) throw '复制数据错误';
  249. pd.sort(function (x, y) {
  250. return x[setting.level] - y[setting.level]
  251. });
  252. if (pd[0][this.setting.pid] !== pasteData[0][0][this.setting.pid]) throw '复制数据错误:仅可操作同层节点';
  253. }
  254. this.newBills = false;
  255. const targetData = await this.getDataByKid(detailId, targetId);
  256. if (!targetData) throw '粘贴数据错误';
  257. const newParentPath = targetData.full_path.replace(targetData.ledger_id, '');
  258. const tpDecimal = this.decimal;
  259. const pasteBillsData = [], leafBillsId = [];
  260. let maxId = await this._getMaxLid(this.ctx.paymentTender.id);
  261. for (const [i, pd] of pasteData.entries()) {
  262. for (const d of pd) {
  263. d.children = pd.filter(function (x) {
  264. return x[setting.pid] === d[setting.id];
  265. });
  266. }
  267. const pbd = [];
  268. for (const [j, d] of pd.entries()) {
  269. const newBills = {
  270. id: this.uuid.v4(),
  271. safe_id: this.uuid.v4(),
  272. b_code: d.b_code,
  273. name: d.name,
  274. unit: d.unit,
  275. unit_price: this.ctx.helper.round(d.unit_price, tpDecimal.up),
  276. memo: d.memo,
  277. };
  278. newBills[setting.mid] = detailId;
  279. newBills[setting.id] = maxId + j + 1;
  280. newBills[setting.pid] = j === 0 ? targetData[setting.pid] : d[setting.pid];
  281. newBills[setting.level] = d[setting.level] + targetData[setting.level] - pd[0][setting.level];
  282. newBills[setting.order] = j === 0 ? targetData[setting.order] + i + 1 : d[setting.order];
  283. newBills[setting.isLeaf] = d[setting.isLeaf];
  284. for (const c of d.children) {
  285. c[setting.pid] = newBills[setting.id];
  286. }
  287. newBills.quantity = this.ctx.helper.round(d.quantity, tpDecimal.qty);
  288. newBills.total_price = this.ctx.helper.mul(newBills.quantity, newBills.unit_price, tpDecimal.tp);
  289. if (defaultData) this.ctx.helper._.assignIn(newBills, defaultData);
  290. pbd.push(newBills);
  291. }
  292. for (const d of pbd) {
  293. const parent = pbd.find(function (x) {
  294. return x[setting.id] === d[setting.pid];
  295. });
  296. d[setting.fullPath] = parent
  297. ? parent[setting.fullPath] + '-' + d[setting.id]
  298. : newParentPath + d[setting.id];
  299. if (defaultData) this.ctx.helper._.assignIn(pbd, defaultData);
  300. pasteBillsData.push(d);
  301. }
  302. maxId = maxId + pbd.length;
  303. }
  304. this.transaction = await this.db.beginTransaction();
  305. try {
  306. // 选中节点的所有后兄弟节点,order+粘贴节点个数
  307. await this._updateChildrenOrder(tid, targetData[setting.pid], targetData[setting.order] + 1, pasteData.length);
  308. // 数据库创建新增节点数据
  309. if (pasteBillsData.length > 0) {
  310. const newData = await this.transaction.insert(this.tableName, pasteBillsData);
  311. }
  312. this._cacheMaxLid(tid, maxId);
  313. await this.transaction.commit();
  314. } catch (err) {
  315. await this.transaction.rollback();
  316. throw err;
  317. }
  318. // 查询应返回的结果
  319. const updateData = await this.getNextsData(targetData[setting.mid], targetData[setting.pid], targetData[setting.order] + pasteData.length);
  320. return { create: pasteBillsData, update: updateData };
  321. }
  322. async addSafeBillsNode(detail, targetId, count) {
  323. if (!detail) return null;
  324. const select = targetId ? await this.getDataByKid(detail.id, targetId) : null;
  325. if (targetId && !select) throw '新增节点数据错误';
  326. this.transaction = await this.db.beginTransaction();
  327. try {
  328. if (select) await this._updateChildrenOrder(detail.id, select[this.setting.pid], select[this.setting.order] + 1, count);
  329. const newDatas = [];
  330. const maxId = await this._getMaxLid(detail.id);
  331. for (let i = 1; i < count + 1; i++) {
  332. const newData = {};
  333. newData[this.setting.kid] = maxId + i;
  334. newData[this.setting.pid] = select ? select[this.setting.pid] : this.rootId;
  335. newData[this.setting.mid] = detail.id;
  336. newData[this.setting.level] = select ? select[this.setting.level] : 1;
  337. newData[this.setting.order] = select ? select[this.setting.order] + i : i;
  338. newData[this.setting.fullPath] = newData[this.setting.level] > 1
  339. ? select[this.setting.fullPath].replace('-' + select[this.setting.kid], '-' + newData[this.setting.kid])
  340. : newData[this.setting.kid] + '';
  341. newData[this.setting.isLeaf] = true;
  342. this._getDefaultData(newData, detail);
  343. newDatas.push(newData);
  344. }
  345. const insertResult = await this.transaction.insert(this.tableName, newDatas);
  346. this._cacheMaxLid(detail.id, maxId + count);
  347. if (insertResult.affectedRows !== count) throw '新增节点数据错误';
  348. await this.transaction.commit();
  349. this.transaction = null;
  350. } catch (err) {
  351. await this.transaction.rollback();
  352. this.transaction = null;
  353. throw err;
  354. }
  355. if (select) {
  356. const createData = await this.getChildBetween(detail.id, select[this.setting.pid], select[this.setting.order], select[this.setting.order] + count + 1);
  357. const updateData = await this.getNextsData(detail.id, select[this.setting.pid], select[this.setting.order] + count);
  358. return {create: createData, update: updateData};
  359. } else {
  360. const createData = await this.getChildBetween(detail.id, -1, 0, count + 1);
  361. return {create: createData};
  362. }
  363. }
  364. async addStdNodeWithParent(detail, targetId, stdData) {
  365. const findPreData = function(list, a) {
  366. if (!list || list.length === 0) { return null; }
  367. for (let i = 0, iLen = list.length; i < iLen; i++) {
  368. if (billsUtils.compareCode(list[i].b_code, a.b_code) > 0) {
  369. return i > 0 ? list[i - 1] : null;
  370. }
  371. }
  372. return list[list.length - 1];
  373. };
  374. let parent = await this.getDataByKid(detail.id, targetId);
  375. if (targetId && !parent) throw '新增节点数据错误,请刷新页面重试';
  376. let children = await this.getChildrenByParentId(detail.id, targetId);
  377. const updateParent = children.length === 0;
  378. const insertData = [];
  379. const maxId = await this._getMaxLid(detail.id);
  380. this.transaction = await this.db.beginTransaction();
  381. try {
  382. if (updateParent) {
  383. const updateData = { id: parent.id };
  384. updateData[this.setting.isLeaf] = false;
  385. this.clearParentingData(updateData);
  386. await this.transaction.update(this.tableName, updateData);
  387. }
  388. // 从最顶层节点依次查询是否存在,否则添加
  389. for (let i = 0, len = stdData.length; i < len; i++) {
  390. const newData = { b_code: stdData[i].b_code, name: stdData[i].name, unit: stdData[i].unit };
  391. newData[this.setting.kid] = maxId + i + 1;
  392. newData[this.setting.pid] = parent ? parent[this.setting.kid] : this.rootId;
  393. newData[this.setting.level] = parent ? parent[this.setting.level] + 1 : 1;
  394. newData[this.setting.fullPath] = parent ? `${parent[this.setting.fullPath]}-${newData[this.setting.kid]}` : `${newData[this.setting.kid]}`;
  395. const pre = findPreData(children, newData);
  396. newData[this.setting.order] = pre ? pre[this.setting.order] + 1 : 1;
  397. if (!pre || children.indexOf(pre) < children.length - 1) {
  398. await this._updateChildrenOrder(detail.id, parent ? parent[this.setting.kid] : this.rootId, pre ? pre[this.setting.order] + 1 : 1);
  399. }
  400. newData[this.setting.isLeaf] = (i === len - 1);
  401. this._getDefaultData(newData, detail);
  402. insertData.push(newData);
  403. parent = newData;
  404. children = [];
  405. }
  406. await this.transaction.insert(this.tableName, insertData);
  407. await this.transaction.commit();
  408. } catch (err) {
  409. await this.transaction.rollback();
  410. throw err;
  411. }
  412. this._cacheMaxLid(detail.id, maxId + stdData.length);
  413. // 查询应返回的结果
  414. const createData = await this.getDataByFullPath(detail.id, insertData[0][this.setting.fullPath] + '%');
  415. const updateData = await this.getNextsData(detail.id, targetId, insertData[0][this.setting.order]);
  416. if (updateParent) {
  417. updateData.push(await this.getDataById(parent.id));
  418. }
  419. return { create: createData, update: updateData };
  420. }
  421. async updateCalc(detail, data) {
  422. const helper = this.ctx.helper;
  423. const decimal = this.decimal;
  424. // 简单验证数据
  425. if (!detail) throw '安全生产费不存在';
  426. if (!data) throw '提交数据错误';
  427. const datas = data instanceof Array ? data : [data];
  428. const ids = datas.map(x => { return x.id; });
  429. const orgData = await this.getAllDataByCondition({ where: { id: ids }});
  430. const updateData = [];
  431. for (const row of datas) {
  432. const oData = orgData.find(x => { return x.id === row.id });
  433. if (!oData || oData.detail_id !== detail.id || oData.tree_id !== row.tree_id) throw '提交数据错误';
  434. let nData = { id: oData.id, tree_id: oData.tree_id, update_user_id: this.ctx.session.sessionUser.accountId };
  435. // 计算相关
  436. if (row.cur_qty !== undefined || row.unit_price !== undefined || row.cur_tp !== undefined) {
  437. nData.unit_price = row.unit_price !== undefined ? helper.round(row.unit_price, decimal.up) : oData.unit_price;
  438. nData.cur_qty = row.cur_qty !== undefined ? helper.round(row.cur_qty, decimal.qty) : oData.cur_qty;
  439. nData.cur_tp = row.cur_tp !== undefined ? helper.round(row.cur_tp, decimal.tp) : helper.mul(nData.unit_price, nData.cur_qty, decimal.tp);
  440. nData.end_qty = helper.add(nData.cur_qty, oData.pre_qty);
  441. nData.end_tp = helper.add(nData.cur_tp, oData.pre_tp);
  442. }
  443. for (const field of SafeBillsFields.textFields) {
  444. if (row[field] !== undefined) nData[field] = row[field];
  445. }
  446. updateData.push(nData);
  447. }
  448. await this.db.updateRows(this.tableName, updateData);
  449. return { update: updateData };
  450. }
  451. async auditCache(transaction, detailId, auditInfo) {
  452. const leaf = await this.getAllDataByCondition({ where: { detail_id: detailId, tree_is_leaf: true} });
  453. const updateData = [];
  454. for (const l of leaf) {
  455. if (l.cur_read_qty !== l.cur_qty || l.cur_read_tp !== l.cur_tp) {
  456. const data = { id: l.id, cur_read_qty: l.cur_qty, cur_read_tp: l.cur_tp };
  457. const his = l.cur_his ? JSON.parse(l.cur_his) : [];
  458. his.push({ ...auditInfo, qty: l.cur_qty, tp: l.cur_tp, up: l.unit_price });
  459. data.cur_his = JSON.stringify(his);
  460. data.cur_read_qty = l.cur_qty;
  461. data.cur_read_tp = l.cur_tp;
  462. updateData.push(data);
  463. }
  464. }
  465. if (updateData.length > 0) await transaction.updateRows(this.tableName, updateData);
  466. }
  467. }
  468. return PaymentSafeBills;
  469. };