compleRationModel.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. let stdRations = await stdRationModel.find({sectionId: sectionId, $or: [{isDeleted: null}, {isDeleted: false}]});
  137. for(let ration of stdRations){
  138. ration._doc.type = 'std';
  139. }
  140. let compleRations = await compleRationModel.find({userId: userId, sectionId: sectionId, deleteInfo: null});
  141. for(let ration of compleRations){
  142. ration._doc.type = 'complementary';
  143. }
  144. let rations = stdRations.concat(compleRations);
  145. rations.sort(function (a, b) {
  146. let rst = 0;
  147. if(a.code > b.code){
  148. rst = 1;
  149. }
  150. else if(a.code < b.code){
  151. rst = -1;
  152. }
  153. return rst;
  154. });
  155. for(let ration of rations){
  156. let hint = '';
  157. for(let rationGlj of ration.rationGljList){
  158. let glj;
  159. if(!isDef(rationGlj.type) || rationGlj.type === 'std'){
  160. glj = await stdGljModel.findOne({ID: rationGlj.gljId});
  161. }
  162. else {
  163. glj = await complementaryGljModel.findOne({uesrId: userId, ID: rationGlj.gljId});
  164. }
  165. if(isDef(glj)){
  166. let unitHint = '' + glj.code + ' ' + glj.name + ' ' + glj.unit + ' ' + rationGlj.consumeAmt + '</br>';
  167. hint += unitHint;
  168. }
  169. }
  170. ration._doc.hint = hint;
  171. }
  172. callback(0, rations);
  173. }
  174. catch(err){
  175. callback(err, null);
  176. }
  177. }
  178. updateRationBasePrc(userID, basePrcArr, callback){
  179. let me = this;
  180. async.each(basePrcArr, function (basePrcObj, finalCb) {
  181. let adjGljId = basePrcObj.gljId, adjBasePrice = basePrcObj.basePrice, adjGljType = basePrcObj.gljType;
  182. async.waterfall([
  183. function (cb) {
  184. if(typeof basePrcObj.delete !== 'undefined' && basePrcObj.delete === 1){
  185. //补充定额
  186. compleRationModel.find({'rationGljList.gljId': adjGljId},{ID: 1, rationGljList: 1}, function (err, compleRst) {
  187. if(err){
  188. cb(err);
  189. }
  190. else {
  191. compleRationModel.update({'rationGljList.gljId': adjGljId}, {$pull: {rationGljList: {gljId: adjGljId}}}, {multi: true}, function (err) {
  192. if(err){
  193. cb(err);
  194. }
  195. else {
  196. cb(null, compleRst);
  197. }
  198. });
  199. }
  200. });
  201. }
  202. else{
  203. compleRationModel.find({'rationGljList.gljId': adjGljId}, function (err, compleRst) {
  204. if(err){
  205. cb(err);
  206. }
  207. else {
  208. cb(null, compleRst);
  209. }
  210. });
  211. }
  212. },
  213. function (result, cb) {
  214. async.each(result, function (rationItem, ecb) {
  215. let rationGljList = rationItem.rationGljList,
  216. gljIds = [];
  217. rationGljList.forEach(function (rationGlj) {
  218. let idObj = Object.create(null);
  219. idObj.id = rationGlj.gljId;
  220. idObj.type = rationGlj.type;
  221. gljIds.push(idObj);
  222. });
  223. me.getGljItemsByIds(userID, gljIds, function(err, gljItems){
  224. if(err){
  225. ecb(err);
  226. }
  227. else{
  228. let gljArr = [];
  229. for(let i=0; i<gljItems.length; i++){
  230. let gljParentType = -1;
  231. if(gljItems[i].ID === adjGljId){
  232. gljItems[i].gljType = adjGljType;
  233. }
  234. if(gljItems[i].gljType <= 3){
  235. gljParentType = gljItems[i].gljType;
  236. }
  237. if(gljItems[i].gljType > 200 && gljItems[i].gljType < 300){
  238. gljParentType = 2;
  239. }
  240. if(gljItems[i].gljType > 300 && gljItems[i].gljType < 400){
  241. gljParentType = 3;
  242. }
  243. if(gljItems[i].ID === adjGljId){
  244. gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice, gljParentType: gljParentType});
  245. }
  246. else {
  247. gljArr.push({gljId: gljItems[i].ID, basePrice: parseFloat(gljItems[i].basePrice), gljParentType: gljParentType});
  248. }
  249. }
  250. gljArr.forEach(function (gljItem) {
  251. rationGljList.forEach(function (rationGlj) {
  252. if(gljItem.gljId === rationGlj.gljId){
  253. gljItem.consumeAmt = parseFloat(rationGlj.consumeAmt);
  254. }
  255. })
  256. });
  257. //recalculate the price of ration
  258. let labourPrc = [], materialPrc = [], machinePrc = [], singlePrc, updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, basePrice: 0};
  259. gljArr.forEach(function (gljItem) {
  260. if(gljItem.gljParentType !== -1){
  261. singlePrc = scMathUtil.roundTo(gljItem.basePrice * gljItem.consumeAmt, -3);
  262. if(gljItem.gljParentType === 1){
  263. labourPrc.push(singlePrc);
  264. }
  265. else if(gljItem.gljParentType ===2){
  266. materialPrc.push(singlePrc);
  267. }
  268. else{
  269. machinePrc.push(singlePrc);
  270. }
  271. }
  272. });
  273. if(labourPrc.length > 0){
  274. let sumLaP = 0;
  275. for(let i=0; i<labourPrc.length; i++){
  276. sumLaP += labourPrc[i];
  277. }
  278. updatePrc.labourPrice = scMathUtil.roundTo(sumLaP, -2);
  279. }
  280. if(materialPrc.length > 0){
  281. let sumMtP = 0;
  282. for(let i= 0; i<materialPrc.length; i++){
  283. sumMtP += materialPrc[i];
  284. }
  285. updatePrc.materialPrice = scMathUtil.roundTo(sumMtP, -2);
  286. }
  287. if(machinePrc.length > 0){
  288. let sumMaP = 0;
  289. for(let i =0; i< machinePrc.length; i++){
  290. sumMaP += machinePrc[i];
  291. }
  292. updatePrc.machinePrice = scMathUtil.roundTo(sumMaP, -2);
  293. }
  294. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.labourPrice + updatePrc.materialPrice + updatePrc.machinePrice, -2);
  295. //updateDataBase
  296. compleRationModel.update({ID: rationItem.ID}, {$set: {labourPrice: updatePrc.labourPrice.toString(), materialPrice: updatePrc.materialPrice.toString(),
  297. machinePrice: updatePrc.machinePrice.toString(), basePrice: updatePrc.basePrice.toString()}},
  298. function (err, result) {
  299. if(err){
  300. ecb(err);
  301. }
  302. else {
  303. ecb(null);
  304. }
  305. });
  306. }
  307. });
  308. }, function(err){
  309. if(err){
  310. cb(err);
  311. }
  312. else {
  313. cb(null);
  314. }
  315. });
  316. },
  317. ], function (err) {
  318. if(err){
  319. finalCb(err);
  320. }
  321. else{
  322. finalCb(null);
  323. }
  324. });
  325. }, function (err) {
  326. if(err){
  327. callback(err, 'Error');
  328. }
  329. else{
  330. callback(0, '');
  331. }
  332. });
  333. }
  334. mixUpdateRationItems (userID, compilationId, rationLibId, sectionId, updateItems, addItems, rIds, callback){
  335. let me = this;
  336. if (updateItems.length == 0 && rIds.length == 0) {
  337. me.addRationItems(userID, compilationId, rationLibId, sectionId, addItems, callback);
  338. } else {
  339. me.removeRationItems(rationLibId, rIds, function(err, message, docs) {
  340. if (err) {
  341. callback(true, false);
  342. } else {
  343. me.updateRationItems(userID, rationLibId, sectionId, updateItems, function(err, results){
  344. if (err) {
  345. callback(true, false);
  346. } else {
  347. if (addItems && addItems.length > 0) {
  348. me.addRationItems(rationLibId, sectionId, addItems, callback);
  349. } else {
  350. callback(0, results);
  351. }
  352. }
  353. });
  354. }
  355. })
  356. }
  357. }
  358. removeRationItems(rationRepId, rIds,callback){
  359. if (rIds.length > 0) {
  360. compleRationModel.remove({rationRepId: rationRepId, ID: {$in: rIds}}, function(err, docs){
  361. if (err) {
  362. callback(true, false);
  363. } else {
  364. callback(0, docs);
  365. }
  366. })
  367. } else {
  368. callback(0, null);
  369. }
  370. }
  371. addRationItems(userID, compilationId, rationLibId, sectionId, items,callback){
  372. if (items && items.length > 0) {
  373. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  374. let maxId = result.value.sequence_value;
  375. let arr = [];
  376. for (let i = 0; i < items.length; i++) {
  377. let obj = new compleRationModel(items[i]);
  378. obj.ID = (maxId - (items.length - 1) + i);
  379. obj.sectionId = sectionId;
  380. obj.rationRepId = rationLibId;
  381. obj.userId = userID;
  382. obj.compilationId = compilationId;
  383. arr.push(obj);
  384. }
  385. compleRationModel.collection.insert(arr, null, function(err, docs){
  386. if (err) {
  387. callback(true, false);
  388. } else {
  389. callback(0, docs);
  390. }
  391. })
  392. });
  393. } else {
  394. callback(true, "Source error!", false);
  395. }
  396. }
  397. updateRationItems(userID, rationLibId, sectionId, items,callback){
  398. let functions = [];
  399. for (let i=0; i < items.length; i++) {
  400. functions.push((function(doc) {
  401. return function(cb) {
  402. var filter = {};
  403. if (doc.ID) {
  404. filter.ID = doc.ID;
  405. } else {
  406. filter.sectionId = sectionId;
  407. filter.userId = userID;
  408. if (rationLibId) filter.rationRepId = rationLibId;
  409. filter.code = doc.code;
  410. }
  411. compleRationModel.update(filter, doc, cb);
  412. };
  413. })(items[i]));
  414. }
  415. async.parallel(functions, function(err, results) {
  416. if(!err){
  417. err = 0;
  418. }
  419. callback(err, results);
  420. });
  421. }
  422. async getInstallation(rationRepId, callback){
  423. try {
  424. let feeItems = await installFeeItemModel.find({rationRepId: rationRepId, $or: [{deleted: false}, {deleted: null}]});
  425. for(let feeItem of feeItems){
  426. let sids = [];
  427. for(let sec of feeItem.section){
  428. sids.push(sec.ID);
  429. }
  430. if(sids.length > 0){
  431. let sections = await installSectionModel.find({ID: {$in: sids}, $or: [{deleted: false}, {deleted: null}]});
  432. feeItem._doc.section = sections;
  433. }
  434. }
  435. callback(0, feeItems);
  436. }
  437. catch(err){
  438. callback(err, null);
  439. }
  440. }
  441. }
  442. function isDef(v){
  443. return v !== undefined && v !== null;
  444. }
  445. export default CompleRatoinDao;