ration_facade.js 16 KB

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