manager.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use strict';
  2. //基类控制器
  3. const BaseController = require('../base.js');
  4. /**
  5. * [exports 继承基类控制骂]
  6. * @type {[type]}
  7. */
  8. module.exports = class ManagerController extends BaseController {
  9. /**
  10. * [useModel 当前使用的模型]
  11. * @author szjcomo
  12. * @date 2020-11-02
  13. * @return {[type]} [description]
  14. */
  15. get useModel() {
  16. throw new Error('未实现使用的模型定义,请检查');
  17. }
  18. /**
  19. * [createValidate 增的验证器]
  20. * @author szjcomo
  21. * @date 2020-11-02
  22. * @return {[type]} [description]
  23. */
  24. get createValidate() {
  25. throw new Error('未实现数据添加的验证器,请检查');
  26. }
  27. /**
  28. * [useModelPk 模型主键]
  29. * @author szjcomo
  30. * @date 2020-11-02
  31. * @return {[type]} [description]
  32. */
  33. get modelPk() {
  34. let that = this;
  35. let keys = Object.keys((that.useModel).primaryKeys);
  36. if(keys.length > 0) return keys[0];
  37. throw new Error('未找到模型主键,请检查');
  38. }
  39. /**
  40. * [modelName 模型名称]
  41. * @author szjcomo
  42. * @date 2020-11-02
  43. * @return {[type]} [description]
  44. */
  45. get modelName() {
  46. return '数据';
  47. }
  48. /**
  49. * [pkValidate 主键验证器]
  50. * @author szjcomo
  51. * @date 2020-11-02
  52. * @return {[type]} [description]
  53. */
  54. get pkValidate() {
  55. let that = this;
  56. let pkRules = {};
  57. pkRules[that.modelPk] = that.ctx.rules.name('主键字段').required().number();
  58. return pkRules;
  59. }
  60. /**
  61. * [selectValidate 数据查找验证器]
  62. * @author szjcomo
  63. * @date 2020-11-02
  64. * @return {[type]} [description]
  65. */
  66. get selectValidate() {
  67. let that = this;
  68. let selectRules = {
  69. page:that.ctx.rules.default(1).number(),
  70. limit:that.ctx.rules.default(20).number()
  71. };
  72. let primary_key = that.modelPk;
  73. selectRules[primary_key] = that.ctx.rules.default(0).number();
  74. return selectRules;
  75. }
  76. /**
  77. * [create 增]
  78. * @author szjcomo
  79. * @date 2020-11-02
  80. * @return {[type]} [description]
  81. */
  82. async create() {
  83. let that = this;
  84. try {
  85. let data = await that.ctx.validate(that.createValidate,await that.ctx.postParse());
  86. let createBean = that.app.comoBean.instance(data);
  87. let result = await that.service.manager.create(createBean,that.useModel,`${that.modelName}添加失败,请稍候重试`);
  88. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  89. } catch(err) {
  90. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  91. }
  92. }
  93. /**
  94. * [delete 删]
  95. * @author szjcomo
  96. * @date 2020-11-02
  97. * @return {[type]} [description]
  98. */
  99. async delete() {
  100. let that = this;
  101. try {
  102. let data = await that.ctx.validate(that.deleteValidate || that.pkValidate,await that.ctx.anyParse());
  103. let primary_key = that.modelPk;
  104. let options = {where:{}};
  105. options.where[primary_key] = data[primary_key];
  106. let deleteBean = that.app.comoBean.instance(data,options);
  107. let result = await that.service.manager.delete(deleteBean,that.useModel,`${that.modelName}删除失败,请稍候重试`);
  108. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  109. } catch(err) {
  110. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  111. }
  112. }
  113. /**
  114. * [update 改]
  115. * @author szjcomo
  116. * @date 2020-11-02
  117. * @return {[type]} [description]
  118. */
  119. async update() {
  120. let that = this;
  121. try {
  122. let data = await that.ctx.validate(that.updateValidate || that.pkValidate,await that.ctx.anyParse());
  123. let primary_key = that.modelPk;
  124. let admin_id = that.service.manager.ActionAdminUserId();
  125. data.admin_id = admin_id;
  126. let options = {where:{},fields:Object.keys(data)};
  127. options.where[primary_key] = data[primary_key];
  128. let updateBean = that.app.comoBean.instance(data,options);
  129. let result = await that.service.manager.update(updateBean,that.useModel,`${that.modelName}更新失败,请稍候重试`);
  130. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  131. } catch(err) {
  132. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  133. }
  134. }
  135. /**
  136. * [select 查]
  137. * @author szjcomo
  138. * @date 2020-11-02
  139. * @return {[type]} [description]
  140. */
  141. async select() {
  142. let that = this;
  143. try {
  144. let data = await that.ctx.validate(that.selectValidate,await that.ctx.anyParse());
  145. let primary_key = that.modelPk;
  146. let options = {where:{}};
  147. if(that.selectOptions instanceof Function) {
  148. options = await that.selectOptions(data);
  149. } else {
  150. if(data[primary_key]) {
  151. options.where[primary_key] = data[primary_key];
  152. } else {
  153. options = {offset:(data.page - 1) * data.limit,limit:data.limit};
  154. }
  155. }
  156. let selectBean = that.app.comoBean.instance(data,options);
  157. let count = (data[primary_key]?false:true);
  158. let result = await that.service.manager.select(selectBean,that.useModel,`${that.modelName}查询失败,请稍候重试`,count,count);
  159. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS',result,false));
  160. } catch(err) {
  161. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  162. }
  163. }
  164. }