compleRationModel.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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}});
  154. }
  155. if(comGljIds.length > 0) {
  156. comGljs = await complementaryGljModel.find({userId: userId, ID: {$in: comGljIds}});
  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}});
  234. }
  235. let gljDatas = gljUtil.sortRationGLJ(stdGljs,true);
  236. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  237. hintsArr.push(`<label class="nomargin font_blue">工作内容:`);
  238. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  239. hintsArr.push("</label>");
  240. hintsArr.push("");
  241. }
  242. for(let glj of gljDatas){
  243. 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>`)
  244. }
  245. hintsArr.push(`基价 元 ${ration.basePrice}`);
  246. if(ration.annotation && ration.annotation.toString().trim() !== ''){
  247. hintsArr.push(`<br>附注:`);
  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. let gljParentType = -1;
  307. if(gljItems[i].ID === adjGljId){
  308. gljItems[i].gljType = adjGljType;
  309. }
  310. if(gljItems[i].gljType <= 3){
  311. gljParentType = gljItems[i].gljType;
  312. }
  313. if(gljItems[i].gljType > 200 && gljItems[i].gljType < 300){
  314. gljParentType = 2;
  315. }
  316. if(gljItems[i].gljType > 300 && gljItems[i].gljType < 400){
  317. gljParentType = 3;
  318. }
  319. if(gljItems[i].ID === adjGljId){
  320. gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice, gljParentType: gljParentType});
  321. }
  322. else {
  323. gljArr.push({gljId: gljItems[i].ID, basePrice: parseFloat(gljItems[i].basePrice), gljParentType: gljParentType});
  324. }
  325. }
  326. gljArr.forEach(function (gljItem) {
  327. rationGljList.forEach(function (rationGlj) {
  328. if(gljItem.gljId === rationGlj.gljId){
  329. gljItem.consumeAmt = parseFloat(rationGlj.consumeAmt);
  330. }
  331. })
  332. });
  333. //recalculate the price of ration
  334. let labourPrc = [], materialPrc = [], machinePrc = [], singlePrc, updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, basePrice: 0};
  335. gljArr.forEach(function (gljItem) {
  336. if(gljItem.gljParentType !== -1){
  337. singlePrc = scMathUtil.roundTo(gljItem.basePrice * gljItem.consumeAmt, -3);
  338. if(gljItem.gljParentType === 1){
  339. labourPrc.push(singlePrc);
  340. }
  341. else if(gljItem.gljParentType ===2){
  342. materialPrc.push(singlePrc);
  343. }
  344. else{
  345. machinePrc.push(singlePrc);
  346. }
  347. }
  348. });
  349. if(labourPrc.length > 0){
  350. let sumLaP = 0;
  351. for(let i=0; i<labourPrc.length; i++){
  352. sumLaP += labourPrc[i];
  353. }
  354. updatePrc.labourPrice = scMathUtil.roundTo(sumLaP, -2);
  355. }
  356. if(materialPrc.length > 0){
  357. let sumMtP = 0;
  358. for(let i= 0; i<materialPrc.length; i++){
  359. sumMtP += materialPrc[i];
  360. }
  361. updatePrc.materialPrice = scMathUtil.roundTo(sumMtP, -2);
  362. }
  363. if(machinePrc.length > 0){
  364. let sumMaP = 0;
  365. for(let i =0; i< machinePrc.length; i++){
  366. sumMaP += machinePrc[i];
  367. }
  368. updatePrc.machinePrice = scMathUtil.roundTo(sumMaP, -2);
  369. }
  370. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.labourPrice + updatePrc.materialPrice + updatePrc.machinePrice, -2);
  371. //updateDataBase
  372. compleRationModel.update({ID: rationItem.ID}, {$set: {labourPrice: updatePrc.labourPrice.toString(), materialPrice: updatePrc.materialPrice.toString(),
  373. machinePrice: updatePrc.machinePrice.toString(), basePrice: updatePrc.basePrice.toString()}},
  374. function (err, result) {
  375. if(err){
  376. ecb(err);
  377. }
  378. else {
  379. ecb(null);
  380. }
  381. });
  382. }
  383. });
  384. }, function(err){
  385. if(err){
  386. cb(err);
  387. }
  388. else {
  389. cb(null);
  390. }
  391. });
  392. },
  393. ], function (err) {
  394. if(err){
  395. finalCb(err);
  396. }
  397. else{
  398. finalCb(null);
  399. }
  400. });
  401. }, function (err) {
  402. if(err){
  403. callback(err, 'Error');
  404. }
  405. else{
  406. callback(0, '');
  407. }
  408. });
  409. }
  410. mixUpdateRationItems (userID, compilationId, rationLibId, sectionId, updateItems, addItems, rIds, callback){
  411. let me = this;
  412. if (updateItems.length == 0 && rIds.length == 0) {
  413. me.addRationItems(userID, compilationId, rationLibId, sectionId, addItems, callback);
  414. } else {
  415. me.removeRationItems(rationLibId, rIds, function(err, message, docs) {
  416. if (err) {
  417. callback(true, false);
  418. } else {
  419. me.updateRationItems(userID, rationLibId, sectionId, updateItems, function(err, results){
  420. if (err) {
  421. callback(true, false);
  422. } else {
  423. if (addItems && addItems.length > 0) {
  424. me.addRationItems(rationLibId, sectionId, addItems, callback);
  425. } else {
  426. callback(0, results);
  427. }
  428. }
  429. });
  430. }
  431. })
  432. }
  433. }
  434. removeRationItems(rationRepId, rIds,callback){
  435. if (rIds.length > 0) {
  436. compleRationModel.remove({rationRepId: rationRepId, ID: {$in: rIds}}, function(err, docs){
  437. if (err) {
  438. callback(true, false);
  439. } else {
  440. callback(0, docs);
  441. }
  442. })
  443. } else {
  444. callback(0, null);
  445. }
  446. }
  447. addRationItems(userID, compilationId, rationLibId, sectionId, items,callback){
  448. if (items && items.length > 0) {
  449. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  450. let maxId = result.sequence_value;
  451. let arr = [];
  452. for (let i = 0; i < items.length; i++) {
  453. let obj = new compleRationModel(items[i]);
  454. obj.ID = (maxId - (items.length - 1) + i);
  455. obj.sectionId = sectionId;
  456. obj.rationRepId = rationLibId;
  457. obj.userId = userID;
  458. obj.compilationId = compilationId;
  459. arr.push(obj);
  460. }
  461. compleRationModel.collection.insert(arr, null, function(err, docs){
  462. if (err) {
  463. callback(true, false);
  464. } else {
  465. callback(0, docs);
  466. }
  467. })
  468. });
  469. } else {
  470. callback(true, "Source error!", false);
  471. }
  472. }
  473. updateRationItems(userID, rationLibId, sectionId, items,callback){
  474. let functions = [];
  475. for (let i=0; i < items.length; i++) {
  476. functions.push((function(doc) {
  477. return function(cb) {
  478. var filter = {};
  479. if (doc.ID) {
  480. filter.ID = doc.ID;
  481. } else {
  482. filter.sectionId = sectionId;
  483. filter.userId = userID;
  484. if (rationLibId) filter.rationRepId = rationLibId;
  485. filter.code = doc.code;
  486. }
  487. compleRationModel.update(filter, doc, cb);
  488. };
  489. })(items[i]));
  490. }
  491. async.parallel(functions, function(err, results) {
  492. if(!err){
  493. err = 0;
  494. }
  495. callback(err, results);
  496. });
  497. }
  498. async getCodes (userId, compilationId) {
  499. const compleRations = await compleRationModel.find({userId, compilationId}, '-_id code').lean();
  500. const codes = [];
  501. compleRations.forEach(item => codes.push(item.code));
  502. return codes;
  503. }
  504. async prepareInitData (userId, compilationId, gljLibId) {
  505. const rationsCodes = await this.getCodes(userId, compilationId);
  506. const gljDistTypeCache = stdgljutil.getStdGljTypeCacheObj().toArray();
  507. const installationData = await installFacade.getInstallation(userId, compilationId);
  508. const rationTreeData = await sectionTreeDao.getComplementaryTree(userId, compilationId);
  509. const mixedTreeData = await gljDao.getMixedTree(gljLibId, userId, compilationId);
  510. const mixedGLJData = await gljDao.getGLJDataSync(gljLibId, userId, compilationId);
  511. return {
  512. rationsCodes,
  513. gljDistTypeCache,
  514. installationData,
  515. rationTreeData,
  516. mixedTreeData,
  517. mixedGLJData
  518. }
  519. }
  520. }
  521. function isDef(v){
  522. return v !== undefined && v !== null;
  523. }
  524. export default CompleRatoinDao;