gljModel.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. async getGljItems (stdGljLibId, userId, compilationId, callback){
  56. let me = this;
  57. let rst = {stdGljs: [], complementaryGljs: []};
  58. //批量获取异步
  59. /* let functions = [];
  60. let count = await stdGljModel.find({repositoryId: stdGljLibId, $or: [{deleted: null}, {deleted: false}]}).count();
  61. let findCount = Math.ceil(count/500);
  62. for(let i = 0, len = findCount; i < len; i++){
  63. functions.push((function(flag) {
  64. return function (cb) {
  65. stdGljModel.find({repositoryId: stdGljLibId, deleted: null}, cb).skip(flag).sort({ID: 1}).limit(500);
  66. }
  67. })(i*500));
  68. }
  69. async.parallel(functions, function (err, results) {
  70. if(err){
  71. callback(err, null);
  72. }
  73. else{
  74. for(let stdGljs of results){
  75. rst.stdGljs = rst.stdGljs.concat(stdGljs);
  76. }
  77. callback(0, rst);
  78. }
  79. });*/
  80. async.parallel([
  81. async function (cb) {
  82. /* stdGljModel.find({repositoryId: stdGljLibId}, function (err, stdGljs) {
  83. if(err){
  84. cb(err);
  85. }
  86. else{
  87. me.sortToNumber(stdGljs);
  88. rst.stdGljs = stdGljs;
  89. cb(null);
  90. }
  91. });*/
  92. try{
  93. let stdGljs = [];
  94. let first = await stdGljModel.find({repositoryId: stdGljLibId}).sort({ID: 1}).limit(1);
  95. let count = await stdGljModel.find({repositoryId: stdGljLibId, $or: [{deleted: null}, {deleted: false}]}).count();
  96. let findCount = Math.ceil(count/500);
  97. let flag = first[0].ID;
  98. //let flag = 0;
  99. //批量获取,非skip
  100. for(let i = 0, len = findCount; i < len; i++){
  101. let tempStdGlj;
  102. if(i === 0){
  103. tempStdGlj = await stdGljModel.find({repositoryId: stdGljLibId, deleted: null, ID: {$gte: flag}}).sort({ID: 1}).limit(500);
  104. if(tempStdGlj.length > 0){
  105. flag = tempStdGlj[tempStdGlj.length - 1].ID;
  106. }
  107. }
  108. else {
  109. tempStdGlj = await stdGljModel.find({repositoryId: stdGljLibId, deleted: null, ID: {$gt: flag}}).sort({ID: 1}).limit(500);
  110. if(tempStdGlj.length > 0){
  111. flag = tempStdGlj[tempStdGlj.length - 1].ID;
  112. }
  113. }
  114. if(tempStdGlj){
  115. stdGljs = stdGljs.concat(tempStdGlj);
  116. }
  117. }
  118. /* for(let i = 0, len = findCount; i < len; i++){
  119. let tempStdGlj = await stdGljModel.find({repositoryId: stdGljLibId, deleted: null}).skip(flag).sort({ID: 1}).limit(500);
  120. if(tempStdGlj.length > 0){
  121. flag = flag + 500;
  122. stdGljs = stdGljs.concat(tempStdGlj);
  123. }
  124. }*/
  125. me.sortToNumber(stdGljs);
  126. rst.stdGljs = stdGljs;
  127. cb(null);
  128. }
  129. catch (err){
  130. cb(err);
  131. }
  132. },
  133. function (cb) {
  134. complementaryGljModel.find({userId: userId, compilationId: compilationId}, function (err, complementaryGljs) {
  135. if(err){
  136. cb(err);
  137. }
  138. else{
  139. me.sortToNumber(complementaryGljs);
  140. rst.complementaryGljs = complementaryGljs;
  141. cb(null);
  142. }
  143. });
  144. }
  145. ], function (err) {
  146. if(err){
  147. callback(err, null);
  148. }
  149. else{
  150. callback(0, rst);
  151. }
  152. })
  153. };
  154. getGljItemsByCode (repositoryId, codes, callback){
  155. gljModel.find({"repositoryId": repositoryId,"code": {"$in": codes}},function(err,data){
  156. if(err) callback(true, "")
  157. else callback(false, data);
  158. })
  159. };
  160. updateComponent(userId, updateArr, callback){
  161. let parallelFucs = [];
  162. for(let i = 0; i < updateArr.length; i++){
  163. parallelFucs.push((function(obj){
  164. return function (cb) {
  165. if(typeof obj.component === 'undefined'){
  166. obj.component = [];
  167. }
  168. complementaryGljModel.update({userId: userId, ID: obj.ID}, obj, function (err, result) {
  169. if(err){
  170. cb(err);
  171. }
  172. else{
  173. cb(null, obj);
  174. }
  175. })
  176. }
  177. })(updateArr[i]));
  178. }
  179. async.parallel(parallelFucs, function (err, result) {
  180. if(err){
  181. callback(err, '更新组成物错误!', null);
  182. }
  183. else{
  184. callback(0, '成功!', result);
  185. }
  186. });
  187. }
  188. //-oprtor
  189. mixUpdateGljItems (userId, compilationId, updateItems, addItems, rIds, callback) {
  190. if (updateItems.length == 0 && rIds.length == 0) {
  191. GljDao.addGljItems(userId, compilationId, addItems, callback);
  192. }
  193. else if(rIds.length > 0 && updateItems.length > 0){
  194. async.parallel([
  195. function (cb) {
  196. GljDao.removeGljItems(rIds, cb);
  197. },
  198. function (cb) {
  199. GljDao.updateGljItems(userId, compilationId, updateItems, cb);
  200. }
  201. ], function (err) {
  202. if(err){
  203. callback(true, "Fail to update and delete", false)
  204. }
  205. else{
  206. callback(false, "Save successfully", false);
  207. }
  208. })
  209. }
  210. else if (rIds.length > 0 && updateItems.length === 0) {
  211. GljDao.removeGljItems(rIds, callback);
  212. }
  213. else if(updateItems.length > 0 || addItems.length > 0){
  214. GljDao.updateGljItems(userId, compilationId, updateItems, function(err, results){
  215. if (err) {
  216. callback(true, "Fail to update", false);
  217. } else {
  218. if (addItems && addItems.length > 0) {
  219. GljDao.addGljItems(userId, compilationId, addItems, callback);
  220. } else {
  221. callback(false, "Save successfully", results);
  222. }
  223. }
  224. });
  225. }
  226. }
  227. /*mixUpdateGljItems (repId, lastOpr, updateItems, addItems, rIds, callback) {
  228. if (updateItems.length == 0 && rIds.length == 0) {
  229. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  230. } else if (rIds.length > 0) {
  231. GljDao.removeGljItems(repId, lastOpr, rIds, function(err, message, docs) {
  232. });
  233. }else{
  234. GljDao.updateGljItems(repId, lastOpr, updateItems, function(err, results){
  235. if (err) {
  236. callback(true, "Fail to update", false);
  237. } else {
  238. if (addItems && addItems.length > 0) {
  239. GljDao.addGljItems(repId, lastOpr, addItems, callback);
  240. } else {
  241. callback(false, "Save successfully", results);
  242. }
  243. }
  244. });
  245. }
  246. };*/
  247. static removeGljItems (rIds, callback) {
  248. if (rIds && rIds.length > 0) {
  249. complementaryGljModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  250. if (err) {
  251. callback(true, "Fail to remove", false);
  252. } else {
  253. callback(false, "Remove successfully", docs);
  254. }
  255. })
  256. } else {
  257. callback(false, "No records were deleted!", null);
  258. }
  259. }
  260. static addGljItems (userId, compilationId, items, callback) {
  261. if (items && items.length > 0) {
  262. counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, function(err, result){
  263. var maxId = result.value.sequence_value;
  264. var arr = [];
  265. for (var i = 0; i < items.length; i++) {
  266. var obj = new complementaryGljModel(items[i]);
  267. obj.ID = (maxId - (items.length - 1) + i);
  268. obj.userId = userId;
  269. obj.compilationId = compilationId;
  270. arr.push(obj);
  271. }
  272. complementaryGljModel.collection.insert(arr, null, function(err, docs){
  273. if (err) {
  274. callback(true, "Fail to add", false);
  275. } else {
  276. callback(false, "Add successfully", docs);
  277. }
  278. });
  279. });
  280. } else {
  281. callback(true, "No source", false);
  282. }
  283. }
  284. static updateGljItems(userId, compilationId, items, callback) {
  285. var functions = [];
  286. for (var i=0; i < items.length; i++) {
  287. functions.push((function(doc) {
  288. return function(cb) {
  289. var filter = {};
  290. if (doc.ID) {
  291. filter.ID = doc.ID;
  292. } else {
  293. filter.userId = userId;
  294. filter.compilationId = compilationId;
  295. filter.code = doc.code;
  296. }
  297. complementaryGljModel.update(filter, doc, cb);
  298. };
  299. })(items[i]));
  300. }
  301. async.parallel(functions, function(err, results) {
  302. callback(err, results);
  303. });
  304. }
  305. /**
  306. * 获取组成物数据
  307. *
  308. * @param {Number} gljId
  309. * @return {Promise}
  310. */
  311. async getComponent(gljId) {
  312. let result = [];
  313. let libGljData = await complementaryGljModel.findOne({ID: gljId});
  314. if (libGljData === null || libGljData.component.length <= 0) {
  315. return result;
  316. }
  317. // 标准工料机库
  318. let componentIdListStd = [];
  319. // 补充工料机库
  320. let componentIdListCpt = [];
  321. let componentConsume = {};
  322. // 整理数据
  323. for (let component of libGljData.component) {
  324. if (component.isStd) {
  325. componentIdListStd.push(component.ID);
  326. } else {
  327. componentIdListCpt.push(component.ID);
  328. }
  329. let isStdFlag = component.isStd ? 'std' : 'cpt';
  330. componentConsume[component.ID + '_' + isStdFlag] = component.consumeAmt;
  331. }
  332. // 查找标准库数据
  333. let condition = {};
  334. let componentStdGljData = [];
  335. if (componentIdListStd.length > 0) {
  336. let gljListModel = new STDGLJLibGLJListModel();
  337. condition = {ID: {$in: componentIdListStd}};
  338. componentStdGljData = await gljListModel.findDataByCondition(condition, null, false);
  339. }
  340. // 查找补充库数据
  341. let componentCptGljData = [];
  342. if (componentIdListCpt.length > 0) {
  343. condition = {ID: {$in: componentIdListCpt}};
  344. componentCptGljData = await complementaryGljModel.find(condition);
  345. }
  346. if (componentCptGljData === null && componentStdGljData === null) {
  347. return result;
  348. }
  349. // 整理数据
  350. if (componentStdGljData.length > 0) {
  351. componentStdGljData = JSON.stringify(componentStdGljData);
  352. componentStdGljData = JSON.parse(componentStdGljData);
  353. for(let gljData of componentStdGljData) {
  354. gljData.connectCode = libGljData.code;
  355. gljData.consumption = componentConsume[gljData.ID + '_std'];
  356. result.push(gljData);
  357. }
  358. }
  359. if (componentCptGljData.length > 0) {
  360. componentCptGljData = JSON.stringify(componentCptGljData);
  361. componentCptGljData = JSON.parse(componentCptGljData);
  362. for(let gljData of componentCptGljData) {
  363. gljData.connectCode = libGljData.code;
  364. gljData.consumption = componentConsume[gljData.ID + '_cpt'];
  365. result.push(gljData);
  366. }
  367. }
  368. return result;
  369. }
  370. }
  371. export default GljDao;