gljController.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * Created by Zhong on 2017/8/22.
  3. */
  4. const BaseController = require("../../common/base/base_controller");
  5. const stdgljutil = require("../../../public/cache/std_glj_type_util");
  6. const GljDao = require("../models/gljModel");
  7. const EngineeringLibModel = require("../../users/models/engineering_lib_model");
  8. const config = require("../../../config/config.js");
  9. const pmFacade = require('../../pm/facade/pm_facade');
  10. const { ShareLibType } = require('../../../public/common_constants');
  11. const { getTitle } = require("../../../public/titleUtil");
  12. let gljDao = new GljDao();
  13. let callback = function(req, res, err, message, data){
  14. res.json({error: err, message: message, data: data});
  15. };
  16. async function getGljLibId(sessionCompilation) {
  17. let gljLibId = null,
  18. rationValuation = sessionCompilation.ration_valuation,
  19. billValuation = sessionCompilation.bill_valuation,
  20. engineeringLibModel = new EngineeringLibModel(),
  21. valuationIDs = [] ;
  22. for(let r of rationValuation){//{ "glj_lib.0": {$exists:1} }
  23. if(r.id){
  24. valuationIDs.push(r.id);
  25. }
  26. }
  27. for(let b of billValuation){
  28. if(b.id){
  29. valuationIDs.push(b.id);
  30. }
  31. }
  32. if(valuationIDs.length > 0){
  33. let engineeringInfo = await engineeringLibModel.findDataByCondition({'valuationID': {"$in": valuationIDs},"glj_lib.0": {$exists:1}});//数组大于0
  34. gljLibId = engineeringInfo.glj_lib.length > 0 && typeof engineeringInfo.glj_lib !== 'undefined' ? engineeringInfo.glj_lib[0].id : null;
  35. }
  36. return gljLibId;
  37. }
  38. class GljController extends BaseController{
  39. async redirectGlj(req, res){
  40. const { userID } = req.params;
  41. const sessionUserID = req.session.sessionUser.id;
  42. const compilationID = req.session.sessionCompilation._id;
  43. if (userID !== sessionUserID) {
  44. const receiveList = await pmFacade.getReceiveLibList(sessionUserID, compilationID, ShareLibType.GLJ_LIB);
  45. const isValid = !!receiveList.find(user => String(user._id) === userID);
  46. if (!isValid) {
  47. return res.redirect(`/complementaryGlj/${sessionUserID}`);
  48. }
  49. }
  50. const sessionCompilation = req.session.sessionCompilation;
  51. const gljLibId = await getGljLibId(sessionCompilation);
  52. let overWriteUrl = sessionCompilation && req.session.sessionCompilation.overWriteUrl &&
  53. compilationID !== '5b4d581023a924000b760f2d' ? req.session.sessionCompilation.overWriteUrl : null;
  54. const isReadOnly = userID !== sessionUserID;
  55. res.render('building_saas/complementary_glj_lib/html/tools-gongliaoji.html',{
  56. isReadOnly,
  57. userID: sessionUserID,
  58. gljLibId: gljLibId,
  59. compilationId: sessionCompilation._id,
  60. compilationName: sessionCompilation.name,
  61. versionName: req.session.compilationVersion,
  62. LicenseKey:config.getLicenseKey(process.env.NODE_ENV),
  63. overWriteUrl: overWriteUrl,
  64. title:getTitle(req.headers.host)
  65. });
  66. }
  67. async prepareInitData(req, res) {
  68. try {
  69. const { userID, projection } = JSON.parse(req.body.data);
  70. const sessionUserID = req.session.sessionUser.id;
  71. const gljLibID = await getGljLibId(req.session.sessionCompilation);
  72. const compilationID = req.session.sessionCompilation._id;
  73. const initData = await gljDao.prepareInitData(gljLibID, userID, sessionUserID, compilationID, projection);
  74. res.json({error: 0, message: 'success', data: initData});
  75. } catch (err) {
  76. console.log(err);
  77. res.json({error: 1, message: 'fail', data: null});
  78. }
  79. }
  80. getGljDistType (req, res) {
  81. let gljDistTypeCache = stdgljutil.getStdGljTypeCacheObj().toArray();
  82. if(gljDistTypeCache.length >0 ){
  83. callback(req, res, 0, '', gljDistTypeCache);
  84. }
  85. else {
  86. callback(req, res, 1, 'Error', null);
  87. }
  88. }
  89. getGljTree(req,res){
  90. let data = JSON.parse(req.body.data);
  91. let gljLibId = data.gljLibId;
  92. gljDao.getGljTypes(gljLibId,function(err,data){
  93. callback(req,res,err, 'Get Tree', data)
  94. });
  95. }
  96. //获取标准分类树和补充分类树
  97. async getMixedTree(req, res){
  98. try {
  99. let data = JSON.parse(req.body.data);
  100. let treeData = await gljDao.getMixedTree(data.gljLibId, req.session.sessionUser.id, req.session.sessionCompilation._id);
  101. callback(req, res, 0, 'success', treeData);
  102. } catch (err) {
  103. console.log(err);
  104. callback(req, res, 1, err, null);
  105. }
  106. }
  107. createNewGljTypeNode(req, res) {
  108. let repId = req.body.repositoryId;
  109. let lastNodeId = req.body.lastNodeId;
  110. let lastOpr = req.body.lastOpr;
  111. let nodeData = JSON.parse(req.body.rawNodeData);
  112. gljDao.createNewNode(repId, lastOpr, lastNodeId, nodeData, function(err, msg, data){
  113. callback(req,res,err,msg, data)
  114. });
  115. }
  116. updateGljNodes(req, res) {
  117. let nodes = JSON.parse(req.body.nodes);
  118. let repId = req.body.repId,
  119. lastOpr = req.body.lastOpr;
  120. gljDao.updateNodes(repId, lastOpr, nodes, function(err,results){
  121. callback(req,res, err, results)
  122. });
  123. }
  124. deleteGljNodes(req, res) {
  125. let nodes = JSON.parse(req.body.nodes);
  126. let preNodeId = req.body.preNodeId;
  127. let preNodeNextId = req.body.preNodeNextId;
  128. let repId = req.body.repId, lastOpr = req.body.lastOpr;
  129. gljDao.removeNodes(repId, lastOpr, nodes, preNodeId, preNodeNextId, function(err,results){
  130. callback(req,res, err, results)
  131. });
  132. }
  133. getGljItems(req, res) {
  134. let data = JSON.parse(req.body.data);
  135. let stdGljLibId = data.stdGljLibId;
  136. let projection = data.projection;
  137. gljDao.getGljItems(stdGljLibId, req.session.sessionUser.id, req.session.sessionCompilation._id, projection, function(err, data){
  138. callback(req,res,err,'Get Items',data)
  139. });
  140. }
  141. getStdItems(req, res) {
  142. let data = JSON.parse(req.body.data),
  143. stdGljLibId = data.stdGljLibId,
  144. projection = data.projection;
  145. gljDao.getStdItems(stdGljLibId, projection, function (err, data) {
  146. callback(req, res, err, '获取人材机数据失败', data);
  147. });
  148. }
  149. updateComponent(req, res){
  150. let userId = req.body.userId;
  151. let updateArr = JSON.parse(req.body.updateArr);
  152. gljDao.updateComponent(userId, updateArr, function (err, message, rst) {
  153. callback(req, res, err, message, rst);
  154. })
  155. }
  156. mixUpdateGljItems(req, res){
  157. let user_id = req.session.sessionUser.id,
  158. compilation_id = req.session.sessionCompilation._id;
  159. let data = JSON.parse(req.body.data);
  160. let updateItems = data.updateItems,
  161. addItems = data.addItems,
  162. removeIds = data.removeIds;
  163. gljDao.mixUpdateGljItems(user_id, compilation_id, updateItems, addItems, removeIds, function(err, message, rst){
  164. if (err) {
  165. callback(req, res, err, message, null);
  166. } else {
  167. callback(req, res, 0, message, rst);
  168. }
  169. });
  170. }
  171. }
  172. module.exports = GljController;