ration_facade.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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(stdRation){
  130. await deleRationSubRecode(projectID,nodeInfo.ID);
  131. let newRation = await updateRation(stdRation,firstLibID,nodeInfo.ID,nodeInfo.billsItemID,projectID,calQuantity);//生成并插入新的定额
  132. return await addRationSubList(stdRation,newRation,nodeInfo.needInstall);
  133. }else {
  134. return null;
  135. }
  136. }
  137. async function addRationSubList(stdRation,newRation,needInstall) {
  138. let startTime = +new Date();
  139. let ration_gljs = await addRationGLJ(stdRation,newRation);
  140. let addRationGLJTime = +new Date();
  141. console.log("添加定额工料机时间-----"+(addRationGLJTime - startTime));
  142. let ration_coes = await addRationCoe(stdRation,newRation);
  143. let addRationCoeTime = +new Date();
  144. console.log("添加定额coe时间-----"+(addRationCoeTime - addRationGLJTime));
  145. let ration_installs = [];
  146. if(needInstall){
  147. ration_installs = await addRationInstallFee(stdRation,newRation);
  148. }
  149. let addRationInstallFeeTime = +new Date();
  150. console.log("添加定额install时间-----"+(addRationInstallFeeTime - addRationCoeTime));
  151. return {ration:newRation,ration_gljs:ration_gljs,ration_coes:ration_coes,ration_installs:ration_installs};
  152. }
  153. async function addRationInstallFee(std,newRation) {
  154. let install_fee_list = [];
  155. if(std.hasOwnProperty('rationInstList') && std.rationInstList.length > 0){
  156. let installFee = await installationFeeModel.findOne({'projectID': newRation.projectID});
  157. for(let ri of std.rationInstList){
  158. let feeItem = _.find(installFee.installFeeItem,{'ID':ri.feeItemId});
  159. let section = _.find(installFee.installSection,{'ID':ri.sectionId});
  160. if(feeItem&&section){
  161. let tem_r_i = {
  162. libID:installFee.libID,
  163. projectID:newRation.projectID,
  164. rationID:newRation.ID,
  165. feeItemId:feeItem.ID,
  166. sectionId:section.ID,
  167. itemName:feeItem.feeItem,
  168. feeType:feeItem.feeType,
  169. sectionName:section.name,
  170. unifiedSetting:1,
  171. ruleId:''
  172. };
  173. if(feeItem.isCal==1&&section.feeRuleId&&section.feeRuleId!=''){//勾选记取时并且有规则ID时才读取
  174. let feeRule = _.find(installFee.feeRule,{'ID':section.feeRuleId});
  175. if(feeRule){
  176. tem_r_i.ruleId = feeRule.ID;
  177. }
  178. }
  179. tem_r_i.ID = uuidV1();
  180. install_fee_list.push(tem_r_i);
  181. }
  182. }
  183. if(install_fee_list.length>0){
  184. await rationInstallationModel.insertMany(install_fee_list);
  185. }
  186. }
  187. return install_fee_list;
  188. }
  189. async function addRationCoe(std,newRation) {
  190. let ration_coe_list = [];
  191. let seq = 0;
  192. if(std.hasOwnProperty('rationCoeList')&&std.rationCoeList.length>0){//添加标准库的工料机
  193. for(let sub of std.rationCoeList){
  194. let libCoe = await coeMolde.findOne({'libID':std.rationRepId,'ID':sub.ID,"$or": [{"isDeleted": null}, {"isDeleted": false}]});//std.rationRepId;
  195. if(libCoe){
  196. let newCoe = {};
  197. newCoe.ID = uuidV1();
  198. newCoe.coeID = sub.ID;
  199. newCoe.seq = seq;
  200. newCoe.name = libCoe.name;
  201. newCoe.content = libCoe.content;
  202. newCoe.isAdjust=0;
  203. newCoe.coes = libCoe.coes;
  204. newCoe.rationID = newRation.ID;
  205. newCoe.projectID = newRation.projectID;
  206. seq++;
  207. ration_coe_list.push(newCoe);
  208. }
  209. }
  210. }
  211. let lastCoe ={
  212. coeID:-1,
  213. name : '自定义系数',
  214. content:'人工×1,材料×1,机械×1,主材×1,设备×1',
  215. isAdjust:0,
  216. seq:seq,
  217. rationID : newRation.ID,
  218. projectID : newRation.projectID
  219. };
  220. lastCoe.ID = uuidV1();
  221. lastCoe.coes = getCustomerCoeData();
  222. ration_coe_list.push(lastCoe);
  223. await ration_coe.insertMany(ration_coe_list);
  224. return ration_coe_list;
  225. }
  226. function getCustomerCoeData() {
  227. var coeList = [];
  228. coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'定额'});
  229. coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'人工'});
  230. coeList.push({ amount:1, operator:'*', gljCode:null, coeType:'材料'});
  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. return coeList;
  235. };
  236. async function addRationGLJ(std,newRation) {
  237. let newRationGLJList = [];
  238. let rationGLJShowList = [];
  239. let unitPriceFileId = await projectDao.getUnitPriceFileId(newRation.projectID);
  240. let sum=0;
  241. let first = +new Date();
  242. if(std.hasOwnProperty('rationGljList') && std.rationGljList.length > 0){
  243. let stdGLJID =[];//标准工料机ID数组
  244. let cptGLJID=[];//补充工料机ID数组
  245. //let stdGLJID = _.map(std.rationGljList,'gljId');
  246. for(let tem_g of std.rationGljList){
  247. if(tem_g.type == 'complementary'){
  248. cptGLJID.push(tem_g.gljId);
  249. }else {
  250. stdGLJID.push(tem_g.gljId);
  251. }
  252. }
  253. let stdGLJList = stdGLJID.length > 0 ? await std_glj_lib_gljList_model.find({'ID':{'$in':stdGLJID}}):[];//速度优化-------先一次性取出所有的工料机列表
  254. let stdGLJMap = _.indexBy(stdGLJList, 'ID');
  255. let cptGLJList = cptGLJID.length > 0 ? await complementary_glj_model.find({'userId':std.userId,'ID':{'$in':cptGLJID}}):[];
  256. let cptGLJMap = _.indexBy(cptGLJList, 'ID');
  257. let stdGLJMapTime = +new Date();
  258. console.log("找到工料机映射表时间-------------------------------"+(stdGLJMapTime - first));
  259. for(let sub of std.rationGljList){
  260. let newGLJ = {};
  261. newGLJ.ID = uuidV1();
  262. newGLJ.projectID = newRation.projectID;
  263. newGLJ.GLJID = sub.gljId;
  264. newGLJ.rationID = newRation.ID;
  265. newGLJ.billsItemID = newRation.billsItemID;
  266. newGLJ.rationItemQuantity = sub.consumeAmt;
  267. newGLJ.quantity = sub.consumeAmt;
  268. newGLJ.glj_repository_id = std.rationRepId;
  269. let std_glj = null;
  270. if(sub.type == 'complementary'){//有可能来自标准工料机库或补充工料机库
  271. std_glj = cptGLJMap[sub.gljId];
  272. newGLJ.from = 'cpt';
  273. }else {
  274. std_glj = stdGLJMap[sub.gljId];
  275. newGLJ.from = 'std';
  276. }
  277. let std_gljTime = +new Date();
  278. if(std_glj){
  279. newGLJ.name = std_glj.name;
  280. newGLJ.code = std_glj.code;
  281. newGLJ.original_code = std_glj.code;
  282. newGLJ.unit = std_glj.unit;
  283. newGLJ.specs = std_glj.specs;
  284. newGLJ.basePrice = std_glj.basePrice;
  285. newGLJ.marketPrice = std_glj.basePrice;
  286. newGLJ.shortName = std_glj.shortName;
  287. newGLJ.type = std_glj.gljType;
  288. newGLJ.repositoryId = std_glj.repositoryId;
  289. newGLJ.adjCoe = std_glj.adjCoe;
  290. newGLJ.materialType = std_glj.materialType;
  291. newGLJ.materialCoe = std_glj.materialCoe;
  292. newGLJ.createType = 'normal';
  293. let info = await ration_glj_facade.getInfoFromProjectGLJ(newGLJ,unitPriceFileId);
  294. newGLJ = ration_glj_facade.createNewRecord(info);
  295. newRationGLJList.push(newGLJ);
  296. rationGLJShowList.push(info);
  297. }
  298. let InfoFromProjectGLJ = +new Date();
  299. console.log("找到项目工料机时间-------------------------------"+(InfoFromProjectGLJ - std_gljTime));
  300. }
  301. }
  302. let before = +new Date();
  303. console.log("总查询时间为-------------------------------"+(before-first));
  304. if(newRationGLJList.length>0){
  305. await ration_glj.insertMany(newRationGLJList);
  306. }
  307. let after = +new Date();
  308. console.log("实际插入时间为-------------------------------"+(after-before));
  309. console.log("总操作时间为-------------------------------"+(after-first));
  310. return rationGLJShowList;
  311. }
  312. async function deleRationSubRecode(projectID,rationID) {//删除挂在定额下的数据,如工程量明细,定额工料机等
  313. let delete_query={projectID: projectID, rationID: rationID};
  314. //删除工程量明细
  315. await quantity_detail.deleteByQuery(delete_query) ;
  316. await ration_coe.deleteMany(delete_query);//删除附注条件
  317. await ration_glj.deleteMany(delete_query);//删除定额工料机
  318. await rationInstallationModel.deleteMany(delete_query);//删除安装增加费
  319. }
  320. async function updateRation(std,firstLibID,rationID,billsItemID,projectID,calQuantity) {
  321. // insertNewRation
  322. let ration ={};
  323. ration.code = std.code;
  324. ration.name = std.name;
  325. ration.caption = std.caption;
  326. ration.unit = std.unit;
  327. ration.libID = std.rationRepId;
  328. ration.content = std.jobContent;
  329. ration.adjustState = '';
  330. ration.isFromDetail=0;
  331. ration.isSubcontract=false;
  332. ration.fees=[];
  333. if (std.chapter) {
  334. ration.comments = std.chapter.explanation;
  335. ration.ruleText = std.chapter.ruleText;
  336. }
  337. ration.from = std.type === 'complementary' ? 'cpt' : 'std';
  338. //定额前缀 none:0, complementary:1, borrow: 2
  339. ration.prefix = '';
  340. //借用优先级比补充高
  341. if(std.rationRepId !== parseInt(firstLibID)){//借用
  342. ration.prefix = '借';
  343. }
  344. else if(std.rationRepId === firstLibID && ration.from === 'cpt') {
  345. ration.prefix = '补';
  346. }
  347. ration.programID = std.feeType;
  348. ration.rationAssList = createRationAss(std);//生成辅助定额
  349. if(calQuantity){
  350. await CalculateQuantity(ration,billsItemID,projectID);
  351. }
  352. let unsetObject = {
  353. "marketUnitFee":1,
  354. 'marketTotalFee':1,
  355. "maskName":1
  356. }
  357. let newRation = await ration_model.model.findOneAndUpdate({ID:rationID,projectID:projectID},{"$set":ration,"$unset":unsetObject},{new: true});//;
  358. return newRation;
  359. }
  360. function createRationAss(std) {
  361. let rationAssList = [];//生成辅助定额
  362. if(std.hasOwnProperty('rationAssList')&&std.rationAssList.length>0){
  363. for(let i=0;i<std.rationAssList.length;i++){
  364. let ass = std.rationAssList[i];
  365. ass.actualValue = ass.stdValue;
  366. rationAssList.push(ass);
  367. }
  368. }
  369. return rationAssList;
  370. }
  371. async function CalculateQuantity (ration,billsItemID,projectID) {
  372. // calculate ration Quantity
  373. let project = await projectModel.findOne({ID:projectID});
  374. let decimalObject =await decimal_facade.getProjectDecimal(projectID,project);
  375. let quantity_decimal = (decimalObject&&decimalObject.ration&&decimalObject.ration.quantity)?decimalObject.ration.quantity:3;
  376. let pbill = await bill_model.model.findOne({projectID:projectID,ID:billsItemID});
  377. let billsQuantity = pbill.quantity ? pbill.quantity : 0;
  378. let bill_decimal = await decimal_facade.getBillsQuantityDecimal(projectID,pbill.unit,project);
  379. billsQuantity=scMathUtil.roundForObj(billsQuantity,bill_decimal);
  380. ration.quantityEXP="QDL";
  381. ration.quantity = scMathUtil.roundForObj(billsQuantity / FilterNumberFromUnit(ration.unit),quantity_decimal);//不管是否打勾都做转换
  382. ration.contain = scMathUtil.roundForObj(ration.quantity/billsQuantity,6);
  383. };
  384. function FilterNumberFromUnit (unit) {
  385. let reg = new RegExp('^[0-9]+');
  386. if (reg.test(unit)) {
  387. return parseInt(unit.match(reg)[0]);
  388. } else {
  389. return 1;
  390. }
  391. };