ration_facade.js 20 KB

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