compleRationModel.js 24 KB

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