compleRationModel.js 20 KB

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