ration_facade.js 14 KB

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