compleRationModel.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /**
  2. * Created by Zhong on 2017/12/21.
  3. */
  4. const mongoose = require('mongoose');
  5. const compleRationSectionTreeModel = mongoose.model('complementary_ration_section_tree');
  6. const compleRationModel = mongoose.model('complementary_ration_items');
  7. const installSectionModel = mongoose.model("std_ration_lib_installationSection");
  8. const installFeeItemModel = mongoose.model("std_ration_lib_installation");
  9. const complementaryGljModel = mongoose.model('complementary_glj_lib');
  10. const stdGljModel = mongoose.model('std_glj_lib_gljList');
  11. const stdgljutil = require('../../../public/cache/std_glj_type_util');
  12. const installFacade = require('../facades/compleInstallFacade');
  13. const async = require('async');
  14. let stdRationModel = require ('../../ration_repository/models/ration_item').Model;
  15. const SectionTreeDao = require('../models/sectionTreeModel');
  16. const sectionTreeDao = new SectionTreeDao();
  17. const GljDao = require("../../complementary_glj_lib/models/gljModel");
  18. const gljDao = new GljDao();
  19. let counter = require('../../../public/counter/counter');
  20. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  21. let gljUtil = require('../../../public/gljUtil');
  22. const { ShareLibType } = require('../../../public/common_constants');
  23. const pmFacade = require('../../pm/facade/pm_facade');
  24. class CompleRatoinDao {
  25. async updateRation(userID, compilationId, updateData, callback){
  26. try{
  27. for(let i = 0, len = updateData.length; i < len; i++){
  28. let updateObj = updateData[i];
  29. if(updateObj.updateType === 'new'){
  30. updateObj.updateData.userID = userID;
  31. updateObj.updateData.compilationId = compilationId;
  32. await compleRationModel.create(updateObj.updateData);
  33. }
  34. else if(updateObj.updateType === 'update'){
  35. await compleRationModel.update({userID: userID, rationRepId: updateObj.updateData.rationRepId}, updateObj.updateData);
  36. }
  37. }
  38. callback(0, '');
  39. }
  40. catch(err){
  41. callback(err, null);
  42. }
  43. }
  44. async getRationItems(sectionId, callback){
  45. try{
  46. let compleRations = await compleRationModel.find({sectionId: sectionId, deleteInfo: null});
  47. callback(0, compleRations);
  48. }
  49. catch(err){
  50. callback(err, null);
  51. }
  52. }
  53. async getRationsCodes(userID, rationRepId, callback){
  54. try{
  55. let stdRationCodes = await stdRationModel.find({rationRepId: rationRepId, $or: [{isDeleted: null}, {isDeleted: false}]}, '-_id code');
  56. let compleRationCodes = await compleRationModel.find({userId: userID, rationRepId: rationRepId, deleteInfo: null}, '-_id code');
  57. let rstCodes = [];
  58. stdRationCodes.concat(compleRationCodes).forEach(function (rationItem) {
  59. rstCodes.push(rationItem.code);
  60. });
  61. callback(0, rstCodes);
  62. }
  63. catch(err){
  64. callback(err, null);
  65. }
  66. }
  67. async getGljItems(gljLibId, callback){
  68. try{
  69. let stdGljs = await stdGljModel.find({repositoryId: gljLibId, $or: [{deleted: null}, {deleted: false}]});
  70. callback(0, stdGljs);
  71. }
  72. catch(err){
  73. callback(err, null);
  74. }
  75. }
  76. async getGljItemsOccupied(gljLibId, occupation, callback){
  77. try{
  78. let stdGls = await stdGljModel.find({repositoryId: gljLibId, $or: [{deleted: null}, {deleted: false}]}, occupation);
  79. callback(0, stdGls);
  80. }
  81. catch (err){
  82. callback(err, null);
  83. }
  84. }
  85. async getGljItemsByIds(userID, ids, callback){
  86. try{
  87. let rst = [];
  88. for(let i = 0, len = ids.length; i < len; i++){
  89. if(ids[i].type === 'std'){
  90. let stdGlj = await stdGljModel.find({ID: ids[i].id, deleteInfo: null});
  91. if(stdGlj.length > 0){
  92. stdGlj[0]._doc.type = 'std';
  93. rst.push(stdGlj[0]);
  94. }
  95. }
  96. else if(ids[i].type === 'complementary'){
  97. const owner = ids[i].fromUser || userID;
  98. let compleGlj = await complementaryGljModel.find({userId: owner, ID: ids[i].id, deleteInfo: null});
  99. if(compleGlj.length > 0){
  100. compleGlj[0]._doc.type = 'complementary';
  101. rst.push(compleGlj[0]);
  102. }
  103. }
  104. }
  105. callback(0, rst);
  106. }
  107. catch(err){
  108. callback(err, null);
  109. }
  110. }
  111. async getGljItemsByCodes(userID, compilationId, rationRepId, codes, callback){
  112. try{
  113. let rst = [];
  114. for(let i = 0, len = codes.length; i < len; i++){
  115. let stdGlj = await stdGljModel.find({repositoryId: rationRepId, code: codes[i]});
  116. if(stdGlj.length > 0){
  117. stdGlj[0]._doc.type = 'std';
  118. rst.push(stdGlj[0]);
  119. }
  120. else {
  121. let compleGlj = await complementaryGljModel.find({userId: userID, compilationId: compilationId, code: codes[i]});
  122. if(compleGlj.length > 0){
  123. compleGlj[0]._doc.type = 'complementary';
  124. rst.push(compleGlj[0]);
  125. }
  126. }
  127. }
  128. callback(0, rst);
  129. }
  130. catch(err){
  131. callback(err, null);
  132. }
  133. }
  134. //根据章节树获取补充定额
  135. async getCompleRationBySection(userId, sectionId) {
  136. const deleteQuery = [{deleteInfo: null}, {'deleteInfo.deleted': false}];
  137. let compleRations = await compleRationModel.find({sectionId: sectionId, $or: deleteQuery});
  138. for(let ration of compleRations){
  139. ration._doc.type = 'complementary';
  140. let hintsArr = [];
  141. let stdGljIds = [],
  142. comGljIds = [],
  143. stdGljs = [],
  144. comGljs = [];
  145. const users = [userId];
  146. let gljAmtMapping = {};
  147. for(let rationGlj of ration.rationGljList){
  148. gljAmtMapping[rationGlj.gljId] = rationGlj.consumeAmt;
  149. if(!isDef(rationGlj.type) || rationGlj.type === 'std'){
  150. stdGljIds.push(rationGlj.gljId);
  151. }
  152. else {
  153. if (rationGlj.fromUser && !users.includes(rationGlj.fromUser)) {
  154. users.push(rationGlj.fromUser);
  155. }
  156. comGljIds.push(rationGlj.gljId);
  157. }
  158. }
  159. if(stdGljIds.length > 0) {
  160. stdGljs = await stdGljModel.find({ID: {$in: stdGljIds}}).lean();
  161. }
  162. if(comGljIds.length > 0) {
  163. comGljs = await complementaryGljModel.find({userId: { $in: users }, ID: {$in: comGljIds}}).lean();
  164. }
  165. let gljDatas = gljUtil.sortRationGLJ(stdGljs.concat(comGljs),true);
  166. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  167. hintsArr.push(`<label class="nomargin font_blue">工作内容:`);
  168. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  169. hintsArr.push("</label>");
  170. hintsArr.push("");
  171. }
  172. for(let glj of gljDatas){
  173. hintsArr.push(`<label class="nomargin ${glj.gljType==4?"font_blue":""}"> ${glj.code} ${glj.name}${glj.specs ? '&nbsp;&nbsp;&nbsp;' + glj.specs : ''}&nbsp;&nbsp&nbsp;${glj.unit}&nbsp;&nbsp;&nbsp;${gljAmtMapping[glj.ID]}</label>`)
  174. }
  175. hintsArr.push(`基价 元 ${ration.basePrice}`);
  176. if(ration.annotation && ration.annotation.toString().trim() !== ''){
  177. hintsArr.push(`<br>附注:`);
  178. hintsArr = hintsArr.concat(ration.annotation.split('\n'));
  179. }
  180. ration._doc.hint = hintsArr.join('<br>');
  181. }
  182. return compleRations;
  183. }
  184. //造价书定额库 根据章节树过去标准定额
  185. async getRationGljItemsBySection(sectionId, callback){
  186. let stdRations = await stdRationModel.find({sectionId: sectionId});
  187. for(let ration of stdRations){
  188. ration._doc.type = 'std';
  189. }
  190. function sortByCode(arr) {
  191. function recurCompare(a, b, index){
  192. if (a[index] && !b[index]) {
  193. return 1;
  194. } else if (!a[index] && b[index]) {
  195. return -1;
  196. } else if (a[index] && b[index]) {
  197. let aV = a[index],
  198. bV = b[index];
  199. if (!isNaN(aV) && !isNaN(bV)) {
  200. aV = parseFloat(a[index]);
  201. bV = parseFloat(b[index]);
  202. }
  203. if (aV > bV) {
  204. return 1;
  205. } else if (aV < bV) {
  206. return -1;
  207. } else {
  208. return recurCompare(a, b, index + 1);
  209. }
  210. }
  211. return 0;
  212. }
  213. arr.sort(function (a, b) {
  214. let aArr = a.code.split('-'),
  215. bArr = b.code.split('-');
  216. return recurCompare(aArr, bArr, 0);
  217. });
  218. }
  219. /*stdRations.sort(function (a, b) {
  220. let rst = 0;
  221. if(a.code > b.code){
  222. rst = 1;
  223. }
  224. else if(a.code < b.code){
  225. rst = -1;
  226. }
  227. return rst;
  228. });*/
  229. sortByCode(stdRations);
  230. for(let ration of stdRations){
  231. let hintsArr = [];
  232. let stdGljIds = [],
  233. stdGljs = [];
  234. let gljAmtMapping = {};
  235. for(let rationGlj of ration.rationGljList){
  236. gljAmtMapping[rationGlj.gljId] = rationGlj.consumeAmt;
  237. stdGljIds.push(rationGlj.gljId);
  238. }
  239. if(stdGljIds.length > 0) {
  240. stdGljs = await stdGljModel.find({ID: {$in: stdGljIds}}).lean();
  241. /*stdGljs.forEach(function (glj) {
  242. glj.type = glj.gljType;
  243. });*/
  244. }
  245. let gljDatas = gljUtil.sortRationGLJ(stdGljs,true);
  246. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  247. hintsArr.push(`<label class="nomargin font_blue">工作内容:`);
  248. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  249. hintsArr.push("</label>");
  250. hintsArr.push("");
  251. }
  252. for(let glj of gljDatas){
  253. hintsArr.push(`<label class="nomargin ${glj.gljType==4?"font_blue":""}"> ${glj.code} ${glj.name}${glj.specs ? '&nbsp;&nbsp;&nbsp;' + glj.specs : ''}&nbsp;&nbsp&nbsp;${glj.unit}&nbsp;&nbsp;&nbsp;${gljAmtMapping[glj.ID]}</label>`)
  254. }
  255. hintsArr.push(`基价 元 ${ration.basePrice}`);
  256. if(ration.annotation && ration.annotation.toString().trim() !== ''){
  257. hintsArr.push(`<br>附注:`);
  258. hintsArr = hintsArr.concat(ration.annotation.split('\n'));
  259. }
  260. ration._doc.hint = hintsArr.join('<br>');
  261. }
  262. return stdRations;
  263. }
  264. updateRationBasePrc(userID, basePrcArr, callback){
  265. let me = this;
  266. async.each(basePrcArr, function (basePrcObj, finalCb) {
  267. let adjGljId = basePrcObj.gljId, adjBasePrice = basePrcObj.basePrice, adjGljType = basePrcObj.gljType;
  268. async.waterfall([
  269. function (cb) {
  270. if(typeof basePrcObj.delete !== 'undefined' && basePrcObj.delete === 1){
  271. //补充定额
  272. compleRationModel.find({'rationGljList.gljId': adjGljId},{ID: 1, rationGljList: 1}, function (err, compleRst) {
  273. if(err){
  274. cb(err);
  275. }
  276. else {
  277. compleRationModel.update({'rationGljList.gljId': adjGljId}, {$pull: {rationGljList: {gljId: adjGljId}}}, {multi: true}, function (err) {
  278. if(err){
  279. cb(err);
  280. }
  281. else {
  282. cb(null, compleRst);
  283. }
  284. });
  285. }
  286. });
  287. }
  288. else{
  289. compleRationModel.find({'rationGljList.gljId': adjGljId}, function (err, compleRst) {
  290. if(err){
  291. cb(err);
  292. }
  293. else {
  294. cb(null, compleRst);
  295. }
  296. });
  297. }
  298. },
  299. function (result, cb) {
  300. async.each(result, function (rationItem, ecb) {
  301. let rationGljList = rationItem.rationGljList,
  302. gljIds = [];
  303. rationGljList.forEach(function (rationGlj) {
  304. let idObj = Object.create(null);
  305. idObj.id = rationGlj.gljId;
  306. idObj.type = rationGlj.type;
  307. gljIds.push(idObj);
  308. });
  309. me.getGljItemsByIds(userID, gljIds, function(err, gljItems){
  310. if(err){
  311. ecb(err);
  312. }
  313. else{
  314. let gljArr = [];
  315. for(let i=0; i<gljItems.length; i++){
  316. let gljParentType = -1;
  317. if(gljItems[i].ID === adjGljId){
  318. gljItems[i].gljType = adjGljType;
  319. }
  320. if(gljItems[i].gljType <= 3){
  321. gljParentType = gljItems[i].gljType;
  322. }
  323. if(gljItems[i].gljType > 200 && gljItems[i].gljType < 300){
  324. gljParentType = 2;
  325. }
  326. if(gljItems[i].gljType > 300 && gljItems[i].gljType < 400){
  327. gljParentType = 3;
  328. }
  329. if(gljItems[i].ID === adjGljId){
  330. gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice, gljParentType: gljParentType});
  331. }
  332. else {
  333. gljArr.push({gljId: gljItems[i].ID, basePrice: parseFloat(gljItems[i].basePrice), gljParentType: gljParentType});
  334. }
  335. }
  336. gljArr.forEach(function (gljItem) {
  337. rationGljList.forEach(function (rationGlj) {
  338. if(gljItem.gljId === rationGlj.gljId){
  339. gljItem.consumeAmt = parseFloat(rationGlj.consumeAmt);
  340. }
  341. })
  342. });
  343. //recalculate the price of ration
  344. let labourPrc = [], materialPrc = [], machinePrc = [], singlePrc, updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, basePrice: 0};
  345. gljArr.forEach(function (gljItem) {
  346. if(gljItem.gljParentType !== -1){
  347. singlePrc = scMathUtil.roundTo(gljItem.basePrice * gljItem.consumeAmt, -3);
  348. if(gljItem.gljParentType === 1){
  349. labourPrc.push(singlePrc);
  350. }
  351. else if(gljItem.gljParentType ===2){
  352. materialPrc.push(singlePrc);
  353. }
  354. else{
  355. machinePrc.push(singlePrc);
  356. }
  357. }
  358. });
  359. if(labourPrc.length > 0){
  360. let sumLaP = 0;
  361. for(let i=0; i<labourPrc.length; i++){
  362. sumLaP += labourPrc[i];
  363. }
  364. updatePrc.labourPrice = scMathUtil.roundTo(sumLaP, -2);
  365. }
  366. if(materialPrc.length > 0){
  367. let sumMtP = 0;
  368. for(let i= 0; i<materialPrc.length; i++){
  369. sumMtP += materialPrc[i];
  370. }
  371. updatePrc.materialPrice = scMathUtil.roundTo(sumMtP, -2);
  372. }
  373. if(machinePrc.length > 0){
  374. let sumMaP = 0;
  375. for(let i =0; i< machinePrc.length; i++){
  376. sumMaP += machinePrc[i];
  377. }
  378. updatePrc.machinePrice = scMathUtil.roundTo(sumMaP, -2);
  379. }
  380. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.labourPrice + updatePrc.materialPrice + updatePrc.machinePrice, -2);
  381. //updateDataBase
  382. compleRationModel.update({ID: rationItem.ID}, {$set: {labourPrice: updatePrc.labourPrice.toString(), materialPrice: updatePrc.materialPrice.toString(),
  383. machinePrice: updatePrc.machinePrice.toString(), basePrice: updatePrc.basePrice.toString()}},
  384. function (err, result) {
  385. if(err){
  386. ecb(err);
  387. }
  388. else {
  389. ecb(null);
  390. }
  391. });
  392. }
  393. });
  394. }, function(err){
  395. if(err){
  396. cb(err);
  397. }
  398. else {
  399. cb(null);
  400. }
  401. });
  402. },
  403. ], function (err) {
  404. if(err){
  405. finalCb(err);
  406. }
  407. else{
  408. finalCb(null);
  409. }
  410. });
  411. }, function (err) {
  412. if(err){
  413. callback(err, 'Error');
  414. }
  415. else{
  416. callback(0, '');
  417. }
  418. });
  419. }
  420. mixUpdateRationItems (userID, compilationId, rationLibId, sectionId, updateItems, addItems, rIds, callback){
  421. let me = this;
  422. if (updateItems.length == 0 && rIds.length == 0) {
  423. me.addRationItems(userID, compilationId, rationLibId, sectionId, addItems, callback);
  424. } else {
  425. me.removeRationItems(rationLibId, rIds, function(err, message, docs) {
  426. if (err) {
  427. callback(true, false);
  428. } else {
  429. me.updateRationItems(userID, rationLibId, sectionId, updateItems, function(err, results){
  430. if (err) {
  431. callback(true, false);
  432. } else {
  433. if (addItems && addItems.length > 0) {
  434. me.addRationItems(rationLibId, sectionId, addItems, callback);
  435. } else {
  436. callback(0, results);
  437. }
  438. }
  439. });
  440. }
  441. })
  442. }
  443. }
  444. removeRationItems(rationRepId, rIds,callback){
  445. if (rIds.length > 0) {
  446. compleRationModel.remove({rationRepId: rationRepId, ID: {$in: rIds}}, function(err, docs){
  447. if (err) {
  448. callback(true, false);
  449. } else {
  450. callback(0, docs);
  451. }
  452. })
  453. } else {
  454. callback(0, null);
  455. }
  456. }
  457. addRationItems(userID, compilationId, rationLibId, sectionId, items,callback){
  458. if (items && items.length > 0) {
  459. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  460. let maxId = result.sequence_value;
  461. let arr = [];
  462. for (let i = 0; i < items.length; i++) {
  463. let obj = new compleRationModel(items[i]);
  464. obj.ID = (maxId - (items.length - 1) + i);
  465. obj.sectionId = sectionId;
  466. obj.rationRepId = rationLibId;
  467. obj.userId = userID;
  468. obj.compilationId = compilationId;
  469. arr.push(obj);
  470. }
  471. compleRationModel.collection.insert(arr, null, function(err, docs){
  472. if (err) {
  473. callback(true, false);
  474. } else {
  475. callback(0, docs);
  476. }
  477. })
  478. });
  479. } else {
  480. callback(true, "Source error!", false);
  481. }
  482. }
  483. updateRationItems(userID, rationLibId, sectionId, items,callback){
  484. let functions = [];
  485. for (let i=0; i < items.length; i++) {
  486. functions.push((function(doc) {
  487. return function(cb) {
  488. var filter = {};
  489. if (doc.ID) {
  490. filter.ID = doc.ID;
  491. } else {
  492. filter.sectionId = sectionId;
  493. filter.userId = userID;
  494. if (rationLibId) filter.rationRepId = rationLibId;
  495. filter.code = doc.code;
  496. }
  497. compleRationModel.update(filter, doc, cb);
  498. };
  499. })(items[i]));
  500. }
  501. async.parallel(functions, function(err, results) {
  502. if(!err){
  503. err = 0;
  504. }
  505. callback(err, results);
  506. });
  507. }
  508. async getCodes (userId, compilationId) {
  509. const compleRations = await compleRationModel.find({userId, compilationId}, '-_id code').lean();
  510. const codes = [];
  511. compleRations.forEach(item => codes.push(item.code));
  512. return codes;
  513. }
  514. async prepareInitData (sessionUserID, userId, compilationId, gljLibId) {
  515. const receiveLibs = await pmFacade.getReceiveLibList(sessionUserID, compilationId, ShareLibType.RATION_LIB);
  516. const shareLibs = sessionUserID === userId ? await pmFacade.getLibShareList(sessionUserID, compilationId, ShareLibType.RATION_LIB) : [];
  517. const rationsCodes = await this.getCodes(userId, compilationId);
  518. const gljDistTypeCache = stdgljutil.getStdGljTypeCacheObj().toArray();
  519. const installationData = await installFacade.getInstallation(userId, compilationId);
  520. const rationTreeData = await sectionTreeDao.getComplementaryTree(userId, compilationId);
  521. const mixedTreeData = await gljDao.getMixedTree(gljLibId, userId, compilationId);
  522. const mixedGLJData = await gljDao.getGLJDataSync(gljLibId, userId, compilationId);
  523. return {
  524. shareLibs,
  525. receiveLibs,
  526. rationsCodes,
  527. gljDistTypeCache,
  528. installationData,
  529. rationTreeData,
  530. mixedTreeData,
  531. mixedGLJData
  532. }
  533. }
  534. }
  535. function isDef(v){
  536. return v !== undefined && v !== null;
  537. }
  538. module.exports = CompleRatoinDao;