gljModel.js 11 KB

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