ration_facade.js 15 KB

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