ration_facade.js 16 KB

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