material_replace_facade.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * Created by zhang on 2018/8/22.
  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 materialLibModel = mongoose.model("std_material_replace_lib");
  9. let StdBillsLib = mongoose.model('std_bills_lib_list');
  10. let materialReplaceLib = {
  11. findByCondition:async function(conditions,options,single=true){
  12. if(single == true){
  13. return await materialLibModel.findOne(conditions,options);
  14. }else {
  15. return await materialLibModel.find(conditions,options);
  16. }
  17. },
  18. addLib : async function (data){
  19. let now = new Date().getTime();
  20. let dateStr = moment(now).format('YYYY-MM-DD HH:mm:ss');
  21. //取编办信息
  22. let compilation = await compilationModel.findOne({_id:data.compilationId});
  23. //取清单规则信息
  24. let billLib = await StdBillsLib.findOne({billsLibId:data.billsLibId});
  25. if(compilation && billLib){
  26. let newLib = {
  27. creator: data.userAccount,
  28. createDate: now,
  29. recentOpr: [{operator: data.userAccount, operateDate: dateStr}],
  30. name: data.name,
  31. compilationId: data.compilationId,
  32. compilationName: compilation.name,
  33. billsLibId:billLib.billsLibId,
  34. billsLibName:billLib.billsLibName,
  35. deleted: false
  36. };
  37. newLib.ID = uuidV1();
  38. return await materialLibModel.create(newLib);
  39. }else {
  40. throw new Error("编办或清单规则有误!");
  41. }
  42. },
  43. saveLib:async function(param) {
  44. return await materialLibModel.findOneAndUpdate(param.query,param.data,{new:true});
  45. },
  46. deleteLibByID:async function(ID){
  47. return materialLibModel.deleteOne({ID:ID});
  48. }
  49. }
  50. export default materialReplaceLib