gljModel.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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(0,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. _exist(data, attr){
  36. return data && data[attr] !== 'undefined' && data[attr];
  37. }
  38. sortToNumber(datas){
  39. for(let i = 0, len = datas.length; i < len; i++){
  40. let data = datas[i]._doc;
  41. if(this._exist(data, 'basePrice')){
  42. data['basePrice'] = parseFloat(data['basePrice']);
  43. }
  44. if(this._exist(data, 'component')){
  45. for(let j = 0, jLen = data['component'].length; j < jLen; j++){
  46. let comGljObj = data['component'][j]._doc;
  47. if(this._exist(comGljObj, 'consumeAmt')){
  48. comGljObj['consumeAmt'] = parseFloat(comGljObj['consumeAmt']);
  49. }
  50. }
  51. }
  52. }
  53. }
  54. //获得用户的补充工料机和用户当前所在编办的标准工料机
  55. getGljItems (stdGljLibId, userId, compilationId, callback){
  56. let me = this;
  57. let rst = {stdGljs: [], complementaryGljs: []};
  58. async.parallel([
  59. function (cb) {
  60. stdGljModel.find({repositoryId: stdGljLibId}, function (err, stdGljs) {
  61. if(err){
  62. cb(err);
  63. }
  64. else{
  65. me.sortToNumber(stdGljs);
  66. rst.stdGljs = stdGljs;
  67. cb(null);
  68. }
  69. });
  70. },
  71. function (cb) {
  72. complementaryGljModel.find({userId: userId, compilationId: compilationId}, function (err, complementaryGljs) {
  73. if(err){
  74. cb(err);
  75. }
  76. else{
  77. me.sortToNumber(complementaryGljs);
  78. rst.complementaryGljs = complementaryGljs;
  79. cb(null);
  80. }
  81. });
  82. }
  83. ], function (err) {
  84. if(err){
  85. callback(err, null);
  86. }
  87. else{
  88. callback(0, rst);
  89. }
  90. })
  91. };
  92. getGljItemsByCode (repositoryId, codes, callback){
  93. gljModel.find({"repositoryId": repositoryId,"code": {"$in": codes}},function(err,data){
  94. if(err) callback(true, "")
  95. else callback(false, data);
  96. })
  97. };
  98. updateComponent(userId, updateArr, callback){
  99. let parallelFucs = [];
  100. for(let i = 0; i < updateArr.length; i++){
  101. parallelFucs.push((function(obj){
  102. return function (cb) {
  103. if(typeof obj.component === 'undefined'){
  104. obj.component = [];
  105. }
  106. complementaryGljModel.update({userId: userId, ID: obj.ID}, obj, function (err, result) {
  107. if(err){
  108. cb(err);
  109. }
  110. else{
  111. cb(null, obj);
  112. }
  113. })
  114. }
  115. })(updateArr[i]));
  116. }
  117. async.parallel(parallelFucs, function (err, result) {
  118. if(err){
  119. callback(err, '更新组成物错误!', null);
  120. }
  121. else{
  122. callback(0, '成功!', result);
  123. }
  124. });
  125. }
  126. //-oprtor
  127. mixUpdateGljItems (userId, compilationId, updateItems, addItems, rIds, callback) {
  128. if (updateItems.length == 0 && rIds.length == 0) {
  129. GljDao.addGljItems(userId, compilationId, addItems, callback);
  130. }
  131. else if(rIds.length > 0 && updateItems.length > 0){
  132. async.parallel([
  133. function (cb) {
  134. GljDao.removeGljItems(rIds, cb);
  135. },
  136. function (cb) {
  137. GljDao.updateGljItems(userId, compilationId, updateItems, cb);
  138. }
  139. ], function (err) {
  140. if(err){
  141. callback(true, "Fail to update and delete", false)
  142. }
  143. else{
  144. callback(false, "Save successfully", false);
  145. }
  146. })
  147. }
  148. else if (rIds.length > 0 && updateItems.length === 0) {
  149. GljDao.removeGljItems(rIds, callback);
  150. }
  151. else if(updateItems.length > 0 || addItems.length > 0){
  152. GljDao.updateGljItems(userId, compilationId, updateItems, function(err, results){
  153. if (err) {
  154. callback(true, "Fail to update", false);
  155. } else {
  156. if (addItems && addItems.length > 0) {
  157. GljDao.addGljItems(userId, compilationId, addItems, callback);
  158. } else {
  159. callback(false, "Save successfully", results);
  160. }
  161. }
  162. });
  163. }
  164. }
  165. /*mixUpdateGljItems (repId, lastOpr, updateItems, addItems, rIds, callback) {
  166. if (updateItems.length == 0 && rIds.length == 0) {
  167. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  168. } else if (rIds.length > 0) {
  169. GljDao.removeGljItems(repId, lastOpr, rIds, function(err, message, docs) {
  170. });
  171. }else{
  172. GljDao.updateGljItems(repId, lastOpr, updateItems, function(err, results){
  173. if (err) {
  174. callback(true, "Fail to update", false);
  175. } else {
  176. if (addItems && addItems.length > 0) {
  177. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  178. } else {
  179. callback(false, "Save successfully", results);
  180. }
  181. }
  182. });
  183. }
  184. };*/
  185. static removeGljItems (rIds, callback) {
  186. if (rIds && rIds.length > 0) {
  187. complementaryGljModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  188. if (err) {
  189. callback(true, "Fail to remove", false);
  190. } else {
  191. callback(false, "Remove successfully", docs);
  192. }
  193. })
  194. } else {
  195. callback(false, "No records were deleted!", null);
  196. }
  197. }
  198. static addGljItems (userId, compilationId, 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 complementaryGljModel(items[i]);
  205. obj.ID = (maxId - (items.length - 1) + i);
  206. obj.userId = userId;
  207. obj.compilationId = compilationId;
  208. arr.push(obj);
  209. }
  210. complementaryGljModel.collection.insert(arr, null, function(err, docs){
  211. if (err) {
  212. callback(true, "Fail to add", false);
  213. } else {
  214. callback(false, "Add successfully", docs);
  215. }
  216. });
  217. });
  218. } else {
  219. callback(true, "No source", false);
  220. }
  221. }
  222. static updateGljItems(userId, compilationId, items, callback) {
  223. var functions = [];
  224. for (var i=0; i < items.length; i++) {
  225. functions.push((function(doc) {
  226. return function(cb) {
  227. var filter = {};
  228. if (doc.ID) {
  229. filter.ID = doc.ID;
  230. } else {
  231. filter.userId = userId;
  232. filter.compilationId = compilationId;
  233. filter.code = doc.code;
  234. }
  235. complementaryGljModel.update(filter, doc, cb);
  236. };
  237. })(items[i]));
  238. }
  239. async.parallel(functions, function(err, results) {
  240. callback(err, results);
  241. });
  242. }
  243. /**
  244. * 获取组成物数据
  245. *
  246. * @param {Number} gljId
  247. * @return {Promise}
  248. */
  249. async getComponent(gljId) {
  250. let result = [];
  251. let libGljData = await complementaryGljModel.find({ID: gljId});
  252. if (libGljData === null || libGljData.component.length <= 0) {
  253. return result;
  254. }
  255. // 标准工料机库
  256. let componentIdListStd = [];
  257. // 补充工料机库
  258. let componentIdListCpt = [];
  259. let componentConsume = {};
  260. // 整理数据
  261. for (let component of libGljData.component) {
  262. if (component.isStd) {
  263. componentIdListStd.push(component.ID);
  264. } else {
  265. componentIdListCpt.push(component.ID);
  266. }
  267. let isStdFlag = component.isStd ? 'std' : 'cpt';
  268. componentConsume[component.ID + '_' + isStdFlag] = component.consumeAmt;
  269. }
  270. // 查找标准库数据
  271. let condition = {};
  272. let componentStdGljData = [];
  273. if (componentIdListStd.length > 0) {
  274. let gljListModel = new STDGLJLibGLJListModel();
  275. condition = {ID: {$in: componentIdListStd}};
  276. componentStdGljData = await gljListModel.findDataByCondition(condition, null, false);
  277. }
  278. // 查找补充库数据
  279. let componentCptGljData = [];
  280. if (componentIdListCpt.length > 0) {
  281. condition = {ID: {$in: componentIdListCpt}};
  282. componentCptGljData = await complementaryGljModel.find(condition);
  283. }
  284. if (componentCptGljData === null && componentStdGljData === null) {
  285. return result;
  286. }
  287. // 整理数据
  288. if (componentStdGljData.length > 0) {
  289. componentStdGljData = JSON.stringify(componentStdGljData);
  290. componentStdGljData = JSON.parse(componentStdGljData);
  291. for(let gljData of componentStdGljData) {
  292. gljData.connectCode = libGljData.code;
  293. gljData.consumption = componentConsume[gljData.ID + '_std'];
  294. result.push(gljData);
  295. }
  296. }
  297. if (componentCptGljData.length > 0) {
  298. componentCptGljData = JSON.stringify(componentCptGljData);
  299. componentCptGljData = JSON.parse(componentCptGljData);
  300. for(let gljData of componentCptGljData) {
  301. gljData.connectCode = libGljData.code;
  302. gljData.consumption = componentConsume[gljData.ID + '_cpt'];
  303. result.push(gljData);
  304. }
  305. }
  306. return result;
  307. }
  308. }
  309. export default GljDao;