gljController.js 7.3 KB

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