'use strict'; //基类控制器 const BaseController = require('../base.js'); /** * [exports 继承基类控制骂] * @type {[type]} */ module.exports = class ManagerController extends BaseController { /** * [useModel 当前使用的模型] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ get useModel() { throw new Error('未实现使用的模型定义,请检查'); } /** * [createValidate 增的验证器] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ get createValidate() { throw new Error('未实现数据添加的验证器,请检查'); } /** * [useModelPk 模型主键] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ get modelPk() { let that = this; let keys = Object.keys((that.useModel).primaryKeys); if(keys.length > 0) return keys[0]; throw new Error('未找到模型主键,请检查'); } /** * [modelName 模型名称] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ get modelName() { return '数据'; } /** * [pkValidate 主键验证器] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ get pkValidate() { let that = this; let pkRules = {}; pkRules[that.modelPk] = that.ctx.rules.name('主键字段').required().number(); return pkRules; } /** * [selectValidate 数据查找验证器] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ get selectValidate() { let that = this; let selectRules = { page:that.ctx.rules.default(1).number(), limit:that.ctx.rules.default(20).number() }; let primary_key = that.modelPk; selectRules[primary_key] = that.ctx.rules.default(0).number(); return selectRules; } /** * [create 增] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ async create() { let that = this; try { let data = await that.ctx.validate(that.createValidate,await that.ctx.postParse()); let createBean = that.app.comoBean.instance(data); let result = await that.service.manager.create(createBean,that.useModel,`${that.modelName}添加失败,请稍候重试`); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false)); } catch(err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [delete 删] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ async delete() { let that = this; try { let data = await that.ctx.validate(that.deleteValidate || that.pkValidate,await that.ctx.anyParse()); let primary_key = that.modelPk; let options = {where:{}}; options.where[primary_key] = data[primary_key]; let deleteBean = that.app.comoBean.instance(data,options); let result = await that.service.manager.delete(deleteBean,that.useModel,`${that.modelName}删除失败,请稍候重试`); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false)); } catch(err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [update 改] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ async update() { let that = this; try { let data = await that.ctx.validate(that.updateValidate || that.pkValidate,await that.ctx.anyParse()); let primary_key = that.modelPk; let admin_id = that.service.manager.ActionAdminUserId(); data.admin_id = admin_id; let options = {where:{},fields:Object.keys(data)}; options.where[primary_key] = data[primary_key]; let updateBean = that.app.comoBean.instance(data,options); let result = await that.service.manager.update(updateBean,that.useModel,`${that.modelName}更新失败,请稍候重试`); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false)); } catch(err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } /** * [select 查] * @author szjcomo * @date 2020-11-02 * @return {[type]} [description] */ async select() { let that = this; try { let data = await that.ctx.validate(that.selectValidate,await that.ctx.anyParse()); let primary_key = that.modelPk; let options = {where:{}}; if(that.selectOptions instanceof Function) { options = await that.selectOptions(data); } else { if(data[primary_key]) { options.where[primary_key] = data[primary_key]; } else { options = {offset:(data.page - 1) * data.limit,limit:data.limit}; } } let selectBean = that.app.comoBean.instance(data,options); let count = (data[primary_key]?false:true); let result = await that.service.manager.select(selectBean,that.useModel,`${that.modelName}查询失败,请稍候重试`,count,count); return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false)); } catch(err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } }