ration_facade.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /**
  2. * Created by zhang on 2018/2/9.
  3. */
  4. let mongoose = require('mongoose');
  5. import SearchDao from '../../complementary_ration_lib/models/searchModel';
  6. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  7. let ration_glj_facade = require("../../ration_glj/facade/ration_glj_facade");
  8. let quantity_detail = require("../facade/quantity_detail_facade");
  9. let ration_glj = mongoose.model('ration_glj');
  10. let ration_coe = mongoose.model('ration_coe');
  11. let ration_model = require('../models/ration');
  12. let bill_model = require('../models/bills');
  13. let decimal_facade = require('./decimal_facade');
  14. let installationFeeModel = mongoose.model("installation_fee");
  15. let rationInstallationModel = mongoose.model('ration_installation');
  16. const uuidV1 = require('uuid/v1');
  17. let std_glj_lib_gljList_model = mongoose.model('std_glj_lib_gljList');
  18. let complementary_glj_model = mongoose.model('complementary_glj_lib');
  19. let rationItemModel = mongoose.model("std_ration_lib_ration_items");
  20. let complementaryRationModel = mongoose.model('complementary_ration_items');
  21. let coeMolde = mongoose.model('std_ration_lib_coe_list');
  22. let compleCoeModel = mongoose.model('complementary_ration_coe_list');
  23. let _= require('lodash');
  24. const projectDao = require('../../pm/models/project_model').project;
  25. let projectModel = mongoose.model('projects');
  26. const fs = require('fs');
  27. module.exports = {
  28. replaceRations: replaceRations,
  29. addNewRation:addNewRation,
  30. addMultiRation: addMultiRation,
  31. getSameSectionRations:getSameSectionRations
  32. };
  33. async function addNewRation(data,compilation) {
  34. let query = data.itemQuery;
  35. let stdRation = null;
  36. let startTime = +new Date();
  37. if(query){
  38. let searchDao = new SearchDao();
  39. stdRation = await searchDao.getRationItem(query.userID, compilation._id, [query.rationRepId],query.code, query.ID);
  40. //data.newData.code = query.code;
  41. }
  42. let stdRationTime = +new Date();
  43. console.log("取std定额时间-------------------------------"+(stdRationTime - startTime));
  44. if(data.brUpdate.length>0){
  45. await updateSerialNo(data.brUpdate);
  46. }
  47. let newRation =await insertNewRation(data.newData,data.defaultLibID,stdRation,data.calQuantity);
  48. let addRationGLJTime = +new Date();
  49. console.log("插入新定额时间-------------------------------"+(addRationGLJTime - stdRationTime));
  50. if(stdRation){
  51. return await addRationSubList(stdRation,newRation,data.needInstall,compilation);
  52. }else {
  53. return {ration:newRation};
  54. }
  55. }
  56. async function addMultiRation(datas,compilation) {
  57. let rst = [];
  58. for(let data of datas){
  59. let r = await addNewRation(data,compilation);
  60. rst.push(r);
  61. }
  62. return rst;
  63. }
  64. async function getSameSectionRations(data,userId,compilationId){
  65. //let userId
  66. //要先根据定额获取所属章节的ID
  67. let from = data.from; //定额类型,是标准的还是用户定义的
  68. let code = data.code;
  69. let libID = data.libID;
  70. let sectionId,rations=[];
  71. if(from == 'std'){
  72. let ration = await rationItemModel.findOne({rationRepId:libID,code:code},['sectionId']);
  73. sectionId = ration? ration.sectionId:null;
  74. }else {
  75. let ration = await complementaryRationModel.findOne({userId:userId,compilationId: compilationId,code:code},['sectionId']);
  76. sectionId = ration?ration.sectionId:null;
  77. }
  78. if(sectionId){
  79. if (from == 'std') {
  80. rations = await rationItemModel.find({sectionId: sectionId});
  81. } else {
  82. rations = await complementaryRationModel.find({userId: userId, sectionId: sectionId});
  83. }
  84. rations = _.sortBy(rations,'code');
  85. }
  86. return rations
  87. }
  88. async function updateSerialNo(serialNoUpdate){
  89. let tasks=[];
  90. for(let data of serialNoUpdate){
  91. let task={
  92. updateOne:{
  93. filter:{
  94. ID:data.ID,
  95. projectID:data.projectID
  96. },
  97. update :{
  98. serialNo:data.serialNo
  99. }
  100. }
  101. };
  102. tasks.push(task);
  103. }
  104. await ration_model.model.bulkWrite(tasks);
  105. }
  106. async function insertNewRation(newData,defaultLibID,std,calQuantity) {//插入新的定额
  107. let startTime = +new Date();
  108. if(std){
  109. newData.code = std.code;
  110. newData.name = std.name;
  111. newData.caption = std.caption;
  112. newData.unit = std.unit;
  113. newData.libID = std.rationRepId;
  114. newData.content = std.jobContent;
  115. newData.annotation = std.annotation;
  116. if (std.chapter) {
  117. newData.comments = std.chapter.explanation;
  118. newData.ruleText = std.chapter.ruleText;
  119. }
  120. newData.prefix = '';
  121. newData.from = std.type === 'complementary' ? 'cpt' : 'std';
  122. if(defaultLibID !== std.rationRepId){//借
  123. newData.prefix = '借';
  124. }
  125. else if(std.rationRepId === defaultLibID && newData.from === 'cpt') {
  126. newData.prefix = '补';
  127. }
  128. if(std.feeType == undefined || std.feeType == null || std.feeType ==''){//定额取费专业为空的情况下,取项目属性中的定额取费专业ID
  129. newData.programID = await getProgramForProject(newData.projectID);
  130. }else {
  131. newData.programID = std.feeType;
  132. }
  133. newData.rationAssList = createRationAss(std);
  134. // calculate ration Quantity
  135. }
  136. if(calQuantity){
  137. await CalculateQuantity(newData,newData.billsItemID,newData.projectID);
  138. }
  139. let addRationGLJTime = +new Date();
  140. console.log("计算消耗量时间-------------------------------"+(addRationGLJTime - startTime));
  141. let newRation = await ration_model.model.create(newData);
  142. return newRation;
  143. /*ration_model.model.create(newData);
  144. return newData;*/
  145. }
  146. async function replaceRations(userID,data,compilation) {
  147. let searchDao = new SearchDao();
  148. let recodes = [];
  149. for(let recode of data.nodeInfo){
  150. let stdRation = await searchDao.getRationItem(userID,compilation._id,data.libIDs,recode.newCode, null);
  151. let newRecode = await replaceRation(recode,stdRation,data.defaultLibID,data.projectID,data.calQuantity,compilation);
  152. if(newRecode){
  153. recodes.push(newRecode);
  154. }else {
  155. break;
  156. }
  157. }
  158. return recodes;
  159. }
  160. async function replaceRation(nodeInfo,stdRation,defaultLibID,projectID,calQuantity,compilation) {
  161. if(nodeInfo.newCode == null||nodeInfo.newCode ==""){//说明是删除编号,则要变成一条空定额
  162. await deleRationSubRecode(projectID,nodeInfo.ID);//删除定额下挂的各种数据,如定额工料机等
  163. return await setEmptyRation(projectID,nodeInfo.ID);
  164. }else if(stdRation){
  165. await deleRationSubRecode(projectID,nodeInfo.ID);//删除定额下挂的各种数据,如定额工料机等
  166. let newRation = await updateRation(stdRation,defaultLibID,nodeInfo.ID,nodeInfo.billsItemID,projectID,calQuantity);//生成并插入新的定额
  167. return await addRationSubList(stdRation,newRation,nodeInfo.needInstall,compilation);
  168. }else {
  169. return null;
  170. }
  171. }
  172. async function addRationSubList(stdRation,newRation,needInstall,compilation) {
  173. let startTime = +new Date();
  174. let ration_gljs = await addRationGLJ(stdRation,newRation);
  175. let addRationGLJTime = +new Date();
  176. console.log("添加定额工料机时间-----"+(addRationGLJTime - startTime));
  177. let ration_coes = await addRationCoe(stdRation,newRation,compilation);
  178. let addRationCoeTime = +new Date();
  179. console.log("添加定额coe时间-----"+(addRationCoeTime - addRationGLJTime));
  180. let ration_installs = [];
  181. if(needInstall){
  182. ration_installs = await addRationInstallFee(stdRation,newRation);
  183. }
  184. let addRationInstallFeeTime = +new Date();
  185. console.log("添加定额install时间-----"+(addRationInstallFeeTime - addRationCoeTime));
  186. return {ration:newRation,ration_gljs:ration_gljs,ration_coes:ration_coes,ration_installs:ration_installs};
  187. }
  188. async function addRationInstallFee(std,newRation) {
  189. let install_fee_list = [];
  190. if(std.hasOwnProperty('rationInstList') && std.rationInstList.length > 0){
  191. let installFee = await installationFeeModel.findOne({'projectID': newRation.projectID});
  192. for(let ri of std.rationInstList){
  193. let feeItem = _.find(installFee.installFeeItem,{'ID':ri.feeItemId});
  194. let section = _.find(installFee.installSection,{'ID':ri.sectionId});
  195. if(feeItem&&section){
  196. let tem_r_i = {
  197. libID:installFee.libID,
  198. projectID:newRation.projectID,
  199. rationID:newRation.ID,
  200. feeItemId:feeItem.ID,
  201. sectionId:section.ID,
  202. itemName:feeItem.feeItem,
  203. feeType:feeItem.feeType,
  204. sectionName:section.name,
  205. unifiedSetting:1,
  206. ruleId:''
  207. };
  208. if(feeItem.isCal==1&&section.feeRuleId&&section.feeRuleId!=''){//勾选记取时并且有规则ID时才读取
  209. let feeRule = _.find(installFee.feeRule,{'ID':section.feeRuleId});
  210. if(feeRule){
  211. tem_r_i.ruleId = feeRule.ID;
  212. }
  213. }
  214. tem_r_i.ID = uuidV1();
  215. install_fee_list.push(tem_r_i);
  216. }
  217. }
  218. if(install_fee_list.length>0){
  219. await rationInstallationModel.insertMany(install_fee_list);
  220. }
  221. }
  222. return install_fee_list;
  223. }
  224. async function addRationCoe(std,newRation,compilation) {
  225. let ration_coe_list = [];
  226. let seq = 0;
  227. if(std.hasOwnProperty('rationCoeList')&&std.rationCoeList.length>0){//添加标准库的工料机
  228. for(let sub of std.rationCoeList){
  229. let libCoe;
  230. if (std.type === 'std') {
  231. libCoe = await coeMolde.findOne({'libID':std.rationRepId,'ID':sub.ID,"$or": [{"isDeleted": null}, {"isDeleted": false}]});//std.rationRepId;
  232. } else {
  233. libCoe = await compleCoeModel.findOne({ID: sub.ID, $or: [{deleteInfo: null}, {'deleteInfo.deleted': false}]});
  234. }
  235. if(libCoe){
  236. let newCoe = {};
  237. newCoe.ID = uuidV1();
  238. newCoe.coeID = sub.ID;
  239. newCoe.seq = seq;
  240. newCoe.name = libCoe.name;
  241. newCoe.content = libCoe.content;
  242. newCoe.isAdjust=0;
  243. newCoe.coes = libCoe.coes;
  244. newCoe.rationID = newRation.ID;
  245. newCoe.projectID = newRation.projectID;
  246. seq++;
  247. ration_coe_list.push(newCoe);
  248. }
  249. }
  250. }
  251. let lastCoe = await getCustomerCoe(newRation.projectID,newRation.ID,seq,compilation);
  252. ration_coe_list.push(lastCoe);
  253. await ration_coe.insertMany(ration_coe_list);
  254. return ration_coe_list;
  255. }
  256. function getCustomerCoeData() {
  257. var coeList = [
  258. {amount:1, operator:'*', gljCode:null, coeType:'定额'},
  259. { amount:1, operator:'*', gljCode:null, coeType:'人工'},
  260. { amount:1, operator:'*', gljCode:null, coeType:'材料'},
  261. { amount:1, operator:'*', gljCode:null, coeType:'机械'},
  262. { amount:1, operator:'*', gljCode:null, coeType:'主材'},
  263. { amount:1, operator:'*', gljCode:null, coeType:'设备'}
  264. ];
  265. return coeList;
  266. };
  267. async function getCustomerCoe(projectID,rationID,seq,compilation){//取自定义乘系数,根据编办不同,内容可能不同
  268. //生成默认的自定义乘系数
  269. let lastCoe ={
  270. coeID:-1,
  271. name : '自定义系数',
  272. content:'人工×1,材料×1,机械×1,主材×1,设备×1',
  273. isAdjust:0,
  274. seq:seq,
  275. rationID : rationID,
  276. projectID : projectID
  277. };
  278. lastCoe.ID = uuidV1();
  279. lastCoe.coes = getCustomerCoeData();
  280. try {
  281. //查看编办中有没有重写路径
  282. if(compilation.overWriteUrl && compilation.overWriteUrl!=""){
  283. let overWrite = require("../../.."+compilation.overWriteUrl);
  284. if(overWrite.getCusCoeContent) lastCoe.content = overWrite.getCusCoeContent();
  285. if(overWrite.getCustomerCoeData) lastCoe.coes = overWrite.getCustomerCoeData();
  286. }
  287. return lastCoe
  288. }catch (err){
  289. console.log("读取自定义系数重写文件失败");
  290. console.log(err.message);
  291. return lastCoe
  292. }
  293. }
  294. async function addRationGLJ(std,newRation) {
  295. let newRationGLJList = [];
  296. let rationGLJShowList = [];
  297. let unitPriceFileId = await projectDao.getUnitPriceFileId(newRation.projectID);
  298. let sum=0;
  299. let first = +new Date();
  300. if(std.hasOwnProperty('rationGljList') && std.rationGljList.length > 0){
  301. let stdGLJID =[];//标准工料机ID数组
  302. let cptGLJID=[];//补充工料机ID数组
  303. //let stdGLJID = _.map(std.rationGljList,'gljId');
  304. for(let tem_g of std.rationGljList){
  305. if(tem_g.type == 'complementary'){
  306. cptGLJID.push(tem_g.gljId);
  307. }else {
  308. stdGLJID.push(tem_g.gljId);
  309. }
  310. }
  311. let stdGLJList = stdGLJID.length > 0 ? await std_glj_lib_gljList_model.find({'ID':{'$in':stdGLJID}}):[];//速度优化-------先一次性取出所有的工料机列表
  312. let stdGLJMap = _.indexBy(stdGLJList, 'ID');
  313. let cptGLJList = cptGLJID.length > 0 ? await complementary_glj_model.find({'userId':std.userId,'ID':{'$in':cptGLJID}}):[];
  314. let cptGLJMap = _.indexBy(cptGLJList, 'ID');
  315. let stdGLJMapTime = +new Date();
  316. console.log("找到工料机映射表时间-------------------------------"+(stdGLJMapTime - first));
  317. for(let sub of std.rationGljList){
  318. let newGLJ = {};
  319. newGLJ.ID = uuidV1();
  320. newGLJ.projectID = newRation.projectID;
  321. newGLJ.GLJID = sub.gljId;
  322. newGLJ.rationID = newRation.ID;
  323. newGLJ.billsItemID = newRation.billsItemID;
  324. newGLJ.rationItemQuantity = sub.consumeAmt;
  325. newGLJ.quantity = sub.consumeAmt;
  326. newGLJ.glj_repository_id = std.rationRepId;
  327. let std_glj = null;
  328. if(sub.type == 'complementary'){//有可能来自标准工料机库或补充工料机库
  329. std_glj = cptGLJMap[sub.gljId];
  330. newGLJ.from = 'cpt';
  331. }else {
  332. std_glj = stdGLJMap[sub.gljId];
  333. newGLJ.from = 'std';
  334. }
  335. let std_gljTime = +new Date();
  336. if(std_glj){
  337. newGLJ.name = std_glj.name;
  338. newGLJ.code = std_glj.code;
  339. newGLJ.original_code = std_glj.code;
  340. newGLJ.unit = std_glj.unit;
  341. newGLJ.specs = std_glj.specs;
  342. newGLJ.model = std_glj.model;
  343. newGLJ.basePrice = std_glj.basePrice;
  344. newGLJ.marketPrice = std_glj.basePrice;
  345. newGLJ.shortName = std_glj.shortName;
  346. newGLJ.type = std_glj.gljType;
  347. newGLJ.repositoryId = std_glj.repositoryId;
  348. newGLJ.adjCoe = std_glj.adjCoe;
  349. newGLJ.materialType = std_glj.materialType;
  350. newGLJ.materialCoe = std_glj.materialCoe;
  351. newGLJ.createType = 'normal';
  352. let info = await ration_glj_facade.getInfoFromProjectGLJ(newGLJ,unitPriceFileId);
  353. newGLJ = ration_glj_facade.createNewRecord(info);
  354. newRationGLJList.push(newGLJ);
  355. rationGLJShowList.push(info);
  356. }
  357. let InfoFromProjectGLJ = +new Date();
  358. console.log("找到项目工料机时间-------------------------------"+(InfoFromProjectGLJ - std_gljTime));
  359. }
  360. }
  361. let before = +new Date();
  362. console.log("总查询时间为-------------------------------"+(before-first));
  363. if(newRationGLJList.length>0){
  364. await ration_glj.insertMany(newRationGLJList);
  365. }
  366. let after = +new Date();
  367. console.log("实际插入时间为-------------------------------"+(after-before));
  368. console.log("总操作时间为-------------------------------"+(after-first));
  369. return rationGLJShowList;
  370. }
  371. async function deleRationSubRecode(projectID,rationID) {//删除挂在定额下的数据,如工程量明细,定额工料机等
  372. let delete_query={projectID: projectID, rationID: rationID};
  373. //删除工程量明细
  374. await quantity_detail.deleteByQuery(delete_query) ;
  375. await ration_coe.deleteMany(delete_query);//删除附注条件
  376. await ration_glj.deleteMany(delete_query);//删除定额工料机
  377. await rationInstallationModel.deleteMany(delete_query);//删除安装增加费
  378. }
  379. async function updateRation(std,defaultLibID,rationID,billsItemID,projectID,calQuantity) {
  380. // insertNewRation
  381. let ration ={};
  382. ration.code = std.code;
  383. ration.name = std.name;
  384. ration.caption = std.caption;
  385. ration.unit = std.unit;
  386. if (std.type === 'std') {
  387. ration.libID = std.rationRepId;
  388. }
  389. ration.content = std.jobContent;
  390. ration.adjustState = '';
  391. ration.isFromDetail=0;
  392. ration.isSubcontract=false;
  393. ration.fees=[];
  394. if (std.chapter) {
  395. ration.comments = std.chapter.explanation;
  396. ration.ruleText = std.chapter.ruleText;
  397. }
  398. ration.from = std.type === 'complementary' ? 'cpt' : 'std';
  399. //定额前缀 none:0, complementary:1, borrow: 2
  400. ration.prefix = '';
  401. //借用优先级比补充高
  402. if(std.rationRepId !== parseInt(defaultLibID)){//借用
  403. ration.prefix = '借';
  404. }
  405. else if(std.rationRepId === defaultLibID && ration.from === 'cpt') {
  406. ration.prefix = '补';
  407. }
  408. if(std.feeType == undefined || std.feeType == null || std.feeType ==''){//定额取费专业为空的情况下,取项目属性中的定额取费专业ID
  409. ration.programID = await getProgramForProject(projectID);
  410. }else {
  411. ration.programID = std.feeType;
  412. }
  413. ration.rationAssList = createRationAss(std);//生成辅助定额
  414. if(calQuantity){
  415. await CalculateQuantity(ration,billsItemID,projectID);
  416. }
  417. let unsetObject = {
  418. "marketUnitFee":1,
  419. 'marketTotalFee':1,
  420. "maskName":1
  421. }
  422. let newRation = await ration_model.model.findOneAndUpdate({ID:rationID,projectID:projectID},{"$set":ration,"$unset":unsetObject},{new: true});//;
  423. return newRation;
  424. }
  425. async function setEmptyRation(projectID,rationID){
  426. let ration ={};
  427. ration.code = "";
  428. ration.name = "";
  429. ration.caption = "";
  430. ration.unit = "";
  431. ration.libID = null;
  432. ration.content = "";
  433. ration.adjustState = '';
  434. ration.isFromDetail=0;
  435. ration.isSubcontract=false;
  436. ration.fees=[];
  437. ration.comments = "";
  438. ration.ruleText = "";
  439. ration.quantity="";
  440. ration.contain="";
  441. ration.quantityEXP="";
  442. ration.from = 'std';
  443. //定额前缀 none:0, complementary:1, borrow: 2
  444. ration.prefix = '';
  445. ration.rationAssList = [];
  446. ration.marketUnitFee ="";
  447. ration.marketTotalFee ="";
  448. ration.maskName = "";
  449. ration.targetTotalFee ='';
  450. ration.targetUnitFee = "";
  451. ration.deleteInfo = null;
  452. ration.quantityCoe = {};
  453. ration.rationQuantityCoe="";
  454. ration.tenderQuantity = "";
  455. ration.programID = null;
  456. let newRation = await ration_model.model.findOneAndUpdate({ID:rationID,projectID:projectID},{"$set":ration},{new: true});//;
  457. return {ration:newRation,ration_gljs:[],ration_coes:[],ration_installs:[]};
  458. }
  459. function createRationAss(std) {
  460. let rationAssList = [];//生成辅助定额
  461. if(std.hasOwnProperty('rationAssList')&&std.rationAssList.length>0){
  462. for(let i=0;i<std.rationAssList.length;i++){
  463. let ass = std.rationAssList[i];
  464. ass.actualValue = ass.stdValue;
  465. rationAssList.push(ass);
  466. }
  467. }
  468. return rationAssList;
  469. }
  470. async function CalculateQuantity (ration,billsItemID,projectID) {
  471. // calculate ration Quantity
  472. let project = await projectModel.findOne({ID:projectID});
  473. let decimalObject =await decimal_facade.getProjectDecimal(projectID,project);
  474. let quantity_decimal = (decimalObject&&decimalObject.ration&&decimalObject.ration.quantity)?decimalObject.ration.quantity:3;
  475. let pbill = await bill_model.model.findOne({projectID:projectID,ID:billsItemID});
  476. let t_unit = ration.unit?ration.unit.replace(/^\d+/,""):"";
  477. if(t_unit!=pbill.unit){//如果定额工程量的单位去除前面的数字后不等于清单单位,定额工程量保持不变
  478. return ;
  479. }
  480. let billsQuantity = pbill.quantity ? pbill.quantity : 0;
  481. let bill_decimal = await decimal_facade.getBillsQuantityDecimal(projectID,pbill.unit,project);
  482. billsQuantity=scMathUtil.roundForObj(billsQuantity,bill_decimal);
  483. ration.quantityEXP="QDL";
  484. ration.quantity = scMathUtil.roundForObj(billsQuantity / FilterNumberFromUnit(ration.unit),quantity_decimal);//不管是否打勾都做转换
  485. ration.contain = scMathUtil.roundForObj(ration.quantity/billsQuantity,6);
  486. };
  487. async function getProgramForProject(projectID){
  488. let project = await projectModel.findOne({ID:projectID});
  489. return project.property.engineering;
  490. }
  491. function FilterNumberFromUnit (unit) {
  492. let reg = new RegExp('^[0-9]+');
  493. if (reg.test(unit)) {
  494. return parseInt(unit.match(reg)[0]);
  495. } else {
  496. return 1;
  497. }
  498. };