glj_facade.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Created by zhang on 2019/1/2.
  3. */
  4. module.exports={ //先导出后require可以解决循环引用问题
  5. changeUnitFile:changeUnitFile,
  6. updateEvaluateMaterial:updateEvaluateMaterial
  7. };
  8. const mongoose = require('mongoose');
  9. const ProjectModel = require('../../pm/models/project_model').project;
  10. const UnitPriceFileModel = require("../models/unit_price_file_model");
  11. const UnitPriceModel = require("../models/unit_price_model");
  12. const MixRatioModel = require("../models/mix_ratio_model");
  13. let evaluateListModel = mongoose.model("evaluate_list");
  14. let bidEvaluationMode = mongoose.model("bid_evaluation_list");
  15. let contractorListModel = mongoose.model("contractor_list");
  16. let projectGLJModel = mongoose.model("glj_list");
  17. let unitFileMode = mongoose.model("unit_price");
  18. async function changeUnitFile(projectData,unitFile,type,userID) {
  19. let projectId = projectData.projectID;
  20. let changeUnitPriceId = unitFile.id;
  21. let newName = unitFile.name;
  22. type = parseInt(type);
  23. let currentUnitPriceId = await ProjectModel.getUnitPriceFileId(projectId);
  24. let unitPriceFileModel = new UnitPriceFileModel();
  25. let insertData = null;
  26. if (type > 0) {
  27. let currentUnitPrice = await unitPriceFileModel.findDataByCondition({id: changeUnitPriceId});
  28. if (currentUnitPrice === null) {
  29. throw '不存在对应单价文件';
  30. }
  31. // 获取当前项目的rootProjectId
  32. let projectData = await ProjectModel.getProject(projectId);
  33. let rootProjectId = projectData.property.rootProjectID !== undefined ? projectData.property.rootProjectID : 0;
  34. insertData = JSON.parse(JSON.stringify(currentUnitPrice));
  35. insertData.root_project_id = rootProjectId;
  36. newName?insertData.name = newName:'';
  37. insertData.user_id = userID;
  38. delete insertData._id;
  39. delete insertData.ID;
  40. }
  41. // 获取即将更改的单价文件信息
  42. let targetUnitPriceFile = type === 0 ? await unitPriceFileModel.findDataByCondition({id: changeUnitPriceId}) :
  43. await unitPriceFileModel.add(insertData);
  44. if (targetUnitPriceFile === null) {
  45. throw '没有找到对应的单价文件';
  46. }
  47. // 查找对应单价文件的项目工料机数据
  48. let unitPriceModel = new UnitPriceModel();
  49. if(type ===1){//从其它项目复制,则先复制一份数据。
  50. let needCopyList = await unitPriceModel.findDataByCondition({unit_price_file_id: changeUnitPriceId}, null, false);
  51. if(needCopyList){
  52. // 过滤mongoose格式
  53. needCopyList = JSON.stringify(needCopyList);
  54. needCopyList = JSON.parse(needCopyList);
  55. let copyList = [];
  56. for(let n of needCopyList){
  57. delete n._id; // 删除原有id信息
  58. delete n.id;
  59. n.unit_price_file_id = targetUnitPriceFile.id;
  60. copyList.push(n);
  61. }
  62. copyList.length>0 ? await unitPriceModel.add(copyList):'';
  63. //也要复制一份组成物信息和材料计算信息
  64. let mixRatioModel = new MixRatioModel();
  65. await mixRatioModel.copyNotExist(changeUnitPriceId, targetUnitPriceFile.id,{},true);//复制组成物
  66. }
  67. }
  68. let copyResult = await unitPriceModel.copyNotExist(currentUnitPriceId, targetUnitPriceFile.id,projectId);
  69. // 复制成功后更改project数据
  70. if (!copyResult) {
  71. throw '复制数据失败';
  72. }
  73. let changeUnitPriceFileInfo = {
  74. id: targetUnitPriceFile.id,
  75. name: targetUnitPriceFile.name
  76. };
  77. let result = ProjectModel.changeUnitPriceFileInfo(projectId, changeUnitPriceFileInfo);
  78. if (!result) {
  79. throw '切换单价文件失败!';
  80. }
  81. return changeUnitPriceFileInfo;
  82. }
  83. async function updateEvaluateMaterial(data) {
  84. let modelMap = {
  85. "glj_list":projectGLJModel,
  86. "evaluate_list":evaluateListModel,
  87. "unit_price":unitFileMode,
  88. "bid_evaluation_list":bidEvaluationMode,
  89. "contractor_list":contractorListModel
  90. };
  91. for(let t of data.tasks){
  92. let model = modelMap[t.type];
  93. switch (t.action) {
  94. case "update":
  95. let query = {};
  96. if(t.hasOwnProperty('ID')) query['ID'] = t.ID;
  97. if(t.hasOwnProperty('id')) query['id'] = t.id;
  98. await model.update(query,t.doc);
  99. break;
  100. case "add":
  101. await model.create(t.doc);
  102. break;
  103. case "delete":
  104. await model.deleteOne({ID:t.ID});
  105. break;
  106. }
  107. }
  108. }