bill_facade.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. const GLJController = require("../../glj/controllers/glj_controller");
  21. const GLJListModel = require('../../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. insertBills: async function(datas) {
  110. let bulks = [];
  111. for (let data of datas) {
  112. if (data.updateType === 'update') {
  113. let updateItem;
  114. if (data.updateData.NextSiblingID !== undefined) {
  115. updateItem = { NextSiblingID: data.updateData.NextSiblingID }
  116. }
  117. if (data.updateData.name !== undefined) {
  118. updateItem = { name: data.updateData.name }
  119. }
  120. if (updateItem) {
  121. bulks.push({updateOne: {filter: {ID: data.updateData.ID}, update: updateItem}});
  122. }
  123. } else {
  124. bulks.push({insertOne: {document: data.updateData}});
  125. }
  126. }
  127. if (bulks.length > 0) {
  128. await bill_Model.bulkWrite(bulks);
  129. }
  130. }
  131. };
  132. async function pasteOtherData(data) {
  133. let bills = data.bills;
  134. let quantity_details = data.quantity_details;
  135. let ration_coes = data.ration_coes;
  136. let ration_installations = data.ration_installations;
  137. let ration_templates = data.ration_templates;
  138. let updateData = data.updateData;
  139. let uModel = null;
  140. let tasks = [];
  141. //生成更新任务
  142. for(let u of updateData){
  143. if(u.type == "bills"){
  144. uModel = bill_Model;
  145. }else {
  146. uModel = ration_Model;
  147. }
  148. let tem = {
  149. updateOne:{
  150. filter:u.query,
  151. update :u.doc
  152. }
  153. };
  154. tasks.push(tem);
  155. }
  156. bills.length > 0 ? await insertMany(bills,bill_Model):'';
  157. quantity_details.length > 0 ? await insertMany(quantity_details,quantity_detail_model):'';
  158. ration_coes.length > 0 ? await insertMany(ration_coes,ration_coe_Model):'';
  159. ration_installations.length > 0 ? await insertMany(ration_installations,ration_installation_Model):'';
  160. ration_templates.length > 0? await insertMany(ration_templates,ration_template_Model):'';
  161. tasks.length>0?await uModel.bulkWrite(tasks):'';
  162. return {bills:bills,quantity_details:quantity_details,ration_coes:ration_coes,ration_installations:ration_installations,ration_templates:ration_templates,updateData:updateData}
  163. }
  164. async function pasteRationsAndRationGLJ (rations,ration_gljs,compilation) {
  165. let projectGljModel = new GLJListModel();
  166. let gljMap = {}, new_ration_gljs=[],projectID=null,projectGLJList=[];
  167. if(rations.length > 0) projectID = rations[0].projectID;
  168. if(projectID==null && ration_gljs.length > 0) projectID = ration_gljs[0].projectID;
  169. if(projectID == null) return {rations:rations,new_ration_gljs:new_ration_gljs};
  170. let [unitFileId,ext] = await ration_glj_facade.prepareExtData(projectID,compilation);
  171. //先根据定额类型的工料机,插入到项目工料机中
  172. for(let r of rations){
  173. if(r.type == rationType.gljRation){
  174. let tg = await getProjectGLJ(r,rationKeyArray,gljMap,unitFileId,ext);
  175. projectGLJList.push(tg)
  176. }
  177. }
  178. for(let rg of ration_gljs){
  179. let tg = await getProjectGLJ(rg,gljKeyArray,gljMap,unitFileId,ext);
  180. let temRecord = ration_glj_facade.createNewRecord(rg);
  181. rg.rcode?temRecord.rcode = rg.rcode:'';
  182. rg.hasOwnProperty("customQuantity")?temRecord.customQuantity = rg.customQuantity:'';
  183. new_ration_gljs.push(temRecord);
  184. projectGLJList.push(tg);
  185. }
  186. rations.length>0?await insertMany(rations,ration_Model):'';
  187. new_ration_gljs.length>0?await insertMany(new_ration_gljs,ration_glj_Model):'';
  188. return {rations:rations,new_ration_gljs:new_ration_gljs,projectGLJList:projectGLJList};
  189. async function getProjectGLJ (glj,keyArray,gljMap,unitFileId,ext) {
  190. let keyIndex = projectGljModel.getIndex(glj,keyArray);
  191. let pgljResult = null;
  192. if(gljMap[keyIndex]){
  193. pgljResult = gljMap[keyIndex]
  194. }else {
  195. let p_glj = ration_glj_facade.getGLJSearchInfo(glj);
  196. pgljResult = await projectGljModel.addList(p_glj,unitFileId,ext);//逐条添加到项目工料机
  197. gljMap[keyIndex] = pgljResult;
  198. }
  199. setResult(glj,pgljResult);
  200. return pgljResult;
  201. }
  202. function setResult(glj,result ) {
  203. let typeString = result.type+'';
  204. glj.marketPrice = result.unit_price.market_price;
  205. glj.adjustPrice = result.unit_price.base_price;
  206. glj.basePrice = result.unit_price.base_price;
  207. glj.isAdd = result.unit_price.is_add;
  208. glj.projectGLJID = result.id;
  209. if (typeString.startsWith("2")||typeString=='4'||typeString=='5') {//只有材料类型才显示是否暂估
  210. glj.isEstimate = result.is_evaluate;
  211. }
  212. }
  213. }
  214. function generateBillTasks(data) {
  215. let tasks=[];
  216. let user_id = data.user_id,projectID = data.projectID;
  217. let deleteInfo={deleted: true, deleteDateTime: new Date(), deleteBy: user_id};
  218. if(data.delete && data.delete.length > 0){
  219. for(let d_ID of data.delete){
  220. //原先是假删除,现在改成真删除
  221. let task = {
  222. deleteOne:{
  223. filter:{
  224. ID:d_ID,
  225. projectID:projectID
  226. }
  227. }
  228. };
  229. /* let task={
  230. updateOne:{
  231. filter:{
  232. ID:d_ID,
  233. projectID:projectID
  234. },
  235. update :{
  236. deleteInfo:deleteInfo
  237. }
  238. }
  239. };*/
  240. tasks.push(task);
  241. }
  242. }
  243. if(data.update && data.update.length > 0){
  244. for(let u_data of data.update){
  245. let task ={
  246. updateOne:{
  247. filter : {
  248. ID:u_data.ID,
  249. projectID:projectID
  250. },
  251. update : u_data.data
  252. }
  253. };
  254. tasks.push(task);
  255. }
  256. }
  257. if(data.create && data.create.length > 0){
  258. for(let n_data of data.create){
  259. let task = {
  260. insertOne :{
  261. document:n_data
  262. }
  263. };
  264. tasks.push(task);
  265. }
  266. }
  267. return tasks;
  268. }
  269. async function insertMany(datas,model) {
  270. let tem = [];
  271. while (datas.length>1000){//因为mongoose限制了批量插入的条数为1000.所以超出限制后需要分批插入
  272. let newList = datas.splice(0,1000);//一次插入1000条
  273. await model.insertMany(newList);
  274. tem = tem.concat(newList);
  275. }
  276. await model.insertMany(datas);
  277. if(tem.length > 0) datas.push(...tem);//还原数组
  278. }