AttributeService.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. var _ = require('lodash');
  2. var path = require("path");
  3. var dao = require(path.join(process.cwd(),"dao/DAO"));
  4. var attributeDao = require(path.join(process.cwd(),"dao/AttributeDAO"));
  5. /**
  6. * 获取属性列表
  7. *
  8. * @param {[type]} cat_id 分类ID
  9. * @param {[type]} sel 类型 // only:输入框(唯一) many:后台下拉列表/前台单选框
  10. * @param {Function} cb 回调函数
  11. */
  12. module.exports.getAttributes = function(cat_id,sel,cb) {
  13. attributeDao.list(cat_id,sel,function(err,attributes) {
  14. if(err) return cb("获取失败");
  15. cb(null,attributes);
  16. });
  17. }
  18. /**
  19. * 创建参数
  20. *
  21. * @param {[type]} info 参数信息
  22. * @param {Function} cb 回调函数
  23. */
  24. module.exports.createAttribute = function(info,cb) {
  25. dao.create("AttributeModel",info,function(err,attribute) {
  26. if(err) return cb("创建失败");
  27. cb(null,attribute);
  28. });
  29. }
  30. /**
  31. * 更新参数
  32. *
  33. * @param {[type]} catId 分类ID
  34. * @param {[type]} attrId 属性ID
  35. * @param {[type]} info 更新内容
  36. * @param {Function} cb 回调函数
  37. */
  38. module.exports.updateAttribute = function(attrId,info,cb) {
  39. dao.update("AttributeModel",attrId,info,function(err,newAttr) {
  40. if(err) return cb(err);
  41. cb(null,_.omit(newAttr,"delete_time"));
  42. });
  43. }
  44. /**
  45. * 删除参数
  46. *
  47. * @param {[type]} attrId 参数ID
  48. * @param {Function} cb 回调函数
  49. */
  50. module.exports.deleteAttribute = function(attrId,cb) {
  51. dao.update("AttributeModel",attrId,{"delete_time":parseInt((Date.now()/1000))},function(err,newAttr){
  52. console.log(newAttr);
  53. if(err) return cb("删除失败");
  54. cb(null,newAttr);
  55. });
  56. }
  57. module.exports.attributeById = function(attrId,cb) {
  58. dao.show("AttributeModel",attrId,function(err,attr) {
  59. if(err) return cb(err);
  60. cb(null,_.omit(attr,"delete_time"));
  61. });
  62. }