project_facade.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * Created by zhang on 2018/1/26.
  3. */
  4. let mongoose = require('mongoose');
  5. let projectsModel = mongoose.model('projects');
  6. let async_n = require("async");
  7. let _ = require('lodash');
  8. let ration_model = require('../models/ration');
  9. let bill_model = require('../models/bills');
  10. let consts = require('../models/project_consts');
  11. let projectConsts = consts.projectConst;
  12. let ration_glj_model = mongoose.model('ration_glj');
  13. let ration_glj_facade = require("../../ration_glj/facade/ration_glj_facade");
  14. const uuidV1 = require('uuid/v1');
  15. module.exports = {
  16. markUpdateProject:markUpdateProject,
  17. removeProjectMark:removeProjectMark,
  18. updateNodes:updateNodes,
  19. calcInstallationFee:calcInstallationFee
  20. };
  21. async function calcInstallationFee(data) {
  22. let result={};
  23. let billTasks = generateTasks(data.bills,data.useID);
  24. let rationTasks = generateTasks(data.ration,data.useID);
  25. if(billTasks.length>0){
  26. await bill_model.model.bulkWrite(billTasks);
  27. }
  28. console.log(rationTasks);
  29. if(rationTasks.length>0){
  30. await ration_model.model.bulkWrite(rationTasks);
  31. }
  32. //如果删除定额,需要删除对应的工料机
  33. if(data.ration.delete.length>0){
  34. let rationIDS = _.map(data.ration.delete,'ID');
  35. await ration_glj_model.deleteMany({projectID: data.ration.delete[0].projectID, rationID: {"$in": rationIDS}});//删除定额工料机
  36. }
  37. let rationGLJTasks = [];
  38. let updateList = [];
  39. if(data.ration.update.length>0){//如果有需要更新的定额工料机
  40. for(let ur of data.ration.update){
  41. for(let g of ur.glj){
  42. let gTasks = {
  43. updateOne:{
  44. filter:{
  45. ID:g.ID,
  46. projectID:g.projectID
  47. },
  48. update :{
  49. quantity:g.quantity,
  50. rationItemQuantity:g.rationItemQuantity
  51. }
  52. }
  53. };
  54. rationGLJTasks.push(gTasks);
  55. updateList.push(g);
  56. }
  57. }
  58. }
  59. if(rationGLJTasks.length>0){
  60. await ration_glj_model.bulkWrite(rationGLJTasks);
  61. }
  62. let newGljList = [];
  63. if(data.ration.add.length>0){//新增的安装子目要增加对应的工料机
  64. for(let nr of data.ration.add){
  65. for(let tkey in nr.glj){
  66. newGljList.push(await addInstallationGLJ(nr.glj[tkey]));
  67. }
  68. }
  69. }
  70. if(newGljList.length>0){
  71. await ration_glj_model.insertMany(newGljList);
  72. }
  73. result.update = updateList;
  74. result.add = newGljList;
  75. return result;
  76. }
  77. async function addInstallationGLJ(glj) {
  78. glj.ID = uuidV1();
  79. let info = await ration_glj_facade.getInfoFromProjectGLJ(glj);
  80. let newRecode = ration_glj_facade.createNewRecord(info);
  81. return newRecode;
  82. }
  83. function generateTasks(data,userID) {
  84. let tasks=[];
  85. let deleteInfo={deleted: true, deleteDateTime: new Date(), deleteBy: userID};
  86. if(data.delete && data.delete.length > 0){
  87. for(let bd of data.delete){
  88. let task={
  89. updateOne:{
  90. filter:{
  91. ID:bd.ID,
  92. projectID:bd.projectID
  93. },
  94. update :{
  95. deleteInfo:deleteInfo
  96. }
  97. }
  98. };
  99. tasks.push(task);
  100. }
  101. }
  102. if(data.add && data.add.length > 0){
  103. for(let n_data of data.add){
  104. let task = {
  105. insertOne :{
  106. document:n_data
  107. }
  108. };
  109. tasks.push(task);
  110. }
  111. }
  112. return tasks;
  113. }
  114. async function updateNodes(datas){
  115. let nodeGroups = _.groupBy(datas,'type');
  116. let rationTasks = [];
  117. let billTasks = [];
  118. let asyncTasks = [];
  119. for(let type in nodeGroups){
  120. for(let node of nodeGroups[type]){
  121. if(type == projectConsts.BILLS){
  122. billTasks.push(getTask(node));
  123. }else if(type == projectConsts.RATION){
  124. rationTasks.push(getTask(node));
  125. }
  126. }
  127. }
  128. rationTasks.length>0?asyncTasks.push(ration_model.model.bulkWrite(rationTasks)):'';
  129. billTasks.length>0?asyncTasks.push(bill_model.model.bulkWrite(billTasks)):"";
  130. return asyncTasks.length>0?await Promise.all(asyncTasks):"";
  131. function getTask(node) {
  132. let task={
  133. updateOne:{
  134. filter:{
  135. //projectID: node.data.projectID, 现在复制项目也重新生成一个新的ID了,所以ID是唯一的
  136. ID: node.data.ID
  137. //deleteInfo: null
  138. },
  139. update :node.data
  140. }
  141. };
  142. return task;
  143. }
  144. }
  145. /*function updateNodes(datas,callback) {
  146. let tasks = [];
  147. for(let node of datas){
  148. tasks.push(updateOne(node))
  149. }
  150. async_n.parallel(tasks, function(err, results) {
  151. if (!err){
  152. callback(0, '', results);
  153. }
  154. else{
  155. console.log(err);
  156. callback(1, 'save project failed'+err.message, null);
  157. }
  158. });
  159. function updateOne(node) {
  160. if(node.type == projectConsts.BILLS){
  161. return function (asCallback) {
  162. bill_model.model.findOneAndUpdate({projectID: node.data.projectID, ID: node.data.ID,deleteInfo: null}, node.data,{new: true}, asCallback);
  163. }
  164. }else if(node.type ==projectConsts.RATION){
  165. return function (asCallback) {
  166. ration_model.model.findOneAndUpdate({projectID: node.data.projectID, ID: node.data.ID,deleteInfo: null}, node.data,{new: true}, asCallback);
  167. }
  168. }
  169. }
  170. }*/
  171. //data = {feeRateID:111111,projectID:1245}; type = feeRate
  172. async function markUpdateProject(data,type) {
  173. let tasks=[];
  174. let query = {deleteInfo:null};
  175. let result = null;
  176. if(type=="feeRate"){//更改了费率
  177. query['property.feeFile.id'] = data.feeRateID;
  178. }
  179. if(type=="unitFile"){//更改了单价文件
  180. query['property.unitPriceFile.id'] = data.unitFileID;//unitPriceFile
  181. }
  182. let projects =await projectsModel.find(query);
  183. for(let p of projects){
  184. if(p.ID!=data.projectID){//当前项目不用更新
  185. tasks.push(generateMarkTask(type,p.ID));
  186. }
  187. }
  188. if(tasks.length>0){
  189. result = await projectsModel.bulkWrite(tasks);
  190. }
  191. return result;
  192. }
  193. async function removeProjectMark(projectID) {
  194. return await projectsModel.findOneAndUpdate({ID:projectID},{"$unset":{"changeMark":1}});
  195. }
  196. function generateMarkTask(value,projectID) {
  197. let task = {
  198. updateOne:{
  199. filter:{
  200. ID:projectID
  201. },
  202. update:{
  203. changeMark:value
  204. }
  205. }
  206. };
  207. return task
  208. }