gljModel.js 12 KB

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