project_facade.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. for(let type in nodeGroups){
  119. for(let node of nodeGroups[type]){
  120. if(type == projectConsts.BILLS){
  121. billTasks.push(getTask(node));
  122. }else if(type == projectConsts.RATION){
  123. rationTasks.push(getTask(node));
  124. }
  125. }
  126. }
  127. if(rationTasks.length>0){
  128. await ration_model.model.bulkWrite(rationTasks);
  129. }
  130. if(billTasks.length>0){
  131. await bill_model.model.bulkWrite(billTasks);
  132. }
  133. function getTask(node) {
  134. let task={
  135. updateOne:{
  136. filter:{
  137. projectID: node.data.projectID,
  138. ID: node.data.ID,
  139. deleteInfo: null
  140. },
  141. update :node.data
  142. }
  143. };
  144. return task;
  145. }
  146. }
  147. /*function updateNodes(datas,callback) {
  148. let tasks = [];
  149. for(let node of datas){
  150. tasks.push(updateOne(node))
  151. }
  152. async_n.parallel(tasks, function(err, results) {
  153. if (!err){
  154. callback(0, '', results);
  155. }
  156. else{
  157. console.log(err);
  158. callback(1, 'save project failed'+err.message, null);
  159. }
  160. });
  161. function updateOne(node) {
  162. if(node.type == projectConsts.BILLS){
  163. return function (asCallback) {
  164. bill_model.model.findOneAndUpdate({projectID: node.data.projectID, ID: node.data.ID,deleteInfo: null}, node.data,{new: true}, asCallback);
  165. }
  166. }else if(node.type ==projectConsts.RATION){
  167. return function (asCallback) {
  168. ration_model.model.findOneAndUpdate({projectID: node.data.projectID, ID: node.data.ID,deleteInfo: null}, node.data,{new: true}, asCallback);
  169. }
  170. }
  171. }
  172. }*/
  173. //data = {feeRateID:111111,projectID:1245}; type = feeRate
  174. async function markUpdateProject(data,type) {
  175. let tasks=[];
  176. let query = {deleteInfo:null};
  177. let result = null;
  178. if(type=="feeRate"){//更改了费率
  179. query['property.feeFile.id'] = data.feeRateID;
  180. }
  181. if(type=="unitFile"){//更改了单价文件
  182. query['property.unitPriceFile.id'] = data.unitFileID;//unitPriceFile
  183. }
  184. let projects =await projectsModel.find(query);
  185. for(let p of projects){
  186. if(p.ID!=data.projectID){//当前项目不用更新
  187. tasks.push(generateMarkTask(type,p.ID));
  188. }
  189. }
  190. if(tasks.length>0){
  191. result = await projectsModel.bulkWrite(tasks);
  192. }
  193. return result;
  194. }
  195. async function removeProjectMark(projectID) {
  196. return await projectsModel.findOneAndUpdate({ID:projectID},{"$unset":{"changeMark":1}});
  197. }
  198. function generateMarkTask(value,projectID) {
  199. let task = {
  200. updateOne:{
  201. filter:{
  202. ID:projectID
  203. },
  204. update:{
  205. changeMark:value
  206. }
  207. }
  208. };
  209. return task
  210. }