compleRationModel.js 23 KB

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