gljModel.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * Created by Zhong on 2017/8/22.
  3. */
  4. import {complementaryGljModel, stdGljModel, gljClassModel} from "./schemas";
  5. import counter from "../../../public/counter/counter";
  6. import async from "async";
  7. class GljDao {
  8. getGljTypes (gljLibId, callback){
  9. gljClassModel.find({"repositoryId": gljLibId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  10. if(data.length) {
  11. callback(false,data);
  12. }
  13. else if(err) callback("获取工料机类型错误!",false);
  14. })
  15. }
  16. getGljItemsByRep(repositoryId,callback){
  17. gljModel.find({"repositoryId": repositoryId},function(err,data){
  18. if(err) callback(true, "")
  19. else callback(false,data);
  20. })
  21. }
  22. getGljItemByType (repositoryId, type, callback){
  23. gljModel.find({"repositoryId": repositoryId, "gljType": type},function(err,data){
  24. if(err) callback(true, "")
  25. else callback(false, data);
  26. })
  27. };
  28. getGljItem (repositoryId, code, callback){
  29. gljModel.find({"repositoryId": repositoryId, "code": code},function(err,data){
  30. if(err) callback(true, "")
  31. else callback(false, data);
  32. })
  33. };
  34. //获得用户的补充工料机和用户当前所在编办的标准工料机
  35. getGljItems (stdGljLibId, userId, compilationId, callback){
  36. let rst = {stdGljs: [], complementaryGljs: []};
  37. async.parallel([
  38. function (cb) {
  39. stdGljModel.find({repositoryId: stdGljLibId}, function (err, stdGljs) {
  40. if(err){
  41. cb(err);
  42. }
  43. else{
  44. rst.stdGljs = stdGljs;
  45. cb(null);
  46. }
  47. });
  48. },
  49. function (cb) {
  50. complementaryGljModel.find({userId: userId, compilationId: compilationId}, function (err, complementaryGljs) {
  51. if(err){
  52. cb(err);
  53. }
  54. else{
  55. rst.complementaryGljs = complementaryGljs;
  56. cb(null);
  57. }
  58. });
  59. }
  60. ], function (err) {
  61. if(err){
  62. callback(true, null);
  63. }
  64. else{
  65. callback(false, rst);
  66. }
  67. })
  68. };
  69. getGljItemsByCode (repositoryId, codes, callback){
  70. gljModel.find({"repositoryId": repositoryId,"code": {"$in": codes}},function(err,data){
  71. if(err) callback(true, "")
  72. else callback(false, data);
  73. })
  74. };
  75. //-oprtor
  76. updateComponent(libId, updateArr, callback){
  77. let parallelFucs = [];
  78. for(let i = 0; i < updateArr.length; i++){
  79. parallelFucs.push((function(obj){
  80. return function (cb) {
  81. if(typeof obj.component === 'undefined'){
  82. obj.component = [];
  83. }
  84. gljModel.update({repositoryId: libId, ID: obj.ID}, obj, function (err, result) {
  85. if(err){
  86. cb(err);
  87. }
  88. else{
  89. cb(null, obj);
  90. }
  91. })
  92. }
  93. })(updateArr[i]));
  94. }
  95. async.parallel(parallelFucs, function (err, result) {
  96. if(err){
  97. callback(err, '更新组成物错误!', null);
  98. }
  99. else{
  100. callback(null, '成功!', result);
  101. }
  102. });
  103. }
  104. //-oprtor
  105. mixUpdateGljItems (userId, compilationId, updateItems, addItems, rIds, callback) {
  106. if (updateItems.length == 0 && rIds.length == 0) {
  107. GljDao.addGljItems(userId, compilationId, addItems, callback);
  108. }
  109. else if(rIds.length > 0 && updateItems.length > 0){
  110. async.parallel([
  111. function (cb) {
  112. GljDao.removeGljItems(rIds, cb);
  113. },
  114. function (cb) {
  115. GljDao.updateGljItems(userId, compilationId, updateItems, cb);
  116. }
  117. ], function (err) {
  118. if(err){
  119. callback(true, "Fail to update and delete", false)
  120. }
  121. else{
  122. callback(false, "Save successfully", false);
  123. }
  124. })
  125. }
  126. else if (rIds.length > 0 && updateItems.length === 0) {
  127. GljDao.removeGljItems(rIds, callback);
  128. }
  129. else if(updateItems.length > 0 || addItems.length > 0){
  130. GljDao.updateGljItems(userId, compilationId, updateItems, function(err, results){
  131. if (err) {
  132. callback(true, "Fail to update", false);
  133. } else {
  134. if (addItems && addItems.length > 0) {
  135. GljDao.addGljItems(userId, compilationId, addItems, callback);
  136. } else {
  137. callback(false, "Save successfully", results);
  138. }
  139. }
  140. });
  141. }
  142. }
  143. /*mixUpdateGljItems (repId, lastOpr, updateItems, addItems, rIds, callback) {
  144. if (updateItems.length == 0 && rIds.length == 0) {
  145. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  146. } else if (rIds.length > 0) {
  147. GljDao.removeGljItems(repId, lastOpr, rIds, function(err, message, docs) {
  148. });
  149. }else{
  150. GljDao.updateGljItems(repId, lastOpr, updateItems, function(err, results){
  151. if (err) {
  152. callback(true, "Fail to update", false);
  153. } else {
  154. if (addItems && addItems.length > 0) {
  155. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  156. } else {
  157. callback(false, "Save successfully", results);
  158. }
  159. }
  160. });
  161. }
  162. };*/
  163. static removeGljItems (rIds, callback) {
  164. if (rIds && rIds.length > 0) {
  165. complementaryGljModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  166. if (err) {
  167. callback(true, "Fail to remove", false);
  168. } else {
  169. callback(false, "Remove successfully", docs);
  170. }
  171. })
  172. } else {
  173. callback(false, "No records were deleted!", null);
  174. }
  175. }
  176. static addGljItems (userId, compilationId, items, callback) {
  177. if (items && items.length > 0) {
  178. counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, function(err, result){
  179. var maxId = result.value.sequence_value;
  180. var arr = [];
  181. for (var i = 0; i < items.length; i++) {
  182. var obj = new complementaryGljModel(items[i]);
  183. obj.ID = (maxId - (items.length - 1) + i);
  184. obj.userId = userId;
  185. obj.compilationId = compilationId;
  186. arr.push(obj);
  187. }
  188. complementaryGljModel.collection.insert(arr, null, function(err, docs){
  189. if (err) {
  190. callback(true, "Fail to add", false);
  191. } else {
  192. callback(false, "Add successfully", docs);
  193. }
  194. });
  195. });
  196. } else {
  197. callback(true, "No source", false);
  198. }
  199. }
  200. static updateGljItems(userId, compilationId, items, callback) {
  201. var functions = [];
  202. for (var i=0; i < items.length; i++) {
  203. functions.push((function(doc) {
  204. return function(cb) {
  205. var filter = {};
  206. if (doc.ID) {
  207. filter.ID = doc.ID;
  208. } else {
  209. filter.userId = userId;
  210. filter.compilationId = compilationId;
  211. filter.code = doc.code;
  212. }
  213. complementaryGljModel.update(filter, doc, cb);
  214. };
  215. })(items[i]));
  216. }
  217. async.parallel(functions, function(err, results) {
  218. callback(err, results);
  219. });
  220. }
  221. }
  222. export default GljDao;