ration_facade.js 18 KB

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