gljModel.js 11 KB

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