gljModel.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. import STDGLJLibGLJListModel from "../../common/std/std_glj_lib_glj_list_model";
  8. class GljDao {
  9. getGljTypes (gljLibId, callback){
  10. gljClassModel.find({"repositoryId": gljLibId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  11. if(data.length) {
  12. callback(false,data);
  13. }
  14. else if(err) callback("获取工料机类型错误!",false);
  15. })
  16. }
  17. getGljItemsByRep(repositoryId,callback){
  18. gljModel.find({"repositoryId": repositoryId},function(err,data){
  19. if(err) callback(true, "")
  20. else callback(false,data);
  21. })
  22. }
  23. getGljItemByType (repositoryId, type, callback){
  24. gljModel.find({"repositoryId": repositoryId, "gljType": type},function(err,data){
  25. if(err) callback(true, "")
  26. else callback(false, data);
  27. })
  28. };
  29. getGljItem (repositoryId, code, callback){
  30. gljModel.find({"repositoryId": repositoryId, "code": code},function(err,data){
  31. if(err) callback(true, "")
  32. else callback(false, data);
  33. })
  34. };
  35. //获得用户的补充工料机和用户当前所在编办的标准工料机
  36. getGljItems (stdGljLibId, userId, compilationId, callback){
  37. let rst = {stdGljs: [], complementaryGljs: []};
  38. async.parallel([
  39. function (cb) {
  40. stdGljModel.find({repositoryId: stdGljLibId}, function (err, stdGljs) {
  41. if(err){
  42. cb(err);
  43. }
  44. else{
  45. rst.stdGljs = stdGljs;
  46. cb(null);
  47. }
  48. });
  49. },
  50. function (cb) {
  51. complementaryGljModel.find({userId: userId, compilationId: compilationId}, function (err, complementaryGljs) {
  52. if(err){
  53. cb(err);
  54. }
  55. else{
  56. rst.complementaryGljs = complementaryGljs;
  57. cb(null);
  58. }
  59. });
  60. }
  61. ], function (err) {
  62. if(err){
  63. callback(true, null);
  64. }
  65. else{
  66. callback(false, rst);
  67. }
  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(userId, 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. complementaryGljModel.update({userId: userId, 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. * 获取组成物数据
  223. *
  224. * @param {Number} gljId
  225. * @return {Promise}
  226. */
  227. async getComponent(gljId) {
  228. let result = [];
  229. let libGljData = await complementaryGljModel.find({ID: gljId});
  230. if (libGljData === null || libGljData.component.length <= 0) {
  231. return result;
  232. }
  233. // 标准工料机库
  234. let componentIdListStd = [];
  235. // 补充工料机库
  236. let componentIdListCpt = [];
  237. let componentConsume = {};
  238. // 整理数据
  239. for (let component of libGljData.component) {
  240. if (component.isStd) {
  241. componentIdListStd.push(component.ID);
  242. } else {
  243. componentIdListCpt.push(component.ID);
  244. }
  245. let isStdFlag = component.isStd ? 'std' : 'cpt';
  246. componentConsume[component.ID + '_' + isStdFlag] = component.consumeAmt;
  247. }
  248. // 查找标准库数据
  249. let condition = {};
  250. let componentStdGljData = [];
  251. if (componentIdListStd.length > 0) {
  252. let gljListModel = new STDGLJLibGLJListModel();
  253. condition = {ID: {$in: componentIdListStd}};
  254. componentStdGljData = await gljListModel.findDataByCondition(condition, null, false);
  255. }
  256. // 查找补充库数据
  257. let componentCptGljData = [];
  258. if (componentIdListCpt.length > 0) {
  259. condition = {ID: {$in: componentIdListCpt}};
  260. componentCptGljData = await complementaryGljModel.find(condition);
  261. }
  262. if (componentCptGljData === null && componentStdGljData === null) {
  263. return result;
  264. }
  265. // 整理数据
  266. if (componentStdGljData.length > 0) {
  267. componentStdGljData = JSON.stringify(componentStdGljData);
  268. componentStdGljData = JSON.parse(componentStdGljData);
  269. for(let gljData of componentStdGljData) {
  270. gljData.connectCode = libGljData.code;
  271. gljData.consumption = componentConsume[gljData.ID + '_std'];
  272. result.push(gljData);
  273. }
  274. }
  275. if (componentCptGljData.length > 0) {
  276. componentCptGljData = JSON.stringify(componentCptGljData);
  277. componentCptGljData = JSON.parse(componentCptGljData);
  278. for(let gljData of componentCptGljData) {
  279. gljData.connectCode = libGljData.code;
  280. gljData.consumption = componentConsume[gljData.ID + '_cpt'];
  281. result.push(gljData);
  282. }
  283. }
  284. return result;
  285. }
  286. }
  287. export default GljDao;