123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const TreeService = require('./base_tree_service');
- // sql拼装器
- const rootId = -1;
- const calcFields = ['unit_price', 'sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'deal_qty', 'deal_tp', 'dgn_qty1', 'dgn_qty2'];
- const readOnlyFields = ['id', 'tender_id', 'ledger_id', 'ledger_pid', 'order', 'level', 'full_path', 'is_leaf'];
- const upFields = ['unit_price'];
- const qtyFields = ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'quantity', 'deal_qty', 'dgn_qty1', 'dgn_qty2'];
- const tpFields = ['sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price', 'deal_tp'];
- class BaseBillsSerivce extends TreeService {
- // 继承方法
- clearParentingData(data) {
- data.unit_price = null;
- data.sgfh_qty = null;
- data.sgfh_tp = null;
- data.sjcl_qty = null;
- data.sjcl_tp = null;
- data.qtcl_qty = null;
- data.qtcl_tp = null;
- data.quantity = null;
- data.total_price = null;
- data.deal_qty = null;
- data.deal_tp = null;
- }
- /**
- * 从标准数据中提取有效数据
- * @param {Object} stdData - 从标准库中查询所得
- * @returns {name, unit, source, code, b_code}
- * @private
- */
- _filterStdData(stdData) {
- const result = {
- name: stdData.name,
- unit: stdData.unit,
- source: stdData.source,
- node_type: stdData.node_type,
- };
- result.code = stdData.code ? stdData.code : '';
- result.b_code = stdData.b_code ? stdData.b_code : '';
- return result;
- }
- async addBillsNode(tenderId, selectId, data, reviseId) {
- if (data) {
- if (reviseId) data.crid = reviseId;
- return await this.addNode(tenderId, selectId, data);
- } else {
- return await this.addNode(tenderId, selectId, {crid: reviseId});
- }
- }
- /**
- * 新增子节点,并排在所有子节点的末尾
- * @param {Number} tenderId - 标段Id
- * @param {Number} selectId - 父节点Id
- * @param {Object} data - 新增节点数据(编号名称等)
- * @returns {Promise<*>}
- */
- async addChild(tenderId, selectId, data, reviseId) {
- if ((tenderId <= 0) || (selectId <= 0)) {
- return [];
- }
- const selectData = await this.getDataByKid(tenderId, selectId);
- if (!selectData) {
- throw '新增节点数据错误';
- }
- const children = await this.getChildrenByParentId(tenderId, selectId);
- const maxId = await this._getMaxLid(tenderId);
- data.id = this.uuid.v4();
- data.tender_id = tenderId;
- data.ledger_id = maxId + 1;
- data.ledger_pid = selectData.ledger_id;
- data.level = selectData.level + 1;
- data.order = children.length + 1;
- data.full_path = selectData.full_path + '-' + data.ledger_id;
- data.is_leaf = true;
- if (reviseId) data.crid = reviseId;
- this.transaction = await this.db.beginTransaction();
- try {
- const result = await this.transaction.insert(this.tableName, data);
- if (children.length === 0) {
- await this.transaction.update(this.tableName,
- {
- is_leaf: false,
- sgfh_qty: null, sgfh_tp: null, qtcl_qty: null, qtcl_tp: null, sjcl_qty: null, sjcl_tp: null,
- quantity: null, unit_price: null, total_price: null, deal_qty: null, deal_tp: null
- },
- { where: {tender_id: tenderId, ledger_id: selectData.ledger_id} });
- }
- await this.transaction.commit();
- } catch(err) {
- this.transaction.rollback();
- throw err;
- }
- this._cacheMaxLid(tenderId, maxId + 1);
- // 查询应返回的结果
- const resultData = {};
- resultData.create = await this.getDataByKid(tenderId, data.ledger_id);
- if (children.length === 0) {
- resultData.update = await this.getDataByKid(tenderId, selectId);
- }
- return resultData;
- }
- /**
- * 添加节点(来自标准清单)
- * @param {Number} tenderId
- * @param {Number} selectId
- * @param {Object} stdData
- * @return {Promise<*>}
- */
- async addStdNode(tenderId, selectId, stdData, reviseId) {
- const newData = this._filterStdData(stdData);
- const result = await this.addBillsNode(tenderId, selectId, newData, reviseId);
- return result;
- }
- /**
- * 添加标准节点,将选择的标准节点,添加为子节点(排序为末尾)
- * @param {Number} tenderId - 标段Id
- * @param {Number} selectId - 添加目标节点Id
- * @param {Object} stdData - 标准节点数据
- * @returns {Promise<*>}
- */
- async addStdNodeAsChild(tenderId, selectId, stdData, reviseId) {
- const newData = this._filterStdData(stdData);
- const result = await this.addChild(tenderId, selectId, newData, reviseId);
- return result;
- }
- /**
- * 根据parentData, data新增数据(新增为parentData的最后一个子项)
- * @param {Number} tenderId - 标段id
- * @param {Object} parentData - 父项数据
- * @param {Object} data - 新增节点,初始数据
- * @return {Promise<*>} - 新增结果
- * @private
- */
- async _addChildNodeData(tenderId, parentData, data, reviseId) {
- if (tenderId <= 0) {
- return undefined;
- }
- if (!data) {
- data = {};
- }
- const pid = parentData ? parentData.ledger_id : rootId;
- const maxId = await this._getMaxLid(tenderId);
- data.id = this.uuid.v4();
- data.tender_id = tenderId;
- data.ledger_id = maxId + 1;
- data.ledger_pid = pid;
- if (data.order === undefined) {
- data.order = 1;
- }
- data.level = parentData ? parentData.level + 1 : 1;
- data.full_path = parentData ? parentData.full_path + '-' + data.ledger_id : '' + data.ledger_id;
- if (data.is_leaf === undefined) {
- data.is_leaf = true;
- }
- if (reviseId) data.crid = reviseId;
- const result = await this.transaction.insert(this.tableName, data);
- this._cacheMaxLid(tenderId, maxId + 1);
- return [result, data];
- }
- /**
- * 根据parentData, data新增数据(自动排序)
- * @param tenderId
- * @param parentData
- * @param data
- * @return {Promise<void>}
- * @private
- */
- async _addChildAutoOrder(tenderId, parentData, data, reviseId) {
- const self = this;
- const findPreData = function(list, a) {
- if (!list || list.length === 0) { return null; }
- for (let i = 0, iLen = list.length; i < iLen; i++) {
- if (self.ctx.helper.compareCode(list[i].code, a.code) > 0) {
- return i > 0 ? list[i - 1] : null;
- }
- }
- return list[list.length - 1];
- };
- const pid = parentData ? parentData.ledger_id : rootId;
- const children = await this.getChildrenByParentId(tenderId, pid);
- const preData = findPreData(children, data);
- if (!preData || children.indexOf(preData) < children.length - 1) {
- await this._updateChildrenOrder(tenderId, pid, preData ? preData.order + 1 : 1);
- }
- data.order = preData ? preData.order + 1 : 1;
- const [addResult, node] = await this._addChildNodeData(tenderId, parentData, data, reviseId);
- return [addResult, node];
- }
- /**
- * 添加节点,并同步添加父节点
- * @param {Number} tenderId - 标段id
- * @param {Number} selectId - 选中节点id
- * @param {Object} stdData - 节点数据
- * @param {StandardLib} stdLib - 标准库
- * @return {Promise<void>}
- */
- async addStdNodeWithParent(tenderId, stdData, stdLib, reviseId) {
- // 查询完整标准清单,并按层次排序
- const fullLevel = await stdLib.getFullLevelDataByFullPath(stdData.list_id, stdData.full_path);
- fullLevel.sort(function(x, y) {
- return x.level - y.level;
- });
- let isNew = false,
- node,
- firstNew,
- updateParent,
- addResult;
- const expandIds = [];
- this.transaction = await this.db.beginTransaction();
- try {
- // 从最顶层节点依次查询是否存在,否则添加
- for (let i = 0, len = fullLevel.length; i < len; i++) {
- const stdNode = fullLevel[i];
- if (isNew) {
- const newData = this._filterStdData(stdNode);
- newData.is_leaf = (i === len - 1);
- [addResult, node] = await this._addChildNodeData(tenderId, node, newData, reviseId);
- } else {
- const parent = node;
- node = await this.getDataByCondition({
- tender_id: tenderId,
- ledger_pid: parent ? parent.ledger_id : rootId,
- code: stdNode.code,
- name: stdNode.name,
- });
- if (!node) {
- isNew = true;
- const newData = this._filterStdData(stdNode);
- newData.is_leaf = (i === len - 1);
- [addResult, node] = await this._addChildAutoOrder(tenderId, parent, newData, reviseId);
- if (parent && parent.is_leaf) {
- await this.transaction.update(this.tableName, { id: parent.id, is_leaf: false,
- unit_price: null, quantity: null, total_price: null, deal_qty: null, deal_tp: null});
- updateParent = parent;
- }
- firstNew = node;
- } else {
- expandIds.push(node.ledger_id);
- }
- }
- }
- await this.transaction.commit();
- } catch (err) {
- await this.transaction.rollback();
- throw err;
- }
- // 查询应返回的结果
- let createData = [],
- updateData = [];
- if (firstNew) {
- createData = await this.getDataByFullPath(tenderId, firstNew.full_path + '%');
- updateData = await this.getNextsData(tenderId, firstNew.ledger_pid, firstNew.order);
- if (updateParent) {
- updateData.push(await this.getDataByCondition({ id: updateParent.id }));
- }
- }
- return { create: createData, update: updateData };
- }
- /**
- * 过滤data中update方式不可提交的字段
- * @param {Number} id - 主键key
- * @param {Object} data
- * @return {Object<{id: *}>}
- * @private
- */
- _filterUpdateInvalidField(id, data) {
- const result = {
- id,
- };
- for (const prop in data) {
- if (readOnlyFields.indexOf(prop) === -1) {
- result[prop] = data[prop];
- }
- }
- return result;
- }
- /**
- * newData中,以orgData为基准,过滤掉orgData中未定义或值相等的部分
- * @param {Object} orgData
- * @param {Object} newData
- * @private
- */
- _filterChangedField(orgData, newData) {
- const result = {};
- let bChanged = false;
- for (const prop in orgData) {
- if (this._.isEmpty(newData[prop]) && newData[prop] !== orgData[prop]) {
- result[prop] = newData[prop];
- bChanged = true;
- }
- }
- return bChanged ? result : undefined;
- }
- /**
- * 检查data中是否含有计算字段
- * @param {Object} data
- * @return {boolean}
- * @private
- */
- _checkCalcField(data) {
- for (const prop in data) {
- if (calcFields.indexOf(prop) >= 0) {
- return true;
- }
- }
- return false;
- }
- /**
- * 提交数据 - 响应计算(增量方式计算)
- * @param {Number} tenderId
- * @param {Object} data
- * @return {Promise<*>}
- */
- async updateCalc(tenderId, data) {
- // 简单验证数据
- if (tenderId <= 0 || !this.ctx.tender) {
- throw '标段不存在';
- }
- const info = this.ctx.tender.info;
- if (!data) {
- throw '提交数据错误';
- }
- const datas = data instanceof Array ? data : [data];
- const ids = [];
- for (const row of datas) {
- if (tenderId !== row.tender_id) {
- throw '提交数据错误';
- }
- ids.push(row.id);
- }
- this.transaction = await this.db.beginTransaction();
- try {
- for (const row of datas) {
- const updateNode = await this.getDataById(row.id);
- if (!updateNode || tenderId !== updateNode.tender_id || row.ledger_id !== updateNode.ledger_id) {
- throw '提交数据错误';
- }
- let updateData;
- if (row.unit) {
- if (row.sgfh_qty === undefined) { row.sgfh_qty = updateNode.sgfh_qty; }
- if (row.sjcl_qty === undefined) { row.sjcl_qty = updateNode.sjcl_qty; }
- if (row.qtcl_qty === undefined) { row.qtcl_qty = updateNode.qtcl_qty; }
- if (row.deal_qty === undefined) { row.deal_qty = updateNode.deal_qty; }
- }
- if (row.b_code) {
- row.dgn_qty1 = null;
- row.dgn_qty2 = null;
- }
- if (this._checkCalcField(row)) {
- let calcData = JSON.parse(JSON.stringify(row));
- const precision = this.ctx.helper.findPrecision(info.precision, row.unit ? row.unit : updateNode.unit);
- // 数量保留小数位数
- this.ctx.helper.checkFieldPrecision(calcData, qtyFields, precision.value);
- // 单位保留小数位数
- this.ctx.helper.checkFieldPrecision(calcData, upFields, info.decimal.up);
- // 未提交单价则读取数据库单价
- if (row.unit_price === undefined) calcData.unit_price = updateNode.unit_price;
- // 计算
- if (row.sgfh_qty !== undefined || row.sjcl_qty !== undefined || row.qtcl_qty !== undefined ||
- row.deal_qty !== undefined || row.unit_price) {
- if (row.sgfh_qty === undefined) calcData.sgfh_qty = updateNode.sgfh_qty;
- if (row.sjcl_qty === undefined) calcData.sjcl_qty = updateNode.sjcl_qty;
- if (row.qtcl_qty === undefined) calcData.qtcl_qty = updateNode.qtcl_qty;
- if (row.deal_qty === undefined) calcData.deal_qty = updateNode.deal_qty;
- calcData.quantity = this.ctx.helper.sum([calcData.sgfh_qty, calcData.sjcl_qty, calcData.qtcl_qty]);
- calcData.sgfh_tp = this.ctx.helper.mul(calcData.sgfh_qty, calcData.unit_price, info.decimal.tp);
- calcData.sjcl_tp = this.ctx.helper.mul(calcData.sjcl_qty, calcData.unit_price, info.decimal.tp);
- calcData.qtcl_tp = this.ctx.helper.mul(calcData.qtcl_qty, calcData.unit_price, info.decimal.tp);
- calcData.total_price = this.ctx.helper.mul(calcData.quantity, calcData.unit_price, info.decimal.tp);
- calcData.deal_tp = this.ctx.helper.mul(calcData.deal_qty, calcData.unit_price, info.decimal.tp);
- } else if (row.sgfh_tp !== undefined || row.sjcl_tp !== undefined || row.qtcl_tp !== undefined || row.deal_tp !== undefined) {
- calcData.sgfh_qty = null;
- calcData.sjcl_qty = null;
- calcData.qtcl_qty = null;
- calcData.quantity = null;
- calcData.deal_qty = null;
- calcData.sgfh_tp = (row.sgfh_tp !== undefined) ? this.ctx.helper.round(calcData.row.sgfh_tp, info.decimal.tp) : updateNode.sgfh_tp;
- calcData.sjcl_tp = (row.sgfh_tp !== undefined) ? this.ctx.helper.round(calcData.row.sjcl_tp, info.decimal.tp) : updateNode.sjcl_tp;
- calcData.qtcl_tp = (row.sgfh_tp !== undefined) ? this.ctx.helper.round(calcData.row.qtcl_tp, info.decimal.tp) : updateNode.qtcl_tp;
- calcData.total_price = this.ctx.helper.sum([calcData.sgfh_tp, calcData.sjcl_tp, calcData.qtcl_tp]);
- calcData.deal_tp = (row.deal_tp !== undefined) ? this.ctx.helper.round(calcData.row.deal_tp, info.decimal.tp) : updateNode.deal_tp;
- } else if (row.unit_price !== undefined) {
- calcData.sgfh_tp = this.ctx.helper.mul(calcData.sgfh_qty, calcData.unit_price, info.decimal.tp);
- calcData.sjcl_tp = this.ctx.helper.mul(calcData.sjcl_qty, calcData.unit_price, info.decimal.tp);
- calcData.qtcl_tp = this.ctx.helper.mul(calcData.qtcl_qty, calcData.unit_price, info.decimal.tp);
- calcData.total_price = this.ctx.helper.mul(calcData.quantity, calcData.unit_price, info.decimal.tp);
- calcData.deal_tp = this.ctx.helper.mul(calcData.deal_qty, calcData.unit_price, info.decimal.tp);
- }
- updateData = this._filterUpdateInvalidField(updateNode.id, calcData);
- } else {
- updateData = this._filterUpdateInvalidField(updateNode.id, row);
- }
- await this.transaction.update(this.tableName, updateData);
- }
- await this.transaction.commit();
- this.transaction = null;
- } catch (err) {
- await this.transaction.rollback();
- this.transaction = null;
- throw err;
- }
- return { update: await this.getDataById(ids) };
- }
- // 统计方法
- async addUp(condition) {
- const sql = 'SELECT Sum(total_price) As total_price, Sum(deal_tp) As deal_tp' +
- ' FROM ' + this.tableName + this.ctx.helper.whereSql(condition);
- const result = await this.db.queryOne(sql);
- return result;
- }
- // 导入Excel
- async importGclExcel(id, sheet, data) {
- const node = await this.getDataById(id);
- if (!node || !node.is_leaf || node.tender_id !== this.ctx.tender.id) throw '数据错误';
- const maxId = await this._getMaxLid(this.ctx.tender.id);
- const AnalysisExcel = require('../lib/analysis_excel').AnalysisGclExcelTree;
- const analysisExcel = new AnalysisExcel(this.ctx);
- const cacheData = analysisExcel.analysisData(sheet, node, maxId, data);
- const datas = [];
- for (const node of cacheData.items) {
- datas.push({
- id: node.id, tender_id: this.ctx.tender.id,
- ledger_id: node.ledger_id,
- ledger_pid: node.ledger_pid,
- level: node.level,
- order: node.order,
- is_leaf: !node.children || node.children.length === 0,
- full_path: node.full_path,
- b_code: node.b_code,
- name: node.name,
- unit: node.unit,
- sgfh_qty: node.sgfh_qty,
- sgfh_tp: node.sgfh_tp,
- quantity: node.quantity,
- unit_price: node.unit_price,
- total_price: node.total_price,
- crid: node.crid,
- });
- }
- const conn = await this.db.beginTransaction();
- try {
- await this.db.update(this.tableName, {id: node.id, is_leaf: false});
- await this.db.insert(this.tableName, datas);
- await conn.commit();
- } catch(err) {
- await conn.rollback();
- throw err;
- }
- this._cacheMaxLid(this.ctx.tender.id, cacheData.keyNodeId);
- node.is_leaf = false;
- return { create: datas, update: [node]};
- }
- }
- module.exports = BaseBillsSerivce;
|