pm_facade.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * Created by zhang on 2018/4/17.
  3. */
  4. let mongoose = require('mongoose');
  5. let _ = require("lodash");
  6. let feeRate_facade = require('../../fee_rates/facade/fee_rates_facade');
  7. const uuidV1 = require('uuid/v1');
  8. let projectModel = mongoose.model('projects');
  9. let feeRateModel = mongoose.model('fee_rates');
  10. let feeRateFileModel = mongoose.model('fee_rate_file');
  11. let unitPriceFileModel = mongoose.model("unit_price_file");
  12. let mixRatioModel = mongoose.model("mix_ratio");
  13. let unitPriceModel = mongoose.model("unit_price");
  14. import CounterModel from "../../glj/models/counter_model";
  15. module.exports={
  16. moveProject:moveProject
  17. };
  18. async function moveProject(data) {
  19. data = JSON.parse(data);
  20. let projectMap = data.projectMap,feeRateMap = data.feeRateMap,unitPriceMap = data.unitPriceMap;
  21. let projectTasks = [],feeRateTask=[],feeRateFileTask=[],unitPriceTask=[],unitPriceFileTask=[],mixRatioTask=[];
  22. let feeUniqMap=[],priceUniqMap={};//费率、单价文件唯一映射表当移动多个项目时,任务有可能出现重复的情况
  23. if(!_.isEmpty(feeRateMap)&&data.rootProjectID){//如果费率有修改
  24. let feeRates =await feeRate_facade.getFeeRatesByProject(data.rootProjectID);//取目标建设项目的所有费率文件,重名检查
  25. for(let projectID in feeRateMap){
  26. let rename = feeRateMap[projectID].name;
  27. let feeRateID = feeRateMap[projectID].query.ID;
  28. let checkFee = _.find(feeRates,{'name':rename});
  29. let newID = feeRateID;
  30. if(checkFee){//说明费率名字已存在,需要重命名
  31. rename = feeUniqMap[feeRateID]?feeUniqMap[feeRateID].name:rename + '(' + new Date().Format('MM-dd hh:mm:ss') + '移动)';
  32. projectMap[projectID]['update']['property.feeFile.name'] = rename;
  33. if(feeRateMap[projectID].action == 'move'){
  34. feeRateMap[projectID].update.name = rename;
  35. }
  36. }
  37. if(feeRateMap[projectID].action == 'copy'){
  38. newID = feeUniqMap[feeRateID]?feeUniqMap[feeRateID].ID:uuidV1();
  39. feeRateMap[projectID].name = rename;
  40. feeRateMap[projectID].ID = newID;
  41. projectMap[projectID]['update']['property.feeFile.id'] = newID;
  42. }
  43. if(!feeUniqMap[feeRateID]){
  44. await generateFeeRateTask(feeRateMap[projectID],feeRateTask,feeRateFileTask);
  45. feeUniqMap[feeRateID] = {name:rename,ID:newID};
  46. }
  47. }
  48. }
  49. if(!_.isEmpty(unitPriceMap)&&data.rootProjectID) {//如果单价文件有修改
  50. let unitPriceFiles = await unitPriceFileModel.find({root_project_id: data.rootProjectID, deleteInfo: null});
  51. for(let projectID in unitPriceMap){
  52. let checkUn = _.find(unitPriceFiles,{'name':unitPriceMap[projectID].name});
  53. let rename = unitPriceMap[projectID].name;
  54. let unitPriceID = unitPriceMap[projectID].query.id;
  55. let newID = unitPriceID;
  56. if(checkUn){//重名检查
  57. rename = priceUniqMap[unitPriceID]?priceUniqMap[unitPriceID].name:rename + '(' + new Date().Format('MM-dd hh:mm:ss') + '移动)';
  58. projectMap[projectID]['update']['property.unitPriceFile.name'] = rename;
  59. if(unitPriceMap[projectID].action =='move'){
  60. unitPriceMap[projectID].update.name = rename;
  61. }
  62. }
  63. if(unitPriceMap[projectID].action =='copy'){
  64. newID = priceUniqMap[unitPriceID]?priceUniqMap[unitPriceID].id: await getCounterID("unit_price_file");
  65. unitPriceMap[projectID].name = rename;
  66. unitPriceMap[projectID].id = newID;
  67. projectMap[projectID]['update']['property.unitPriceFile.id'] = newID;
  68. }
  69. if(!priceUniqMap[unitPriceID]){
  70. await generateUnitPriceTask(unitPriceMap[projectID],projectID,data.user_id,unitPriceTask,unitPriceFileTask,mixRatioTask);
  71. priceUniqMap[unitPriceID] = {name:rename,id:newID};
  72. }
  73. }
  74. }
  75. if(!_.isEmpty(projectMap)){
  76. for(let projectID in projectMap){
  77. projectTasks.push({updateOne:{filter : projectMap[projectID].query, update : projectMap[projectID].update}});
  78. }
  79. }
  80. projectTasks.length>0?await projectModel.bulkWrite(projectTasks):'';
  81. feeRateTask.length>0?await feeRateModel.bulkWrite(feeRateTask):'';
  82. feeRateFileTask.length>0?await feeRateFileModel.bulkWrite(feeRateFileTask):'';
  83. unitPriceFileTask.length>0?await unitPriceFileModel.bulkWrite(unitPriceFileTask):'';
  84. unitPriceTask.length>0?await insertMany(unitPriceTask,unitPriceModel):'';
  85. mixRatioTask.length>0?await insertMany(mixRatioTask,mixRatioModel):'';
  86. return projectMap;
  87. }
  88. async function generateFeeRateTask(data,feeRateTask,feeRateFileTask) {
  89. if(data.action == 'move'){
  90. let task = {
  91. updateOne:{
  92. filter : data.query,
  93. update : data.update
  94. }
  95. };
  96. feeRateFileTask.push(task);
  97. }
  98. if(data.action == 'copy'){//copy 费率的时候用insertOne方法
  99. let [feeRateFile,feeRate] =await feeRate_facade.getFeeRateByID(data.query.ID);
  100. let newFeeRateID = uuidV1();
  101. let newFeeRate = {
  102. ID:newFeeRateID,
  103. rates:feeRate.rates
  104. };
  105. let newFeeRateFile = {
  106. ID:data.ID,
  107. rootProjectID:data.rootProjectID,
  108. userID:feeRateFile.userID,
  109. name:data.name,
  110. libID:feeRateFile.libID,
  111. libName:feeRateFile.libName,
  112. feeRateID:newFeeRateID
  113. };
  114. feeRateTask.push({insertOne :{document:newFeeRate}});
  115. feeRateFileTask.push({insertOne :{document:newFeeRateFile}});
  116. }
  117. }
  118. async function generateUnitPriceTask(data,projectID,user_id,unitPriceTask,unitPriceFileTask,mixRatioTask){
  119. if(data.action == 'move'){
  120. let task = {
  121. updateOne:{
  122. filter : data.query,
  123. update : data.update
  124. }
  125. };
  126. unitPriceFileTask.push(task);
  127. }
  128. if(data.action == 'copy'){
  129. let newUnitFile = {
  130. id:data.id,
  131. name: data.name,
  132. project_id:projectID,
  133. user_id:user_id,
  134. root_project_id: data.rootProjectID
  135. };
  136. unitPriceFileTask.push({insertOne :{document:newUnitFile}});
  137. let mixRatios = await mixRatioModel.find({unit_price_file_id: data.query.id});
  138. mixRatios = JSON.stringify(mixRatios);
  139. mixRatios = JSON.parse(mixRatios);
  140. for(let m of mixRatios){
  141. delete m._id; // 删除原有id信息
  142. delete m.id;
  143. m.unit_price_file_id = data.id;
  144. m.id = await getCounterID('mix_ratio');
  145. mixRatioTask.push(m);
  146. }
  147. let unitPrices = await unitPriceModel.find({unit_price_file_id: data.query.id});//unit_price_file_id
  148. unitPrices = JSON.stringify(unitPrices);
  149. unitPrices = JSON.parse(unitPrices);
  150. for(let u of unitPrices){
  151. delete u._id; // 删除原有id信息
  152. delete u.id;
  153. u.unit_price_file_id = data.id;
  154. u.id = await getCounterID('unit_price');
  155. unitPriceTask.push(u);
  156. }
  157. }
  158. }
  159. async function getCounterID(collectionName){
  160. let counterModel = new CounterModel();
  161. return await counterModel.getId(collectionName);
  162. }
  163. async function insertMany(datas,model) {
  164. while (datas.length>1000){//因为mongoose限制了批量插入的条数为1000.所以超出限制后需要分批插入
  165. let newList = datas.splice(0,1000);//一次插入1000条
  166. model.insertMany(newList);
  167. }
  168. model.insertMany(datas);
  169. }