123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- package com.fdkankan.agent.controller;
- import com.alibaba.nacos.common.codec.Base64;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.fdkankan.agent.common.BaseController;
- import com.fdkankan.agent.common.PageInfo;
- import com.fdkankan.agent.common.ResultCode;
- import com.fdkankan.agent.common.ResultData;
- import com.fdkankan.agent.common.util.JwtUtil;
- import com.fdkankan.agent.entity.AgentNew;
- import com.fdkankan.agent.exception.BusinessException;
- import com.fdkankan.agent.request.CameraParam;
- import com.fdkankan.agent.service.IAgentNewCameraService;
- import com.fdkankan.agent.service.IAgentNewService;
- import com.fdkankan.agent.service.ICameraService;
- import com.fdkankan.agent.service.IExcelService;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.io.FileUtils;
- import org.apache.ibatis.annotations.Param;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.URLEncoder;
- import java.nio.charset.StandardCharsets;
- import java.util.List;
- /**
- * <p>
- * 相机主表 前端控制器
- * </p>
- *
- * @author
- * @since 2022-11-09
- */
- @RestController
- @Slf4j
- @RequestMapping("/agent/camera")
- public class CameraController extends BaseController {
- @Autowired
- ICameraService cameraService;
- @Autowired
- IAgentNewService agentNewService;
- @Autowired
- IAgentNewCameraService agentNewCameraService;
- @Autowired
- IExcelService excelService;
- @PostMapping("/list")
- public ResultData list(@RequestBody CameraParam param){
- param.setAgentId(getAgent().getId());
- if(getAgent().getParentId() != null) {
- List<Long> cameraIds = agentNewCameraService.getBySubAgent(getAgent().getId());
- if(cameraIds.isEmpty()){
- return ResultData.ok(PageInfo.PageInfo(new Page<>(param.getPageNum(),param.getPageSize())));
- }
- param.setAuthCameraIds(cameraIds);
- }
- return ResultData.ok(cameraService.pageList(param));
- }
- @PostMapping("/giveCamera")
- public ResultData giveCamera(@RequestBody CameraParam param){
- if(param.getId() == null || param.getSubAgentId() == null){
- throw new BusinessException(ResultCode.PARAM_MISS);
- }
- param.setAgentId(getAgent().getId());
- agentNewCameraService.giveCamera(param);
- return ResultData.ok();
- }
- @PostMapping("/unBindCamera")
- public ResultData unBindCamera(@RequestBody CameraParam param){
- if(param.getId() == null ){
- throw new BusinessException(ResultCode.PARAM_MISS);
- }
- param.setAgentId(getAgent().getId());
- agentNewCameraService.unBindCamera(param);
- return ResultData.ok();
- }
- @PostMapping("/giveCameraBatch")
- public ResultData giveCameraBatch(
- @RequestParam(required = false) Integer subAgentId,
- @RequestParam(required = false) MultipartFile file){
- Integer count = excelService.uploadExcel(file,getAgent().getId(),subAgentId);
- return ResultData.ok(count);
- }
- @GetMapping("/downTemplate")
- public void downInTemplate(@RequestParam(required = false,defaultValue = "0") Integer type,
- HttpServletResponse res, HttpServletRequest req) throws IOException {
- String fileName = "";
- switch (getLang()){
- case "en" : fileName = "giveCamera_en.xlsx"; break;
- default: fileName = "giveCamera.xlsx"; break;
- }
- InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/"+fileName);
- OutputStream os = res.getOutputStream();
- try {
- String agent = req.getHeader("USER-AGENT");
- if (agent != null && !agent.contains("MSIE") && !agent.contains("like Gecko")) {// FF
- String enableFileName = "=?UTF-8?B?" + (new String(Base64.encodeBase64(fileName.getBytes(StandardCharsets.UTF_8))))
- + "?=";
- res.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
- } else { // IE
- String enableFileName = URLEncoder.encode(fileName, "UTF-8");
- res.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
- }
- File file = new File(fileName);
- FileUtils.copyInputStreamToFile(inputStream,file);
- os.write(FileUtils.readFileToByteArray(file));
- os.flush();
- } finally {
- os.close();
- }
- }
- }
|