manager.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. const that = this;
  35. const 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. const that = this;
  56. const pkRules = {};
  57. pkRules[that.modelPk] = that.ctx.rules.name('主键字段')
  58. .required()
  59. .number();
  60. return pkRules;
  61. }
  62. /**
  63. * [selectValidate 数据查找验证器]
  64. * @author szjcomo
  65. * @date 2020-11-02
  66. * @return {[type]} [description]
  67. */
  68. get selectValidate() {
  69. const that = this;
  70. const selectRules = {
  71. page: that.ctx.rules.default(1)
  72. .number(),
  73. limit: that.ctx.rules.default(20)
  74. .number(),
  75. };
  76. const primary_key = that.modelPk;
  77. selectRules[primary_key] = that.ctx.rules.default(0)
  78. .number();
  79. return selectRules;
  80. }
  81. /**
  82. * [create 增]
  83. * @author szjcomo
  84. * @date 2020-11-02
  85. * @return {[type]} [description]
  86. */
  87. async create() {
  88. const that = this;
  89. try {
  90. const data = await that.ctx.validate(that.createValidate, await that.ctx.postParse());
  91. const createBean = that.app.comoBean.instance(data);
  92. const result = await that.service.manager.create(createBean, that.useModel, `${that.modelName}添加失败,请稍候重试`);
  93. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  94. } catch (err) {
  95. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  96. }
  97. }
  98. /**
  99. * [delete 删]
  100. * @author szjcomo
  101. * @date 2020-11-02
  102. * @return {[type]} [description]
  103. */
  104. async delete() {
  105. const that = this;
  106. try {
  107. const data = await that.ctx.validate(that.deleteValidate || that.pkValidate, await that.ctx.anyParse());
  108. const primary_key = that.modelPk;
  109. const options = { where: {} };
  110. options.where[primary_key] = data[primary_key];
  111. const deleteBean = that.app.comoBean.instance(data, options);
  112. const result = await that.service.manager.delete(deleteBean, that.useModel, `${that.modelName}删除失败,请稍候重试`);
  113. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  114. } catch (err) {
  115. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  116. }
  117. }
  118. /**
  119. * [update 改]
  120. * @author szjcomo
  121. * @date 2020-11-02
  122. * @return {[type]} [description]
  123. */
  124. async update() {
  125. const that = this;
  126. try {
  127. const data = await that.ctx.validate(that.updateValidate || that.pkValidate, await that.ctx.anyParse());
  128. const primary_key = that.modelPk;
  129. const admin_id = that.service.manager.ActionAdminUserId();
  130. data.admin_id = admin_id;
  131. const options = { where: {}, fields: Object.keys(data) };
  132. options.where[primary_key] = data[primary_key];
  133. const updateBean = that.app.comoBean.instance(data, options);
  134. const result = await that.service.manager.update(updateBean, that.useModel, `${that.modelName}更新失败,请稍候重试`);
  135. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  136. } catch (err) {
  137. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  138. }
  139. }
  140. /**
  141. * [select 查]
  142. * @author szjcomo
  143. * @date 2020-11-02
  144. * @return {[type]} [description]
  145. */
  146. async select() {
  147. const that = this;
  148. try {
  149. const data = await that.ctx.validate(that.selectValidate, await that.ctx.anyParse());
  150. const primary_key = that.modelPk;
  151. let options = { where: {} };
  152. if (that.selectOptions instanceof Function) {
  153. options = await that.selectOptions(data);
  154. } else {
  155. if (data[primary_key]) {
  156. options.where[primary_key] = data[primary_key];
  157. } else {
  158. options = { offset: (data.page - 1) * data.limit, limit: data.limit };
  159. }
  160. }
  161. const selectBean = that.app.comoBean.instance(data, options);
  162. const count = (!data[primary_key]);
  163. const result = await that.service.manager.select(selectBean, that.useModel, `${that.modelName}查询失败,请稍候重试`, count, count);
  164. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  165. } catch (err) {
  166. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  167. }
  168. }
  169. };