bill_facade.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * Created by zhang on 2018/1/22.
  3. */
  4. let mongoose = require('mongoose');
  5. let projectConst = require('../models/project_consts');
  6. const rationType = projectConst.rationType;
  7. const rationKeyArray = projectConst.rationKeyArray;
  8. const gljKeyArray = projectConst.gljKeyArray;
  9. let quantity_detail = require("./quantity_detail_facade");
  10. let quantity_detail_model = mongoose.model('quantity_detail');
  11. let ration_glj_facade = require("../../ration_glj/facade/ration_glj_facade");
  12. let Bills_Lib = mongoose.model('std_bills_lib_bills');
  13. let ration_Model = mongoose.model('ration');
  14. let ration_glj_Model = mongoose.model('ration_glj');
  15. let ration_coe_Model = mongoose.model('ration_coe');
  16. let ration_installation_Model = mongoose.model('ration_installation');
  17. let bill_Model = require('../models/bills').model;
  18. import GLJController from "../../glj/controllers/glj_controller";
  19. import GLJListModel from '../../glj/models/glj_list_model';
  20. let _ = require("lodash");
  21. module.exports={
  22. getSectionInfo : async function (data) {
  23. let conditions=[];
  24. let fxList=[];
  25. let sectionInfo ={};
  26. for(let libID in data){
  27. let codes=[];
  28. let temp={};
  29. codes= _.keys(data[libID]);
  30. temp['billsLibId']=libID;
  31. temp['code'] = {"$in": codes};
  32. conditions.push(temp);
  33. }
  34. if(conditions.length>0){
  35. fxList = await Bills_Lib.find({"$or":conditions});
  36. }
  37. if(fxList.length>0){
  38. let sectionIDs = {};
  39. for(let f of fxList){
  40. if(f._doc.sectionInfo){
  41. f._doc.sectionInfo.first?sectionIDs[f._doc.sectionInfo.first]=true:"";
  42. f._doc.sectionInfo.second?sectionIDs[f._doc.sectionInfo.second]=true:"";
  43. f._doc.sectionInfo.third?sectionIDs[f._doc.sectionInfo.third]=true:"";
  44. }
  45. }
  46. let IDList = _.keys(sectionIDs);
  47. let sectionList = await Bills_Lib.find({'ID':{'$in':IDList}});
  48. let sectionMap = _.mapKeys(sectionList,'ID');
  49. let fxMap = _.mapKeys(fxList,'code');
  50. sectionInfo={
  51. fxMap:fxMap,
  52. sectionMap :sectionMap
  53. }
  54. return sectionInfo;
  55. }
  56. return null;
  57. },
  58. reorganizeFBFX:async function(data){
  59. let result = {};
  60. let tasks = generateBillTasks(data);
  61. if(data.delete && data.delete.length > 0){
  62. let qd_query={projectID: data.projectID, billID: {"$in": data.delete}};
  63. await quantity_detail.deleteByQuery(qd_query) ;
  64. }
  65. if(tasks.length > 0){
  66. result =await bill_Model.bulkWrite(tasks);
  67. }
  68. return result;
  69. },
  70. pasteBlock : async function(data){
  71. let pasteTasks = [
  72. pasteRationsAndRationGLJ(data.rations,data.ration_gljs),//这一步会花费比较多时间
  73. pasteOtherData(data)
  74. ];
  75. let [rationsAndRationGLJ,resultMap] = await Promise.all(pasteTasks);
  76. let gljController = new GLJController();
  77. let projectGLJ = await gljController.getProjectGLJsByProjectID(data.projectID);
  78. resultMap.rations = rationsAndRationGLJ.rations;
  79. resultMap.ration_gljs = rationsAndRationGLJ.new_ration_gljs;
  80. resultMap.gljData = projectGLJ.data;
  81. return resultMap;
  82. }
  83. };
  84. async function pasteOtherData(data) {
  85. let bills = data.bills;
  86. let quantity_details = data.quantity_details;
  87. let ration_coes = data.ration_coes;
  88. let ration_installations = data.ration_installations;
  89. let updateData = data.updateData;
  90. let uModel = null;
  91. let tasks = [];
  92. //生成更新任务
  93. for(let u of updateData){
  94. if(u.type == "bills"){
  95. uModel = bill_Model;
  96. }else {
  97. uModel = ration_Model;
  98. }
  99. let tem = {
  100. updateOne:{
  101. filter:u.query,
  102. update :u.doc
  103. }
  104. };
  105. tasks.push(tem);
  106. }
  107. bills.length > 0 ? await insertMany(bills,bill_Model):'';
  108. quantity_details.length > 0 ? await insertMany(quantity_details,quantity_detail_model):'';
  109. ration_coes.length > 0 ? await insertMany(ration_coes,ration_coe_Model):'';
  110. ration_installations.length > 0 ? await insertMany(ration_installations,ration_installation_Model):'';
  111. tasks.length>0?await uModel.bulkWrite(tasks):'';
  112. return {bills:bills,quantity_details:quantity_details,ration_coes:ration_coes,ration_installations:ration_installations,updateData:updateData}
  113. }
  114. async function pasteRationsAndRationGLJ (rations,ration_gljs) {
  115. let projectGljModel = new GLJListModel();
  116. let gljMap = {};
  117. let new_ration_gljs=[];
  118. //先根据定额类型的工料机,插入到项目工料机中
  119. for(let r of rations){
  120. if(r.type == rationType.gljRation){
  121. await getProjectGLJ(r,rationKeyArray,gljMap);
  122. }
  123. }
  124. for(let rg of ration_gljs){
  125. await getProjectGLJ(rg,gljKeyArray,gljMap);
  126. let temRecord = ration_glj_facade.createNewRecord(rg);
  127. rg.rcode?temRecord.rcode = rg.rcode:'';
  128. rg.hasOwnProperty("customQuantity")?temRecord.customQuantity = rg.customQuantity:'';
  129. new_ration_gljs.push(temRecord);
  130. }
  131. rations.length>0?await insertMany(rations,ration_Model):'';
  132. new_ration_gljs.length>0?await insertMany(new_ration_gljs,ration_glj_Model):'';
  133. return{rations:rations,new_ration_gljs:new_ration_gljs};
  134. async function getProjectGLJ (glj,keyArray,gljMap) {
  135. let keyIndex = projectGljModel.getIndex(glj,keyArray);
  136. let pgljResult = null;
  137. if(gljMap[keyIndex]){
  138. pgljResult = gljMap[keyIndex]
  139. }else {
  140. let p_glj = ration_glj_facade.getGLJSearchInfo(glj);
  141. pgljResult = await projectGljModel.addList(p_glj);//逐条添加到项目工料机
  142. gljMap[keyIndex] = pgljResult;
  143. }
  144. setResult(glj,pgljResult);
  145. }
  146. function setResult(glj,result ) {
  147. let typeString = result.type+'';
  148. glj.marketPrice = result.unit_price.market_price;
  149. glj.adjustPrice = result.unit_price.base_price;
  150. glj.basePrice = result.unit_price.base_price;
  151. glj.isAdd = result.unit_price.is_add;
  152. glj.projectGLJID = result.id;
  153. if (typeString.startsWith("2")||typeString=='4'||typeString=='5') {//只有材料类型才显示是否暂估
  154. glj.isEstimate = result.is_evaluate;
  155. }
  156. }
  157. }
  158. function generateBillTasks(data) {
  159. let tasks=[];
  160. let user_id = data.user_id,projectID = data.projectID;
  161. let deleteInfo={deleted: true, deleteDateTime: new Date(), deleteBy: user_id};
  162. if(data.delete && data.delete.length > 0){
  163. for(let d_ID of data.delete){
  164. let task={
  165. updateOne:{
  166. filter:{
  167. ID:d_ID,
  168. projectID:projectID
  169. },
  170. update :{
  171. deleteInfo:deleteInfo
  172. }
  173. }
  174. };
  175. tasks.push(task);
  176. }
  177. }
  178. if(data.update && data.update.length > 0){
  179. for(let u_data of data.update){
  180. let task ={
  181. updateOne:{
  182. filter : {
  183. ID:u_data.ID,
  184. projectID:projectID
  185. },
  186. update : u_data.data
  187. }
  188. };
  189. tasks.push(task);
  190. }
  191. }
  192. if(data.create && data.create.length > 0){
  193. for(let n_data of data.create){
  194. let task = {
  195. insertOne :{
  196. document:n_data
  197. }
  198. };
  199. tasks.push(task);
  200. }
  201. }
  202. return tasks;
  203. }
  204. async function insertMany(datas,model) {
  205. while (datas.length>1000){//因为mongoose限制了批量插入的条数为1000.所以超出限制后需要分批插入
  206. let newList = datas.splice(0,1000);//一次插入1000条
  207. await model.insertMany(newList);
  208. }
  209. await model.insertMany(datas);
  210. }