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