123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556 |
- 'use strict';
- /**
- *
- * 支付审批-安全生产
- * @author Mai
- * @date
- * @version
- */
- const defaultBills = require('../const/payment').defaultSafeBills;
- const billsUtils = require('../lib/bills_utils');
- const SafeBillsFields = {
- textFields: ['b_code', 'name', 'unit', 'spec', 'invoice_code', 'memo'],
- calcFields: ['unit_price', 'cur_qty', 'cur_tp', 'end_qty', 'end_tp'],
- fixedFields: ['safe_id', 'tender_id', 'pre_qty', 'pre_tp', 'cur_read_qty', 'cur_read_tp', 'cur_his', 'add_user_id', 'add_time'],
- treeFields: ['id', 'detail_id', 'safe_id', 'tree_id', 'tree_pid', 'tree_level', 'tree_order', 'tree_full_path', 'tree_is_leaf'],
- };
- SafeBillsFields.editQueryFields = [...SafeBillsFields.treeFields, ...SafeBillsFields.textFields, ...SafeBillsFields.calcFields, 'pre_qty', 'pre_tp'];
- SafeBillsFields.readQueryFields = [...SafeBillsFields.treeFields, ...SafeBillsFields.textFields, 'unit_price', 'pre_qty', 'pre_tp', 'cur_read_qty', 'cur_read_tp'];
- SafeBillsFields.compareQueryFields = [...SafeBillsFields.treeFields, ...SafeBillsFields.textFields, 'unit_price', 'cur_his', 'cur_qty', 'cur_tp'];
- SafeBillsFields.preQueryFields = [...SafeBillsFields.treeFields, ...SafeBillsFields.textFields, 'tender_id', 'unit_price', 'end_qty', 'end_tp', 'add_user_id', 'add_time', 'update_user_id', 'update_time'];
- const auditConst = require('../const/audit').stage;
- module.exports = app => {
- class PaymentSafeBills extends app.BaseTreeService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx, {
- mid: 'detail_id',
- kid: 'tree_id',
- pid: 'tree_pid',
- order: 'tree_order',
- level: 'tree_level',
- isLeaf: 'tree_is_leaf',
- fullPath: 'tree_full_path',
- keyPre: 'safe_bills_maxLid:',
- uuid: true,
- });
- // this.depart = 10;
- this.tableName = 'payment_safe_bills';
- this.decimal = { tp: 2, up: 2, qty: 3 };
- }
- // 继承方法
- clearParentingData(data) {
- for (const f of SafeBillsFields.calcFields) {
- data[f] = 0;
- }
- }
- _getDefaultData(data, detail) {
- data.id = this.uuid.v4();
- data.safe_id = data.id;
- data.tender_id = detail.tender_id;
- data.detail_id = detail.id;
- data.add_user_id = this.ctx.session.sessionUser.accountId;
- }
- async getEditData(detail) {
- return await this.getAllDataByCondition({
- where: { detail_id: detail.id },
- columns: SafeBillsFields.editQueryFields
- });
- }
- async getReadData(detail) {
- const helper = this.ctx.helper;
- const result = await this.getAllDataByCondition({
- where: { detail_id: detail.id },
- columns: SafeBillsFields.readQueryFields
- });
- result.forEach(x => {
- x.cur_qty = x.cur_read_qty;
- x.cur_tp = x.cur_read_tp;
- x.end_qty = helper.add(x.pre_qty, x.cur_qty);
- x.end_tp = helper.add(x.pre_tp, x.cur_tp);
- });
- return result;
- }
- async getCompareData(detail) {
- const result = await this.getAllDataByCondition({
- where: { detail_id: detail.id },
- columns: SafeBillsFields.compareQueryFields
- });
- result.forEach(x => {
- x.cur_his = x.cur_his ? JSON.parse(x.cur_his) : [];
- });
- return result;
- }
- async init(detail, transaction) {
- if (!detail || !transaction) throw '安全生产费数据错误';
- const insertData = [];
- for (const b of defaultBills) {
- const bills = JSON.parse(JSON.stringify(b));
- this._getDefaultData(bills, detail);
- insertData.push(bills);
- }
- const operate = await transaction.insert(this.tableName, insertData);
- return operate.affectedRows === insertData.length;
- }
- async initByPre(detail, preDetail, transaction) {
- if (!detail || !preDetail || !transaction) throw '安全生产费数据错误';
- const preBills = await this.getAllDataByCondition({
- where: { detail_id: preDetail.id },
- columns: SafeBillsFields.preQueryFields
- });
- const insertData = [];
- for (const bills of preBills) {
- //const bills = JSON.parse(JSON.stringify(b));
- bills.id = this.uuid.v4();
- bills.detail_id = detail.id;
- delete bills.invoice_code;
- bills.pre_qty = bills.end_qty;
- bills.pre_tp = bills.end_tp;
- insertData.push(bills);
- }
- const operate = await transaction.insert(this.tableName, insertData);
- return operate.affectedRows === insertData.length;
- }
- /**
- * 新增数据(供内部或其他service类调用, controller不可直接使用)
- * @param {Array|Object} data - 新增数据
- * @param {Number} detailId - 标段id
- * @param {Object} transaction - 新增事务
- * @return {Promise<boolean>} - {Promise<是否正确新增成功>}
- */
- async innerAdd(data, detailId, transaction) {
- const datas = data instanceof Array ? data : [data];
- if (detailId <= 0) {
- throw '标段id错误';
- }
- if (datas.length <= 0) {
- throw '插入数据为空';
- }
- if (!transaction) {
- throw '内部错误';
- }
- // 整理数据
- const insertData = [];
- for (const tmp of datas) {
- tmp[this.setting.id] = tmp.template_id;
- tmp[this.setting.pid] = tmp.pid;
- tmp[this.setting.mid] = detailId;
- delete tmp.template_id;
- delete tmp.pid;
- tmp.id = this.uuid.v4();
- insertData.push(tmp);
- }
- const operate = await transaction.insert(this.tableName, insertData);
- return operate.affectedRows === datas.length;
- }
- /**
- * 新增数据
- *
- * @param {Object} data - 新增的数据(可批量)
- * @param {Number} detailId - 支付审批期id
- * @return {Boolean} - 返回新增的结果
- */
- async add(data, detailId) {
- this.transaction = await this.db.beginTransaction();
- let result = false;
- try {
- result = await this.innerAdd(data, detailId, this.transaction);
- if (!result) {
- throw '新增数据错误';
- }
- await this.transaction.commit();
- } catch (error) {
- await this.transaction.rollback();
- result = false;
- }
- return result;
- }
- /**
- * 根据节点Id获取数据
- *
- * @param {Number} detailId - 标段id
- * @param {Number} nodeId - 项目节/工程量清单节点id
- * @return {Object} - 返回查询到的节点数据
- */
- async getDataByNodeId(detailId, nodeId) {
- if ((nodeId <= 0) || (detailId <= 0)) {
- return undefined;
- }
- const where = {};
- where[this.setting.mid] = detailId;
- where[this.setting.id] = nodeId;
- const data = await this.db.getDataByCondition(where);
- return data;
- }
- /**
- * 根据节点Id获取数据
- * @param {Number} detailId - 期Id
- * @param {Array} nodesIds - 节点Id
- * @return {Array}
- */
- async getDataByNodeIds(detailId, nodesIds) {
- if (detailId <= 0) {
- return [];
- }
- const where = {};
- where[this.setting.mid] = detailId;
- where[this.setting.id] = nodesIds;
- const data = await this.db.getAllDataByCondition({ where });
- return this._.sortBy(data, function(d) {
- return nodesIds.indexOf(d.ledger_id);
- });
- }
- /**
- * 根据主键id获取数据
- * @param {Array|Number} id - 主键id
- * @return {Promise<*>}
- */
- async getDataByIds(id) {
- if (!id) {
- return [];
- }
- const ids = id instanceof Array ? id : [id];
- if (ids.length === 0) {
- return [];
- }
- const data = await this.db.getAllDataByCondition({ where: { id: ids } });
- return data;
- }
- /**
- * 根据 父节点id 获取子节点
- * @param detailId
- * @param nodeId
- * @return {Promise<*>}
- */
- async getChildrenByParentId(detailId, nodeId) {
- if (detailId <= 0 || !nodeId) {
- return undefined;
- }
- const nodeIds = nodeId instanceof Array ? nodeId : [nodeId];
- if (nodeIds.length === 0) {
- return [];
- }
- const where = {};
- where[this.setting.mid] = detailId;
- where[this.setting.pid] = nodeIds;
- const data = await this.getAllDataByCondition({ where, orders: [[this.setting.order, 'ASC']] });
- return data;
- }
- async pasteBlockData(detailId, targetId, pasteData, defaultData) {
- const setting = this.setting;
- if ((detailId <= 0) || (sid <= 0)) return [];
- if (!pasteData || pasteData.length <= 0) throw '复制数据错误';
- for (const pd of pasteData) {
- if (!pd || pd.length <= 0) throw '复制数据错误';
- pd.sort(function (x, y) {
- return x[setting.level] - y[setting.level]
- });
- if (pd[0][this.setting.pid] !== pasteData[0][0][this.setting.pid]) throw '复制数据错误:仅可操作同层节点';
- }
- this.newBills = false;
- const targetData = await this.getDataByKid(detailId, targetId);
- if (!targetData) throw '粘贴数据错误';
- const newParentPath = targetData.full_path.replace(targetData.ledger_id, '');
- const tpDecimal = this.ctx.detail && this.ctx.detail.decimal ? this.ctx.detail.decimal : this.decimal;
- const pasteBillsData = [], leafBillsId = [];
- let maxId = await this._getMaxLid(this.ctx.paymentTender.id);
- for (const [i, pd] of pasteData.entries()) {
- for (const d of pd) {
- d.children = pd.filter(function (x) {
- return x[setting.pid] === d[setting.id];
- });
- }
- const pbd = [];
- for (const [j, d] of pd.entries()) {
- const newBills = {
- id: this.uuid.v4(),
- safe_id: this.uuid.v4(),
- b_code: d.b_code,
- name: d.name,
- unit: d.unit,
- unit_price: this.ctx.helper.round(d.unit_price, tpDecimal.up),
- memo: d.memo,
- };
- newBills[setting.mid] = detailId;
- newBills[setting.id] = maxId + j + 1;
- newBills[setting.pid] = j === 0 ? targetData[setting.pid] : d[setting.pid];
- newBills[setting.level] = d[setting.level] + targetData[setting.level] - pd[0][setting.level];
- newBills[setting.order] = j === 0 ? targetData[setting.order] + i + 1 : d[setting.order];
- newBills[setting.isLeaf] = d[setting.isLeaf];
- for (const c of d.children) {
- c[setting.pid] = newBills[setting.id];
- }
- newBills.quantity = this.ctx.helper.round(d.quantity, tpDecimal.qty);
- newBills.total_price = this.ctx.helper.mul(newBills.quantity, newBills.unit_price, tpDecimal.tp);
- if (defaultData) this.ctx.helper._.assignIn(newBills, defaultData);
- pbd.push(newBills);
- }
- for (const d of pbd) {
- const parent = pbd.find(function (x) {
- return x[setting.id] === d[setting.pid];
- });
- d[setting.fullPath] = parent
- ? parent[setting.fullPath] + '-' + d[setting.id]
- : newParentPath + d[setting.id];
- if (defaultData) this.ctx.helper._.assignIn(pbd, defaultData);
- pasteBillsData.push(d);
- }
- maxId = maxId + pbd.length;
- }
- this.transaction = await this.db.beginTransaction();
- try {
- // 选中节点的所有后兄弟节点,order+粘贴节点个数
- await this._updateChildrenOrder(tid, targetData[setting.pid], targetData[setting.order] + 1, pasteData.length);
- // 数据库创建新增节点数据
- if (pasteBillsData.length > 0) {
- const newData = await this.transaction.insert(this.tableName, pasteBillsData);
- }
- this._cacheMaxLid(tid, maxId);
- await this.transaction.commit();
- } catch (err) {
- await this.transaction.rollback();
- throw err;
- }
- // 查询应返回的结果
- const updateData = await this.getNextsData(targetData[setting.mid], targetData[setting.pid], targetData[setting.order] + pasteData.length);
- return { create: pasteBillsData, update: updateData };
- }
- async addSafeBillsNode(detail, targetId, count) {
- if (!detail) return null;
- const select = targetId ? await this.getDataByKid(detail.id, targetId) : null;
- if (targetId && !select) throw '新增节点数据错误';
- this.transaction = await this.db.beginTransaction();
- try {
- if (select) await this._updateChildrenOrder(detail.id, select[this.setting.pid], select[this.setting.order] + 1, count);
- const newDatas = [];
- const maxId = await this._getMaxLid(detail.id);
- for (let i = 1; i < count + 1; i++) {
- const newData = {};
- newData[this.setting.kid] = maxId + i;
- newData[this.setting.pid] = select ? select[this.setting.pid] : this.rootId;
- newData[this.setting.mid] = detail.id;
- newData[this.setting.level] = select ? select[this.setting.level] : 1;
- newData[this.setting.order] = select ? select[this.setting.order] + i : i;
- newData[this.setting.fullPath] = newData[this.setting.level] > 1
- ? select[this.setting.fullPath].replace('-' + select[this.setting.kid], '-' + newData[this.setting.kid])
- : newData[this.setting.kid] + '';
- newData[this.setting.isLeaf] = true;
- this._getDefaultData(newData, detail);
- newDatas.push(newData);
- }
- const insertResult = await this.transaction.insert(this.tableName, newDatas);
- this._cacheMaxLid(detail.id, maxId + count);
- if (insertResult.affectedRows !== count) throw '新增节点数据错误';
- await this.transaction.commit();
- this.transaction = null;
- } catch (err) {
- await this.transaction.rollback();
- this.transaction = null;
- throw err;
- }
- if (select) {
- const createData = await this.getChildBetween(detail.id, select[this.setting.pid], select[this.setting.order], select[this.setting.order] + count + 1);
- const updateData = await this.getNextsData(detail.id, select[this.setting.pid], select[this.setting.order] + count);
- return {create: createData, update: updateData};
- } else {
- const createData = await this.getChildBetween(detail.id, -1, 0, count + 1);
- return {create: createData};
- }
- }
- async addStdNodeWithParent(detail, targetId, stdData) {
- const findPreData = function(list, a) {
- if (!list || list.length === 0) { return null; }
- for (let i = 0, iLen = list.length; i < iLen; i++) {
- if (billsUtils.compareCode(list[i].b_code, a.b_code) > 0) {
- return i > 0 ? list[i - 1] : null;
- }
- }
- return list[list.length - 1];
- };
- let parent = await this.getDataByKid(detail.id, targetId);
- if (targetId && !parent) throw '新增节点数据错误,请刷新页面重试';
- const orgParentId = parent.id;
- let children = await this.getChildrenByParentId(detail.id, targetId);
- const updateParent = children.length === 0;
- const insertData = [];
- const maxId = await this._getMaxLid(detail.id);
- this.transaction = await this.db.beginTransaction();
- try {
- if (updateParent) {
- const updateData = { id: parent.id };
- updateData[this.setting.isLeaf] = false;
- this.clearParentingData(updateData);
- await this.transaction.update(this.tableName, updateData);
- }
- // 从最顶层节点依次查询是否存在,否则添加
- for (let i = 0, len = stdData.length; i < len; i++) {
- const newData = { b_code: stdData[i].b_code, name: stdData[i].name, unit: stdData[i].unit };
- newData[this.setting.kid] = maxId + i + 1;
- newData[this.setting.pid] = parent ? parent[this.setting.kid] : this.rootId;
- newData[this.setting.level] = parent ? parent[this.setting.level] + 1 : 1;
- newData[this.setting.fullPath] = parent ? `${parent[this.setting.fullPath]}-${newData[this.setting.kid]}` : `${newData[this.setting.kid]}`;
- const pre = findPreData(children, newData);
- newData[this.setting.order] = pre ? pre[this.setting.order] + 1 : 1;
- if (!pre || children.indexOf(pre) < children.length - 1) {
- await this._updateChildrenOrder(detail.id, parent ? parent[this.setting.kid] : this.rootId, pre ? pre[this.setting.order] + 1 : 1);
- }
- newData[this.setting.isLeaf] = (i === len - 1);
- this._getDefaultData(newData, detail);
- insertData.push(newData);
- parent = newData;
- children = [];
- }
- await this.transaction.insert(this.tableName, insertData);
- await this.transaction.commit();
- } catch (err) {
- await this.transaction.rollback();
- throw err;
- }
- this._cacheMaxLid(detail.id, maxId + stdData.length);
- // 查询应返回的结果
- const createData = await this.getDataByFullPath(detail.id, insertData[0][this.setting.fullPath] + '%');
- const updateData = await this.getNextsData(detail.id, targetId, insertData[0][this.setting.order]);
- if (updateParent) {
- updateData.push(await this.getDataByKid(detail.id, targetId));
- }
- return { create: createData, update: updateData };
- }
- async updateCalc(detail, data) {
- const helper = this.ctx.helper;
- const decimal = this.ctx.detail && this.ctx.detail.decimal ? this.ctx.detail.decimal : this.decimal;
- // 简单验证数据
- if (!detail) throw '安全生产费不存在';
- if (!data) throw '提交数据错误';
- const datas = data instanceof Array ? data : [data];
- const ids = datas.map(x => { return x.id; });
- const orgData = await this.getAllDataByCondition({ where: { id: ids }});
- const updateData = [];
- for (const row of datas) {
- const oData = orgData.find(x => { return x.id === row.id });
- if (!oData || oData.detail_id !== detail.id || oData.tree_id !== row.tree_id) throw '提交数据错误';
- let nData = { id: oData.id, tree_id: oData.tree_id, update_user_id: this.ctx.session.sessionUser.accountId };
- // 计算相关
- if (row.cur_qty !== undefined || row.unit_price !== undefined || row.cur_tp !== undefined) {
- nData.unit_price = row.unit_price !== undefined ? helper.round(row.unit_price, decimal.up) : oData.unit_price;
- nData.cur_qty = row.cur_qty !== undefined ? helper.round(row.cur_qty, decimal.qty) : oData.cur_qty;
- nData.cur_tp = row.cur_tp !== undefined ? helper.round(row.cur_tp, decimal.tp) : helper.mul(nData.unit_price, nData.cur_qty, decimal.tp);
- nData.end_qty = helper.add(nData.cur_qty, oData.pre_qty);
- nData.end_tp = helper.add(nData.cur_tp, oData.pre_tp);
- }
- for (const field of SafeBillsFields.textFields) {
- if (row[field] !== undefined) nData[field] = row[field];
- }
- updateData.push(nData);
- }
- await this.db.updateRows(this.tableName, updateData);
- return { update: updateData };
- }
- async auditCache(transaction, detailId, auditInfo) {
- const leaf = await this.getAllDataByCondition({ where: { detail_id: detailId, tree_is_leaf: true} });
- const updateData = [];
- for (const l of leaf) {
- if (l.cur_read_qty !== l.cur_qty || l.cur_read_tp !== l.cur_tp) {
- const data = { id: l.id, cur_read_qty: l.cur_qty, cur_read_tp: l.cur_tp };
- const his = l.cur_his ? JSON.parse(l.cur_his) : [];
- his.push({ ...auditInfo, qty: l.cur_qty, tp: l.cur_tp, up: l.unit_price });
- data.cur_his = JSON.stringify(his);
- data.cur_read_qty = l.cur_qty;
- data.cur_read_tp = l.cur_tp;
- updateData.push(data);
- }
- }
- if (updateData.length > 0) await transaction.updateRows(this.tableName, updateData);
- }
- async setDecimal(decimal) {
- if (!this.ctx.detail) throw '读取数据错误';
- if (this.ctx.detail.order !== this.ctx.detail.highOrder) throw '往期不可修改小数位数';
- if (this.ctx.detail.status !== auditConst.status.uncheck && this.ctx.detail.status !== auditConst.status.checkNo) throw '仅原报可修改小数位数';
- const orgDecimal = this.ctx.detail.decimal ? this.ctx.detail.decimal : this.decimal;
- const calcQty = decimal.qty < orgDecimal.qty;
- const calcUp = decimal.up < orgDecimal.up;
- const calcTp = decimal.tp < orgDecimal.tp;
- this.ctx.detail.decimal = { up: decimal.up, tp: decimal.tp, qty: decimal.qty };
- const updateData = [];
- if (calcQty || calcUp || calcTp) {
- const calcData = await this.getAllDataByCondition({ where: {detail_id: this.ctx.detail.id, tree_is_leaf: 1 } });
- for (const cd of calcData) {
- const nd = { id: cd.id, tree_id: cd.tree_id };
- nd.unit_price = calcUp ? this.ctx.helper.round(cd.unit_price, decimal.up) : cd.unit_price;
- nd.cur_qty = calcQty ? this.ctx.helper.round(cd.cur_qty, decimal.qty) : cd.cur_qty;
- nd.cur_tp = this.ctx.helper.mul(nd.unit_price, nd.cur_qty, decimal.tp);
- nd.end_qty = this.ctx.helper.add(cd.pre_qty, nd.cur_qty);
- nd.end_tp = this.ctx.helper.add(cd.pre_tp, nd.cur_tp);
- updateData.push(nd);
- }
- }
- const conn = await this.db.beginTransaction();
- try {
- await conn.update(this.ctx.service.paymentDetail.tableName, { id: this.ctx.detail.id, bills_decimal: JSON.stringify(this.ctx.detail.decimal)});
- if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
- await conn.commit();
- return { calc: calcQty || calcUp || calcTp, update: updateData };
- } catch(err) {
- await conn.rollback();
- return { calc: false, update: [] };
- }
- }
- }
- return PaymentSafeBills;
- };
|