compleRationModel.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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) {
  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}}).lean();
  157. }
  158. let gljDatas = gljUtil.sortRationGLJ(stdGljs.concat(comGljs),true);
  159. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  160. hintsArr.push(`<label class="nomargin font_blue">工作内容:`);
  161. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  162. hintsArr.push("</label>");
  163. hintsArr.push("");
  164. }
  165. for(let glj of gljDatas){
  166. hintsArr.push(`<label class="nomargin ${glj.gljType==4?"font_blue":""}"> ${glj.code} ${glj.name}${glj.specs ? '&nbsp;&nbsp;&nbsp;' + glj.specs : ''}&nbsp;&nbsp&nbsp;${glj.unit}&nbsp;&nbsp;&nbsp;${gljAmtMapping[glj.ID]}</label>`)
  167. }
  168. hintsArr.push(`基价 元 ${ration.basePrice}`);
  169. if(ration.annotation && ration.annotation.toString().trim() !== ''){
  170. hintsArr.push(`<br>附注:`);
  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, callback){
  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}}).lean();
  234. /*stdGljs.forEach(function (glj) {
  235. glj.type = glj.gljType;
  236. });*/
  237. }
  238. let gljDatas = gljUtil.sortRationGLJ(stdGljs,true);
  239. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  240. hintsArr.push(`<label class="nomargin font_blue">工作内容:`);
  241. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  242. hintsArr.push("</label>");
  243. hintsArr.push("");
  244. }
  245. for(let glj of gljDatas){
  246. hintsArr.push(`<label class="nomargin ${glj.gljType==4?"font_blue":""}"> ${glj.code} ${glj.name}${glj.specs ? '&nbsp;&nbsp;&nbsp;' + glj.specs : ''}&nbsp;&nbsp&nbsp;${glj.unit}&nbsp;&nbsp;&nbsp;${gljAmtMapping[glj.ID]}</label>`)
  247. }
  248. hintsArr.push(`基价 元 ${ration.basePrice}`);
  249. if(ration.annotation && ration.annotation.toString().trim() !== ''){
  250. hintsArr.push(`<br>附注:`);
  251. hintsArr = hintsArr.concat(ration.annotation.split('\n'));
  252. }
  253. ration._doc.hint = hintsArr.join('<br>');
  254. }
  255. return stdRations;
  256. }
  257. updateRationBasePrc(userID, basePrcArr, callback){
  258. let me = this;
  259. async.each(basePrcArr, function (basePrcObj, finalCb) {
  260. let adjGljId = basePrcObj.gljId, adjBasePrice = basePrcObj.basePrice, adjGljType = basePrcObj.gljType;
  261. async.waterfall([
  262. function (cb) {
  263. if(typeof basePrcObj.delete !== 'undefined' && basePrcObj.delete === 1){
  264. //补充定额
  265. compleRationModel.find({'rationGljList.gljId': adjGljId},{ID: 1, rationGljList: 1}, function (err, compleRst) {
  266. if(err){
  267. cb(err);
  268. }
  269. else {
  270. compleRationModel.update({'rationGljList.gljId': adjGljId}, {$pull: {rationGljList: {gljId: adjGljId}}}, {multi: true}, function (err) {
  271. if(err){
  272. cb(err);
  273. }
  274. else {
  275. cb(null, compleRst);
  276. }
  277. });
  278. }
  279. });
  280. }
  281. else{
  282. compleRationModel.find({'rationGljList.gljId': adjGljId}, function (err, compleRst) {
  283. if(err){
  284. cb(err);
  285. }
  286. else {
  287. cb(null, compleRst);
  288. }
  289. });
  290. }
  291. },
  292. function (result, cb) {
  293. async.each(result, function (rationItem, ecb) {
  294. let rationGljList = rationItem.rationGljList,
  295. gljIds = [];
  296. rationGljList.forEach(function (rationGlj) {
  297. let idObj = Object.create(null);
  298. idObj.id = rationGlj.gljId;
  299. idObj.type = rationGlj.type;
  300. gljIds.push(idObj);
  301. });
  302. me.getGljItemsByIds(userID, gljIds, function(err, gljItems){
  303. if(err){
  304. ecb(err);
  305. }
  306. else{
  307. let gljArr = [];
  308. for(let i=0; i<gljItems.length; i++){
  309. let gljParentType = -1;
  310. if(gljItems[i].ID === adjGljId){
  311. gljItems[i].gljType = adjGljType;
  312. }
  313. if(gljItems[i].gljType <= 3){
  314. gljParentType = gljItems[i].gljType;
  315. }
  316. if(gljItems[i].gljType > 200 && gljItems[i].gljType < 300){
  317. gljParentType = 2;
  318. }
  319. if(gljItems[i].gljType > 300 && gljItems[i].gljType < 400){
  320. gljParentType = 3;
  321. }
  322. if(gljItems[i].ID === adjGljId){
  323. gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice, gljParentType: gljParentType});
  324. }
  325. else {
  326. gljArr.push({gljId: gljItems[i].ID, basePrice: parseFloat(gljItems[i].basePrice), gljParentType: gljParentType});
  327. }
  328. }
  329. gljArr.forEach(function (gljItem) {
  330. rationGljList.forEach(function (rationGlj) {
  331. if(gljItem.gljId === rationGlj.gljId){
  332. gljItem.consumeAmt = parseFloat(rationGlj.consumeAmt);
  333. }
  334. })
  335. });
  336. //recalculate the price of ration
  337. let labourPrc = [], materialPrc = [], machinePrc = [], singlePrc, updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, basePrice: 0};
  338. gljArr.forEach(function (gljItem) {
  339. if(gljItem.gljParentType !== -1){
  340. singlePrc = scMathUtil.roundTo(gljItem.basePrice * gljItem.consumeAmt, -3);
  341. if(gljItem.gljParentType === 1){
  342. labourPrc.push(singlePrc);
  343. }
  344. else if(gljItem.gljParentType ===2){
  345. materialPrc.push(singlePrc);
  346. }
  347. else{
  348. machinePrc.push(singlePrc);
  349. }
  350. }
  351. });
  352. if(labourPrc.length > 0){
  353. let sumLaP = 0;
  354. for(let i=0; i<labourPrc.length; i++){
  355. sumLaP += labourPrc[i];
  356. }
  357. updatePrc.labourPrice = scMathUtil.roundTo(sumLaP, -2);
  358. }
  359. if(materialPrc.length > 0){
  360. let sumMtP = 0;
  361. for(let i= 0; i<materialPrc.length; i++){
  362. sumMtP += materialPrc[i];
  363. }
  364. updatePrc.materialPrice = scMathUtil.roundTo(sumMtP, -2);
  365. }
  366. if(machinePrc.length > 0){
  367. let sumMaP = 0;
  368. for(let i =0; i< machinePrc.length; i++){
  369. sumMaP += machinePrc[i];
  370. }
  371. updatePrc.machinePrice = scMathUtil.roundTo(sumMaP, -2);
  372. }
  373. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.labourPrice + updatePrc.materialPrice + updatePrc.machinePrice, -2);
  374. //updateDataBase
  375. compleRationModel.update({ID: rationItem.ID}, {$set: {labourPrice: updatePrc.labourPrice.toString(), materialPrice: updatePrc.materialPrice.toString(),
  376. machinePrice: updatePrc.machinePrice.toString(), basePrice: updatePrc.basePrice.toString()}},
  377. function (err, result) {
  378. if(err){
  379. ecb(err);
  380. }
  381. else {
  382. ecb(null);
  383. }
  384. });
  385. }
  386. });
  387. }, function(err){
  388. if(err){
  389. cb(err);
  390. }
  391. else {
  392. cb(null);
  393. }
  394. });
  395. },
  396. ], function (err) {
  397. if(err){
  398. finalCb(err);
  399. }
  400. else{
  401. finalCb(null);
  402. }
  403. });
  404. }, function (err) {
  405. if(err){
  406. callback(err, 'Error');
  407. }
  408. else{
  409. callback(0, '');
  410. }
  411. });
  412. }
  413. mixUpdateRationItems (userID, compilationId, rationLibId, sectionId, updateItems, addItems, rIds, callback){
  414. let me = this;
  415. if (updateItems.length == 0 && rIds.length == 0) {
  416. me.addRationItems(userID, compilationId, rationLibId, sectionId, addItems, callback);
  417. } else {
  418. me.removeRationItems(rationLibId, rIds, function(err, message, docs) {
  419. if (err) {
  420. callback(true, false);
  421. } else {
  422. me.updateRationItems(userID, rationLibId, sectionId, updateItems, function(err, results){
  423. if (err) {
  424. callback(true, false);
  425. } else {
  426. if (addItems && addItems.length > 0) {
  427. me.addRationItems(rationLibId, sectionId, addItems, callback);
  428. } else {
  429. callback(0, results);
  430. }
  431. }
  432. });
  433. }
  434. })
  435. }
  436. }
  437. removeRationItems(rationRepId, rIds,callback){
  438. if (rIds.length > 0) {
  439. compleRationModel.remove({rationRepId: rationRepId, ID: {$in: rIds}}, function(err, docs){
  440. if (err) {
  441. callback(true, false);
  442. } else {
  443. callback(0, docs);
  444. }
  445. })
  446. } else {
  447. callback(0, null);
  448. }
  449. }
  450. addRationItems(userID, compilationId, rationLibId, sectionId, items,callback){
  451. if (items && items.length > 0) {
  452. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  453. let maxId = result.sequence_value;
  454. let arr = [];
  455. for (let i = 0; i < items.length; i++) {
  456. let obj = new compleRationModel(items[i]);
  457. obj.ID = (maxId - (items.length - 1) + i);
  458. obj.sectionId = sectionId;
  459. obj.rationRepId = rationLibId;
  460. obj.userId = userID;
  461. obj.compilationId = compilationId;
  462. arr.push(obj);
  463. }
  464. compleRationModel.collection.insert(arr, null, function(err, docs){
  465. if (err) {
  466. callback(true, false);
  467. } else {
  468. callback(0, docs);
  469. }
  470. })
  471. });
  472. } else {
  473. callback(true, "Source error!", false);
  474. }
  475. }
  476. updateRationItems(userID, rationLibId, sectionId, items,callback){
  477. let functions = [];
  478. for (let i=0; i < items.length; i++) {
  479. functions.push((function(doc) {
  480. return function(cb) {
  481. var filter = {};
  482. if (doc.ID) {
  483. filter.ID = doc.ID;
  484. } else {
  485. filter.sectionId = sectionId;
  486. filter.userId = userID;
  487. if (rationLibId) filter.rationRepId = rationLibId;
  488. filter.code = doc.code;
  489. }
  490. compleRationModel.update(filter, doc, cb);
  491. };
  492. })(items[i]));
  493. }
  494. async.parallel(functions, function(err, results) {
  495. if(!err){
  496. err = 0;
  497. }
  498. callback(err, results);
  499. });
  500. }
  501. async getCodes (userId, compilationId) {
  502. const compleRations = await compleRationModel.find({userId, compilationId}, '-_id code').lean();
  503. const codes = [];
  504. compleRations.forEach(item => codes.push(item.code));
  505. return codes;
  506. }
  507. async prepareInitData (userId, compilationId, gljLibId) {
  508. const rationsCodes = await this.getCodes(userId, compilationId);
  509. const gljDistTypeCache = stdgljutil.getStdGljTypeCacheObj().toArray();
  510. const installationData = await installFacade.getInstallation(userId, compilationId);
  511. const rationTreeData = await sectionTreeDao.getComplementaryTree(userId, compilationId);
  512. const mixedTreeData = await gljDao.getMixedTree(gljLibId, userId, compilationId);
  513. const mixedGLJData = await gljDao.getGLJDataSync(gljLibId, userId, compilationId);
  514. return {
  515. rationsCodes,
  516. gljDistTypeCache,
  517. installationData,
  518. rationTreeData,
  519. mixedTreeData,
  520. mixedGLJData
  521. }
  522. }
  523. }
  524. function isDef(v){
  525. return v !== undefined && v !== null;
  526. }
  527. export default CompleRatoinDao;