glj_facade.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Created by zhang on 2019/1/2.
  3. */
  4. module.exports={ //先导出后require可以解决循环引用问题
  5. changeUnitFile:changeUnitFile
  6. };
  7. const ProjectModel = require('../../pm/models/project_model').project;
  8. import UnitPriceFileModel from "../models/unit_price_file_model";
  9. import UnitPriceModel from "../models/unit_price_model";
  10. async function changeUnitFile(projectData,unitFile,type,userID) {
  11. let projectId = projectData.projectID;
  12. let changeUnitPriceId = unitFile.id;
  13. let newName = unitFile.name;
  14. type = parseInt(type);
  15. let currentUnitPriceId = await ProjectModel.getUnitPriceFileId(projectId);
  16. let unitPriceFileModel = new UnitPriceFileModel();
  17. let insertData = null;
  18. if (type > 0) {
  19. let currentUnitPrice = await unitPriceFileModel.findDataByCondition({id: changeUnitPriceId});
  20. if (currentUnitPrice === null) {
  21. throw '不存在对应单价文件';
  22. }
  23. // 获取当前项目的rootProjectId
  24. let projectData = await ProjectModel.getProject(projectId);
  25. let rootProjectId = projectData.property.rootProjectID !== undefined ? projectData.property.rootProjectID : 0;
  26. insertData = JSON.parse(JSON.stringify(currentUnitPrice));
  27. insertData.root_project_id = rootProjectId;
  28. newName?insertData.name = newName:'';
  29. insertData.user_id = userID;
  30. delete insertData._id;
  31. delete insertData.ID;
  32. }
  33. // 获取即将更改的单价文件信息
  34. let targetUnitPriceFile = type === 0 ? await unitPriceFileModel.findDataByCondition({id: changeUnitPriceId}) :
  35. await unitPriceFileModel.add(insertData);
  36. if (targetUnitPriceFile === null) {
  37. throw '没有找到对应的单价文件';
  38. }
  39. // 查找对应单价文件的项目工料机数据
  40. let unitPriceModel = new UnitPriceModel();
  41. if(type ===1){//从其它项目复制,则先复制一份数据。
  42. let needCopyList = await unitPriceModel.findDataByCondition({unit_price_file_id: changeUnitPriceId}, null, false);
  43. if(needCopyList){
  44. // 过滤mongoose格式
  45. needCopyList = JSON.stringify(needCopyList);
  46. needCopyList = JSON.parse(needCopyList);
  47. let copyList = [];
  48. for(let n of needCopyList){
  49. delete n._id; // 删除原有id信息
  50. delete n.id;
  51. n.unit_price_file_id = targetUnitPriceFile.id;
  52. copyList.push(n);
  53. }
  54. copyList.length>0 ? await unitPriceModel.add(copyList):'';
  55. }
  56. }
  57. let copyResult = await unitPriceModel.copyNotExist(currentUnitPriceId, targetUnitPriceFile.id,projectId);
  58. // 复制成功后更改project数据
  59. if (!copyResult) {
  60. throw '复制数据失败';
  61. }
  62. let changeUnitPriceFileInfo = {
  63. id: targetUnitPriceFile.id,
  64. name: targetUnitPriceFile.name
  65. };
  66. let result = ProjectModel.changeUnitPriceFileInfo(projectId, changeUnitPriceFileInfo);
  67. if (!result) {
  68. throw '切换单价文件失败!';
  69. }
  70. return changeUnitPriceFileInfo;
  71. }