bill_facade.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.gljData = projectGLJ.data;
  83. return resultMap;
  84. },
  85. createNewBills:async function(newDatas){
  86. // billsLibDao.getStdBillsByCode 从这里取数据
  87. let results = [];
  88. for(let bills of newDatas){
  89. let stdB = await billsLibDao.getStdBillsByCode({billsLibId:bills.billsLibId,code:bills.billsLocation});
  90. if(stdB){
  91. stdB = JSON.parse(JSON.stringify(stdB));
  92. if(!bills.unit && /\//.test(stdB.unit)){
  93. bills.unit = stdB.unit.split(/\//)[0];
  94. }
  95. //处理项目特征和工作内容,现在默认都添加到项目特征列中
  96. stdB.itemCharacterText = "[项目特征]\n[工作内容]\n"+stdB.jobContentText;
  97. stdB.jobContentText="";
  98. bills = _.merge(stdB,bills);
  99. }
  100. delete bills.billsLocation;
  101. results.push(bills);
  102. }
  103. if(results.length > 0){
  104. await bill_Model.create(results);
  105. }
  106. return results;
  107. },
  108. insertBills: async function(datas) {
  109. let bulks = [];
  110. for (let data of datas) {
  111. if (data.updateType === 'update') {
  112. bulks.push({updateOne: {filter: {ID: data.updateData.ID}, update: {NextSiblingID: data.updateData.NextSiblingID}}});
  113. } else {
  114. bulks.push({insertOne: {document: data.updateData}});
  115. }
  116. }
  117. if (bulks.length > 0) {
  118. await bill_Model.bulkWrite(bulks);
  119. }
  120. }
  121. };
  122. async function pasteOtherData(data) {
  123. let bills = data.bills;
  124. let quantity_details = data.quantity_details;
  125. let ration_coes = data.ration_coes;
  126. let ration_installations = data.ration_installations;
  127. let ration_templates = data.ration_templates;
  128. let updateData = data.updateData;
  129. let uModel = null;
  130. let tasks = [];
  131. //生成更新任务
  132. for(let u of updateData){
  133. if(u.type == "bills"){
  134. uModel = bill_Model;
  135. }else {
  136. uModel = ration_Model;
  137. }
  138. let tem = {
  139. updateOne:{
  140. filter:u.query,
  141. update :u.doc
  142. }
  143. };
  144. tasks.push(tem);
  145. }
  146. bills.length > 0 ? await insertMany(bills,bill_Model):'';
  147. quantity_details.length > 0 ? await insertMany(quantity_details,quantity_detail_model):'';
  148. ration_coes.length > 0 ? await insertMany(ration_coes,ration_coe_Model):'';
  149. ration_installations.length > 0 ? await insertMany(ration_installations,ration_installation_Model):'';
  150. ration_templates.length > 0? await insertMany(ration_templates,ration_template_Model):'';
  151. tasks.length>0?await uModel.bulkWrite(tasks):'';
  152. return {bills:bills,quantity_details:quantity_details,ration_coes:ration_coes,ration_installations:ration_installations,ration_templates:ration_templates,updateData:updateData}
  153. }
  154. async function pasteRationsAndRationGLJ (rations,ration_gljs,compilation) {
  155. let projectGljModel = new GLJListModel();
  156. let gljMap = {}, new_ration_gljs=[],projectID=null;
  157. if(rations.length > 0) projectID = rations[0].projectID;
  158. if(projectID==null && ration_gljs.length > 0) projectID = ration_gljs[0].projectID;
  159. if(projectID == null) return {rations:rations,new_ration_gljs:new_ration_gljs};
  160. let [unitFileId,ext] = await ration_glj_facade.prepareExtData(projectID,compilation);
  161. //先根据定额类型的工料机,插入到项目工料机中
  162. for(let r of rations){
  163. if(r.type == rationType.gljRation){
  164. await getProjectGLJ(r,rationKeyArray,gljMap,unitFileId,ext);
  165. }
  166. }
  167. for(let rg of ration_gljs){
  168. await getProjectGLJ(rg,gljKeyArray,gljMap,unitFileId,ext);
  169. let temRecord = ration_glj_facade.createNewRecord(rg);
  170. rg.rcode?temRecord.rcode = rg.rcode:'';
  171. rg.hasOwnProperty("customQuantity")?temRecord.customQuantity = rg.customQuantity:'';
  172. new_ration_gljs.push(temRecord);
  173. }
  174. rations.length>0?await insertMany(rations,ration_Model):'';
  175. new_ration_gljs.length>0?await insertMany(new_ration_gljs,ration_glj_Model):'';
  176. return {rations:rations,new_ration_gljs:new_ration_gljs};
  177. async function getProjectGLJ (glj,keyArray,gljMap,unitFileId,ext) {
  178. let keyIndex = projectGljModel.getIndex(glj,keyArray);
  179. let pgljResult = null;
  180. if(gljMap[keyIndex]){
  181. pgljResult = gljMap[keyIndex]
  182. }else {
  183. let p_glj = ration_glj_facade.getGLJSearchInfo(glj);
  184. pgljResult = await projectGljModel.addList(p_glj,unitFileId,ext);//逐条添加到项目工料机
  185. gljMap[keyIndex] = pgljResult;
  186. }
  187. setResult(glj,pgljResult);
  188. }
  189. function setResult(glj,result ) {
  190. let typeString = result.type+'';
  191. glj.marketPrice = result.unit_price.market_price;
  192. glj.adjustPrice = result.unit_price.base_price;
  193. glj.basePrice = result.unit_price.base_price;
  194. glj.isAdd = result.unit_price.is_add;
  195. glj.projectGLJID = result.id;
  196. if (typeString.startsWith("2")||typeString=='4'||typeString=='5') {//只有材料类型才显示是否暂估
  197. glj.isEstimate = result.is_evaluate;
  198. }
  199. }
  200. }
  201. function generateBillTasks(data) {
  202. let tasks=[];
  203. let user_id = data.user_id,projectID = data.projectID;
  204. let deleteInfo={deleted: true, deleteDateTime: new Date(), deleteBy: user_id};
  205. if(data.delete && data.delete.length > 0){
  206. for(let d_ID of data.delete){
  207. //原先是假删除,现在改成真删除
  208. let task = {
  209. deleteOne:{
  210. filter:{
  211. ID:d_ID,
  212. projectID:projectID
  213. }
  214. }
  215. };
  216. /* let task={
  217. updateOne:{
  218. filter:{
  219. ID:d_ID,
  220. projectID:projectID
  221. },
  222. update :{
  223. deleteInfo:deleteInfo
  224. }
  225. }
  226. };*/
  227. tasks.push(task);
  228. }
  229. }
  230. if(data.update && data.update.length > 0){
  231. for(let u_data of data.update){
  232. let task ={
  233. updateOne:{
  234. filter : {
  235. ID:u_data.ID,
  236. projectID:projectID
  237. },
  238. update : u_data.data
  239. }
  240. };
  241. tasks.push(task);
  242. }
  243. }
  244. if(data.create && data.create.length > 0){
  245. for(let n_data of data.create){
  246. let task = {
  247. insertOne :{
  248. document:n_data
  249. }
  250. };
  251. tasks.push(task);
  252. }
  253. }
  254. return tasks;
  255. }
  256. async function insertMany(datas,model) {
  257. let tem = [];
  258. while (datas.length>1000){//因为mongoose限制了批量插入的条数为1000.所以超出限制后需要分批插入
  259. let newList = datas.splice(0,1000);//一次插入1000条
  260. await model.insertMany(newList);
  261. tem = tem.concat(newList);
  262. }
  263. await model.insertMany(datas);
  264. if(tem.length > 0) datas.push(...tem);//还原数组
  265. }