pm_facade.js 8.1 KB

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