bill_facade.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 pmFacade = require('../../pm/facade/pm_facade');
  21. const { getSortedTreeData } = require('../../../public/common_util');
  22. const { constructionFeeNodeID, constructionEquipmentFeeNodeID, BudgetArea, fixedFlag } = require('../../../public/common_constants');
  23. const uuidV1 = require('uuid/v1');
  24. const GLJController = require("../../glj/controllers/glj_controller");
  25. const GLJListModel = require('../../glj/models/glj_list_model');
  26. let _ = require("lodash");
  27. module.exports={
  28. getSectionInfo : async function (data) {
  29. let conditions=[];
  30. let fxList=[];
  31. let sectionInfo ={};
  32. for(let libID in data){
  33. let codes=[];
  34. let temp={};
  35. codes= _.keys(data[libID]);
  36. temp['billsLibId']=libID;
  37. temp['code'] = {"$in": codes};
  38. conditions.push(temp);
  39. }
  40. if(conditions.length>0){
  41. fxList = await Bills_Lib.find({"$or":conditions});
  42. }
  43. if(fxList.length>0){
  44. let sectionIDs = {};
  45. for(let f of fxList){
  46. if(f._doc.sectionInfo){
  47. f._doc.sectionInfo.first?sectionIDs[f._doc.sectionInfo.first]=true:"";
  48. f._doc.sectionInfo.second?sectionIDs[f._doc.sectionInfo.second]=true:"";
  49. f._doc.sectionInfo.third?sectionIDs[f._doc.sectionInfo.third]=true:"";
  50. }
  51. }
  52. let IDList = _.keys(sectionIDs);
  53. let sectionList = await Bills_Lib.find({'ID':{'$in':IDList}});
  54. let sectionMap = _.mapKeys(sectionList,'ID');
  55. let fxMap = _.mapKeys(fxList,'code');
  56. sectionInfo={
  57. fxMap:fxMap,
  58. sectionMap :sectionMap
  59. }
  60. return sectionInfo;
  61. }
  62. return null;
  63. },
  64. reorganizeFBFX:async function(data){
  65. let result = {};
  66. let tasks = generateBillTasks(data);
  67. if(data.delete && data.delete.length > 0){
  68. let qd_query={projectID: data.projectID, billID: {"$in": data.delete}};
  69. await quantity_detail.deleteByQuery(qd_query) ;
  70. }
  71. if(tasks.length > 0){
  72. result =await bill_Model.bulkWrite(tasks);
  73. }
  74. return result;
  75. },
  76. pasteBlock : async function(data,compilation){
  77. let pasteTasks = [
  78. pasteRationsAndRationGLJ(data.rations,data.ration_gljs,compilation),//这一步会花费比较多时间
  79. pasteOtherData(data)
  80. ];
  81. let [rationsAndRationGLJ,resultMap] = await Promise.all(pasteTasks);
  82. let gljController = new GLJController();
  83. // let projectGLJ = await gljController.getProjectGLJsByProjectID(data.projectID);
  84. resultMap.rations = rationsAndRationGLJ.rations;
  85. resultMap.ration_gljs = rationsAndRationGLJ.new_ration_gljs;
  86. resultMap.projectGLJList = rationsAndRationGLJ.projectGLJList;
  87. // resultMap.gljData = projectGLJ.data;
  88. return resultMap;
  89. },
  90. createNewBills:async function(newDatas){
  91. // billsLibDao.getStdBillsByCode 从这里取数据
  92. let results = [];
  93. for(let bills of newDatas){
  94. let stdB = await billsLibDao.getStdBillsByCode({billsLibId:bills.billsLibId,code:bills.billsLocation});
  95. if(stdB){
  96. stdB = JSON.parse(JSON.stringify(stdB));
  97. if(!bills.unit && /\//.test(stdB.unit)){
  98. bills.unit = stdB.unit.split(/\//)[0];
  99. }
  100. //处理项目特征和工作内容,现在默认都添加到项目特征列中
  101. stdB.itemCharacterText = "[项目特征]\n[工作内容]\n"+stdB.jobContentText;
  102. stdB.jobContentText="";
  103. bills = _.merge(stdB,bills);
  104. }
  105. delete bills.billsLocation;
  106. results.push(bills);
  107. }
  108. if(results.length > 0){
  109. await bill_Model.create(results);
  110. }
  111. return results;
  112. },
  113. insertBills: async function(datas) {
  114. let bulks = [];
  115. for (let data of datas) {
  116. if (data.updateType === 'update') {
  117. bulks.push({updateOne: {filter: {ID: data.updateData.ID}, update: {NextSiblingID: data.updateData.NextSiblingID}}});
  118. } else {
  119. bulks.push({insertOne: {document: data.updateData}});
  120. }
  121. }
  122. if (bulks.length > 0) {
  123. await bill_Model.bulkWrite(bulks);
  124. }
  125. },
  126. // 获取概算汇总数据
  127. getBudgetSummary: async function (constructionID) {
  128. // 获取建设项目清单数据(工程建设其他费用 - 建设项目总概算)部分
  129. const constructionOtherFeeBills = await bill_Model.find({ projectID: constructionID }).lean();
  130. const sortedOtherFeeBills = getSortedTreeData('-1', constructionOtherFeeBills);
  131. constructionOtherFeeBills.forEach(item => item.area = BudgetArea.CONSTRUCTION_OTHER_FEE);
  132. // 获取工程费用数据
  133. const constructionFeeBills = await this.getConstructionFeeData(constructionID, sortedOtherFeeBills[0].ID);
  134. return [...constructionFeeBills, ...constructionOtherFeeBills];
  135. },
  136. // 获取工程费用数据,作为概算汇总数据的拼接树数据
  137. getConstructionFeeData: async function (constructionID, nextID) {
  138. const projects = await pmFacade.getPosterityProjects([constructionID], true, { _id: 0, ID: 1, ParentID: 1, NextSiblingID: 1, name: 1, 'property.feeStandardName': 1});
  139. const construction = projects.find(p => p.ID === constructionID);
  140. const items = getSortedTreeData(construction.ParentID, projects);
  141. // 转换为uuid
  142. const IDMap = {};
  143. items.forEach((item, index) => {
  144. if (index == 0) {
  145. IDMap[item.ID] = constructionFeeNodeID;
  146. } else {
  147. IDMap[item.ID] = uuidV1()
  148. }
  149. });
  150. // 将项目调整为工程费用数据
  151. let curConstructionID;
  152. let curSingleNo = 0;
  153. let curSingleID;
  154. let curUnitNo = 0;
  155. let latestSingleNode;
  156. items.forEach((item, index) => {
  157. item.area = BudgetArea.CONSTRUCTION_FEE;
  158. item.ID = IDMap[item.ID];
  159. item.ParentID = IDMap[item.ParentID] || '-1';
  160. item.NextSiblingID = IDMap[item.NextSiblingID] || '-1';
  161. if (index === 0) {
  162. curConstructionID = item.ID;
  163. item.flags = [{ fieldName: 'fixed', flag: fixedFlag.CONSTRUCTION_FEE }];
  164. item.code = '1';
  165. item.name = '工程费用';
  166. item.NextSiblingID = nextID;
  167. } else {
  168. if (item.ParentID === curConstructionID) {
  169. latestSingleNode = item;
  170. curSingleNo += 1;
  171. curUnitNo = 0;
  172. curSingleID = item.ID;
  173. item.code = `1.${curSingleNo}`
  174. } else if (item.ParentID === curSingleID) {
  175. curUnitNo += 1;
  176. item.code = `1.${curSingleNo}.${curUnitNo}`
  177. }
  178. }
  179. });
  180. const constructionFeeNode = items[0];
  181. // 设备及工器具购置费
  182. const constructionEquipmentNode = {
  183. area: BudgetArea.CONSTRUCTION_FEE,
  184. flags: [{ fieldName: 'fixed', flag: fixedFlag.CONSTRUCTION_EQUIPMENT_FEE }],
  185. ID: constructionEquipmentFeeNodeID,
  186. ParentID: constructionFeeNode.ID,
  187. NextSiblingID: '-1',
  188. code: `1.${curSingleNo + 1}`,
  189. name: '设备及工器具购置费',
  190. };
  191. latestSingleNode.NextSiblingID = constructionEquipmentNode.ID;
  192. items.push(constructionEquipmentNode);
  193. return items;
  194. }
  195. };
  196. async function pasteOtherData(data) {
  197. let bills = data.bills;
  198. let quantity_details = data.quantity_details;
  199. let ration_coes = data.ration_coes;
  200. let ration_installations = data.ration_installations;
  201. let ration_templates = data.ration_templates;
  202. let updateData = data.updateData;
  203. let uModel = null;
  204. let tasks = [];
  205. //生成更新任务
  206. for(let u of updateData){
  207. if(u.type == "bills"){
  208. uModel = bill_Model;
  209. }else {
  210. uModel = ration_Model;
  211. }
  212. let tem = {
  213. updateOne:{
  214. filter:u.query,
  215. update :u.doc
  216. }
  217. };
  218. tasks.push(tem);
  219. }
  220. bills.length > 0 ? await insertMany(bills,bill_Model):'';
  221. quantity_details.length > 0 ? await insertMany(quantity_details,quantity_detail_model):'';
  222. ration_coes.length > 0 ? await insertMany(ration_coes,ration_coe_Model):'';
  223. ration_installations.length > 0 ? await insertMany(ration_installations,ration_installation_Model):'';
  224. ration_templates.length > 0? await insertMany(ration_templates,ration_template_Model):'';
  225. tasks.length>0?await uModel.bulkWrite(tasks):'';
  226. return {bills:bills,quantity_details:quantity_details,ration_coes:ration_coes,ration_installations:ration_installations,ration_templates:ration_templates,updateData:updateData}
  227. }
  228. async function pasteRationsAndRationGLJ (rations,ration_gljs,compilation) {
  229. let projectGljModel = new GLJListModel();
  230. let gljMap = {}, new_ration_gljs=[],projectID=null,projectGLJList =[];
  231. if(rations.length > 0) projectID = rations[0].projectID;
  232. if(projectID==null && ration_gljs.length > 0) projectID = ration_gljs[0].projectID;
  233. if(projectID == null) return {rations:rations,new_ration_gljs:new_ration_gljs};
  234. let [unitFileId,ext] = await ration_glj_facade.prepareExtData(projectID,compilation);
  235. //先根据定额类型的工料机,插入到项目工料机中
  236. for(let r of rations){
  237. if(r.type == rationType.gljRation){
  238. let projectGLJ = await getProjectGLJ(r,rationKeyArray,gljMap,unitFileId,ext);
  239. projectGLJList.push(projectGLJ);
  240. }
  241. }
  242. for(let rg of ration_gljs){
  243. let projectGLJ = await getProjectGLJ(rg,gljKeyArray,gljMap,unitFileId,ext);
  244. let temRecord = ration_glj_facade.createNewRecord(rg);
  245. rg.rcode?temRecord.rcode = rg.rcode:'';
  246. rg.hasOwnProperty("customQuantity")?temRecord.customQuantity = rg.customQuantity:'';
  247. new_ration_gljs.push(temRecord);
  248. projectGLJList.push(projectGLJ);
  249. }
  250. rations.length>0?await insertMany(rations,ration_Model):'';
  251. new_ration_gljs.length>0?await insertMany(new_ration_gljs,ration_glj_Model):'';
  252. return {rations:rations,new_ration_gljs:new_ration_gljs,projectGLJList:projectGLJList};
  253. async function getProjectGLJ (glj,keyArray,gljMap,unitFileId,ext) {
  254. let keyIndex = projectGljModel.getIndex(glj,keyArray);
  255. let pgljResult = null;
  256. if(gljMap[keyIndex]){
  257. pgljResult = gljMap[keyIndex]
  258. }else {
  259. let p_glj = ration_glj_facade.getGLJSearchInfo(glj);
  260. pgljResult = await projectGljModel.addList(p_glj,unitFileId,ext);//逐条添加到项目工料机
  261. gljMap[keyIndex] = pgljResult;
  262. }
  263. setResult(glj,pgljResult);
  264. return pgljResult
  265. }
  266. function setResult(glj,result ) {
  267. let typeString = result.type+'';
  268. glj.marketPrice = result.unit_price.market_price;
  269. glj.adjustPrice = result.unit_price.base_price;
  270. glj.basePrice = result.unit_price.base_price;
  271. glj.isAdd = result.unit_price.is_add;
  272. glj.projectGLJID = result.id;
  273. if (typeString.startsWith("2")||typeString=='4'||typeString=='5') {//只有材料类型才显示是否暂估
  274. glj.isEstimate = result.is_evaluate;
  275. }
  276. }
  277. }
  278. function generateBillTasks(data) {
  279. let tasks=[];
  280. let user_id = data.user_id,projectID = data.projectID;
  281. let deleteInfo={deleted: true, deleteDateTime: new Date(), deleteBy: user_id};
  282. if(data.delete && data.delete.length > 0){
  283. for(let d_ID of data.delete){
  284. //原先是假删除,现在改成真删除
  285. let task = {
  286. deleteOne:{
  287. filter:{
  288. ID:d_ID,
  289. projectID:projectID
  290. }
  291. }
  292. };
  293. /* let task={
  294. updateOne:{
  295. filter:{
  296. ID:d_ID,
  297. projectID:projectID
  298. },
  299. update :{
  300. deleteInfo:deleteInfo
  301. }
  302. }
  303. };*/
  304. tasks.push(task);
  305. }
  306. }
  307. if(data.update && data.update.length > 0){
  308. for(let u_data of data.update){
  309. let task ={
  310. updateOne:{
  311. filter : {
  312. ID:u_data.ID,
  313. projectID:projectID
  314. },
  315. update : u_data.data
  316. }
  317. };
  318. tasks.push(task);
  319. }
  320. }
  321. if(data.create && data.create.length > 0){
  322. for(let n_data of data.create){
  323. let task = {
  324. insertOne :{
  325. document:n_data
  326. }
  327. };
  328. tasks.push(task);
  329. }
  330. }
  331. return tasks;
  332. }
  333. async function insertMany(datas,model) {
  334. let tem = [];
  335. console.log(datas);
  336. while (datas.length>1000){//因为mongoose限制了批量插入的条数为1000.所以超出限制后需要分批插入
  337. let newList = datas.splice(0,1000);//一次插入1000条
  338. await model.insertMany(newList);
  339. tem = tem.concat(newList);
  340. }
  341. await model.insertMany(datas);
  342. if(tem.length > 0) datas.push(...tem);//还原数组
  343. }