'use strict'; /** * * * @author Mai * @date 2018/9/26 * @version */ const settingConst = require('../const/setting.js'); module.exports = app => { class Category extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'category'; } async addCategory(pid, name, type) { const count = await this.count({pid: pid, name: name}); if (count > 0) { throw '存在同名类别'; } const category = { pid: pid, name: name, type: type, }; const result = await this.db.insert(this.tableName, category); if (result.affectedRows !== 1) { throw '提交数据失败'; } else { category.id = result.insertId; } category.value = category.value && category.value !== '' ? JSON.parse(category.value) : []; return category; } async getCategory(id) { const data = await this.getDataByCondition({id: id}); data.value = await this.ctx.service.categoryValue.getAllDataByCondition({ where: {cid: id} }); return data; } /** * 获取标段分类数据 * * @param {Number} pid - 标段id * @returns {Promise<*>} */ async getAllCategory(pid) { const data = await this.getAllDataByCondition({where: {pid: pid}}); const values = await this.ctx.service.categoryValue.getAllDataByCondition({ where: {pid: pid} }); for (const d of data) { d.value = values.filter(function (v) { return v.cid === d.id; }); } return data; } async resetCategoryLevel(data) { this.transaction = await this.db.beginTransaction(); try { for (const d of data) { this.transaction.update(this.tableName, {id: d.id, level: d.level}); } this.transaction.commit(); } catch (err) { await this.transaction.rollback(); throw err; } } } return Category; };