compleRationModel.js 22 KB

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