compleRationModel.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. if(gljItems[i].ID === adjGljId){
  316. gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice});
  317. } else {
  318. gljArr.push({gljId: gljItems[i].ID, basePrice: parseFloat(gljItems[i].basePrice)});
  319. }
  320. }
  321. gljArr.forEach(function (gljItem) {
  322. rationGljList.forEach(function (rationGlj) {
  323. if(gljItem.gljId === rationGlj.gljId){
  324. gljItem.consumeAmt = parseFloat(rationGlj.consumeAmt);
  325. }
  326. })
  327. });
  328. //recalculate the price of ration
  329. let basePrice = 0;
  330. gljArr.forEach(function (gljItem) {
  331. basePrice += gljItem.basePrice * gljItem.consumeAmt;
  332. });
  333. basePrice = scMathUtil.roundTo(basePrice, 0);
  334. //updateDataBase
  335. compleRationModel.update({ID: rationItem.ID}, {$set: {basePrice: basePrice.toString()}},
  336. function (err, result) {
  337. if(err){
  338. ecb(err);
  339. }
  340. else {
  341. ecb(null);
  342. }
  343. });
  344. }
  345. });
  346. }, function(err){
  347. if(err){
  348. cb(err);
  349. }
  350. else {
  351. cb(null);
  352. }
  353. });
  354. },
  355. ], function (err) {
  356. if(err){
  357. finalCb(err);
  358. }
  359. else{
  360. finalCb(null);
  361. }
  362. });
  363. }, function (err) {
  364. if(err){
  365. callback(err, 'Error');
  366. }
  367. else{
  368. callback(0, '');
  369. }
  370. });
  371. }
  372. mixUpdateRationItems (userID, compilationId, rationLibId, sectionId, updateItems, addItems, rIds, callback){
  373. let me = this;
  374. if (updateItems.length == 0 && rIds.length == 0) {
  375. me.addRationItems(userID, compilationId, rationLibId, sectionId, addItems, callback);
  376. } else {
  377. me.removeRationItems(rationLibId, rIds, function(err, message, docs) {
  378. if (err) {
  379. callback(true, false);
  380. } else {
  381. me.updateRationItems(userID, rationLibId, sectionId, updateItems, function(err, results){
  382. if (err) {
  383. callback(true, false);
  384. } else {
  385. if (addItems && addItems.length > 0) {
  386. me.addRationItems(rationLibId, sectionId, addItems, callback);
  387. } else {
  388. callback(0, results);
  389. }
  390. }
  391. });
  392. }
  393. })
  394. }
  395. }
  396. removeRationItems(rationRepId, rIds,callback){
  397. if (rIds.length > 0) {
  398. compleRationModel.remove({rationRepId: rationRepId, ID: {$in: rIds}}, function(err, docs){
  399. if (err) {
  400. callback(true, false);
  401. } else {
  402. callback(0, docs);
  403. }
  404. })
  405. } else {
  406. callback(0, null);
  407. }
  408. }
  409. addRationItems(userID, compilationId, rationLibId, sectionId, items,callback){
  410. if (items && items.length > 0) {
  411. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  412. let maxId = result.sequence_value;
  413. let arr = [];
  414. for (let i = 0; i < items.length; i++) {
  415. let obj = new compleRationModel(items[i]);
  416. obj.ID = (maxId - (items.length - 1) + i);
  417. obj.sectionId = sectionId;
  418. obj.rationRepId = rationLibId;
  419. obj.userId = userID;
  420. obj.compilationId = compilationId;
  421. arr.push(obj);
  422. }
  423. compleRationModel.collection.insert(arr, null, function(err, docs){
  424. if (err) {
  425. callback(true, false);
  426. } else {
  427. callback(0, docs);
  428. }
  429. })
  430. });
  431. } else {
  432. callback(true, "Source error!", false);
  433. }
  434. }
  435. updateRationItems(userID, rationLibId, sectionId, items,callback){
  436. let functions = [];
  437. for (let i=0; i < items.length; i++) {
  438. functions.push((function(doc) {
  439. return function(cb) {
  440. var filter = {};
  441. if (doc.ID) {
  442. filter.ID = doc.ID;
  443. } else {
  444. filter.sectionId = sectionId;
  445. filter.userId = userID;
  446. if (rationLibId) filter.rationRepId = rationLibId;
  447. filter.code = doc.code;
  448. }
  449. compleRationModel.update(filter, doc, cb);
  450. };
  451. })(items[i]));
  452. }
  453. async.parallel(functions, function(err, results) {
  454. if(!err){
  455. err = 0;
  456. }
  457. callback(err, results);
  458. });
  459. }
  460. async getInstallation(rationRepId, callback){
  461. try {
  462. let feeItems = await installFeeItemModel.find({rationRepId: rationRepId, $or: [{deleted: false}, {deleted: null}]});
  463. for(let feeItem of feeItems){
  464. let sids = [];
  465. for(let sec of feeItem.section){
  466. sids.push(sec.ID);
  467. }
  468. if(sids.length > 0){
  469. let sections = await installSectionModel.find({ID: {$in: sids}, $or: [{deleted: false}, {deleted: null}]});
  470. feeItem._doc.section = sections;
  471. }
  472. }
  473. callback(0, feeItems);
  474. }
  475. catch(err){
  476. callback(err, null);
  477. }
  478. }
  479. }
  480. function isDef(v){
  481. return v !== undefined && v !== null;
  482. }
  483. export default CompleRatoinDao;