'use strict'; /** * * * @author Mai * @date 2018/8/14 * @version */ module.exports = app => { class ChangeCompany extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'change_company'; } /** * 队列修改和添加公司信息 * @param {Object} data - 公司信息集合 * @return {Object} - 返回新增的公司列表,和替换下拉选择公司列表 */ async setCompanyList(data) { const tenderId = parseInt(data.tid); const updateIdArray = data.uci; const updateArray = data.uc; const addArray = data.ac; const addCompany = []; const selectCompany = []; // 初始化事务 this.transaction = await this.db.beginTransaction(); try { // 更新公司信息 if (updateIdArray.length !== 0) { for (const index in updateIdArray) { if (updateArray[index].trim() === '') { await this.transaction.delete(this.tableName, { id: updateIdArray[index] }); } else { const updateData = { id: updateIdArray[index], name: updateArray[index], }; await this.transaction.update(this.tableName, updateData); selectCompany.push(updateData); } } } if (addArray.length !== 0) { for (const name of addArray) { const addData = { tid: tenderId, name, }; const operate = await this.transaction.insert(this.tableName, addData); if (operate.affectedRows !== 1) { throw '提交数据失败'; } else { selectCompany.push({ id: operate.insertId, name }); addCompany.push({ id: operate.insertId, name }); } } } await this.transaction.commit(); } catch (error) { console.log(error); // 回滚 await this.transaction.rollback(); } return [addCompany, selectCompany]; } } return ChangeCompany; };