compleRationModel.js 19 KB

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