CameraController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package com.fdkankan.ucenter.controller;
  2. import cn.hutool.core.date.DateUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.fdkankan.common.exception.BusinessException;
  6. import com.fdkankan.common.util.JwtUtil;
  7. import com.fdkankan.ucenter.common.BaseController;
  8. import com.fdkankan.ucenter.common.Result;
  9. import com.fdkankan.ucenter.common.ResultData;
  10. import com.fdkankan.ucenter.constant.LoginConstant;
  11. import com.fdkankan.ucenter.entity.Camera;
  12. import com.fdkankan.ucenter.entity.CameraDetail;
  13. import com.fdkankan.ucenter.entity.ScenePro;
  14. import com.fdkankan.ucenter.entity.User;
  15. import com.fdkankan.ucenter.service.*;
  16. import com.fdkankan.ucenter.util.DateUserUtil;
  17. import com.fdkankan.ucenter.vo.request.CameraParam;
  18. import com.fdkankan.ucenter.vo.request.ExportCameraParam;
  19. import com.fdkankan.ucenter.vo.response.CameraAppVo;
  20. import com.fdkankan.ucenter.vo.response.CameraExcelVo;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.apache.poi.hssf.usermodel.HSSFRow;
  23. import org.apache.poi.hssf.usermodel.HSSFSheet;
  24. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.util.CollectionUtils;
  27. import org.springframework.web.bind.annotation.*;
  28. import java.io.OutputStream;
  29. import java.util.ArrayList;
  30. import java.util.Date;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.stream.Collectors;
  34. @RestController
  35. @RequestMapping("/ucenter/user/camera")
  36. public class CameraController extends BaseController {
  37. @Autowired
  38. ICameraDetailService cameraDetailService;
  39. @Autowired
  40. ICameraService cameraService;
  41. @Autowired
  42. ISceneResourceService sceneResourceService;
  43. @Autowired
  44. IUserService userService;
  45. @Autowired
  46. ISceneProService sceneProService;
  47. /**
  48. * 获取设备到期数量
  49. */
  50. @PostMapping("/deadlineNumber")
  51. public Result deadlineNumber(){
  52. String username = JwtUtil.getUsername(getToken());
  53. return Result.success(cameraDetailService.deadlineNumber(username));
  54. }
  55. /**
  56. * 分页列表
  57. */
  58. @PostMapping("/listNew")
  59. public Result listNew(@RequestBody CameraParam param){
  60. String username = JwtUtil.getUsername(getToken());
  61. param.setUserName(username);
  62. return Result.success(cameraService.pageList(param));
  63. }
  64. /**
  65. * 获取用户设备到期信息
  66. */
  67. @PostMapping("/deadline")
  68. public Result deadline(){
  69. String username = JwtUtil.getUsername(getToken());
  70. return Result.success(cameraService.deadline(username));
  71. }
  72. /**
  73. * 用户解绑设备
  74. */
  75. @PostMapping("/unbind")
  76. public Result unbind(@RequestBody JSONObject jsonObject){
  77. String username = JwtUtil.getUsername(getToken());
  78. cameraService.unbind(jsonObject.getLong("cameraId"),jsonObject.getString("ids"),username);
  79. return Result.success();
  80. }
  81. /**
  82. *用户绑定设备
  83. */
  84. @PostMapping("/add")
  85. public Result add(@RequestBody JSONObject jsonObject){
  86. String username = JwtUtil.getUsername(getToken());
  87. cameraService.bind(jsonObject.getInteger("cameraType"),jsonObject.getString("snCode"),username);
  88. return Result.success();
  89. }
  90. /**
  91. * 添加相机协作用户
  92. */
  93. @PostMapping("/saveCooperationUser")
  94. public Result saveCooperationUser(@RequestBody JSONObject jsonObject){
  95. String username = JwtUtil.getUsername(getToken());
  96. if(jsonObject.getString("userName").equals(username)){
  97. throw new BusinessException(LoginConstant.FAILURE_CODE_3025, LoginConstant.FAILURE_MSG_3025);
  98. }
  99. cameraService.saveCooperationUser(jsonObject.getString("ids"),
  100. jsonObject.getLong("cameraId"),jsonObject.getString("resourceIds"),jsonObject.getString("userName"));
  101. return Result.success();
  102. }
  103. /**
  104. * 删除相机协作用户
  105. */
  106. @PostMapping("/deleteCooperationUser")
  107. public Result deleteCooperationUser(@RequestBody JSONObject jsonObject){
  108. cameraService.deleteCooperationUser(jsonObject.getLong("cameraId"));
  109. return Result.success();
  110. }
  111. /**
  112. * 根据相机id获取场景资源集合
  113. */
  114. @PostMapping("/sceneResourceByCameraId")
  115. public Result sceneResourceByCameraId(@RequestBody JSONObject jsonObject){
  116. if(jsonObject.get("cameraId") == null){
  117. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  118. }
  119. return Result.success(sceneResourceService.sceneResourceByCameraId(jsonObject.getLong("cameraId")));
  120. }
  121. /**
  122. * 获取用户设备--新(根据sn返回全部不分页)
  123. */
  124. @PostMapping("/listNewAll")
  125. public Result listNewAll(@RequestBody JSONObject jsonObject){
  126. if(jsonObject.get("childName") == null){
  127. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  128. }
  129. return Result.success(cameraService.getAllList(jsonObject.getString("childName"),getToken()));
  130. }
  131. /**
  132. * 获取协作相机sn码集合
  133. */
  134. @PostMapping(value = "/getCooperationSnCodes")
  135. public Result getCooperationSnCodes() throws Exception {
  136. User user = userService.getByToken(getToken());
  137. LambdaQueryWrapper<CameraDetail> condition = new LambdaQueryWrapper<>();
  138. condition.eq(CameraDetail::getCooperationUser,user.getId());
  139. condition.eq(CameraDetail::getGoodsId,10);
  140. List<CameraDetail> list = cameraDetailService.list(condition);
  141. if(CollectionUtils.isEmpty(list)){
  142. return Result.success();
  143. }
  144. LambdaQueryWrapper<Camera> condition2 = new LambdaQueryWrapper<>();
  145. condition2.in(Camera::getId,list.stream().map(CameraDetail::getCameraId)
  146. .collect(Collectors.toList()));
  147. List<Camera> cameraEntities = cameraService.list(condition2);
  148. return Result.success(cameraEntities.stream().map(Camera::getSnCode).collect(Collectors.toList()));
  149. }
  150. /**
  151. * 相机登录之后使用 获取用户设备
  152. */
  153. @PostMapping(value = "/detail")
  154. public Result detail(@RequestBody JSONObject jsonObject) {
  155. if(jsonObject.get("childName") == null){
  156. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  157. }
  158. return Result.success(userService.findCameraDetailByChildName(getToken(), jsonObject.getString("childName")));
  159. }
  160. /**
  161. * 获取四维看看PRO - 我的相机列表 并到处execl
  162. * @return
  163. */
  164. @RequestMapping(value = "/export", method = RequestMethod.POST)
  165. public HSSFWorkbook export(@RequestBody ExportCameraParam param) throws Exception {
  166. String fileName = "我的相机";
  167. User user = userService.getByToken(getToken());
  168. List<CameraExcelVo> responseCameraList = cameraDetailService.getListByUserAndTypeEx(user.getId());
  169. List<Long> cameraIdList = responseCameraList.stream().map(CameraExcelVo::getCameraId).collect(Collectors.toList());
  170. List<ScenePro> sceneEntityList = new ArrayList<>();
  171. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  172. if(cameraIdList.size() >0){
  173. wrapper.in(ScenePro::getCameraId,cameraIdList);
  174. sceneEntityList = sceneProService.list(wrapper);
  175. }
  176. HashMap<Long,List<ScenePro>> sceneProMap = new HashMap<>();
  177. for (ScenePro scenePro : sceneEntityList) {
  178. if(sceneProMap.get(scenePro.getCameraId()) == null){
  179. List<ScenePro> proList = new ArrayList<>();
  180. proList.add(scenePro);
  181. sceneProMap.put(scenePro.getCameraId(),proList);
  182. }else {
  183. sceneProMap.get(scenePro.getCameraId()).add(scenePro);
  184. }
  185. }
  186. HSSFWorkbook wb = new HSSFWorkbook();
  187. if (responseCameraList != null) {
  188. HSSFSheet sheet = wb.createSheet("camera");
  189. //在sheet里创建第一行,这里即是表头
  190. HSSFRow rowTitle = sheet.createRow(0);
  191. rowTitle.createCell(0).setCellValue("S/N码");
  192. rowTitle.createCell(1).setCellValue("云容量");
  193. rowTitle.createCell(2).setCellValue("到期时间");
  194. rowTitle.createCell(3).setCellValue("协作者");
  195. rowTitle.createCell(4).setCellValue("拍摄场景数量");
  196. rowTitle.createCell(5).setCellValue("最后拍摄时间");
  197. int i = 1;
  198. String rl = "";
  199. String UsedSpaceStr = "";
  200. String TotalSpaceStr = "";
  201. for (CameraExcelVo rc : responseCameraList) {
  202. HSSFRow rowData = sheet.createRow(i);
  203. rowData.createCell(0).setCellValue(rc.getSnCode());
  204. if(rc.getUsedSpaceStr() == null || StringUtils.isEmpty(rc.getUsedSpaceStr())){
  205. UsedSpaceStr = "0GB";
  206. }else {
  207. UsedSpaceStr = rc.getUsedSpaceStr();
  208. }
  209. if(rc.getTotalSpaceStr() == null || StringUtils.isEmpty(rc.getTotalSpaceStr())){
  210. TotalSpaceStr = "0GB";
  211. }else {
  212. TotalSpaceStr = rc.getTotalSpaceStr();
  213. }
  214. if(rc.getSpaceEndStr() == null){
  215. rowData.createCell(1).setCellValue(UsedSpaceStr + " / " + TotalSpaceStr);
  216. }else {
  217. if(rc.getIsExpired() == null || rc.getIsExpired() == 1){
  218. rowData.createCell(1).setCellValue(UsedSpaceStr + " / " + TotalSpaceStr);
  219. }else{
  220. rowData.createCell(1).setCellValue(UsedSpaceStr);
  221. }
  222. }
  223. if(rc.getSpaceEndStr() == null ){
  224. rowData.createCell(2).setCellValue("");
  225. }else{
  226. rowData.createCell(2).setCellValue(rc.getSpaceEndStr());
  227. }
  228. if(rc.getCooperationUser() == null ){
  229. rowData.createCell(3).setCellValue("");
  230. }else{
  231. rowData.createCell(3).setCellValue(rc.getCooperationUser());
  232. }
  233. List<ScenePro> proList = sceneProMap.get(rc.getCameraId());
  234. if(proList == null || proList.size() <=0){
  235. rowData.createCell(4).setCellValue("");
  236. rowData.createCell(5).setCellValue("");
  237. }else {
  238. rowData.createCell(4).setCellValue(proList.size());
  239. String lastTime = null;
  240. for (ScenePro scenePro : proList) {
  241. if(lastTime == null){
  242. lastTime = scenePro.getCreateTime();
  243. }else {
  244. Date date1 = DateUserUtil.getDate(lastTime);
  245. Date date2 = DateUserUtil.getDate(scenePro.getCreateTime());
  246. if(date2.getTime() >date1.getTime()){
  247. lastTime = scenePro.getCreateTime();
  248. }
  249. }
  250. }
  251. rowData.createCell(5).setCellValue(lastTime);
  252. }
  253. i++;
  254. }
  255. }
  256. if(sceneEntityList.size() >0){
  257. if(StringUtils.isNotBlank(param.getStartTime())){
  258. wrapper.ge(ScenePro::getCreateTime,param.getStartTime());
  259. }
  260. if(StringUtils.isNotBlank(param.getEndTime())){
  261. String date = DateUserUtil.getLastZeroTime(param.getEndTime());
  262. wrapper.le(ScenePro::getCreateTime,date);
  263. }
  264. if(StringUtils.isNotBlank(param.getStartTime()) || StringUtils.isNotBlank(param.getEndTime())){
  265. sceneEntityList = sceneProService.list(wrapper);
  266. }
  267. }
  268. HashMap<Long, Camera> cameraMap = cameraService.getByIds(cameraIdList);
  269. HashMap<Long, Integer> cameraSnCodeCountMap = new HashMap<>();
  270. for (ScenePro sceneProEntity : sceneEntityList) {
  271. if(cameraSnCodeCountMap.get(sceneProEntity.getCameraId()) ==null){
  272. cameraSnCodeCountMap.put(sceneProEntity.getCameraId(),1);
  273. continue;
  274. }
  275. Integer totalNum = 1 + cameraSnCodeCountMap.get(sceneProEntity.getCameraId()) ;
  276. cameraSnCodeCountMap.put(sceneProEntity.getCameraId(),totalNum);
  277. }
  278. HSSFSheet sheet = wb.createSheet("scene");
  279. //在sheet里创建第一行,这里即是表头
  280. HSSFRow rowTitle = sheet.createRow(0);
  281. rowTitle.createCell(0).setCellValue("S/N码");
  282. rowTitle.createCell(1).setCellValue("开始时间");
  283. rowTitle.createCell(2).setCellValue("结束时间");
  284. rowTitle.createCell(3).setCellValue("场景数量");
  285. int i = 1;
  286. for (Long cameraId : cameraMap.keySet()) {
  287. HSSFRow rowData = sheet.createRow(i);
  288. i++;
  289. rowData.createCell(0).setCellValue(cameraMap.get(cameraId) == null ? "" : cameraMap.get(cameraId).getSnCode());
  290. rowData.createCell(1).setCellValue(param.getStartTime() == null ? "" : param.getStartTime());
  291. rowData.createCell(2).setCellValue(param.getEndTime() == null ? "" : param.getEndTime());
  292. if(cameraSnCodeCountMap.get(cameraId) == null){
  293. rowData.createCell(3).setCellValue("");
  294. continue;
  295. }
  296. rowData.createCell(3).setCellValue(cameraSnCodeCountMap.get(cameraId));
  297. }
  298. HSSFSheet sheet2 = wb.createSheet("info");
  299. //在sheet里创建第一行,这里即是表头
  300. HSSFRow rowTitle2 = sheet2.createRow(0);
  301. rowTitle2.createCell(0).setCellValue("S/N码");
  302. rowTitle2.createCell(1).setCellValue("场景码");
  303. rowTitle2.createCell(2).setCellValue("场景名称");
  304. rowTitle2.createCell(3).setCellValue("场景链接");
  305. rowTitle2.createCell(4).setCellValue("创建时间");
  306. int k = 1;
  307. for (ScenePro sceneProEntity : sceneEntityList) {
  308. HSSFRow rowData = sheet2.createRow(k);
  309. k++;
  310. if(cameraMap.get(sceneProEntity.getCameraId()) == null){
  311. rowData.createCell(0).setCellValue("");
  312. }else {
  313. rowData.createCell(0).setCellValue(cameraMap.get(sceneProEntity.getCameraId()).getSnCode());
  314. }
  315. rowData.createCell(1).setCellValue(sceneProEntity.getNum() == null ? "" :sceneProEntity.getNum());
  316. rowData.createCell(2).setCellValue(sceneProEntity.getSceneName() == null ? "" :sceneProEntity.getSceneName());
  317. rowData.createCell(3).setCellValue(sceneProEntity.getWebSite() == null ? "" :sceneProEntity.getWebSite());
  318. rowData.createCell(4).setCellValue(sceneProEntity.getCreateTime() == null ? "" : sceneProEntity.getCreateTime());
  319. }
  320. //输出Excel文件
  321. OutputStream output = response.getOutputStream();
  322. response.reset();
  323. //中文名称要进行编码处理
  324. response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("GB2312"), "ISO8859-1") + ".xls");
  325. response.setContentType("application/msexcel");
  326. wb.write(output);
  327. output.close();
  328. return wb;
  329. }
  330. }