compleRationModel.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**
  2. * Created by Zhong on 2017/12/21.
  3. */
  4. import {compleRationModel} 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. updateRationBasePrc(userID, basePrcArr, callback){
  129. let me = this;
  130. async.each(basePrcArr, function (basePrcObj, finalCb) {
  131. let adjGljId = basePrcObj.gljId, adjBasePrice = basePrcObj.basePrice, adjGljType = basePrcObj.gljType;
  132. async.waterfall([
  133. function (cb) {
  134. if(typeof basePrcObj.delete !== 'undefined' && basePrcObj.delete === 1){
  135. //补充定额
  136. compleRationModel.find({'rationGljList.gljId': adjGljId},{ID: 1, rationGljList: 1}, function (err, compleRst) {
  137. if(err){
  138. cb(err);
  139. }
  140. else {
  141. compleRationModel.update({'rationGljList.gljId': adjGljId}, {$pull: {rationGljList: {gljId: adjGljId}}}, {multi: true}, function (err) {
  142. if(err){
  143. cb(err);
  144. }
  145. else {
  146. cb(null, compleRst);
  147. }
  148. });
  149. }
  150. });
  151. }
  152. else{
  153. compleRationModel.find({'rationGljList.gljId': adjGljId}, function (err, compleRst) {
  154. if(err){
  155. cb(err);
  156. }
  157. else {
  158. cb(null, compleRst);
  159. }
  160. });
  161. }
  162. },
  163. function (result, cb) {
  164. async.each(result, function (rationItem, ecb) {
  165. let rationGljList = rationItem.rationGljList,
  166. gljIds = [];
  167. rationGljList.forEach(function (rationGlj) {
  168. let idObj = Object.create(null);
  169. idObj.id = rationGlj.gljId;
  170. idObj.type = rationGlj.type;
  171. gljIds.push(idObj);
  172. });
  173. me.getGljItemsByIds(userID, gljIds, function(err, gljItems){
  174. if(err){
  175. ecb(err);
  176. }
  177. else{
  178. let gljArr = [];
  179. for(let i=0; i<gljItems.length; i++){
  180. let gljParentType = -1;
  181. if(gljItems[i].ID === adjGljId){
  182. gljItems[i].gljType = adjGljType;
  183. }
  184. if(gljItems[i].gljType <= 3){
  185. gljParentType = gljItems[i].gljType;
  186. }
  187. if(gljItems[i].gljType > 200 && gljItems[i].gljType < 300){
  188. gljParentType = 2;
  189. }
  190. if(gljItems[i].gljType > 300 && gljItems[i].gljType < 400){
  191. gljParentType = 3;
  192. }
  193. if(gljItems[i].ID === adjGljId){
  194. gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice, gljParentType: gljParentType});
  195. }
  196. else {
  197. gljArr.push({gljId: gljItems[i].ID, basePrice: parseFloat(gljItems[i].basePrice), gljParentType: gljParentType});
  198. }
  199. }
  200. gljArr.forEach(function (gljItem) {
  201. rationGljList.forEach(function (rationGlj) {
  202. if(gljItem.gljId === rationGlj.gljId){
  203. gljItem.consumeAmt = parseFloat(rationGlj.consumeAmt);
  204. }
  205. })
  206. });
  207. //recalculate the price of ration
  208. let labourPrc = [], materialPrc = [], machinePrc = [], singlePrc, updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, basePrice: 0};
  209. gljArr.forEach(function (gljItem) {
  210. if(gljItem.gljParentType !== -1){
  211. singlePrc = scMathUtil.roundTo(gljItem.basePrice * gljItem.consumeAmt, -3);
  212. if(gljItem.gljParentType === 1){
  213. labourPrc.push(singlePrc);
  214. }
  215. else if(gljItem.gljParentType ===2){
  216. materialPrc.push(singlePrc);
  217. }
  218. else{
  219. machinePrc.push(singlePrc);
  220. }
  221. }
  222. });
  223. if(labourPrc.length > 0){
  224. let sumLaP = 0;
  225. for(let i=0; i<labourPrc.length; i++){
  226. sumLaP += labourPrc[i];
  227. }
  228. updatePrc.labourPrice = scMathUtil.roundTo(sumLaP, -2);
  229. }
  230. if(materialPrc.length > 0){
  231. let sumMtP = 0;
  232. for(let i= 0; i<materialPrc.length; i++){
  233. sumMtP += materialPrc[i];
  234. }
  235. updatePrc.materialPrice = scMathUtil.roundTo(sumMtP, -2);
  236. }
  237. if(machinePrc.length > 0){
  238. let sumMaP = 0;
  239. for(let i =0; i< machinePrc.length; i++){
  240. sumMaP += machinePrc[i];
  241. }
  242. updatePrc.machinePrice = scMathUtil.roundTo(sumMaP, -2);
  243. }
  244. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.labourPrice + updatePrc.materialPrice + updatePrc.machinePrice, -2);
  245. //updateDataBase
  246. compleRationModel.update({ID: rationItem.ID}, {$set: {labourPrice: updatePrc.labourPrice.toString(), materialPrice: updatePrc.materialPrice.toString(),
  247. machinePrice: updatePrc.machinePrice.toString(), basePrice: updatePrc.basePrice.toString()}},
  248. function (err, result) {
  249. if(err){
  250. ecb(err);
  251. }
  252. else {
  253. ecb(null);
  254. }
  255. });
  256. }
  257. });
  258. }, function(err){
  259. if(err){
  260. cb(err);
  261. }
  262. else {
  263. cb(null);
  264. }
  265. });
  266. },
  267. ], function (err) {
  268. if(err){
  269. finalCb(err);
  270. }
  271. else{
  272. finalCb(null);
  273. }
  274. });
  275. }, function (err) {
  276. if(err){
  277. callback(err, 'Error');
  278. }
  279. else{
  280. callback(0, '');
  281. }
  282. });
  283. }
  284. mixUpdateRationItems (userID, compilationId, rationLibId, sectionId, updateItems, addItems, rIds, callback){
  285. let me = this;
  286. if (updateItems.length == 0 && rIds.length == 0) {
  287. me.addRationItems(userID, compilationId, rationLibId, sectionId, addItems, callback);
  288. } else {
  289. me.removeRationItems(rationLibId, rIds, function(err, message, docs) {
  290. if (err) {
  291. callback(true, false);
  292. } else {
  293. me.updateRationItems(userID, rationLibId, sectionId, updateItems, function(err, results){
  294. if (err) {
  295. callback(true, false);
  296. } else {
  297. if (addItems && addItems.length > 0) {
  298. me.addRationItems(rationLibId, sectionId, addItems, callback);
  299. } else {
  300. callback(0, results);
  301. }
  302. }
  303. });
  304. }
  305. })
  306. }
  307. }
  308. removeRationItems(rationRepId, rIds,callback){
  309. if (rIds.length > 0) {
  310. compleRationModel.remove({rationRepId: rationRepId, ID: {$in: rIds}}, function(err, docs){
  311. if (err) {
  312. callback(true, false);
  313. } else {
  314. callback(0, docs);
  315. }
  316. })
  317. } else {
  318. callback(0, null);
  319. }
  320. }
  321. addRationItems(userID, compilationId, rationLibId, sectionId, items,callback){
  322. if (items && items.length > 0) {
  323. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  324. let maxId = result.value.sequence_value;
  325. let arr = [];
  326. for (let i = 0; i < items.length; i++) {
  327. let obj = new compleRationModel(items[i]);
  328. obj.ID = (maxId - (items.length - 1) + i);
  329. obj.sectionId = sectionId;
  330. obj.rationRepId = rationLibId;
  331. obj.userId = userID;
  332. obj.compilationId = compilationId;
  333. arr.push(obj);
  334. }
  335. compleRationModel.collection.insert(arr, null, function(err, docs){
  336. if (err) {
  337. callback(true, false);
  338. } else {
  339. callback(0, docs);
  340. }
  341. })
  342. });
  343. } else {
  344. callback(true, "Source error!", false);
  345. }
  346. }
  347. updateRationItems(userID, rationLibId, sectionId, items,callback){
  348. let functions = [];
  349. for (let i=0; i < items.length; i++) {
  350. functions.push((function(doc) {
  351. return function(cb) {
  352. var filter = {};
  353. if (doc.ID) {
  354. filter.ID = doc.ID;
  355. } else {
  356. filter.sectionId = sectionId;
  357. filter.userId = userID;
  358. if (rationLibId) filter.rationRepId = rationLibId;
  359. filter.code = doc.code;
  360. }
  361. compleRationModel.update(filter, doc, cb);
  362. };
  363. })(items[i]));
  364. }
  365. async.parallel(functions, function(err, results) {
  366. if(!err){
  367. err = 0;
  368. }
  369. callback(err, results);
  370. });
  371. }
  372. }
  373. export default CompleRatoinDao;