bill_facade.js 10 KB

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