ration_glj_controller.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * Created by chen on 2017/6/29.
  3. */
  4. let mongoose = require("mongoose")
  5. let ration_glj_facade = require('../facade/ration_glj_facade')
  6. let logger = require("../../../logs/log_helper").logger;
  7. const { COMPILATION, COMPLEMENTARY_LIB, ShareLibType } = require('../../../public/common_constants');
  8. const pmFacade = require('../../pm/facade/pm_facade');
  9. module.exports={
  10. createRationGLJ:createRationGLJ,
  11. testGetQuantify:testGetQuantify,
  12. getGLJData:getGLJData,
  13. getGLJDataByCodes:getGLJDataByCodes,
  14. addGLJ:addGLJ,
  15. replaceGLJ:replaceGLJ,
  16. mReplaceGLJ:mReplaceGLJ,
  17. updateRationGLJByEdit:updateRationGLJByEdit,
  18. getGLJClass:getGLJClass,
  19. deleteRationGLJ:deleteRationGLJ,
  20. updateProportion:updateProportion
  21. }
  22. function createRationGLJ() {
  23. let gls = mongoose.model('ration_glj');
  24. gls.create({'GLJID':1,'basePrice':23.23,'name':"testgls"},function(err, gls){
  25. console.log(gls)
  26. })
  27. }
  28. //测试获取消耗量:
  29. async function testGetQuantify() {
  30. var condition = {
  31. projectID:99,
  32. projectGLJIDList:[60,61,62]
  33. }
  34. try{
  35. let result = await ration_glj_facade.getQuantityByProjectGLJ(condition);
  36. console.log(result)
  37. }catch (err){
  38. console.log(err);
  39. }
  40. }
  41. async function getGLJDataByCodes(req,res){
  42. let result={
  43. error:0
  44. }
  45. try {
  46. let data = req.body.data;
  47. data = JSON.parse(data);
  48. let datas= await ration_glj_facade.getGLJDataByCodes(data,req.session.sessionCompilation);
  49. result.data=datas;
  50. }catch (err){
  51. logger.err(err);
  52. result.error=1;
  53. result.message = err.message;
  54. }
  55. res.json(result);
  56. }
  57. async function getGLJData(req, res) {
  58. let result={
  59. error:0
  60. }
  61. try {
  62. let libData = null;
  63. let { engineerID, gljLibId, isInitial } = req.params;
  64. const userID = req.session.sessionUser.id;
  65. const compilationId = req.session.sessionCompilation._id;
  66. const cptLibReg = new RegExp(COMPLEMENTARY_LIB);
  67. if (!cptLibReg.test(gljLibId)) {
  68. gljLibId = +gljLibId;
  69. }
  70. isInitial = JSON.parse(isInitial);
  71. if (!gljLibId || isInitial) { // 替换人材机的话,可能存在gljLibID,但是是初始化的操作
  72. libData = engineerID === COMPILATION
  73. ? await ration_glj_facade.getLibOptionsForCompilation(req.session.sessionCompilation._id)
  74. : await ration_glj_facade.getLibOptions(engineerID);
  75. libData.push({ name: '我的补充工料机库', gljLibId: COMPLEMENTARY_LIB });
  76. // 设置被分享的人材机库
  77. const receiveData = await pmFacade.getReceiveLibList(userID, compilationId, ShareLibType.GLJ_LIB);
  78. receiveData.forEach(user => {
  79. libData.push({ name: `${user.real_name}的补充工料机库`, gljLibId: `${COMPLEMENTARY_LIB}*${user._id}` });
  80. });
  81. if (gljLibId) { // 替换人材机初始化会触发此条件
  82. const orgDefalutLib = libData.find(lib => lib.isDefault);
  83. const newDefaultLib = libData.find(lib => lib.gljLibId === gljLibId);
  84. if (orgDefalutLib && newDefaultLib) {
  85. orgDefalutLib.isDefault = false;
  86. newDefaultLib.isDefault = true;
  87. }
  88. }
  89. }
  90. if (!gljLibId && libData) {
  91. gljLibId = (libData.find(lib => lib.isDefault) || {}).gljLibId;
  92. }
  93. let info;
  94. if (cptLibReg.test(gljLibId)) {
  95. const owner = gljLibId.split('*')[1] || userID;
  96. info = { gljLibId: null, userID: owner, compilationId };
  97. } else {
  98. info = { gljLibId, userID: null, compilationId: null };
  99. }
  100. ration_glj_facade.getGLJData(info,function (err,datas) {
  101. if(err){
  102. result.error=1;
  103. result.message = err.message;
  104. }else {
  105. //多单价字段
  106. if(req.session.sessionCompilation.priceProperties) datas.priceProperties = req.session.sessionCompilation.priceProperties;
  107. result.datas = datas;
  108. result.datas.libData = libData;
  109. }
  110. res.json(result);
  111. });
  112. }catch (err){
  113. logger.err(err);
  114. result.error=1;
  115. result.message = err.message;
  116. res.json(result);
  117. }
  118. }
  119. /* async function getGLJData(req, res) {
  120. let result={
  121. error:0
  122. }
  123. try {
  124. let info = await ration_glj_facade.getLibInfo(req);
  125. ration_glj_facade.getGLJData(info,function (err,datas) {
  126. if(err){
  127. result.error=1;
  128. result.message = err.message;
  129. }else {
  130. //多单价字段
  131. if(req.session.sessionCompilation.priceProperties) datas.priceProperties = req.session.sessionCompilation.priceProperties;
  132. result.datas = datas;
  133. }
  134. res.json(result);
  135. });
  136. }catch (err){
  137. logger.err(err);
  138. result.error=1;
  139. result.message = err.message;
  140. res.json(result);
  141. }
  142. } */
  143. async function addGLJ(req, res){
  144. let result={
  145. error:0
  146. }
  147. try {
  148. let data = req.body.data;
  149. data = JSON.parse(data);
  150. let datas= await ration_glj_facade.addGLJ(data,req.session.sessionCompilation);
  151. result.data=datas;
  152. }catch (err){
  153. logger.err(err);
  154. result.error=1;
  155. result.message = err.message;
  156. }
  157. res.json(result);
  158. }
  159. async function deleteRationGLJ (req,res) {
  160. let result={
  161. error:0
  162. }
  163. try {
  164. let data = req.body.data;
  165. data = JSON.parse(data);
  166. let datas= await ration_glj_facade.deleteRationGLJ(data);
  167. result.data=datas;
  168. }catch (err){
  169. logger.err(err);
  170. result.error=1;
  171. result.message = err.message;
  172. }
  173. res.json(result);
  174. }
  175. async function updateProportion(req, res) {
  176. try {
  177. const data = JSON.parse(req.body.data);
  178. const rst = await ration_glj_facade.updateProportion(data.proportionList, data.projectID, data.rationID);
  179. res.json({ error: 0, data: rst, message: 'success' });
  180. } catch (err) {
  181. res.json({ error: 1, data: null, message: err.message });
  182. }
  183. }
  184. async function replaceGLJ(req, res){
  185. let result={
  186. error:0
  187. }
  188. try {
  189. let data = req.body.data;
  190. data = JSON.parse(data);
  191. let rdata= await ration_glj_facade.replaceGLJ(data,req.session.sessionCompilation);
  192. result.data=rdata;
  193. }catch (err){
  194. logger.err(err);
  195. result.error=1;
  196. result.message = err.message;
  197. }
  198. res.json(result);
  199. }
  200. async function mReplaceGLJ(req, res){
  201. let result={
  202. error:0
  203. }
  204. try {
  205. let data = req.body.data;
  206. data = JSON.parse(data);
  207. let mresult= await ration_glj_facade.mReplaceGLJ(data,req.session.sessionCompilation);
  208. result.data=mresult;
  209. }catch (err){
  210. logger.err(err);
  211. result.error=1;
  212. result.message = err.message;
  213. }
  214. res.json(result);
  215. }
  216. async function updateRationGLJByEdit(req, res) {
  217. let result={
  218. error:0
  219. }
  220. try {
  221. let data = req.body.data;
  222. data = JSON.parse(data);
  223. let uresult= await ration_glj_facade.updateRationGLJByEdit(data);
  224. result.data=uresult;
  225. }catch (err){
  226. logger.err(err);
  227. result.error=1;
  228. result.message = err.message;
  229. }
  230. res.json(result);
  231. }
  232. async function getGLJClass(req, res) {
  233. let result={
  234. error:0
  235. }
  236. try {
  237. let data = req.body.data;
  238. data = JSON.parse(data);
  239. let info = await ration_glj_facade.getLibInfo(req);
  240. let tresult= await ration_glj_facade.getGLJClass(info,data);
  241. result.data=tresult;
  242. }catch (err){
  243. logger.err(err);
  244. result.error=1;
  245. result.message = err.message;
  246. }
  247. res.json(result);
  248. }