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