gljModel.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /**
  2. * Created by Zhong on 2017/8/11.
  3. */
  4. import {gljMapModel, gljModel, gljClassModel, gljClassTemplateModel} from "./schemas";
  5. import {OprDao} from "./gljMapModel";
  6. import moment from "moment";
  7. import counter from "../../../public/counter/counter";
  8. import async from "async";
  9. class GljDao extends OprDao{
  10. getGljTypes (gljLibId, callback){
  11. gljClassModel.find({"repositoryId": gljLibId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  12. if(data.length) {
  13. callback(false,data);
  14. }
  15. else if(err) callback("获取工料机类型错误!",false)
  16. else {
  17. gljClassTemplateModel.find({'$or': [{isDeleted: null}, {isDeleted: false}]}, function (err, datas) {
  18. if(err){
  19. callback("获取工料机类型错误!", false);
  20. }
  21. else{
  22. let rst = [];
  23. async.each(datas, function (data, cb) {
  24. let newClassObj = {};
  25. newClassObj.ID = data.ID;
  26. newClassObj.ParentID = data.ParentID;
  27. newClassObj.NextSiblingID = data.NextSiblingID;
  28. newClassObj.Name = data.Name;
  29. newClassObj.repositoryId = gljLibId;
  30. gljClassModel.create(newClassObj, function(err){
  31. if(err)cb(err);
  32. else{
  33. rst.push(newClassObj);
  34. cb(null);
  35. }
  36. });
  37. }, function (err) {
  38. if(err) callback("新增工料机类型错误!", false);
  39. else callback(false, rst);
  40. });
  41. }
  42. });
  43. }
  44. })
  45. }
  46. getGljItemsByRep(repositoryId,callback){
  47. gljModel.find({"repositoryId": repositoryId},function(err,data){
  48. if(err) callback(true, "")
  49. else callback(false,data);
  50. })
  51. }
  52. getGljItemByType (repositoryId, type, callback){
  53. gljModel.find({"repositoryId": repositoryId, "gljType": type},function(err,data){
  54. if(err) callback(true, "")
  55. else callback(false, data);
  56. })
  57. };
  58. getGljItem (repositoryId, code, callback){
  59. gljModel.find({"repositoryId": repositoryId, "code": code},function(err,data){
  60. if(err) callback(true, "")
  61. else callback(false, data);
  62. })
  63. };
  64. getGljItems (gljIds, callback){
  65. gljModel.find({"ID": {"$in": gljIds}},function(err,data){
  66. if(err) callback(true, "")
  67. else callback(false, data);
  68. })
  69. };
  70. getGljItemsByCode (repositoryId, codes, callback){
  71. gljModel.find({"repositoryId": repositoryId,"code": {"$in": codes}},function(err,data){
  72. if(err) callback(true, "")
  73. else callback(false, data);
  74. })
  75. };
  76. updateComponent(libId, oprtor, 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. parallelFucs.push((function () {
  96. return function (cb) {
  97. GljDao.updateOprArr({ID: libId}, oprtor, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  98. if(err){
  99. cb(err);
  100. }
  101. else{
  102. cb(null);
  103. }
  104. })
  105. }
  106. })());
  107. async.parallel(parallelFucs, function (err, result) {
  108. if(err){
  109. callback(err, '更新组成物错误!', null);
  110. }
  111. else{
  112. callback(null, '成功!', result);
  113. }
  114. });
  115. }
  116. mixUpdateGljItems (repId, lastOpr, updateItems, addItems, rIds, callback) {
  117. if (updateItems.length == 0 && rIds.length == 0) {
  118. console.log(1);
  119. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  120. }
  121. else if(rIds.length > 0 && updateItems.length > 0){
  122. console.log(2);
  123. async.parallel([
  124. function (cb) {
  125. GljDao.removeGljItems(repId, lastOpr, rIds, cb);
  126. },
  127. function (cb) {
  128. GljDao.updateGljItems(repId, lastOpr, updateItems, cb);
  129. }
  130. ], function (err) {
  131. if(err){
  132. callback(true, "Fail to update and delete", false)
  133. }
  134. else{
  135. callback(false, "Save successfully", false);
  136. }
  137. })
  138. }
  139. else if (rIds.length > 0 && updateItems.length === 0) {
  140. console.log(3);
  141. GljDao.removeGljItems(repId, lastOpr, rIds, callback);
  142. }
  143. else if(updateItems.length > 0 || addItems.length > 0){
  144. console.log(4);
  145. GljDao.updateGljItems(repId, lastOpr, updateItems, function(err, results){
  146. if (err) {
  147. callback(true, "Fail to update", false);
  148. } else {
  149. if (addItems && addItems.length > 0) {
  150. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  151. } else {
  152. callback(false, "Save successfully", results);
  153. }
  154. }
  155. });
  156. }
  157. }
  158. /*mixUpdateGljItems (repId, lastOpr, updateItems, addItems, rIds, callback) {
  159. if (updateItems.length == 0 && rIds.length == 0) {
  160. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  161. } else if (rIds.length > 0) {
  162. GljDao.removeGljItems(repId, lastOpr, rIds, function(err, message, docs) {
  163. });
  164. }else{
  165. GljDao.updateGljItems(repId, lastOpr, updateItems, function(err, results){
  166. if (err) {
  167. callback(true, "Fail to update", false);
  168. } else {
  169. if (addItems && addItems.length > 0) {
  170. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  171. } else {
  172. callback(false, "Save successfully", results);
  173. }
  174. }
  175. });
  176. }
  177. };*/
  178. static removeGljItems (repId, lastOpr, rIds, callback) {
  179. if (rIds && rIds.length > 0) {
  180. gljModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  181. if (err) {
  182. callback(true, "Fail to remove", false);
  183. } else {
  184. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  185. if(err){
  186. callback(true, "Fail to update operator", false);
  187. }
  188. else{
  189. callback(false, "Remove successfully", docs);
  190. }
  191. });
  192. }
  193. })
  194. } else {
  195. callback(false, "No records were deleted!", null);
  196. }
  197. }
  198. static addGljItems (repId, lastOpr, items, callback) {
  199. if (items && items.length > 0) {
  200. counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, function(err, result){
  201. var maxId = result.value.sequence_value;
  202. var arr = [];
  203. for (var i = 0; i < items.length; i++) {
  204. var obj = new gljModel(items[i]);
  205. obj.ID = (maxId - (items.length - 1) + i);
  206. obj.repositoryId = repId;
  207. arr.push(obj);
  208. }
  209. gljModel.collection.insert(arr, null, function(err, docs){
  210. if (err) {
  211. callback(true, "Fail to add", false);
  212. } else {
  213. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  214. if(err){
  215. callback(true, "Fail to update Operator", false);
  216. }
  217. else{
  218. callback(false, "Add successfully", docs);
  219. }
  220. });
  221. }
  222. })
  223. });
  224. } else {
  225. callback(true, "No source", false);
  226. }
  227. }
  228. static updateGljItems(repId, lastOpr, items, callback) {
  229. var functions = [];
  230. for (var i=0; i < items.length; i++) {
  231. functions.push((function(doc) {
  232. return function(cb) {
  233. var filter = {};
  234. if (doc.ID) {
  235. filter.ID = doc.ID;
  236. } else {
  237. filter.repositoryId = repId;
  238. filter.code = doc.code;
  239. }
  240. gljModel.update(filter, doc, cb);
  241. };
  242. })(items[i]));
  243. }
  244. functions.push((function () {
  245. return function (cb) {
  246. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  247. if(err){
  248. cb(err);
  249. }
  250. else{
  251. cb(null);
  252. }
  253. })
  254. }
  255. })());
  256. async.parallel(functions, function(err, results) {
  257. callback(err, results);
  258. });
  259. }
  260. getRationGljIds(rationLibs, callback){
  261. }
  262. updateNodes (repId, lastOpr, nodes, callback) {
  263. var functions = [];
  264. for (var i=0; i < nodes.length; i++) {
  265. functions.push((function(doc) {
  266. return function(cb) {
  267. gljClassModel.update({ID: doc.ID}, doc, cb);
  268. };
  269. })(nodes[i]));
  270. }
  271. functions.push((function () {
  272. return function (cb) {
  273. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  274. if(err){
  275. cb(err);
  276. }
  277. else{
  278. cb(null);
  279. }
  280. })
  281. }
  282. })());
  283. async.parallel(functions, function(err, results) {
  284. callback(err, results);
  285. });
  286. }
  287. removeNodes (repId, lastOpr, nodeIds, preNodeId, preNodeNextId, callback){
  288. var functions = [];
  289. if (preNodeId != -1) {
  290. functions.push((function(nodeId, nextId) {
  291. return function(cb) {
  292. gljClassModel.update({ID: nodeId}, {"NextSiblingID": nextId}, cb);
  293. };
  294. })(preNodeId, preNodeNextId));
  295. }
  296. for (var i=0; i < nodeIds.length; i++) {
  297. functions.push((function(nodeId) {
  298. return function(cb) {
  299. gljClassModel.update({ID: nodeId}, {"isDeleted": true}, cb);
  300. };
  301. })(nodeIds[i]));
  302. }
  303. functions.push((function () {
  304. return function (cb) {
  305. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  306. if(err){
  307. cb(err);
  308. }
  309. else{
  310. cb(null);
  311. }
  312. })
  313. }
  314. })());
  315. async.parallel(functions, function(err, results) {
  316. callback(err, results);
  317. });
  318. }
  319. createNewNode(repId, lastOpr, lastNodeId, nodeData, callback) {
  320. return counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, 1, function(err, result){
  321. nodeData.repositoryId = repId;
  322. nodeData.ID = result.value.sequence_value;
  323. var node = new gljModel(nodeData);
  324. async.parallel([
  325. function (cb) {
  326. node.save(function (err, result) {
  327. if (err) {
  328. cb("章节树ID错误!", false);
  329. } else {
  330. if (lastNodeId > 0) {
  331. gljClassModel.update({ID: lastNodeId}, {"NextSiblingID": nodeData.ID}, function(err, rst){
  332. if (err) {
  333. cb("章节树ID错误!", false);
  334. } else {
  335. cb(false, result);
  336. }
  337. });
  338. } else cb(false, result);
  339. }
  340. });
  341. },
  342. function (cb) {
  343. GljDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  344. if(err){
  345. cb(err);
  346. }
  347. else{
  348. cb(null);
  349. }
  350. })
  351. }
  352. ], function (err, result) {
  353. if(err){
  354. callback(true, "章节树错误!", false);
  355. }
  356. else{
  357. callback(false, '', result[0]);
  358. }
  359. })
  360. });
  361. }
  362. }
  363. export default GljDao;