gljModel.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /**
  2. * Created by Zhong on 2017/8/22.
  3. */
  4. const mongoose = require('mongoose');
  5. const complementaryGljModel = mongoose.model('complementary_glj_lib');
  6. const stdGljModel = mongoose.model('std_glj_lib_gljList');
  7. const gljClassModel = mongoose.model('std_glj_lib_gljClass');
  8. const compleClassModel = mongoose.model('complementary_glj_section');
  9. import counter from "../../../public/counter/counter";
  10. import async from "async";
  11. import STDGLJLibGLJListModel from "../../common/std/std_glj_lib_glj_list_model";
  12. class GljDao {
  13. getGljTypes (gljLibId, callback){
  14. gljClassModel.find({"repositoryId": gljLibId},function(err,data){
  15. if(data.length) {
  16. callback(0,data);
  17. }
  18. else if(err) callback("获取人材机类型错误!",false);
  19. })
  20. }
  21. _exist(data, attr){
  22. return data && data[attr] !== 'undefined' && data[attr];
  23. }
  24. sortToNumber(datas){
  25. for(let i = 0, len = datas.length; i < len; i++){
  26. let data = datas[i]._doc;
  27. if(this._exist(data, 'basePrice')){
  28. data['basePrice'] = parseFloat(data['basePrice']);
  29. }
  30. if(this._exist(data, 'component')){
  31. for(let j = 0, jLen = data['component'].length; j < jLen; j++){
  32. let comGljObj = data['component'][j]._doc;
  33. if(this._exist(comGljObj, 'consumeAmt')){
  34. comGljObj['consumeAmt'] = parseFloat(comGljObj['consumeAmt']);
  35. }
  36. }
  37. }
  38. }
  39. }
  40. //获得用户的补充工料机和用户当前所在编办的标准工料机
  41. async getGljItems (stdGljLibId, userId, compilationId, callback){
  42. let me = this;
  43. let rst = {stdGljs: [], complementaryGljs: []};
  44. //批量获取异步
  45. /* let functions = [];
  46. let count = await stdGljModel.find({repositoryId: stdGljLibId, $or: [{deleted: null}, {deleted: false}]}).count();
  47. let findCount = Math.ceil(count/500);
  48. for(let i = 0, len = findCount; i < len; i++){
  49. functions.push((function(flag) {
  50. return function (cb) {
  51. stdGljModel.find({repositoryId: stdGljLibId, deleted: null}, cb).skip(flag).sort({ID: 1}).limit(500);
  52. }
  53. })(i*500));
  54. }
  55. async.parallel(functions, function (err, results) {
  56. if(err){
  57. callback(err, null);
  58. }
  59. else{
  60. for(let stdGljs of results){
  61. rst.stdGljs = rst.stdGljs.concat(stdGljs);
  62. }
  63. callback(0, rst);
  64. }
  65. });*/
  66. async.parallel([
  67. async function (cb) {
  68. try{
  69. let stdGljs = [];
  70. let first = await stdGljModel.find({repositoryId: stdGljLibId}).sort({ID: 1}).limit(1);
  71. let count = await stdGljModel.find({repositoryId: stdGljLibId, $or: [{deleted: null}, {deleted: false}]}).count();
  72. let findCount = Math.ceil(count/500);
  73. let flag = first[0].ID;
  74. //let flag = 0;
  75. //批量获取,非skip
  76. for(let i = 0, len = findCount; i < len; i++){
  77. let tempStdGlj;
  78. if(i === 0){
  79. tempStdGlj = await stdGljModel.find({repositoryId: stdGljLibId, deleted: null, ID: {$gte: flag}}).sort({ID: 1}).limit(500);
  80. if(tempStdGlj.length > 0){
  81. flag = tempStdGlj[tempStdGlj.length - 1].ID;
  82. }
  83. }
  84. else {
  85. tempStdGlj = await stdGljModel.find({repositoryId: stdGljLibId, deleted: null, ID: {$gt: flag}}).sort({ID: 1}).limit(500);
  86. if(tempStdGlj.length > 0){
  87. flag = tempStdGlj[tempStdGlj.length - 1].ID;
  88. }
  89. }
  90. if(tempStdGlj){
  91. stdGljs = stdGljs.concat(tempStdGlj);
  92. }
  93. }
  94. me.sortToNumber(stdGljs);
  95. rst.stdGljs = stdGljs;
  96. cb(null);
  97. }
  98. catch (err){
  99. cb(err);
  100. }
  101. },
  102. function (cb) {
  103. complementaryGljModel.find({userId: userId, compilationId: compilationId}, function (err, complementaryGljs) {
  104. if(err){
  105. cb(err);
  106. }
  107. else{
  108. me.sortToNumber(complementaryGljs);
  109. rst.complementaryGljs = complementaryGljs;
  110. cb(null);
  111. }
  112. });
  113. }
  114. ], function (err) {
  115. if(err){
  116. callback(err, null);
  117. }
  118. else{
  119. callback(0, rst);
  120. }
  121. })
  122. };
  123. getGljItemsByCode (repositoryId, codes, callback){
  124. gljModel.find({"repositoryId": repositoryId,"code": {"$in": codes}},function(err,data){
  125. if(err) callback(true, "")
  126. else callback(false, data);
  127. })
  128. };
  129. updateComponent(userId, updateArr, callback){
  130. let parallelFucs = [];
  131. for(let i = 0; i < updateArr.length; i++){
  132. parallelFucs.push((function(obj){
  133. return function (cb) {
  134. if(typeof obj.component === 'undefined'){
  135. obj.component = [];
  136. }
  137. complementaryGljModel.update({userId: userId, ID: obj.ID}, obj, function (err, result) {
  138. if(err){
  139. cb(err);
  140. }
  141. else{
  142. cb(null, obj);
  143. }
  144. })
  145. }
  146. })(updateArr[i]));
  147. }
  148. async.parallel(parallelFucs, function (err, result) {
  149. if(err){
  150. callback(err, '更新组成物错误!', null);
  151. }
  152. else{
  153. callback(0, '成功!', result);
  154. }
  155. });
  156. }
  157. //-oprtor
  158. mixUpdateGljItems (userId, compilationId, updateItems, addItems, rIds, callback) {
  159. if (updateItems.length == 0 && rIds.length == 0) {
  160. GljDao.addGljItems(userId, compilationId, addItems, callback);
  161. }
  162. else if(rIds.length > 0 && updateItems.length > 0){
  163. async.parallel([
  164. function (cb) {
  165. GljDao.removeGljItems(rIds, cb);
  166. },
  167. function (cb) {
  168. GljDao.updateGljItems(userId, compilationId, updateItems, cb);
  169. }
  170. ], function (err) {
  171. if(err){
  172. callback(true, "Fail to update and delete", false)
  173. }
  174. else{
  175. callback(false, "Save successfully", false);
  176. }
  177. })
  178. }
  179. else if (rIds.length > 0 && updateItems.length === 0) {
  180. GljDao.removeGljItems(rIds, callback);
  181. }
  182. else if(updateItems.length > 0 || addItems.length > 0){
  183. GljDao.updateGljItems(userId, compilationId, updateItems, function(err, results){
  184. if (err) {
  185. callback(true, "Fail to update", false);
  186. } else {
  187. if (addItems && addItems.length > 0) {
  188. GljDao.addGljItems(userId, compilationId, addItems, callback);
  189. } else {
  190. callback(false, "Save successfully", results);
  191. }
  192. }
  193. });
  194. }
  195. }
  196. static removeGljItems (rIds, callback) {
  197. if (rIds && rIds.length > 0) {
  198. complementaryGljModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  199. if (err) {
  200. callback(true, "Fail to remove", false);
  201. } else {
  202. callback(false, "Remove successfully", docs);
  203. }
  204. })
  205. } else {
  206. callback(false, "No records were deleted!", null);
  207. }
  208. }
  209. static addGljItems (userId, compilationId, items, callback) {
  210. if (items && items.length > 0) {
  211. counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, function(err, result){
  212. var maxId = result.sequence_value;
  213. var arr = [];
  214. for (var i = 0; i < items.length; i++) {
  215. var obj = new complementaryGljModel(items[i]);
  216. obj.ID = (maxId - (items.length - 1) + i);
  217. obj.userId = userId;
  218. obj.compilationId = compilationId;
  219. arr.push(obj);
  220. }
  221. complementaryGljModel.collection.insert(arr, null, function(err, docs){
  222. if (err) {
  223. callback(true, "Fail to add", false);
  224. } else {
  225. callback(false, "Add successfully", docs);
  226. }
  227. });
  228. });
  229. } else {
  230. callback(true, "No source", false);
  231. }
  232. }
  233. static updateGljItems(userId, compilationId, items, callback) {
  234. var functions = [];
  235. for (var i=0; i < items.length; i++) {
  236. functions.push((function(doc) {
  237. return function(cb) {
  238. var filter = {};
  239. if (doc.ID) {
  240. filter.ID = doc.ID;
  241. } else {
  242. filter.userId = userId;
  243. filter.compilationId = compilationId;
  244. filter.code = doc.code;
  245. }
  246. complementaryGljModel.update(filter, doc, cb);
  247. };
  248. })(items[i]));
  249. }
  250. async.parallel(functions, function(err, results) {
  251. callback(err, results);
  252. });
  253. }
  254. /**
  255. * 获取组成物数据
  256. *
  257. * @param {Number} gljId
  258. * @return {Promise}
  259. */
  260. async getComponent(gljId) {
  261. let result = [];
  262. let libGljData = await complementaryGljModel.findOne({ID: gljId});
  263. if (libGljData === null || libGljData.component.length <= 0) {
  264. return result;
  265. }
  266. // 标准工料机库
  267. let componentIdListStd = [];
  268. // 补充工料机库
  269. let componentIdListCpt = [];
  270. let componentConsume = {};
  271. // 整理数据
  272. for (let component of libGljData.component) {
  273. if (component.isStd) {
  274. componentIdListStd.push(component.ID);
  275. } else {
  276. componentIdListCpt.push(component.ID);
  277. }
  278. let isStdFlag = component.isStd ? 'std' : 'cpt';
  279. //对于补充人材机的组成物,不会有多单价的情况
  280. componentConsume[component.ID + '_' + isStdFlag] = component.consumeAmt;
  281. }
  282. // 查找标准库数据
  283. let condition = {};
  284. let componentStdGljData = [];
  285. if (componentIdListStd.length > 0) {
  286. let gljListModel = new STDGLJLibGLJListModel();
  287. condition = {ID: {$in: componentIdListStd}};
  288. componentStdGljData = await gljListModel.findDataByCondition(condition, null, false);
  289. }
  290. // 查找补充库数据
  291. let componentCptGljData = [];
  292. if (componentIdListCpt.length > 0) {
  293. condition = {ID: {$in: componentIdListCpt}};
  294. componentCptGljData = await complementaryGljModel.find(condition);
  295. }
  296. if (componentCptGljData === null && componentStdGljData === null) {
  297. return result;
  298. }
  299. // 整理数据
  300. if (componentStdGljData.length > 0) {
  301. componentStdGljData = JSON.stringify(componentStdGljData);
  302. componentStdGljData = JSON.parse(componentStdGljData);
  303. for(let gljData of componentStdGljData) {
  304. gljData.connectCode = libGljData.code;
  305. gljData.consumption = componentConsume[gljData.ID + '_std'];
  306. gljData.from='std';
  307. result.push(gljData);
  308. }
  309. }
  310. if (componentCptGljData.length > 0) {
  311. componentCptGljData = JSON.stringify(componentCptGljData);
  312. componentCptGljData = JSON.parse(componentCptGljData);
  313. for(let gljData of componentCptGljData) {
  314. gljData.connectCode = libGljData.code;
  315. gljData.consumption = componentConsume[gljData.ID + '_cpt'];
  316. gljData.from='cpt';
  317. result.push(gljData);
  318. }
  319. }
  320. return result;
  321. }
  322. async getMixedTree(gljLibId, userId, compilationId){
  323. let rst = {std: [], comple: []};
  324. rst.std = await gljClassModel.find({repositoryId: gljLibId});
  325. rst.comple = await compleClassModel.find({userId: userId, compilationId: compilationId});
  326. return rst;
  327. }
  328. }
  329. export default GljDao;