'use strict'; /** * * * @author Mai * @date 2018/5/8 * @version */ module.exports = app => { class DealBills extends app.BaseService { /** * 构造函数 * @param ctx */ constructor(ctx) { super(ctx); this.tableName = 'deal_bills'; } /** * 导入Excel数据 * * @param {Array} sheet - Excel文件中的全部工作表 * @param {Number} tenderId - 所属标段Id * @returns {Promise} */ async importData(sheet, tenderId) { let result = false; const transaction = await this.db.beginTransaction(); try { const bills = []; for (const row of sheet.data) { if (this.ctx.helper.validBillsCode(row[0])) { bills.push({ deal_id: bills.length + 1, tender_id: tenderId, code: row[0], name: row[1], unit: row[2], unit_price: row[3], quantity: row[4], }); } } if (bills.length > 0) { await transaction.delete(this.tableName, {tender_id: tenderId}); const billsResult = await transaction.insert(this.tableName, bills); if (billsResult.affectedRows !== bills.length) { throw '导入签约清单数据出错'; } } else { throw 'Excel文件中无签约清单数据'; } await transaction.commit(); result = true; } catch (err) { await transaction.rollback(); throw err; } return result; } } return DealBills; }