CategoryService.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. var _ = require('lodash');
  2. var path = require("path");
  3. var dao = require(path.join(process.cwd(),"dao/DAO"));
  4. /**
  5. * 判断是否删除
  6. *
  7. * @param {[type]} keyCategories 所有数据
  8. * @param {[type]} cat [description]
  9. * @return {Boolean} [description]
  10. */
  11. function isDelete(keyCategories,cat) {
  12. if(cat.cat_pid == 0) {
  13. return cat.cat_deleted;
  14. } else if(cat.cat_deleted) {
  15. return true;
  16. } else {
  17. parentCat = keyCategories[cat.cat_pid];
  18. if(!parentCat) return true;
  19. return isDelete(keyCategories,parentCat);
  20. }
  21. }
  22. /**
  23. * 获取树状结果
  24. * @param {[type]} keyCategories [description]
  25. * @return {[type]} [description]
  26. */
  27. function getTreeResult(keyCategories,categories,type) {
  28. var result = [];
  29. for(idx in categories) {
  30. var cat = categories[idx];
  31. // 判断是否被删除
  32. if(isDelete(keyCategories,cat)) continue;
  33. if(cat.cat_pid == 0) {
  34. result.push(cat);
  35. } else {
  36. if(cat.cat_level >= type) continue;
  37. var parantCat = keyCategories[cat.cat_pid];
  38. if(!parantCat) continue;
  39. if(!parantCat.children) {
  40. parantCat["children"] = [];
  41. }
  42. parantCat.children.push(cat);
  43. }
  44. }
  45. return result;
  46. }
  47. /**
  48. * 获取所有分类
  49. *
  50. * @param {[type]} type 描述显示层级
  51. * @param {Function} cb 回调函数
  52. */
  53. module.exports.getAllCategories = function(type,conditions,cb) {
  54. dao.list("CategoryModel",{"cat_deleted":false},function(err,categories) {
  55. var keyCategories = _.keyBy(categories,'cat_id');
  56. if(!type) type = 3;
  57. result = getTreeResult(keyCategories,categories,type);
  58. if(conditions) {
  59. count = result.length;
  60. pagesize = parseInt(conditions.pagesize);
  61. pagenum = parseInt(conditions.pagenum) - 1;
  62. result = _.take(_.drop(result,pagenum * pagesize),pagesize)
  63. var resultDta = {};
  64. resultDta["total"] = count;
  65. resultDta["pagenum"] = pagenum;
  66. resultDta["pagesize"] = pagesize;
  67. resultDta["result"] = result;
  68. return cb(null,resultDta);
  69. }
  70. cb(null,result);
  71. });
  72. }
  73. /**
  74. * 获取具体分类对象
  75. *
  76. * @param {[type]} id 分类ID
  77. * @param {Function} cb 回调函数
  78. */
  79. module.exports.getCategoryById = function(id,cb) {
  80. dao.show("CategoryModel",id,function(err,category){
  81. if(err) return cb("获取分类对象失败");
  82. cb(null,category);
  83. })
  84. }
  85. /**
  86. * 添加分类
  87. *
  88. * @param {[type]} cat 分类数据
  89. * {
  90. * cat_pid => 父类ID(如果是根类就赋值为0),
  91. * cat_name => 分类名称,
  92. * cat_level => 层级 (顶层为 0)
  93. * }
  94. *
  95. * @param {Function} cb 回调函数
  96. */
  97. module.exports.addCategory = function(cat,cb) {
  98. dao.create("CategoryModel",{"cat_pid":cat.cat_pid,"cat_name":cat.cat_name,"cat_level":cat.cat_level},function(err,newCat) {
  99. if(err) return cb("创建分类失败");
  100. cb(null,newCat);
  101. });
  102. }
  103. /**
  104. * 更新分类
  105. *
  106. * @param {[type]} cat_id 分类ID
  107. * @param {[type]} newName 新的名称
  108. * @param {Function} cb 回调函数
  109. */
  110. module.exports.updateCategory = function(cat_id,newName,cb) {
  111. dao.update("CategoryModel",cat_id,{"cat_name":newName},function(err,newCat) {
  112. if(err) return cb("更新失败");
  113. cb(null,newCat);
  114. });
  115. }
  116. /**
  117. * 删除分类
  118. *
  119. * @param {[type]} cat_id 分类ID
  120. * @param {Function} cb 回调函数
  121. */
  122. module.exports.deleteCategory = function(cat_id,cb) {
  123. dao.update("CategoryModel",cat_id,{"cat_deleted":true},function(err,newCat){
  124. if(err) return cb("删除失败");
  125. cb("删除成功");
  126. });
  127. }