1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- '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(condition) {
- const data = await this.getDataByCondition(condition);
- data.value = data.value && data.value !== '' ? JSON.parse(data.value) : [];
- return data;
- }
- async getAllCategory(condition) {
- const data = await this.getAllDataByCondition(condition);
- for (const d of data) {
- d.value = d.value && d.value !== '' ? JSON.parse(d.value) : [];
- }
- return data;
- }
- }
- return Category;
- };
|