gljController.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. let config = require("../../../config/config.js");
  9. const pmFacade = require('../../pm/facade/pm_facade');
  10. const { ShareLibType } = require('../../../public/common_constants');
  11. const getValuationBasisURL = require('../../common/const/valuationBasis');
  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 priceProperties = sessionCompilation.priceProperties || [];
  55. const consumeAmtProperties = sessionCompilation.consumeAmtProperties || [];
  56. const isReadOnly = userID !== sessionUserID;
  57. res.render('building_saas/complementary_glj_lib/html/tools-gongliaoji.html',{
  58. isReadOnly,
  59. userID: sessionUserID,
  60. gljLibId: gljLibId,
  61. compilationId: sessionCompilation._id,
  62. compilationName: sessionCompilation.name,
  63. valuationBasisURL: getValuationBasisURL(sessionCompilation.name),
  64. priceProperties: JSON.stringify(priceProperties),
  65. consumeAmtProperties: JSON.stringify(consumeAmtProperties),
  66. versionName: req.session.compilationVersion,
  67. LicenseKey:config.getLicenseKey(process.env.NODE_ENV),
  68. overWriteUrl: overWriteUrl,
  69. });
  70. }
  71. async prepareInitData(req, res) {
  72. try {
  73. const { userID, projection } = JSON.parse(req.body.data);
  74. const sessionUserID = req.session.sessionUser.id;
  75. const gljLibID = await getGljLibId(req.session.sessionCompilation);
  76. const compilationID = req.session.sessionCompilation._id;
  77. const initData = await gljDao.prepareInitData(gljLibID, userID, sessionUserID, compilationID, projection);
  78. res.json({error: 0, message: 'success', data: initData});
  79. } catch (err) {
  80. console.log(err);
  81. res.json({error: 1, message: 'fail', data: null});
  82. }
  83. }
  84. getGljDistType (req, res) {
  85. let gljDistTypeCache = stdgljutil.getStdGljTypeCacheObj().toArray();
  86. if(gljDistTypeCache.length >0 ){
  87. callback(req, res, 0, '', gljDistTypeCache);
  88. }
  89. else {
  90. callback(req, res, 1, 'Error', null);
  91. }
  92. }
  93. getGljTree(req,res){
  94. let data = JSON.parse(req.body.data);
  95. let gljLibId = data.gljLibId;
  96. gljDao.getGljTypes(gljLibId,function(err,data){
  97. callback(req,res,err, 'Get Tree', data)
  98. });
  99. }
  100. //获取标准分类树和补充分类树
  101. async getMixedTree(req, res){
  102. try {
  103. let data = JSON.parse(req.body.data);
  104. let treeData = await gljDao.getMixedTree(data.gljLibId, req.session.sessionUser.id, req.session.sessionCompilation._id);
  105. callback(req, res, 0, 'success', treeData);
  106. } catch (err) {
  107. console.log(err);
  108. callback(req, res, 1, err, null);
  109. }
  110. }
  111. createNewGljTypeNode(req, res) {
  112. let repId = req.body.repositoryId;
  113. let lastNodeId = req.body.lastNodeId;
  114. let lastOpr = req.body.lastOpr;
  115. let nodeData = JSON.parse(req.body.rawNodeData);
  116. gljDao.createNewNode(repId, lastOpr, lastNodeId, nodeData, function(err, msg, data){
  117. callback(req,res,err,msg, data)
  118. });
  119. }
  120. updateGljNodes(req, res) {
  121. let nodes = JSON.parse(req.body.nodes);
  122. let repId = req.body.repId,
  123. lastOpr = req.body.lastOpr;
  124. gljDao.updateNodes(repId, lastOpr, nodes, function(err,results){
  125. callback(req,res, err, results)
  126. });
  127. }
  128. deleteGljNodes(req, res) {
  129. let nodes = JSON.parse(req.body.nodes);
  130. let preNodeId = req.body.preNodeId;
  131. let preNodeNextId = req.body.preNodeNextId;
  132. let repId = req.body.repId, lastOpr = req.body.lastOpr;
  133. gljDao.removeNodes(repId, lastOpr, nodes, preNodeId, preNodeNextId, function(err,results){
  134. callback(req,res, err, results)
  135. });
  136. }
  137. getGljItems(req, res) {
  138. let data = JSON.parse(req.body.data);
  139. let stdGljLibId = data.stdGljLibId;
  140. let projection = data.projection;
  141. gljDao.getGljItems(stdGljLibId, req.session.sessionUser.id, req.session.sessionCompilation._id, projection, function(err, data){
  142. callback(req,res,err,'Get Items',data)
  143. });
  144. }
  145. getStdItems(req, res) {
  146. let data = JSON.parse(req.body.data),
  147. stdGljLibId = data.stdGljLibId,
  148. projection = data.projection;
  149. gljDao.getStdItems(stdGljLibId, projection, function (err, data) {
  150. callback(req, res, err, '获取人材机数据失败', data);
  151. });
  152. }
  153. updateComponent(req, res){
  154. let userId = req.body.userId;
  155. let updateArr = JSON.parse(req.body.updateArr);
  156. gljDao.updateComponent(userId, updateArr, function (err, message, rst) {
  157. callback(req, res, err, message, rst);
  158. })
  159. }
  160. mixUpdateGljItems(req, res){
  161. let user_id = req.session.sessionUser.id,
  162. compilation_id = req.session.sessionCompilation._id;
  163. let data = JSON.parse(req.body.data);
  164. let updateItems = data.updateItems,
  165. addItems = data.addItems,
  166. removeIds = data.removeIds;
  167. gljDao.mixUpdateGljItems(user_id, compilation_id, updateItems, addItems, removeIds, function(err, message, rst){
  168. if (err) {
  169. callback(req, res, err, message, null);
  170. } else {
  171. callback(req, res, 0, message, rst);
  172. }
  173. });
  174. }
  175. }
  176. module.exports = GljController;