bill_facade.js 10 KB

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