glj_facade.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Created by zhang on 2019/1/2.
  3. */
  4. module.exports={ //先导出后require可以解决循环引用问题
  5. changeUnitFile:changeUnitFile,
  6. changeVvTaxFile:changeVvTaxFile,
  7. addMixRatioForNew:addMixRatioForNew
  8. };
  9. const mongoose = require('mongoose');
  10. const ProjectModel = require('../../pm/models/project_model').project;
  11. import UnitPriceFileModel from "../models/unit_price_file_model";
  12. import UnitPriceModel from "../models/unit_price_model";
  13. import MixRatioModel from "../models/mix_ratio_model";
  14. import CounterModel from "../models/counter_model";
  15. import GLJListModel from '../../glj/models/glj_list_model';
  16. let std_glj_lib_gljList_model = mongoose.model('std_glj_lib_gljList');
  17. let ration_glj = require('../../ration_glj/facade/ration_glj_facade');
  18. let vvTaxModel = mongoose.model("std_vehicleVesselTax_items");
  19. let gljUtil = require('../../../public/gljUtil');
  20. let _ = require("lodash");
  21. async function changeUnitFile(projectData,unitFile,type,userID) {
  22. let projectId = projectData.projectID;
  23. let changeUnitPriceId = unitFile.id;
  24. let newName = unitFile.name;
  25. type = parseInt(type);
  26. let currentUnitPriceId = await ProjectModel.getUnitPriceFileId(projectId);
  27. let unitPriceFileModel = new UnitPriceFileModel();
  28. let insertData = null;
  29. if (type > 0) {
  30. let currentUnitPrice = await unitPriceFileModel.findDataByCondition({id: changeUnitPriceId});
  31. if (currentUnitPrice === null) {
  32. throw '不存在对应单价文件';
  33. }
  34. // 获取当前项目的rootProjectId
  35. let projectData = await ProjectModel.getProject(projectId);
  36. let rootProjectId = projectData.property.rootProjectID !== undefined ? projectData.property.rootProjectID : 0;
  37. insertData = JSON.parse(JSON.stringify(currentUnitPrice));
  38. insertData.root_project_id = rootProjectId;
  39. newName?insertData.name = newName:'';
  40. insertData.user_id = userID;
  41. delete insertData._id;
  42. delete insertData.ID;
  43. }
  44. // 获取即将更改的单价文件信息
  45. let targetUnitPriceFile = type === 0 ? await unitPriceFileModel.findDataByCondition({id: changeUnitPriceId}) :
  46. await unitPriceFileModel.add(insertData);
  47. if (targetUnitPriceFile === null) {
  48. throw '没有找到对应的单价文件';
  49. }
  50. // 查找对应单价文件的项目工料机数据
  51. let unitPriceModel = new UnitPriceModel();
  52. if(type ===1){//从其它项目复制,则先复制一份数据。
  53. let needCopyList = await unitPriceModel.findDataByCondition({unit_price_file_id: changeUnitPriceId}, null, false);
  54. if(needCopyList){
  55. // 过滤mongoose格式
  56. needCopyList = JSON.stringify(needCopyList);
  57. needCopyList = JSON.parse(needCopyList);
  58. let copyList = [];
  59. for(let n of needCopyList){
  60. delete n._id; // 删除原有id信息
  61. delete n.id;
  62. n.unit_price_file_id = targetUnitPriceFile.id;
  63. copyList.push(n);
  64. }
  65. copyList.length>0 ? await unitPriceModel.add(copyList):'';
  66. }
  67. }
  68. let copyResult = await unitPriceModel.copyNotExist(currentUnitPriceId, targetUnitPriceFile.id,projectId);
  69. // 复制成功后更改project数据
  70. if (!copyResult) {
  71. throw '复制数据失败';
  72. }
  73. let changeUnitPriceFileInfo = {
  74. id: targetUnitPriceFile.id,
  75. name: targetUnitPriceFile.name
  76. };
  77. let result = ProjectModel.changeUnitPriceFileInfo(projectId, changeUnitPriceFileInfo);
  78. if (!result) {
  79. throw '切换单价文件失败!';
  80. }
  81. //处理车船税记录
  82. await handleVvTaxForChang(targetUnitPriceFile.id,targetUnitPriceFile.vvTaxFileID);
  83. return changeUnitPriceFileInfo;
  84. }
  85. async function changeVvTaxFile(data){//对于车船税,现在只是在组成物表中插入信息,取项目工料机时虚拟一条车船况的记录。 因为车船税涉及到切换,清空,单价文件共用等复杂的情况,虚拟一条记录相对好一点。以后不合适可再修改
  86. let unitFileID = data.unitFileID,newVvTaxFileID = data.newVvTaxFileID;
  87. let unitPriceFileModel = new UnitPriceFileModel();
  88. //先更新单价文件中车船税文件ID
  89. await unitPriceFileModel.model.update({id:unitFileID},{vvTaxFileID:newVvTaxFileID});
  90. await handleVvTaxForChang(unitFileID,newVvTaxFileID);
  91. }
  92. async function handleVvTaxForChang(unitFileID,newVvTaxFileID) {//切换车船税,选择共用的单价文件时调用
  93. let mixRatioModel = new MixRatioModel();
  94. if(!newVvTaxFileID || newVvTaxFileID == ''){//如果新的车船税ID为空,则删除组成物表中车船税的记录
  95. await mixRatioModel.model.deleteMany({unit_price_file_id:unitFileID,code:'80CCS'});
  96. }else {//如果是从空选择车船税或者是切换车船税,则要插入或修改车船税组成物信息
  97. let vvTaxItems = await vvTaxModel.find({libID:newVvTaxFileID});
  98. if(vvTaxItems.length <=0) return;
  99. let allMixRatio = await mixRatioModel.model.find({unit_price_file_id:unitFileID});//先查出所有组成物
  100. let mxiMap = _.groupBy(allMixRatio,"connect_key");
  101. let counterModel = new CounterModel();
  102. let useMap = {},tasks = [];
  103. for(let item of vvTaxItems){
  104. let connect_key = gljUtil.getIndex(item);
  105. if(mxiMap[connect_key]){//如果存在,说明要插入或更新车船税
  106. let ccs = _.find(mxiMap[connect_key],{'code':'80CCS'});
  107. if(ccs){//存在且不相等则更新
  108. if(ccs.consumption != item.vehicleVesselTax) tasks.push({updateOne: {filter: {id: ccs.id}, update: {consumption: item.vehicleVesselTax}}});
  109. }else {//不存在则插入
  110. let id = await counterModel.getId('mix_ratio');
  111. let newM = gljUtil.getBaseCCSMixRatio(unitFileID,item.vehicleVesselTax,connect_key);
  112. newM.id = id;
  113. tasks.push({insertOne: {document: newM}});
  114. }
  115. useMap[connect_key] = true;
  116. }
  117. }
  118. //找出旧的车船税文件有的,新的车船税文件没有的,则删除该记录
  119. for(let ckey in mxiMap){
  120. if(useMap[ckey]) continue;
  121. let deccs = _.find(mxiMap[ckey],{'code':'80CCS'});
  122. if(deccs){
  123. tasks.push( {deleteOne:{filter:{id: deccs.id}}});
  124. }
  125. }
  126. if(tasks.length > 0) await mixRatioModel.model.bulkWrite(tasks);
  127. }
  128. }
  129. //新增单价文件的同时,在项目工料机和空白的单价文件中先插入机械组成物等信息
  130. async function addMixRatioForNew(projectID,unitFileId,engineerID,ext){
  131. /* 1050001机械工 工日 机上人工/ 3003001 重油 kg 普通材料/ 3003002 汽油 93号 kg 普通材料
  132. 3003003 柴油 0号,-10号,-20号 kg 普通材料/ 3003004 丙烷 kg 普通材料/ 3005001 煤 t 普通材料
  133. 3005002 电 kw·h 普通材料/ 3005004 水 m3 普通材料/ 3005005 液化天然气 m3 普通材料
  134. 02JXF 折旧费 元 机械组成物/ 03WHF 检修费 元 机械组成物/ 04ACFZF 维护费 元 机械组成物
  135. 05QT 安拆辅助费 元 机械组成物/ 80CCS 车船税 元 机械组成物*/
  136. let mixCodes = ['1050001','3003001','3003002','3003003','3003004','3005001','3005002','3005004','3005005','02JXF','03WHF','04ACFZF','05QT','80CCS'];
  137. let libID = await ration_glj.getGLJLibByEngineerID(engineerID);
  138. let stdGljs = await std_glj_lib_gljList_model.find({'repositoryId':libID,'code':{'$in':mixCodes}});
  139. let projectGljModel = new GLJListModel();
  140. for (let glj of stdGljs){
  141. let tem = {projectID:projectID};
  142. ration_glj.setPropertyFromStd(tem,glj);
  143. await projectGljModel.addList(ration_glj.getGLJSearchInfo(tem),unitFileId,ext);
  144. }
  145. }