gljModel.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. updateComponent(userId, updateArr, callback){
  76. let parallelFucs = [];
  77. for(let i = 0; i < updateArr.length; i++){
  78. parallelFucs.push((function(obj){
  79. return function (cb) {
  80. if(typeof obj.component === 'undefined'){
  81. obj.component = [];
  82. }
  83. complementaryGljModel.update({userId: userId, ID: obj.ID}, obj, function (err, result) {
  84. if(err){
  85. cb(err);
  86. }
  87. else{
  88. cb(null, obj);
  89. }
  90. })
  91. }
  92. })(updateArr[i]));
  93. }
  94. async.parallel(parallelFucs, function (err, result) {
  95. if(err){
  96. callback(err, '更新组成物错误!', null);
  97. }
  98. else{
  99. callback(null, '成功!', result);
  100. }
  101. });
  102. }
  103. //-oprtor
  104. mixUpdateGljItems (userId, compilationId, updateItems, addItems, rIds, callback) {
  105. if (updateItems.length == 0 && rIds.length == 0) {
  106. GljDao.addGljItems(userId, compilationId, addItems, callback);
  107. }
  108. else if(rIds.length > 0 && updateItems.length > 0){
  109. async.parallel([
  110. function (cb) {
  111. GljDao.removeGljItems(rIds, cb);
  112. },
  113. function (cb) {
  114. GljDao.updateGljItems(userId, compilationId, updateItems, cb);
  115. }
  116. ], function (err) {
  117. if(err){
  118. callback(true, "Fail to update and delete", false)
  119. }
  120. else{
  121. callback(false, "Save successfully", false);
  122. }
  123. })
  124. }
  125. else if (rIds.length > 0 && updateItems.length === 0) {
  126. GljDao.removeGljItems(rIds, callback);
  127. }
  128. else if(updateItems.length > 0 || addItems.length > 0){
  129. GljDao.updateGljItems(userId, compilationId, updateItems, function(err, results){
  130. if (err) {
  131. callback(true, "Fail to update", false);
  132. } else {
  133. if (addItems && addItems.length > 0) {
  134. GljDao.addGljItems(userId, compilationId, addItems, callback);
  135. } else {
  136. callback(false, "Save successfully", results);
  137. }
  138. }
  139. });
  140. }
  141. }
  142. /*mixUpdateGljItems (repId, lastOpr, updateItems, addItems, rIds, callback) {
  143. if (updateItems.length == 0 && rIds.length == 0) {
  144. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  145. } else if (rIds.length > 0) {
  146. GljDao.removeGljItems(repId, lastOpr, rIds, function(err, message, docs) {
  147. });
  148. }else{
  149. GljDao.updateGljItems(repId, lastOpr, updateItems, function(err, results){
  150. if (err) {
  151. callback(true, "Fail to update", false);
  152. } else {
  153. if (addItems && addItems.length > 0) {
  154. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  155. } else {
  156. callback(false, "Save successfully", results);
  157. }
  158. }
  159. });
  160. }
  161. };*/
  162. static removeGljItems (rIds, callback) {
  163. if (rIds && rIds.length > 0) {
  164. complementaryGljModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  165. if (err) {
  166. callback(true, "Fail to remove", false);
  167. } else {
  168. callback(false, "Remove successfully", docs);
  169. }
  170. })
  171. } else {
  172. callback(false, "No records were deleted!", null);
  173. }
  174. }
  175. static addGljItems (userId, compilationId, items, callback) {
  176. if (items && items.length > 0) {
  177. counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, function(err, result){
  178. var maxId = result.value.sequence_value;
  179. var arr = [];
  180. for (var i = 0; i < items.length; i++) {
  181. var obj = new complementaryGljModel(items[i]);
  182. obj.ID = (maxId - (items.length - 1) + i);
  183. obj.userId = userId;
  184. obj.compilationId = compilationId;
  185. arr.push(obj);
  186. }
  187. complementaryGljModel.collection.insert(arr, null, function(err, docs){
  188. if (err) {
  189. callback(true, "Fail to add", false);
  190. } else {
  191. callback(false, "Add successfully", docs);
  192. }
  193. });
  194. });
  195. } else {
  196. callback(true, "No source", false);
  197. }
  198. }
  199. static updateGljItems(userId, compilationId, items, callback) {
  200. var functions = [];
  201. for (var i=0; i < items.length; i++) {
  202. functions.push((function(doc) {
  203. return function(cb) {
  204. var filter = {};
  205. if (doc.ID) {
  206. filter.ID = doc.ID;
  207. } else {
  208. filter.userId = userId;
  209. filter.compilationId = compilationId;
  210. filter.code = doc.code;
  211. }
  212. complementaryGljModel.update(filter, doc, cb);
  213. };
  214. })(items[i]));
  215. }
  216. async.parallel(functions, function(err, results) {
  217. callback(err, results);
  218. });
  219. }
  220. }
  221. export default GljDao;