gljModel.js 12 KB

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