bill_facade.js 8.3 KB

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