gljModel.js 11 KB

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