glj_facade.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * Created by zhang on 2019/1/2.
  3. */
  4. module.exports={ //先导出后require可以解决循环引用问题
  5. changeUnitFile:changeUnitFile,
  6. changeVvTaxFile:changeVvTaxFile,
  7. changeAssistProductionFeeRate:changeAssistProductionFeeRate,
  8. addMixRatioForNew:addMixRatioForNew,
  9. updateMaterialCalc:updateMaterialCalc,
  10. updateMaterialCalcTasks:updateMaterialCalcTasks,
  11. updateUserFreight:updateUserFreight,
  12. getUserFreights:getUserFreights
  13. };
  14. const mongoose = require('mongoose');
  15. const ProjectModel = require('../../pm/models/project_model').project;
  16. import UnitPriceFileModel from "../models/unit_price_file_model";
  17. import UnitPriceModel from "../models/unit_price_model";
  18. import MixRatioModel from "../models/mix_ratio_model";
  19. import CounterModel from "../models/counter_model";
  20. import GLJListModel from '../../glj/models/glj_list_model';
  21. let std_glj_lib_gljList_model = mongoose.model('std_glj_lib_gljList');
  22. let original_calc_model = mongoose.model('original_calc');
  23. let freight_calc_model = mongoose.model('freight_calc');
  24. let unit_price_model = mongoose.model('unit_price');
  25. let user_freights_model = mongoose.model('user_freights');
  26. let ration_glj = require('../../ration_glj/facade/ration_glj_facade');
  27. const uuidV1 = require('uuid/v1');
  28. let vvTaxModel = mongoose.model("std_vehicleVesselTax_items");
  29. let gljUtil = require('../../../public/gljUtil');
  30. let _ = require("lodash");
  31. async function changeUnitFile(projectData,unitFile,type,userID) {
  32. let projectId = projectData.projectID;
  33. let changeUnitPriceId = unitFile.id;
  34. let newName = unitFile.name;
  35. type = parseInt(type);
  36. let currentUnitPriceId = await ProjectModel.getUnitPriceFileId(projectId);
  37. let unitPriceFileModel = new UnitPriceFileModel();
  38. let insertData = null;
  39. if (type > 0) {
  40. let currentUnitPrice = await unitPriceFileModel.findDataByCondition({id: changeUnitPriceId});
  41. if (currentUnitPrice === null) {
  42. throw '不存在对应单价文件';
  43. }
  44. // 获取当前项目的rootProjectId
  45. let projectData = await ProjectModel.getProject(projectId);
  46. let rootProjectId = projectData.property.rootProjectID !== undefined ? projectData.property.rootProjectID : 0;
  47. insertData = JSON.parse(JSON.stringify(currentUnitPrice));
  48. insertData.root_project_id = rootProjectId;
  49. newName?insertData.name = newName:'';
  50. insertData.user_id = userID;
  51. delete insertData._id;
  52. delete insertData.ID;
  53. }
  54. // 获取即将更改的单价文件信息
  55. let targetUnitPriceFile = type === 0 ? await unitPriceFileModel.findDataByCondition({id: changeUnitPriceId}) :
  56. await unitPriceFileModel.add(insertData);
  57. if (targetUnitPriceFile === null) {
  58. throw '没有找到对应的单价文件';
  59. }
  60. // 查找对应单价文件的项目工料机数据
  61. let unitPriceModel = new UnitPriceModel();
  62. /* if(type ===1){//从其它项目复制,则先复制一份数据。 后面会做复制操作了,
  63. let needCopyList = await unitPriceModel.findDataByCondition({unit_price_file_id: changeUnitPriceId}, null, false);
  64. if(needCopyList){
  65. // 过滤mongoose格式
  66. needCopyList = JSON.stringify(needCopyList);
  67. needCopyList = JSON.parse(needCopyList);
  68. let copyList = [];
  69. for(let n of needCopyList){
  70. delete n._id; // 删除原有id信息
  71. delete n.id;
  72. n.unit_price_file_id = targetUnitPriceFile.id;
  73. copyList.push(n);
  74. }
  75. copyList.length>0 ? await unitPriceModel.add(copyList):'';
  76. }
  77. }*/
  78. let copyResult = await unitPriceModel.copyNotExist(currentUnitPriceId, targetUnitPriceFile.id,projectId);
  79. // 复制成功后更改project数据
  80. if (!copyResult) {
  81. throw '复制数据失败';
  82. }
  83. let changeUnitPriceFileInfo = {
  84. id: targetUnitPriceFile.id,
  85. name: targetUnitPriceFile.name
  86. };
  87. let result = ProjectModel.changeUnitPriceFileInfo(projectId, changeUnitPriceFileInfo);
  88. if (!result) {
  89. throw '切换单价文件失败!';
  90. }
  91. //处理车船税记录
  92. await handleVvTaxForChang(targetUnitPriceFile.id,targetUnitPriceFile.vvTaxFileID);
  93. return changeUnitPriceFileInfo;
  94. }
  95. async function changeVvTaxFile(data){//对于车船税,现在只是在组成物表中插入信息,取项目工料机时虚拟一条车船况的记录。 因为车船税涉及到切换,清空,单价文件共用等复杂的情况,虚拟一条记录相对好一点。以后不合适可再修改
  96. let unitFileID = data.unitFileID,newVvTaxFileID = data.newVvTaxFileID;
  97. let unitPriceFileModel = new UnitPriceFileModel();
  98. //先更新单价文件中车船税文件ID
  99. await unitPriceFileModel.model.update({id:unitFileID},{vvTaxFileID:newVvTaxFileID});
  100. await handleVvTaxForChang(unitFileID,newVvTaxFileID);
  101. }
  102. async function changeAssistProductionFeeRate(data){
  103. let unitFileID = data.unitFileID,assistProductionFeeRate = data.assistProductionFeeRate;
  104. let unitPriceFileModel = new UnitPriceFileModel();
  105. //先更新单价文件中车船税文件ID
  106. await unitPriceFileModel.model.update({id:unitFileID},{assistProductionFeeRate:assistProductionFeeRate});
  107. }
  108. async function handleVvTaxForChang(unitFileID,newVvTaxFileID) {//切换车船税,选择共用的单价文件时调用
  109. let mixRatioModel = new MixRatioModel();
  110. if(!newVvTaxFileID || newVvTaxFileID == ''){//如果新的车船税ID为空,则删除组成物表中车船税的记录
  111. await mixRatioModel.model.deleteMany({unit_price_file_id:unitFileID,code:'80CCS'});
  112. }else {//如果是从空选择车船税或者是切换车船税,则要插入或修改车船税组成物信息
  113. let vvTaxItems = await vvTaxModel.find({libID:newVvTaxFileID});
  114. if(vvTaxItems.length <=0) return;
  115. let allMixRatio = await mixRatioModel.model.find({unit_price_file_id:unitFileID});//先查出所有组成物
  116. let mxiMap = _.groupBy(allMixRatio,"connect_key");
  117. let counterModel = new CounterModel();
  118. let useMap = {},tasks = [];
  119. for(let item of vvTaxItems){
  120. let connect_key = gljUtil.getIndex(item);
  121. if(mxiMap[connect_key]){//如果存在,说明要插入或更新车船税
  122. let ccs = _.find(mxiMap[connect_key],{'code':'80CCS'});
  123. if(ccs){//存在且不相等则更新
  124. if(ccs.consumption != item.vehicleVesselTax) tasks.push({updateOne: {filter: {id: ccs.id}, update: {consumption: item.vehicleVesselTax}}});
  125. }else {//不存在则插入
  126. let id = await counterModel.getId('mix_ratio');
  127. let newM = gljUtil.getBaseCCSMixRatio(unitFileID,item.vehicleVesselTax,connect_key);
  128. newM.id = id;
  129. tasks.push({insertOne: {document: newM}});
  130. }
  131. useMap[connect_key] = true;
  132. }
  133. }
  134. //找出旧的车船税文件有的,新的车船税文件没有的,则删除该记录
  135. for(let ckey in mxiMap){
  136. if(useMap[ckey]) continue;
  137. let deccs = _.find(mxiMap[ckey],{'code':'80CCS'});
  138. if(deccs){
  139. tasks.push( {deleteOne:{filter:{id: deccs.id}}});
  140. }
  141. }
  142. if(tasks.length > 0) await mixRatioModel.model.bulkWrite(tasks);
  143. }
  144. }
  145. //新增单价文件的同时,在项目工料机和空白的单价文件中先插入机械组成物等信息
  146. async function addMixRatioForNew(projectID,unitFileId,engineerID,ext){
  147. /* 1050001机械工 工日 机上人工/ 3003001 重油 kg 普通材料/ 3003002 汽油 93号 kg 普通材料
  148. 3003003 柴油 0号,-10号,-20号 kg 普通材料/ 3003004 丙烷 kg 普通材料/ 3005001 煤 t 普通材料
  149. 3005002 电 kw·h 普通材料/ 3005004 水 m3 普通材料/ 3005005 液化天然气 m3 普通材料
  150. 02JXF 折旧费 元 机械组成物/ 03WHF 检修费 元 机械组成物/ 04ACFZF 维护费 元 机械组成物
  151. 05QT 安拆辅助费 元 机械组成物/ 80CCS 车船税 元 机械组成物*/
  152. let mixCodes = ['1050001','3003001','3003002','3003003','3003004','3005001','3005002','3005004','3005005','02JXF','03WHF','04ACFZF','05QT','80CCS'];
  153. let libID = await ration_glj.getGLJLibByEngineerID(engineerID);
  154. let stdGljs = await std_glj_lib_gljList_model.find({'repositoryId':libID,'code':{'$in':mixCodes}});
  155. let projectGljModel = new GLJListModel();
  156. for (let glj of stdGljs){
  157. let tem = {projectID:projectID};
  158. ration_glj.setPropertyFromStd(tem,glj);
  159. await projectGljModel.addList(ration_glj.getGLJSearchInfo(tem),unitFileId,ext);
  160. }
  161. }
  162. async function updateUnitPrice(unitPrice) {//同时支持单个或多个更新
  163. if(Array.isArray(unitPrice)){
  164. let tasks = [];
  165. for(let u of unitPrice){
  166. if(u && !_.isEmpty(u.doc)) tasks.push({updateOne: {filter: {id: u.id,unit_price_file_id:u.unit_price_file_id}, update: u.doc}});
  167. }
  168. if(tasks.length > 0) await unit_price_model.bulkWrite(tasks);
  169. }else {
  170. if(unitPrice && !_.isEmpty(unitPrice.doc)){
  171. await unit_price_model.findOneAndUpdate({id:unitPrice.id,unit_price_file_id:unitPrice.unit_price_file_id},unitPrice.doc);
  172. }
  173. }
  174. }
  175. async function updateUserFreight(data,req) {
  176. let datas = [];
  177. if(data.action == "add"){
  178. for(let f of data.freights){
  179. f.ID = uuidV1();
  180. f.compilationID = req.session.sessionCompilation._id;
  181. f.userID = req.session.sessionUser.id;
  182. datas.push(f);
  183. }
  184. console.log(datas);
  185. return await user_freights_model.create(datas);
  186. }else if(data.action == "update"){//暂时做成只支持单个更新
  187. return await user_freights_model.update({"ID":data.ID},data.doc);
  188. }
  189. }
  190. async function getUserFreights(req) {
  191. return await user_freights_model.find({"userID":req.session.sessionUser.id,"compilationID": req.session.sessionCompilation._id}).lean();
  192. }
  193. async function updateMaterialCalcTasks(data) {
  194. let freightTasks = [];
  195. let priceTasks = [];
  196. for(let key in data){
  197. if(key == "freight"){
  198. let fu = data[key];
  199. genTasks(fu,freightTasks);
  200. }
  201. if(key =="price"){
  202. let pu = data[key];
  203. genTasks(pu,priceTasks);
  204. }
  205. }
  206. if(freightTasks.length > 0) await freight_calc_model.bulkWrite(freightTasks);
  207. if(priceTasks.length > 0) await original_calc_model.bulkWrite(priceTasks);
  208. if(data["unitPrice"]) await updateUnitPrice(data["unitPrice"]);
  209. function genTasks(obj,tasks) {
  210. for(let key in obj){
  211. if(key == "update"){
  212. let u = obj[key];
  213. for(let ID in u ){
  214. tasks.push({updateOne: {filter: {ID: ID}, update: u[ID]}})
  215. }
  216. }
  217. if(key == "add"){
  218. let a = obj[key];
  219. for(let d of a){
  220. // d.ID = uuidV1(); 改成在前端自动生成
  221. tasks.push({insertOne: {document: d}});
  222. }
  223. }
  224. if(key == "delete"){
  225. let r = obj[key];
  226. for(let ID in r){
  227. tasks.push({deleteOne: {filter:{ID: ID}}});
  228. }
  229. }
  230. }
  231. }
  232. }
  233. async function updateMaterialCalc(data) {
  234. if(data.tasks.length > 0) await unit_price_model.bulkWrite(data.tasks);
  235. return {}
  236. }