bill_facade.js 11 KB

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