CameraController.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.fdkankan.agent.controller;
  2. import com.alibaba.nacos.common.codec.Base64;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.fdkankan.agent.common.BaseController;
  5. import com.fdkankan.agent.common.PageInfo;
  6. import com.fdkankan.agent.common.ResultCode;
  7. import com.fdkankan.agent.common.ResultData;
  8. import com.fdkankan.agent.common.util.JwtUtil;
  9. import com.fdkankan.agent.entity.AgentNew;
  10. import com.fdkankan.agent.exception.BusinessException;
  11. import com.fdkankan.agent.request.CameraParam;
  12. import com.fdkankan.agent.service.IAgentNewCameraService;
  13. import com.fdkankan.agent.service.IAgentNewService;
  14. import com.fdkankan.agent.service.ICameraService;
  15. import com.fdkankan.agent.service.IExcelService;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.apache.commons.io.FileUtils;
  18. import org.apache.ibatis.annotations.Param;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.web.bind.annotation.*;
  21. import org.springframework.web.multipart.MultipartFile;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.io.OutputStream;
  28. import java.net.URLEncoder;
  29. import java.nio.charset.StandardCharsets;
  30. import java.util.List;
  31. /**
  32. * <p>
  33. * 相机主表 前端控制器
  34. * </p>
  35. *
  36. * @author
  37. * @since 2022-11-09
  38. */
  39. @RestController
  40. @Slf4j
  41. @RequestMapping("/agent/camera")
  42. public class CameraController extends BaseController {
  43. @Autowired
  44. ICameraService cameraService;
  45. @Autowired
  46. IAgentNewService agentNewService;
  47. @Autowired
  48. IAgentNewCameraService agentNewCameraService;
  49. @Autowired
  50. IExcelService excelService;
  51. @PostMapping("/list")
  52. public ResultData list(@RequestBody CameraParam param){
  53. param.setAgentId(getAgent().getId());
  54. if(getAgent().getParentId() != null) {
  55. List<Long> cameraIds = agentNewCameraService.getBySubAgent(getAgent().getId());
  56. if(cameraIds.isEmpty()){
  57. return ResultData.ok(PageInfo.PageInfo(new Page<>(param.getPageNum(),param.getPageSize())));
  58. }
  59. param.setAuthCameraIds(cameraIds);
  60. }
  61. return ResultData.ok(cameraService.pageList(param));
  62. }
  63. @PostMapping("/giveCamera")
  64. public ResultData giveCamera(@RequestBody CameraParam param){
  65. if(param.getId() == null || param.getSubAgentId() == null){
  66. throw new BusinessException(ResultCode.PARAM_MISS);
  67. }
  68. param.setAgentId(getAgent().getId());
  69. agentNewCameraService.giveCamera(param);
  70. return ResultData.ok();
  71. }
  72. @PostMapping("/unBindCamera")
  73. public ResultData unBindCamera(@RequestBody CameraParam param){
  74. if(param.getId() == null ){
  75. throw new BusinessException(ResultCode.PARAM_MISS);
  76. }
  77. param.setAgentId(getAgent().getId());
  78. agentNewCameraService.unBindCamera(param);
  79. return ResultData.ok();
  80. }
  81. @PostMapping("/giveCameraBatch")
  82. public ResultData giveCameraBatch(
  83. @RequestParam(required = false) Integer subAgentId,
  84. @RequestParam(required = false) MultipartFile file){
  85. Integer count = excelService.uploadExcel(file,getAgent().getId(),subAgentId);
  86. return ResultData.ok(count);
  87. }
  88. @GetMapping("/downTemplate")
  89. public void downInTemplate(@RequestParam(required = false,defaultValue = "0") Integer type,
  90. HttpServletResponse res, HttpServletRequest req) throws IOException {
  91. String fileName = "";
  92. switch (getLang()){
  93. case "en" : fileName = "giveCamera_en.xlsx"; break;
  94. default: fileName = "giveCamera.xlsx"; break;
  95. }
  96. InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/"+fileName);
  97. OutputStream os = res.getOutputStream();
  98. try {
  99. String agent = req.getHeader("USER-AGENT");
  100. if (agent != null && !agent.contains("MSIE") && !agent.contains("like Gecko")) {// FF
  101. String enableFileName = "=?UTF-8?B?" + (new String(Base64.encodeBase64(fileName.getBytes(StandardCharsets.UTF_8))))
  102. + "?=";
  103. res.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
  104. } else { // IE
  105. String enableFileName = URLEncoder.encode(fileName, "UTF-8");
  106. res.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
  107. }
  108. File file = new File(fileName);
  109. FileUtils.copyInputStreamToFile(inputStream,file);
  110. os.write(FileUtils.readFileToByteArray(file));
  111. os.flush();
  112. } finally {
  113. os.close();
  114. }
  115. }
  116. }