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