compleRationModel.js 21 KB

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