main_col_facade.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * Created by zhang on 2018/7/14.
  3. */
  4. import mongoose from "mongoose";
  5. const uuidV1 = require('uuid/v1');
  6. let moment = require("moment");
  7. let compilationModel = mongoose.model("compilation");
  8. let mainColLibModel = mongoose.model("std_main_col_lib");
  9. let mainCol={
  10. addLib:addLib,
  11. getAllLibs:getAllLibs,
  12. deleteLibByID:deleteLibByID,
  13. getLibByID:getLibByID,
  14. saveLib:saveLib,
  15. getColLibsByCompilationID:getColLibsByCompilationID
  16. };
  17. async function saveLib(param) {
  18. if(param.data.main_tree_col){
  19. param.data.main_tree_col = JSON.parse(param.data.main_tree_col);
  20. }
  21. return await mainColLibModel.findOneAndUpdate(param.query,param.data,{new:true});
  22. }
  23. async function getLibByID(ID) {
  24. return await mainColLibModel.findOne({ID:ID});
  25. }
  26. async function getColLibsByCompilationID(compilationID) {
  27. return await mainColLibModel.find({compilationId:compilationID},['ID','name']);
  28. }
  29. async function deleteLibByID(ID){
  30. return mainColLibModel.deleteOne({ID:ID});
  31. }
  32. async function getAllLibs() {
  33. return await mainColLibModel.find();
  34. }
  35. async function addLib(data){
  36. let now = new Date().getTime();
  37. let dateStr = moment(now).format('YYYY-MM-DD HH:mm:ss');
  38. //取编办信息
  39. let compilation = await compilationModel.findOne({_id:data.compilationId});
  40. if(compilation){
  41. let newLib = {
  42. creator: data.userAccount,
  43. createDate: now,
  44. recentOpr: [{operator: data.userAccount, operateDate: dateStr}],
  45. name: data.name,
  46. compilationId: data.compilationId,
  47. compilationName: compilation.name,
  48. deleted: false
  49. };
  50. newLib.ID = uuidV1();
  51. return await mainColLibModel.create(newLib);
  52. }else {
  53. throw new Error("没有找到该编办!");
  54. }
  55. }
  56. export default mainCol