compleRationModel.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. class CompleRatoinDao {
  10. async updateRation(userID, compilationId, updateData, callback){
  11. try{
  12. for(let i = 0, len = updateData.length; i < len; i++){
  13. let updateObj = updateData[i];
  14. if(updateObj.updateType === 'new'){
  15. updateObj.updateData.userID = userID;
  16. updateObj.updateData.compilationId = compilationId;
  17. await compleRationModel.create(updateObj.updateData);
  18. }
  19. else if(updateObj.updateType === 'update'){
  20. await compleRationModel.update({userID: userID, rationRepId: updateObj.updateData.rationRepId}, updateObj.updateData);
  21. }
  22. }
  23. callback(0, '');
  24. }
  25. catch(err){
  26. callback(err, null);
  27. }
  28. }
  29. async getRationItems(userID, rationRepId, sectionId, callback){
  30. try{
  31. let stdRations = await stdRationModel.find({rationRepId: rationRepId, sectionId: sectionId, $or: [{isDeleted: null}, {isDeleted: false}]});
  32. //mark std
  33. for(let i = 0, len = stdRations.length; i < len; i++){
  34. stdRations[i]._doc.type = 'std';
  35. }
  36. let compleRations = await compleRationModel.find({userId: userID, rationRepId: rationRepId, sectionId: sectionId, deleteInfo: null});
  37. //mark complementary
  38. for(let i = 0, len = compleRations.length; i < len; i++){
  39. compleRations[i]._doc.type = 'complementary';
  40. }
  41. callback(0, stdRations.concat(compleRations));
  42. }
  43. catch(err){
  44. callback(err, null);
  45. }
  46. }
  47. async getRationsCodes(userID, rationRepId, callback){
  48. try{
  49. let stdRationCodes = await stdRationModel.find({rationRepId: rationRepId, $or: [{isDeleted: null}, {isDeleted: false}]}, '-_id code');
  50. let compleRationCodes = await compleRationModel.find({userId: userID, rationRepId: rationRepId, deleteInfo: null}, '-_id code');
  51. let rstCodes = [];
  52. stdRationCodes.concat(compleRationCodes).forEach(function (rationItem) {
  53. rstCodes.push(rationItem.code);
  54. });
  55. callback(0, rstCodes);
  56. }
  57. catch(err){
  58. callback(err, null);
  59. }
  60. }
  61. async getGljItems(gljLibId, callback){
  62. try{
  63. let stdGljs = await stdGljModel.find({repositoryId: gljLibId, $or: [{deleted: null}, {deleted: false}]});
  64. callback(0, stdGljs);
  65. }
  66. catch(err){
  67. callback(err, null);
  68. }
  69. }
  70. async getGljItemsOccupied(gljLibId, occupation, callback){
  71. try{
  72. let stdGls = await stdGljModel.find({repositoryId: gljLibId, $or: [{deleted: null}, {deleted: false}]}, occupation);
  73. callback(0, stdGls);
  74. }
  75. catch (err){
  76. callback(err, null);
  77. }
  78. }
  79. async getGljItemsByIds(userID, ids, callback){
  80. try{
  81. let rst = [];
  82. for(let i = 0, len = ids.length; i < len; i++){
  83. if(ids[i].type === 'std'){
  84. let stdGlj = await stdGljModel.find({ID: ids[i].id, deleteInfo: null});
  85. if(stdGlj.length > 0){
  86. stdGlj[0]._doc.type = 'std';
  87. rst.push(stdGlj[0]);
  88. }
  89. }
  90. else if(ids[i].type === 'complementary'){
  91. let compleGlj = await complementaryGljModel.find({userId: userID, ID: ids[i].id, deleteInfo: null});
  92. if(compleGlj.length > 0){
  93. compleGlj[0]._doc.type = 'complementary';
  94. rst.push(compleGlj[0]);
  95. }
  96. }
  97. }
  98. callback(0, rst);
  99. }
  100. catch(err){
  101. callback(err, null);
  102. }
  103. }
  104. async getGljItemsByCodes(userID, compilationId, rationRepId, codes, callback){
  105. try{
  106. console.log(userID);
  107. console.log(compilationId);
  108. console.log(codes);
  109. let rst = [];
  110. for(let i = 0, len = codes.length; i < len; i++){
  111. let stdGlj = await stdGljModel.find({repositoryId: rationRepId, code: codes[i]});
  112. if(stdGlj.length > 0){
  113. stdGlj[0]._doc.type = 'std';
  114. rst.push(stdGlj[0]);
  115. }
  116. else {
  117. console.log(codes[i]);
  118. let compleGlj = await complementaryGljModel.find({userId: userID, compilationId: compilationId, code: codes[i]});
  119. console.log(compleGlj);
  120. if(compleGlj.length > 0){
  121. compleGlj[0]._doc.type = 'complementary';
  122. rst.push(compleGlj[0]);
  123. }
  124. }
  125. }
  126. callback(0, rst);
  127. }
  128. catch(err){
  129. callback(err, null);
  130. }
  131. }
  132. mixUpdateRationItems (userID, compilationId, rationLibId, sectionId, updateItems, addItems, rIds, callback){
  133. let me = this;
  134. if (updateItems.length == 0 && rIds.length == 0) {
  135. me.addRationItems(userID, compilationId, rationLibId, sectionId, addItems, callback);
  136. } else {
  137. me.removeRationItems(rationLibId, rIds, function(err, message, docs) {
  138. if (err) {
  139. callback(true, false);
  140. } else {
  141. me.updateRationItems(userID, rationLibId, sectionId, updateItems, function(err, results){
  142. if (err) {
  143. callback(true, false);
  144. } else {
  145. if (addItems && addItems.length > 0) {
  146. me.addRationItems(rationLibId, sectionId, addItems, callback);
  147. } else {
  148. callback(0, results);
  149. }
  150. }
  151. });
  152. }
  153. })
  154. }
  155. }
  156. removeRationItems(rationRepId, rIds,callback){
  157. if (rIds.length > 0) {
  158. compleRationModel.remove({rationRepId: rationRepId, ID: {$in: rIds}}, function(err, docs){
  159. if (err) {
  160. callback(true, false);
  161. } else {
  162. callback(0, docs);
  163. }
  164. })
  165. } else {
  166. callback(0, null);
  167. }
  168. }
  169. addRationItems(userID, compilationId, rationLibId, sectionId, items,callback){
  170. if (items && items.length > 0) {
  171. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  172. let maxId = result.value.sequence_value;
  173. let arr = [];
  174. for (let i = 0; i < items.length; i++) {
  175. let obj = new compleRationModel(items[i]);
  176. obj.ID = (maxId - (items.length - 1) + i);
  177. obj.sectionId = sectionId;
  178. obj.rationRepId = rationLibId;
  179. obj.userId = userID;
  180. obj.compilationId = compilationId;
  181. arr.push(obj);
  182. }
  183. compleRationModel.collection.insert(arr, null, function(err, docs){
  184. if (err) {
  185. callback(true, false);
  186. } else {
  187. callback(0, docs);
  188. }
  189. })
  190. });
  191. } else {
  192. callback(true, "Source error!", false);
  193. }
  194. }
  195. updateRationItems(userID, rationLibId, sectionId, items,callback){
  196. let functions = [];
  197. for (let i=0; i < items.length; i++) {
  198. functions.push((function(doc) {
  199. return function(cb) {
  200. var filter = {};
  201. if (doc.ID) {
  202. filter.ID = doc.ID;
  203. } else {
  204. filter.sectionId = sectionId;
  205. filter.userId = userID;
  206. if (rationLibId) filter.rationRepId = rationLibId;
  207. filter.code = doc.code;
  208. }
  209. compleRationModel.update(filter, doc, cb);
  210. };
  211. })(items[i]));
  212. }
  213. async.parallel(functions, function(err, results) {
  214. if(!err){
  215. err = 0;
  216. }
  217. callback(err, results);
  218. });
  219. }
  220. }
  221. export default CompleRatoinDao;