compleRationModel.js 24 KB

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