RightsController.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package cn.efunbox.audio.controller;
  2. import cn.efunbox.audio.consts.Status;
  3. import cn.efunbox.audio.entity.Channel;
  4. import cn.efunbox.audio.entity.Grouping;
  5. import cn.efunbox.audio.entity.Record;
  6. import cn.efunbox.audio.entity.Rights;
  7. import cn.efunbox.audio.service.*;
  8. import cn.efunbox.audio.utils.ApiCode;
  9. import cn.efunbox.audio.utils.Common;
  10. import cn.efunbox.audio.utils.HttpUtil;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.data.domain.Page;
  14. import org.springframework.web.bind.annotation.RequestBody;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RequestMethod;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20. import java.sql.Timestamp;
  21. import java.util.*;
  22. /**
  23. * Created by yao on 17-9-26.
  24. */
  25. @RestController
  26. @Slf4j
  27. @RequestMapping(value = "/rights")
  28. public class RightsController {
  29. @Autowired
  30. RightsService rightsService;
  31. @Autowired
  32. ChannelService channelService;
  33. @Autowired
  34. GroupingService groupingService;
  35. @Autowired
  36. AdminService adminService;
  37. @Autowired
  38. TrailService trailService;
  39. @RequestMapping(value = "/search" ,method = RequestMethod.POST)
  40. public void Search(HttpServletRequest request, HttpServletResponse response){
  41. String idRights = request.getParameter("idRights");
  42. String idChannel = request.getParameter("idChannel");
  43. String idGroup = request.getParameter("idGroup");
  44. String page = request.getParameter("page");
  45. String size = request.getParameter("size");
  46. if(page==null || page.length()<1)
  47. page = "0";
  48. if(size==null || size.length()<1)
  49. size = "0";
  50. // if(idRights==null && idChannel==null && idGroup==null){
  51. // HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
  52. // return;
  53. // }
  54. Map<String,Object> map = new HashMap<>();
  55. List<Rights> list = null;
  56. Page<Rights> pageRes = null;
  57. if(idRights!=null && idRights.length()>0)
  58. list = rightsService.SearchById(Long.valueOf(idRights));
  59. else if(idChannel!=null && idChannel.length()>0 && idGroup!=null && idGroup.length()>0)
  60. list = rightsService.SearchByIdChannelAndIdGroup(Long.valueOf(idChannel), Long.valueOf(idGroup));
  61. else if(idChannel!=null && idChannel.length()>0)
  62. pageRes = rightsService.SearchByIdChannel(Long.valueOf(idChannel), Integer.valueOf(page), Integer.valueOf(size));
  63. else if(idGroup!=null && idGroup.length()>0)
  64. pageRes = rightsService.SearchByIdGroup(Long.valueOf(idGroup), Integer.valueOf(page), Integer.valueOf(size));
  65. else
  66. pageRes = rightsService.SearchAll(Integer.valueOf(page), Integer.valueOf(size));
  67. // if(list==null || list.size()<1){
  68. // HttpUtil.responseApiCode(request, response, ApiCode.NOT_FOUND);
  69. // return;
  70. // }
  71. if(list!=null){
  72. map.put("data", list);
  73. map.put("size", list.size());
  74. map.put("total", list.size());
  75. }else if(pageRes!=null){
  76. map.put("data", pageRes.getContent());
  77. map.put("size", pageRes.getNumberOfElements());
  78. map.put("total", pageRes.getTotalElements());
  79. }
  80. HttpUtil.responseOkData(request, response, map);
  81. return;
  82. }
  83. // @RequestMapping(value = "/insert", method = RequestMethod.POST)
  84. // public void Insert(HttpServletRequest request, HttpServletResponse response){
  85. // String idChannel = request.getParameter("idChannel");
  86. // String idGroup = request.getParameter("idGroup");
  87. // if(idChannel==null || idGroup==null){
  88. // HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
  89. // return;
  90. // }
  91. //
  92. // List<Rights> list = rightsService.SearchByIdChannelAndIdGroup(Long.valueOf(idChannel), Long.valueOf(idGroup));
  93. // if(list!=null && list.size()>0){
  94. // HttpUtil.responseApiCode(request, response, ApiCode.RECORD_EXIST);
  95. // return;
  96. // }
  97. // List<Channel> cList = channelService.SearchById(Long.valueOf(idChannel));
  98. // List<Grouping> gList = groupingService.SearchById(Long.valueOf(idGroup));
  99. // if(cList==null || cList.size()<1 || gList==null || gList.size()<1){
  100. // HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
  101. // return;
  102. // }
  103. // Rights rights = new Rights();
  104. // rights.setIdChannel(cList.get(0).getId());
  105. // rights.setNameChannel(cList.get(0).getName());
  106. // rights.setIdGroup(gList.get(0).getId());
  107. // rights.setNameGroup(gList.get(0).getName());
  108. // rights = rightsService.Insert(rights);
  109. //
  110. // HttpUtil.responseOutWithJson(request, response, rights);
  111. // return;
  112. // }
  113. @RequestMapping(value = "/insert", method = RequestMethod.POST)
  114. public void Insert(HttpServletRequest request, HttpServletResponse response, @RequestBody Rights rights){
  115. if(rights.getIdChannel()==null || rights.getIdGroup()==null){
  116. HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
  117. return;
  118. }
  119. List<Rights> list = rightsService.SearchByIdChannelAndIdGroup(rights.getIdChannel(), rights.getIdGroup());
  120. if(list!=null && list.size()>0){
  121. HttpUtil.responseApiCode(request, response, ApiCode.RECORD_EXIST);
  122. return;
  123. }
  124. Channel channel = channelService.SearchById(rights.getIdChannel());
  125. List<Grouping> gList = groupingService.SearchById(rights.getIdGroup());
  126. if(Objects.isNull(channel) || gList==null || gList.size()<1){
  127. HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
  128. return;
  129. }
  130. rights.setNameChannel(channel.getName());
  131. rights.setNameGroup(gList.get(0).getName());
  132. rights.setStatus(Status.ONLINE.getCode());
  133. rights.setCreated(new Timestamp(new Date().getTime()));
  134. rights = rightsService.Insert(rights);
  135. TrailController.Insert(adminService, trailService, request, "rights", rights.getId(), " 新建渠道对资源的访问权限:" + rights.getNameChannel()+",资源:"+rights.getNameGroup());
  136. HttpUtil.responseOkData(request, response, rights);
  137. return;
  138. }
  139. @RequestMapping(value = "/update", method = RequestMethod.POST)
  140. public void Update(HttpServletRequest request, HttpServletResponse response, @RequestBody Rights rights){
  141. Rights rightsOld = rightsService.GetOne(rights.getId());
  142. if(rightsOld==null){
  143. HttpUtil.responseApiCode(request, response, ApiCode.PARAMETER_ERROR);
  144. return;
  145. }
  146. rights = (Rights) Common.CopyValue(rights, rightsOld);
  147. rights = rightsService.Insert(rights);
  148. TrailController.Insert(adminService, trailService, request, "rights", rights.getId(), " 更新渠道对资源的访问权限:" + rights.getNameChannel()+",资源:"+rights.getNameGroup());
  149. HttpUtil.responseOkData(request, response, rights);
  150. return;
  151. }
  152. @RequestMapping(value = "/delete", method = RequestMethod.POST)
  153. public void Delete(HttpServletRequest request, HttpServletResponse response){
  154. String idRights = request.getParameter("idRights");
  155. rightsService.Delete(Long.valueOf(idRights));
  156. TrailController.Insert(adminService, trailService, request, "rights", Long.valueOf(idRights), " 删除渠道对资源的访问权限:" + idRights);
  157. HttpUtil.responseOk(request, response);
  158. return;
  159. }
  160. }