compleRationModel.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. const async = require('async');
  14. let stdRationModel = require ('../../ration_repository/models/ration_item').Model;
  15. const SectionTreeDao = require('../models/sectionTreeModel');
  16. const sectionTreeDao = new SectionTreeDao();
  17. const GljDao = require("../../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. let gljUtil = require('../../../public/gljUtil');
  22. const { ShareLibType } = require('../../../public/common_constants');
  23. const pmFacade = require('../../pm/facade/pm_facade');
  24. class CompleRatoinDao {
  25. async updateRation(userID, compilationId, updateData, callback){
  26. try{
  27. for(let i = 0, len = updateData.length; i < len; i++){
  28. let updateObj = updateData[i];
  29. if(updateObj.updateType === 'new'){
  30. updateObj.updateData.userID = userID;
  31. updateObj.updateData.compilationId = compilationId;
  32. await compleRationModel.create(updateObj.updateData);
  33. }
  34. else if(updateObj.updateType === 'update'){
  35. await compleRationModel.update({userID: userID, rationRepId: updateObj.updateData.rationRepId}, updateObj.updateData);
  36. }
  37. }
  38. callback(0, '');
  39. }
  40. catch(err){
  41. callback(err, null);
  42. }
  43. }
  44. async getRationItems(sectionId, callback){
  45. try{
  46. let compleRations = await compleRationModel.find({sectionId: sectionId, deleteInfo: null});
  47. callback(0, compleRations);
  48. }
  49. catch(err){
  50. callback(err, null);
  51. }
  52. }
  53. async getRationsCodes(userID, rationRepId, callback){
  54. try{
  55. let stdRationCodes = await stdRationModel.find({rationRepId: rationRepId, $or: [{isDeleted: null}, {isDeleted: false}]}, '-_id code');
  56. let compleRationCodes = await compleRationModel.find({userId: userID, rationRepId: rationRepId, deleteInfo: null}, '-_id code');
  57. let rstCodes = [];
  58. stdRationCodes.concat(compleRationCodes).forEach(function (rationItem) {
  59. rstCodes.push(rationItem.code);
  60. });
  61. callback(0, rstCodes);
  62. }
  63. catch(err){
  64. callback(err, null);
  65. }
  66. }
  67. async getGljItems(gljLibId, callback){
  68. try{
  69. let stdGljs = await stdGljModel.find({repositoryId: gljLibId, $or: [{deleted: null}, {deleted: false}]});
  70. callback(0, stdGljs);
  71. }
  72. catch(err){
  73. callback(err, null);
  74. }
  75. }
  76. async getGljItemsOccupied(gljLibId, occupation, callback){
  77. try{
  78. let stdGls = await stdGljModel.find({repositoryId: gljLibId, $or: [{deleted: null}, {deleted: false}]}, occupation);
  79. callback(0, stdGls);
  80. }
  81. catch (err){
  82. callback(err, null);
  83. }
  84. }
  85. async getGljItemsByIds(userID, ids, callback){
  86. try{
  87. let rst = [];
  88. for(let i = 0, len = ids.length; i < len; i++){
  89. if(ids[i].type === 'std'){
  90. let stdGlj = await stdGljModel.find({ID: ids[i].id, deleteInfo: null});
  91. if(stdGlj.length > 0){
  92. stdGlj[0]._doc.type = 'std';
  93. rst.push(stdGlj[0]);
  94. }
  95. }
  96. else if(ids[i].type === 'complementary'){
  97. const owner = ids[i].fromUser || userID;
  98. let compleGlj = await complementaryGljModel.find({userId: owner, ID: ids[i].id, deleteInfo: null});
  99. if(compleGlj.length > 0){
  100. compleGlj[0]._doc.type = 'complementary';
  101. rst.push(compleGlj[0]);
  102. }
  103. }
  104. }
  105. callback(0, rst);
  106. }
  107. catch(err){
  108. callback(err, null);
  109. }
  110. }
  111. async getGljItemsByCodes(userID, compilationId, rationRepId, codes, callback){
  112. try{
  113. let rst = [];
  114. for(let i = 0, len = codes.length; i < len; i++){
  115. let stdGlj = await stdGljModel.find({repositoryId: rationRepId, code: codes[i]});
  116. if(stdGlj.length > 0){
  117. stdGlj[0]._doc.type = 'std';
  118. rst.push(stdGlj[0]);
  119. }
  120. else {
  121. let compleGlj = await complementaryGljModel.find({userId: userID, compilationId: compilationId, code: codes[i]});
  122. if(compleGlj.length > 0){
  123. compleGlj[0]._doc.type = 'complementary';
  124. rst.push(compleGlj[0]);
  125. }
  126. }
  127. }
  128. callback(0, rst);
  129. }
  130. catch(err){
  131. callback(err, null);
  132. }
  133. }
  134. //根据章节树获取补充定额
  135. async getCompleRationBySection(userId, sectionId,gUtil) {
  136. const deleteQuery = [{deleteInfo: null}, {'deleteInfo.deleted': false}];
  137. let compleRations = await compleRationModel.find({sectionId: sectionId, $or: deleteQuery});
  138. for(let ration of compleRations){
  139. ration._doc.type = 'complementary';
  140. let hintsArr = [];
  141. let stdGljIds = [],
  142. comGljIds = [],
  143. stdGljs = [],
  144. comGljs = [];
  145. const users = [userId];
  146. let gljAmtMapping = {};
  147. for(let rationGlj of ration.rationGljList){
  148. gljAmtMapping[rationGlj.gljId] = rationGlj.consumeAmt;
  149. if(!isDef(rationGlj.type) || rationGlj.type === 'std'){
  150. stdGljIds.push(rationGlj.gljId);
  151. }
  152. else {
  153. if (rationGlj.fromUser && !users.includes(rationGlj.fromUser)) {
  154. users.push(rationGlj.fromUser);
  155. }
  156. comGljIds.push(rationGlj.gljId);
  157. }
  158. }
  159. if(stdGljIds.length > 0) {
  160. stdGljs = await stdGljModel.find({ID: {$in: stdGljIds}}).lean();
  161. }
  162. if(comGljIds.length > 0) {
  163. comGljs = await complementaryGljModel.find({userId: { $in: users }, ID: {$in: comGljIds}}).lean();
  164. }
  165. if(!gUtil) gUtil = gljUtil;
  166. let gljDatas = gUtil.sortRationGLJ(stdGljs.concat(comGljs), true);
  167. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  168. hintsArr.push(`工作内容:`);
  169. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  170. hintsArr.push("");
  171. }
  172. for(let glj of gljDatas){
  173. hintsArr.push(` ${glj.code} ${glj.name}${glj.specs ? '&nbsp;&nbsp;&nbsp;' + glj.specs : ''}&nbsp;&nbsp&nbsp;${glj.unit}&nbsp;&nbsp;&nbsp;${gljAmtMapping[glj.ID]}`)
  174. }
  175. hintsArr.push(`基价 元 ${ration.basePrice}`);
  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,gUtil){
  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. sortByCode(stdRations);
  220. for(let ration of stdRations){
  221. let hintsArr = [];
  222. let stdGljIds = [],
  223. stdGljs = [];
  224. let gljAmtMapping = {};
  225. for(let rationGlj of ration.rationGljList){
  226. gljAmtMapping[rationGlj.gljId] = rationGlj.consumeAmt;
  227. stdGljIds.push(rationGlj.gljId);
  228. }
  229. if(stdGljIds.length > 0) {
  230. stdGljs = await stdGljModel.find({ID: {$in: stdGljIds}}).lean();
  231. }
  232. if(!gUtil) gUtil = gljUtil;
  233. let gljDatas = gUtil.sortRationGLJ(stdGljs, true);
  234. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  235. hintsArr.push(`工作内容:`);
  236. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  237. hintsArr.push("");
  238. }
  239. for(let glj of gljDatas){
  240. hintsArr.push(` ${glj.code} ${glj.name}${glj.specs ? '&nbsp;&nbsp;&nbsp;' + glj.specs : ''}&nbsp;&nbsp&nbsp;${glj.unit}&nbsp;&nbsp;&nbsp;${gljAmtMapping[glj.ID]}`)
  241. }
  242. hintsArr.push(`基价 元 ${ration.basePrice}`);
  243. if(ration.annotation && ration.annotation.toString().trim() !== ''){
  244. hintsArr.push(`附注:`);
  245. hintsArr = hintsArr.concat(ration.annotation.split('\n'));
  246. }
  247. ration._doc.hint = hintsArr.join('<br>');
  248. }
  249. return stdRations;
  250. }
  251. updateRationBasePrc(userID, basePrcArr, callback){
  252. let me = this;
  253. async.each(basePrcArr, function (basePrcObj, finalCb) {
  254. let adjGljId = basePrcObj.gljId, adjBasePrice = basePrcObj.basePrice, adjGljType = basePrcObj.gljType;
  255. async.waterfall([
  256. function (cb) {
  257. if(typeof basePrcObj.delete !== 'undefined' && basePrcObj.delete === 1){
  258. //补充定额
  259. compleRationModel.find({'rationGljList.gljId': adjGljId},{ID: 1, rationGljList: 1}, function (err, compleRst) {
  260. if(err){
  261. cb(err);
  262. }
  263. else {
  264. compleRationModel.update({'rationGljList.gljId': adjGljId}, {$pull: {rationGljList: {gljId: adjGljId}}}, {multi: true}, function (err) {
  265. if(err){
  266. cb(err);
  267. }
  268. else {
  269. cb(null, compleRst);
  270. }
  271. });
  272. }
  273. });
  274. }
  275. else{
  276. compleRationModel.find({'rationGljList.gljId': adjGljId}, function (err, compleRst) {
  277. if(err){
  278. cb(err);
  279. }
  280. else {
  281. cb(null, compleRst);
  282. }
  283. });
  284. }
  285. },
  286. function (result, cb) {
  287. async.each(result, function (rationItem, ecb) {
  288. let rationGljList = rationItem.rationGljList,
  289. gljIds = [];
  290. rationGljList.forEach(function (rationGlj) {
  291. let idObj = Object.create(null);
  292. idObj.id = rationGlj.gljId;
  293. idObj.type = rationGlj.type;
  294. gljIds.push(idObj);
  295. });
  296. me.getGljItemsByIds(userID, gljIds, function(err, gljItems){
  297. if(err){
  298. ecb(err);
  299. }
  300. else{
  301. let gljArr = [];
  302. for(let i=0; i<gljItems.length; i++){
  303. if(gljItems[i].ID === adjGljId){
  304. gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice});
  305. } else {
  306. gljArr.push({gljId: gljItems[i].ID, basePrice: parseFloat(gljItems[i].basePrice)});
  307. }
  308. }
  309. gljArr.forEach(function (gljItem) {
  310. rationGljList.forEach(function (rationGlj) {
  311. if(gljItem.gljId === rationGlj.gljId){
  312. gljItem.consumeAmt = parseFloat(rationGlj.consumeAmt);
  313. }
  314. })
  315. });
  316. //recalculate the price of ration
  317. let basePrice = 0;
  318. gljArr.forEach(function (gljItem) {
  319. basePrice += gljItem.basePrice * gljItem.consumeAmt;
  320. });
  321. basePrice = scMathUtil.roundTo(basePrice, 0);
  322. //updateDataBase
  323. compleRationModel.update({ID: rationItem.ID}, {$set: {basePrice: basePrice.toString()}},
  324. function (err, result) {
  325. if(err){
  326. ecb(err);
  327. }
  328. else {
  329. ecb(null);
  330. }
  331. });
  332. }
  333. });
  334. }, function(err){
  335. if(err){
  336. cb(err);
  337. }
  338. else {
  339. cb(null);
  340. }
  341. });
  342. },
  343. ], function (err) {
  344. if(err){
  345. finalCb(err);
  346. }
  347. else{
  348. finalCb(null);
  349. }
  350. });
  351. }, function (err) {
  352. if(err){
  353. callback(err, 'Error');
  354. }
  355. else{
  356. callback(0, '');
  357. }
  358. });
  359. }
  360. mixUpdateRationItems (userID, compilationId, rationLibId, sectionId, updateItems, addItems, rIds, callback){
  361. let me = this;
  362. if (updateItems.length == 0 && rIds.length == 0) {
  363. me.addRationItems(userID, compilationId, rationLibId, sectionId, addItems, callback);
  364. } else {
  365. me.removeRationItems(rationLibId, rIds, function(err, message, docs) {
  366. if (err) {
  367. callback(true, false);
  368. } else {
  369. me.updateRationItems(userID, rationLibId, sectionId, updateItems, function(err, results){
  370. if (err) {
  371. callback(true, false);
  372. } else {
  373. if (addItems && addItems.length > 0) {
  374. me.addRationItems(rationLibId, sectionId, addItems, callback);
  375. } else {
  376. callback(0, results);
  377. }
  378. }
  379. });
  380. }
  381. })
  382. }
  383. }
  384. removeRationItems(rationRepId, rIds,callback){
  385. if (rIds.length > 0) {
  386. compleRationModel.remove({rationRepId: rationRepId, ID: {$in: rIds}}, function(err, docs){
  387. if (err) {
  388. callback(true, false);
  389. } else {
  390. callback(0, docs);
  391. }
  392. })
  393. } else {
  394. callback(0, null);
  395. }
  396. }
  397. addRationItems(userID, compilationId, rationLibId, sectionId, items,callback){
  398. if (items && items.length > 0) {
  399. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  400. let maxId = result.sequence_value;
  401. let arr = [];
  402. for (let i = 0; i < items.length; i++) {
  403. let obj = new compleRationModel(items[i]);
  404. obj.ID = (maxId - (items.length - 1) + i);
  405. obj.sectionId = sectionId;
  406. obj.rationRepId = rationLibId;
  407. obj.userId = userID;
  408. obj.compilationId = compilationId;
  409. arr.push(obj);
  410. }
  411. compleRationModel.collection.insert(arr, null, function(err, docs){
  412. if (err) {
  413. callback(true, false);
  414. } else {
  415. callback(0, docs);
  416. }
  417. })
  418. });
  419. } else {
  420. callback(true, "Source error!", false);
  421. }
  422. }
  423. updateRationItems(userID, rationLibId, sectionId, items,callback){
  424. let functions = [];
  425. for (let i=0; i < items.length; i++) {
  426. functions.push((function(doc) {
  427. return function(cb) {
  428. var filter = {};
  429. if (doc.ID) {
  430. filter.ID = doc.ID;
  431. } else {
  432. filter.sectionId = sectionId;
  433. filter.userId = userID;
  434. if (rationLibId) filter.rationRepId = rationLibId;
  435. filter.code = doc.code;
  436. }
  437. compleRationModel.update(filter, doc, cb);
  438. };
  439. })(items[i]));
  440. }
  441. async.parallel(functions, function(err, results) {
  442. if(!err){
  443. err = 0;
  444. }
  445. callback(err, results);
  446. });
  447. }
  448. async getCodes (userId, compilationId) {
  449. const compleRations = await compleRationModel.find({userId, compilationId}, '-_id code').lean();
  450. const codes = [];
  451. compleRations.forEach(item => codes.push(item.code));
  452. return codes;
  453. }
  454. async prepareInitData (sessionUserID, userId, compilationId, gljLibId) {
  455. const receiveLibs = await pmFacade.getReceiveLibList(sessionUserID, compilationId, ShareLibType.RATION_LIB);
  456. const shareLibs = sessionUserID === userId ? await pmFacade.getLibShareList(sessionUserID, compilationId, ShareLibType.RATION_LIB) : [];
  457. const rationsCodes = await this.getCodes(userId, compilationId);
  458. const gljDistTypeCache = stdgljutil.getStdGljTypeCacheObj().toArray();
  459. const installationData = await installFacade.getInstallation(userId, compilationId);
  460. const rationTreeData = await sectionTreeDao.getComplementaryTree(userId, compilationId);
  461. const mixedTreeData = await gljDao.getMixedTree(gljLibId, userId, compilationId);
  462. const mixedGLJData = await gljDao.getGLJDataSync(gljLibId, userId, compilationId);
  463. return {
  464. shareLibs,
  465. receiveLibs,
  466. rationsCodes,
  467. gljDistTypeCache,
  468. installationData,
  469. rationTreeData,
  470. mixedTreeData,
  471. mixedGLJData
  472. }
  473. }
  474. }
  475. function isDef(v){
  476. return v !== undefined && v !== null;
  477. }
  478. module.exports = CompleRatoinDao;