compleRationModel.js 23 KB

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