ManagerDAO.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. var path = require("path");
  2. daoModule = require("./DAO");
  3. databaseModule = require(path.join(process.cwd(),"modules/database"));
  4. /**
  5. * 创建管理员
  6. *
  7. * @param {[type]} obj 管理员信息
  8. * @param {Function} cb 回调函数
  9. */
  10. module.exports.create = function(obj,cb) {
  11. daoModule.create("ManagerModel",obj,cb);
  12. }
  13. /**
  14. * 获取管理员列表
  15. *
  16. * @param {[type]} conditions 查询条件
  17. * @param {Function} cb 回调函数
  18. */
  19. module.exports.list = function(conditions,cb) {
  20. daoModule.list("ManagerModel",conditions,function(err,models) {
  21. if(err) return cb(err,null);
  22. cb(null,models);
  23. });
  24. }
  25. /**
  26. * 通过查询条件获取管理员对象
  27. *
  28. * @param {[type]} conditions 条件
  29. * @param {Function} cb 回调函数
  30. */
  31. module.exports.findOne = function(conditions,cb) {
  32. daoModule.findOne("ManagerModel",conditions,cb);
  33. }
  34. /**
  35. * 通过关键词查询用户
  36. *
  37. * @param {[type]} key 关键词
  38. * @param {[type]} offset
  39. * @param {[type]} limit
  40. * @param {Function} cb 回调函数
  41. */
  42. module.exports.findByKey = function(key,offset,limit,cb) {
  43. db = databaseModule.getDatabase();
  44. sql = "SELECT * FROM sp_manager as mgr LEFT JOIN sp_role as role ON mgr.role_id = role.role_id";
  45. if(key) {
  46. sql += " WHERE mg_name LIKE ? LIMIT ?,?";
  47. database.driver.execQuery(
  48. sql
  49. ,["%" + key + "%",offset,limit],function(err,managers){
  50. if(err) return cb("查询执行出错");
  51. cb(null,managers);
  52. });
  53. } else {
  54. sql += " LIMIT ?,? ";
  55. database.driver.execQuery(sql,[offset,limit],function(err,managers){
  56. if(err) return cb("查询执行出错");
  57. cb(null,managers);
  58. });
  59. }
  60. }
  61. /**
  62. * 判断是否存在管理员
  63. *
  64. * @param {[type]} username 用户名
  65. * @param {Function} cb 回调函数
  66. *
  67. */
  68. module.exports.exists = function(username,cb) {
  69. var db = databaseModule.getDatabase();
  70. var Model = db.models.ManagerModel;
  71. Model.exists({"mg_name":username},function(err,isExists){
  72. if(err) return cb("查询失败");
  73. cb(null,isExists);
  74. });
  75. }
  76. /**
  77. * 模糊查询用户数量
  78. *
  79. * @param {[type]} key 关键词
  80. * @param {Function} cb 回调函数
  81. */
  82. module.exports.countByKey = function(key,cb) {
  83. db = databaseModule.getDatabase();
  84. sql = "SELECT count(*) as count FROM sp_manager";
  85. if(key) {
  86. sql += " WHERE mg_name LIKE ?";
  87. database.driver.execQuery(
  88. sql
  89. ,["%" + key + "%"],function(err,result){
  90. if(err) return cb("查询执行出错");
  91. cb(null,result[0]["count"]);
  92. });
  93. } else {
  94. database.driver.execQuery(sql,function(err,result){
  95. if(err) return cb("查询执行出错");
  96. cb(null,result[0]["count"]);
  97. });
  98. }
  99. }
  100. /**
  101. * 通过ID获取管理员对象数据
  102. *
  103. * @param {[type]} id 管理员主键ID
  104. * @param {Function} cb 回调函数
  105. */
  106. module.exports.show = function(id,cb) {
  107. daoModule.show("ManagerModel",id,cb);
  108. }
  109. /**
  110. * 更新管理员信息
  111. *
  112. * @param {[type]} obj 管理员对象
  113. * @param {Function} cb 回调函数
  114. */
  115. module.exports.update = function(obj,cb) {
  116. daoModule.update("ManagerModel",obj.mg_id,obj,cb);
  117. }
  118. /**
  119. * 删除管理员对象数据
  120. *
  121. * @param {[type]} id 主键ID
  122. * @param {Function} cb 回调函数
  123. */
  124. module.exports.destroy = function(id,cb) {
  125. daoModule.destroy("ManagerModel",id,function(err){
  126. if(err) return cb(err);
  127. return cb(null);
  128. });
  129. }
  130. /**
  131. * 保存管理员信息
  132. *
  133. * @param {[type]} obj 管理员对象
  134. * @param {Function} cb 回调函数
  135. */
  136. module.exports.save = function(obj,cb) {
  137. daoModule.show(obj.mg_id,function(err,oldObj){
  138. if(err) {
  139. daoModule.create("ManagerModel",obj,cb);
  140. } else {
  141. daoModule.update("ManagerModel",obj.mg_id,obj,cb);
  142. }
  143. })
  144. }
  145. /**
  146. * 获取管理员数量
  147. *
  148. * @param {Function} cb 回调函数
  149. */
  150. module.exports.count = function(cb) {
  151. daoModule("ManagerModel",cb);
  152. }