optionsController.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Created by Zhong on 2017/9/28.
  3. */
  4. import BaseController from '../../common/base/base_controller';
  5. import OptionsDao from '../models/optionsModel';
  6. import optionsTypes from '../models/optionTypes';
  7. let optionsDao = new OptionsDao();
  8. class OptionController extends BaseController {
  9. //获得所有选项类型的选项
  10. async getOptions(req, res){
  11. let resJson = {error: null, message: '', data: []};
  12. let user_id = req.session.sessionUser.id,
  13. compilation_id = req.session.sessionCompilation._id;
  14. let defaultOpts = {
  15. GENERALOPTS: {
  16. rationQuanACToBillsQuan: true,//自动根据清单工程量填写定额工程量
  17. rationQuanACToRationUnit: true//自动根据定额单位转换定额工程量
  18. }
  19. };
  20. try{
  21. resJson.data = await optionsDao.getOptions(user_id, compilation_id);
  22. if(!resJson.data){
  23. resJson.data = await optionsDao.saveOptions(user_id, compilation_id, optionsTypes.GENERALOPTS, defaultOpts.GENERALOPTS);
  24. }
  25. }
  26. catch (err){
  27. resJson.error = true;
  28. resJson.message = '获取失败';
  29. resJson.data = null;
  30. }
  31. res.json(resJson);
  32. }
  33. //获得特定选项类型的选项
  34. async getOptionsByType(req, res){
  35. let resJson = {error: null, message: '', data: null};
  36. let user_id = req.session.sessionUser.id,
  37. compilation_id = req.session.sessionCompilation._id,
  38. optsType = req.body.optsType;
  39. try{
  40. let hasThisOpts = false;
  41. for(let i in optionsTypes){
  42. if(optionsTypes[i] === optsType){
  43. hasThisOpts = true;
  44. break;
  45. }
  46. }
  47. if(!hasThisOpts) throw '不存在此选项类';
  48. resJson.data = await optionsDao.getOptionsByType(user_id, compilation_id, optsType);
  49. }
  50. catch (err){
  51. resJson.error = true;
  52. resJson.message = '获取失败';
  53. resJson.data = null;
  54. }
  55. res.json(resJson);
  56. }
  57. async saveOptions(req, res){
  58. let resJson = {error: null, message: '', data: null};
  59. let user_id = req.session.sessionUser.id,
  60. compilation_id = req.session.sessionCompilation._id,
  61. optsType = req.body.optsType,
  62. opts = JSON.parse(req.body.opts);
  63. try{
  64. let hasThisOpts = false;
  65. for(let i in optionsTypes){
  66. if(optionsTypes[i] == optsType){
  67. hasThisOpts = true;
  68. break;
  69. }
  70. }
  71. if(!hasThisOpts) throw '不存在此选项类';
  72. resJson.data = await optionsDao.saveOptions(user_id, compilation_id, optsType, opts);
  73. }
  74. catch (err){
  75. resJson.error = true;
  76. resJson.message = '保存失败';
  77. resJson.data = null;
  78. }
  79. res.json(resJson);
  80. }
  81. }
  82. export default OptionController;