ration_facade.js 14 KB

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