installation_facade.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Created by zhang on 2018/1/31.
  3. */
  4. import {installSectionModel,installFeeItemModel} from "../../complementary_ration_lib/models/schemas";
  5. import installationFeeModel from "../models/installation_fee";
  6. import engineeringModel from "../../users/models/schema/engineering_lib";
  7. const uuidV1 = require('uuid/v1');
  8. let consts = require('../../main/models/project_consts')
  9. module.exports={
  10. copyInstallationFeeFromLib:copyInstallationFeeFromLib,
  11. getData:getData
  12. };
  13. async function copyInstallationFeeFromLib(projectID,engineering_id) {
  14. //安装增加费用内嵌文档的方式
  15. let engineering = await engineeringModel.findById(engineering_id);
  16. let ration_lib = engineering.ration_lib;
  17. let installationFeeList = [];
  18. for(let rl of ration_lib){
  19. let installFeeItems = await installFeeItemModel.find({'rationRepId':rl.id});
  20. let installSections = await installSectionModel.find({'rationRepId':rl.id});
  21. let newInstallationFee = {
  22. libID:rl.id,
  23. libName:rl.name,
  24. projectID:projectID
  25. };
  26. newInstallationFee.ID = uuidV1();
  27. let create = false;
  28. if(installFeeItems && installFeeItems.length > 0) {//费用项
  29. create = true;
  30. let tem_installFeeItem = [];
  31. for(let ifee of installFeeItems){
  32. let tem_fee ={
  33. feeItem:ifee.feeItem,
  34. feeType:ifee.feeType,
  35. position:ifee.position,
  36. ID:ifee.ID
  37. };
  38. tem_installFeeItem.push(tem_fee);
  39. }
  40. newInstallationFee.installFeeItem = tem_installFeeItem;
  41. }
  42. if(installSections && installSections.length > 0){//章节项
  43. create = true;
  44. let tem_installSections = [];
  45. let tem_feeRules = [];
  46. for(let isect of installSections){
  47. let tem_sec={
  48. ID:isect.ID,
  49. feeItemId:isect.feeItemId,
  50. name:isect.name
  51. };
  52. if(isect.feeRule && isect.feeRule.length > 0){//规则项
  53. tem_sec.feeRuleId = isect.feeRule[0].ID; //选中第一个
  54. for(let ifeeR of isect.feeRule){
  55. let tem_feeRule = {
  56. ID: ifeeR.ID,
  57. code: ifeeR.code,
  58. rule: ifeeR.rule,
  59. base: ifeeR.base,
  60. feeRate: ifeeR.feeRate,
  61. labour: ifeeR.labour,
  62. material: ifeeR.material,
  63. machine: ifeeR.machine
  64. };
  65. tem_feeRule.sectionId = isect.ID;
  66. tem_feeRule.feeItemId = isect.feeItemId;
  67. tem_feeRules.push(tem_feeRule);
  68. }
  69. }
  70. tem_installSections.push(tem_sec);
  71. }
  72. newInstallationFee.installSection = tem_installSections;
  73. newInstallationFee.feeRule = tem_feeRules;
  74. }
  75. if(create==true){
  76. installationFeeList.push(newInstallationFee);
  77. }
  78. }
  79. console.log(installationFeeList);
  80. if(installationFeeList.length > 0){
  81. await installationFeeModel.insertMany(installationFeeList);
  82. }
  83. }
  84. function getData(projectID, callback) {
  85. installationFeeModel.find({'projectID': projectID}, (err, datas) => {
  86. if (err) {
  87. callback(1, '', null);
  88. } else {
  89. callback(0, consts.projectConst.INSTALLATION_FEE, datas);
  90. }
  91. })
  92. }