compleRationModel.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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}}).lean();
  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), true);
  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. sortByCode(stdRations);
  213. for(let ration of stdRations){
  214. let hintsArr = [];
  215. let stdGljIds = [],
  216. stdGljs = [];
  217. let gljAmtMapping = {};
  218. for(let rationGlj of ration.rationGljList){
  219. gljAmtMapping[rationGlj.gljId] = rationGlj.consumeAmt;
  220. stdGljIds.push(rationGlj.gljId);
  221. }
  222. if(stdGljIds.length > 0) {
  223. stdGljs = await stdGljModel.find({ID: {$in: stdGljIds}}).lean();
  224. }
  225. if(!gUtil) gUtil = gljUtil;
  226. let gljDatas = gUtil.sortRationGLJ(stdGljs, true);
  227. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  228. hintsArr.push(`工作内容:`);
  229. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  230. hintsArr.push("");
  231. }
  232. for(let glj of gljDatas){
  233. hintsArr.push(` ${glj.code} ${glj.name}${glj.specs ? '&nbsp;&nbsp;&nbsp;' + glj.specs : ''}&nbsp;&nbsp&nbsp;${glj.unit}&nbsp;&nbsp;&nbsp;${gljAmtMapping[glj.ID]}`)
  234. }
  235. hintsArr.push(`基价 元 ${ration.basePrice}`);
  236. if(ration.annotation && ration.annotation.toString().trim() !== ''){
  237. hintsArr.push(`附注:`);
  238. hintsArr = hintsArr.concat(ration.annotation.split('\n'));
  239. }
  240. ration._doc.hint = hintsArr.join('<br>');
  241. }
  242. return stdRations;
  243. }
  244. updateRationBasePrc(userID, basePrcArr, callback){
  245. let me = this;
  246. async.each(basePrcArr, function (basePrcObj, finalCb) {
  247. let adjGljId = basePrcObj.gljId, adjBasePrice = basePrcObj.basePrice, adjGljType = basePrcObj.gljType;
  248. async.waterfall([
  249. function (cb) {
  250. if(typeof basePrcObj.delete !== 'undefined' && basePrcObj.delete === 1){
  251. //补充定额
  252. compleRationModel.find({'rationGljList.gljId': adjGljId},{ID: 1, rationGljList: 1}, function (err, compleRst) {
  253. if(err){
  254. cb(err);
  255. }
  256. else {
  257. compleRationModel.update({'rationGljList.gljId': adjGljId}, {$pull: {rationGljList: {gljId: adjGljId}}}, {multi: true}, function (err) {
  258. if(err){
  259. cb(err);
  260. }
  261. else {
  262. cb(null, compleRst);
  263. }
  264. });
  265. }
  266. });
  267. }
  268. else{
  269. compleRationModel.find({'rationGljList.gljId': adjGljId}, function (err, compleRst) {
  270. if(err){
  271. cb(err);
  272. }
  273. else {
  274. cb(null, compleRst);
  275. }
  276. });
  277. }
  278. },
  279. function (result, cb) {
  280. async.each(result, function (rationItem, ecb) {
  281. let rationGljList = rationItem.rationGljList,
  282. gljIds = [];
  283. rationGljList.forEach(function (rationGlj) {
  284. let idObj = Object.create(null);
  285. idObj.id = rationGlj.gljId;
  286. idObj.type = rationGlj.type;
  287. gljIds.push(idObj);
  288. });
  289. me.getGljItemsByIds(userID, gljIds, function(err, gljItems){
  290. if(err){
  291. ecb(err);
  292. }
  293. else{
  294. let gljArr = [];
  295. for(let i=0; i<gljItems.length; i++){
  296. if(gljItems[i].ID === adjGljId){
  297. gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice});
  298. } else {
  299. gljArr.push({gljId: gljItems[i].ID, basePrice: parseFloat(gljItems[i].basePrice)});
  300. }
  301. }
  302. gljArr.forEach(function (gljItem) {
  303. rationGljList.forEach(function (rationGlj) {
  304. if(gljItem.gljId === rationGlj.gljId){
  305. gljItem.consumeAmt = parseFloat(rationGlj.consumeAmt);
  306. }
  307. })
  308. });
  309. //recalculate the price of ration
  310. let basePrice = 0;
  311. gljArr.forEach(function (gljItem) {
  312. basePrice += gljItem.basePrice * gljItem.consumeAmt;
  313. });
  314. basePrice = scMathUtil.roundTo(basePrice, 0);
  315. //updateDataBase
  316. compleRationModel.update({ID: rationItem.ID}, {$set: {basePrice: basePrice.toString()}},
  317. function (err, result) {
  318. if(err){
  319. ecb(err);
  320. }
  321. else {
  322. ecb(null);
  323. }
  324. });
  325. }
  326. });
  327. }, function(err){
  328. if(err){
  329. cb(err);
  330. }
  331. else {
  332. cb(null);
  333. }
  334. });
  335. },
  336. ], function (err) {
  337. if(err){
  338. finalCb(err);
  339. }
  340. else{
  341. finalCb(null);
  342. }
  343. });
  344. }, function (err) {
  345. if(err){
  346. callback(err, 'Error');
  347. }
  348. else{
  349. callback(0, '');
  350. }
  351. });
  352. }
  353. mixUpdateRationItems (userID, compilationId, rationLibId, sectionId, updateItems, addItems, rIds, callback){
  354. let me = this;
  355. if (updateItems.length == 0 && rIds.length == 0) {
  356. me.addRationItems(userID, compilationId, rationLibId, sectionId, addItems, callback);
  357. } else {
  358. me.removeRationItems(rationLibId, rIds, function(err, message, docs) {
  359. if (err) {
  360. callback(true, false);
  361. } else {
  362. me.updateRationItems(userID, rationLibId, sectionId, updateItems, function(err, results){
  363. if (err) {
  364. callback(true, false);
  365. } else {
  366. if (addItems && addItems.length > 0) {
  367. me.addRationItems(rationLibId, sectionId, addItems, callback);
  368. } else {
  369. callback(0, results);
  370. }
  371. }
  372. });
  373. }
  374. })
  375. }
  376. }
  377. removeRationItems(rationRepId, rIds,callback){
  378. if (rIds.length > 0) {
  379. compleRationModel.remove({rationRepId: rationRepId, ID: {$in: rIds}}, function(err, docs){
  380. if (err) {
  381. callback(true, false);
  382. } else {
  383. callback(0, docs);
  384. }
  385. })
  386. } else {
  387. callback(0, null);
  388. }
  389. }
  390. addRationItems(userID, compilationId, rationLibId, sectionId, items,callback){
  391. if (items && items.length > 0) {
  392. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  393. let maxId = result.sequence_value;
  394. let arr = [];
  395. for (let i = 0; i < items.length; i++) {
  396. let obj = new compleRationModel(items[i]);
  397. obj.ID = (maxId - (items.length - 1) + i);
  398. obj.sectionId = sectionId;
  399. obj.rationRepId = rationLibId;
  400. obj.userId = userID;
  401. obj.compilationId = compilationId;
  402. arr.push(obj);
  403. }
  404. compleRationModel.collection.insert(arr, null, function(err, docs){
  405. if (err) {
  406. callback(true, false);
  407. } else {
  408. callback(0, docs);
  409. }
  410. })
  411. });
  412. } else {
  413. callback(true, "Source error!", false);
  414. }
  415. }
  416. updateRationItems(userID, rationLibId, sectionId, items,callback){
  417. let functions = [];
  418. for (let i=0; i < items.length; i++) {
  419. functions.push((function(doc) {
  420. return function(cb) {
  421. var filter = {};
  422. if (doc.ID) {
  423. filter.ID = doc.ID;
  424. } else {
  425. filter.sectionId = sectionId;
  426. filter.userId = userID;
  427. if (rationLibId) filter.rationRepId = rationLibId;
  428. filter.code = doc.code;
  429. }
  430. compleRationModel.update(filter, doc, cb);
  431. };
  432. })(items[i]));
  433. }
  434. async.parallel(functions, function(err, results) {
  435. if(!err){
  436. err = 0;
  437. }
  438. callback(err, results);
  439. });
  440. }
  441. async getCodes (userId, compilationId) {
  442. const compleRations = await compleRationModel.find({userId, compilationId}, '-_id code').lean();
  443. const codes = [];
  444. compleRations.forEach(item => codes.push(item.code));
  445. return codes;
  446. }
  447. async prepareInitData (userId, compilationId, gljLibId) {
  448. const rationsCodes = await this.getCodes(userId, compilationId);
  449. const gljDistTypeCache = stdgljutil.getStdGljTypeCacheObj().toArray();
  450. const installationData = await installFacade.getInstallation(userId, compilationId);
  451. const rationTreeData = await sectionTreeDao.getComplementaryTree(userId, compilationId);
  452. const mixedTreeData = await gljDao.getMixedTree(gljLibId, userId, compilationId);
  453. const mixedGLJData = await gljDao.getGLJDataSync(gljLibId, userId, compilationId);
  454. return {
  455. rationsCodes,
  456. gljDistTypeCache,
  457. installationData,
  458. rationTreeData,
  459. mixedTreeData,
  460. mixedGLJData
  461. }
  462. }
  463. }
  464. function isDef(v){
  465. return v !== undefined && v !== null;
  466. }
  467. export default CompleRatoinDao;