compleRationModel.js 19 KB

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