CameraService.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package com.iden.bms.service;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollUtil;
  4. import cn.hutool.core.collection.CollectionUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import com.alibaba.excel.EasyExcel;
  7. import com.alibaba.excel.ExcelWriter;
  8. import com.alibaba.excel.write.metadata.WriteSheet;
  9. import com.alibaba.excel.write.metadata.fill.FillConfig;
  10. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  11. import com.baomidou.mybatisplus.core.metadata.IPage;
  12. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  13. import com.iden.common.entity.IdenCamera;
  14. import com.iden.common.entity.IdenCommunity;
  15. import com.iden.common.enums.CameraTypeEnum;
  16. import com.iden.common.enums.ConnectStatusEnum;
  17. import com.iden.common.exceltool.CommonExcelParse;
  18. import com.iden.common.exceltool.ExcelUtil;
  19. import com.iden.common.exceltool.IExcelParser;
  20. import com.iden.common.exceltool.MultipartFileToFile;
  21. import com.iden.common.service.IdenCameraService;
  22. import com.iden.common.service.IdenCommunityService;
  23. import com.iden.common.util.MyBeanUtils;
  24. import com.iden.common.vo.CameraVO;
  25. import com.iden.common.vo.PageReqVO;
  26. import com.iden.common.vo.UserLoginedConvertVO;
  27. import org.apache.poi.ss.usermodel.Sheet;
  28. import org.apache.poi.ss.usermodel.Workbook;
  29. import org.springframework.beans.BeanUtils;
  30. import org.springframework.stereotype.Service;
  31. import org.springframework.transaction.annotation.Transactional;
  32. import org.springframework.util.StringUtils;
  33. import org.springframework.web.multipart.MultipartFile;
  34. import javax.annotation.Resource;
  35. import javax.servlet.http.HttpServletResponse;
  36. import java.io.File;
  37. import java.io.IOException;
  38. import java.io.InputStream;
  39. import java.net.URLEncoder;
  40. import java.util.ArrayList;
  41. import java.util.Date;
  42. import java.util.List;
  43. /**
  44. *
  45. * @author makejava
  46. * @since 2021-05-21 00:08:38
  47. */
  48. @Service
  49. public class CameraService {
  50. @Resource
  51. private IdenCameraService idenCameraService;
  52. @Resource
  53. private IdenCommunityService idenCommunityService;
  54. public Integer countCamera(String type, String district, String subdistrict, Long communityId, String name, UserLoginedConvertVO loginUser) {
  55. QueryWrapper<IdenCamera> queryWrapper = new QueryWrapper<>();
  56. queryWrapper.lambda().like(StrUtil.isNotEmpty(name),IdenCamera::getName,name)
  57. .eq(StrUtil.isNotEmpty(district),IdenCamera::getDistrict,district)
  58. .eq(StrUtil.isNotEmpty(subdistrict),IdenCamera::getSubdistrict,subdistrict)
  59. .eq(communityId != null,IdenCamera::getCommunityId,communityId)
  60. .eq(StrUtil.isNotEmpty(type),IdenCamera::getType,type);
  61. return this.idenCameraService.count(queryWrapper);
  62. }
  63. /**
  64. * 查询摄像头列表
  65. * @return
  66. */
  67. public IPage<CameraVO> listCamera(String type, String district, String subdistrict, Long communityId, String name, UserLoginedConvertVO loginUser, PageReqVO pageReqVo) {
  68. IPage<IdenCamera> page = new Page<>(pageReqVo.getCurrent(), pageReqVo.getPageSize());
  69. QueryWrapper<IdenCamera> queryWrapper = new QueryWrapper<>();
  70. queryWrapper.lambda().like(StrUtil.isNotEmpty(name),IdenCamera::getName,name)
  71. .eq(StrUtil.isNotEmpty(district),IdenCamera::getDistrict,district)
  72. .eq(StrUtil.isNotEmpty(subdistrict),IdenCamera::getSubdistrict,subdistrict)
  73. .eq(communityId != null,IdenCamera::getCommunityId,communityId)
  74. .eq(StrUtil.isNotEmpty(type),IdenCamera::getType,type)
  75. .orderByAsc(IdenCamera::getCode);
  76. IPage<IdenCamera> pageRes = this.idenCameraService.page(page, queryWrapper);
  77. IPage<CameraVO> results = new Page<>(pageRes.getCurrent(),pageRes.getSize(),pageRes.getTotal());
  78. if(CollUtil.isNotEmpty(pageRes.getRecords())){
  79. List<CameraVO> list = new ArrayList<>();
  80. pageRes.getRecords().forEach(item -> {
  81. CameraVO resVO = new CameraVO();
  82. BeanUtils.copyProperties(item,resVO);
  83. resVO.setStatusName(ConnectStatusEnum.getValueToName(resVO.getStatus()));
  84. resVO.setTypeName(CameraTypeEnum.getValueToName(resVO.getType()));
  85. Long communityId1 = resVO.getCommunityId();
  86. if(communityId1 != null){
  87. IdenCommunity idenCommunity = this.idenCommunityService.getById(communityId1);
  88. if(idenCommunity != null ){
  89. resVO.setCommunityName(idenCommunity.getName());
  90. }
  91. }
  92. list.add(resVO);
  93. });
  94. results.setRecords(list);
  95. }
  96. return results;
  97. }
  98. /**
  99. * 删除摄像头
  100. * @param id
  101. * @return
  102. */
  103. public boolean deleteById(Long id){
  104. return this.idenCameraService.removeById(id);
  105. }
  106. /**
  107. * 详情
  108. * @param id
  109. * @return
  110. */
  111. public CameraVO getCameraById(Long id){
  112. IdenCamera idenCamera = this.idenCameraService.getById(id);
  113. if (idenCamera!=null){
  114. CameraVO resVO = new CameraVO();
  115. BeanUtil.copyProperties(idenCamera,resVO);
  116. resVO.setStatusName(ConnectStatusEnum.getValueToName(resVO.getCode()));
  117. resVO.setTypeName(CameraTypeEnum.getValueToName(resVO.getType()));
  118. Long communityId1 = resVO.getCommunityId();
  119. if(communityId1 != null){
  120. IdenCommunity idenCommunity = this.idenCommunityService.getById(communityId1);
  121. if(idenCommunity != null ){
  122. resVO.setCommunityName(idenCommunity.getName());
  123. }
  124. }
  125. return resVO;
  126. }
  127. return null;
  128. }
  129. /**
  130. * 保存摄像头
  131. * @param vo
  132. */
  133. @Transactional(rollbackFor = Exception.class)
  134. public int createCamera(CameraVO vo, UserLoginedConvertVO loginUser){
  135. QueryWrapper<IdenCamera> queryWrapper = new QueryWrapper<>();
  136. queryWrapper.lambda().eq(IdenCamera::getName,vo.getName());
  137. if(idenCameraService.count(queryWrapper) > 0){
  138. return 1;
  139. }
  140. QueryWrapper<IdenCamera> queryWrapper2 = new QueryWrapper<>();
  141. queryWrapper2.lambda().eq(IdenCamera::getCode,vo.getCode());
  142. if(idenCameraService.count(queryWrapper2) > 0){
  143. return 2;
  144. }
  145. //保存摄像头
  146. IdenCamera idenCamera = new IdenCamera();
  147. BeanUtil.copyProperties(vo,idenCamera);
  148. idenCamera.setCreateTime(new Date());
  149. this.idenCameraService.save(idenCamera);
  150. return 0;
  151. }
  152. /**
  153. * 修改摄像头
  154. * @param vo
  155. */
  156. @Transactional(rollbackFor = Exception.class)
  157. public void updateCamera(CameraVO vo){
  158. //保存摄像头
  159. IdenCamera idenCamera = new IdenCamera();
  160. BeanUtil.copyProperties(vo,idenCamera);
  161. idenCamera.setModifyTime(new Date());
  162. this.idenCameraService.updateById(idenCamera);
  163. }
  164. public void exportToExcel(String type, String district, String subdistrict, Long communityId, String name, UserLoginedConvertVO loginUser, HttpServletResponse response) throws Exception {
  165. QueryWrapper<IdenCamera> queryWrapper = new QueryWrapper<>();
  166. queryWrapper.lambda().like(StrUtil.isNotEmpty(name),IdenCamera::getName,name)
  167. .eq(StrUtil.isNotEmpty(district),IdenCamera::getDistrict,district)
  168. .eq(StrUtil.isNotEmpty(subdistrict),IdenCamera::getSubdistrict,subdistrict)
  169. .eq(communityId != null,IdenCamera::getCommunityId,communityId)
  170. .eq(StrUtil.isNotEmpty(type),IdenCamera::getType,type)
  171. .orderByAsc(IdenCamera::getCode);
  172. List<IdenCamera> list = this.idenCameraService.list(queryWrapper);
  173. List<CameraVO> records = new ArrayList<>();
  174. if (CollUtil.isNotEmpty(list)) {
  175. list.forEach(item->{
  176. CameraVO vo = new CameraVO();
  177. BeanUtil.copyProperties(item, vo);
  178. vo.setStatusName(ConnectStatusEnum.getValueToName(vo.getStatus()));
  179. vo.setTypeName(CameraTypeEnum.getValueToName(vo.getType()));
  180. Long communityId1 = vo.getCommunityId();
  181. if(communityId1 != null){
  182. IdenCommunity idenCommunity = this.idenCommunityService.getById(communityId1);
  183. if(idenCommunity != null ){
  184. vo.setCommunityName(idenCommunity.getName());
  185. }
  186. }
  187. records.add(vo);
  188. });
  189. }
  190. InputStream excelTemplateIs = null;
  191. try {
  192. response.reset(); // 非常重要
  193. response.addHeader("Access-Control-Allow-Origin", "*");
  194. response.setContentType("application/vnd.ms-excel");
  195. response.setCharacterEncoding("utf-8");
  196. final String fileName = URLEncoder.encode("Camera", "UTF-8");
  197. response.setHeader("Content-disposition", "attachment;filename=" + fileName +"_"+ System.currentTimeMillis() + ".xlsx");
  198. // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
  199. // {} 代表普通变量 {.} 代表是list的变量
  200. excelTemplateIs = this.getClass().getResourceAsStream("/static/template/export/IdenCameraExportTemplate.xlsx");
  201. ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(excelTemplateIs).build();
  202. WriteSheet writeSheet = EasyExcel.writerSheet("摄像头表").build();
  203. // 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
  204. // forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
  205. // 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
  206. // 如果数据量大 list不是最后一行 参照另一个
  207. FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.FALSE).build();
  208. excelWriter.fill(records, fillConfig, writeSheet);
  209. excelWriter.finish();
  210. } catch (Exception e) {
  211. e.printStackTrace();
  212. } finally {
  213. if(excelTemplateIs != null){
  214. try {
  215. excelTemplateIs.close();
  216. } catch (IOException ioe) {
  217. ioe.printStackTrace();
  218. }
  219. }
  220. }
  221. }
  222. public void downloadFormwork(UserLoginedConvertVO loginUser, HttpServletResponse response) throws Exception {
  223. List<CameraVO> records = new ArrayList<>();
  224. InputStream excelTemplateIs = null;
  225. try {
  226. response.reset(); // 非常重要
  227. response.addHeader("Access-Control-Allow-Origin", "*");
  228. response.setContentType("application/vnd.ms-excel");
  229. response.setCharacterEncoding("utf-8");
  230. final String fileName = URLEncoder.encode("Camera", "UTF-8");
  231. response.setHeader("Content-disposition", "attachment;filename=" + fileName +"_"+ System.currentTimeMillis() + ".xlsx");
  232. // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
  233. // {} 代表普通变量 {.} 代表是list的变量
  234. excelTemplateIs = this.getClass().getResourceAsStream("/static/template/export/IdenCameraExportTemplate.xlsx");
  235. ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(excelTemplateIs).build();
  236. WriteSheet writeSheet = EasyExcel.writerSheet("摄像头表").build();
  237. // 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
  238. // forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
  239. // 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
  240. // 如果数据量大 list不是最后一行 参照另一个
  241. FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.FALSE).build();
  242. excelWriter.fill(records, fillConfig, writeSheet);
  243. excelWriter.finish();
  244. } catch (Exception e) {
  245. e.printStackTrace();
  246. } finally {
  247. if(excelTemplateIs != null){
  248. try {
  249. excelTemplateIs.close();
  250. } catch (IOException ioe) {
  251. ioe.printStackTrace();
  252. }
  253. }
  254. }
  255. }
  256. /**
  257. * 导入
  258. * @param multipartFile
  259. * @return
  260. * @throws Exception
  261. */
  262. @Transactional(rollbackFor = Exception.class)
  263. public Boolean importWithExcel(String type,MultipartFile multipartFile) throws Exception {
  264. if(StringUtils.isEmpty(type)){
  265. return false;
  266. }
  267. final File excelFile = MultipartFileToFile.multipartFileToFile(multipartFile);
  268. final Workbook workbook = ExcelUtil.chooseWorkbook(excelFile);
  269. final Sheet sheet = workbook.getSheetAt(0);
  270. final IExcelParser excelParser = new CommonExcelParse();
  271. final List<CameraVO> listParser = excelParser.parse(sheet, new CameraVO());
  272. if (CollectionUtil.isNotEmpty(listParser)) {
  273. List<IdenCamera> listDb = new ArrayList<>();
  274. MultipartFileToFile.delteTempFile(excelFile);
  275. for(CameraVO tmpObj : listParser){
  276. if(StringUtils.isEmpty(tmpObj.getCode())
  277. || StringUtils.isEmpty(tmpObj.getName())
  278. || StringUtils.isEmpty(tmpObj.getDistrict())
  279. || StringUtils.isEmpty(tmpObj.getSubdistrict())
  280. || StringUtils.isEmpty(tmpObj.getCommunityName())
  281. || StringUtils.isEmpty(tmpObj.getLatitude())
  282. || StringUtils.isEmpty(tmpObj.getLongitude())){
  283. continue;
  284. }
  285. final IdenCamera obj = new IdenCamera();
  286. BeanUtils.copyProperties(tmpObj,obj);
  287. obj.setStatus(ConnectStatusEnum.getNameToValue(tmpObj.getName()));
  288. String communityName = tmpObj.getCommunityName();
  289. QueryWrapper<IdenCommunity> queryWrapper = new QueryWrapper<>();
  290. queryWrapper.lambda().eq(IdenCommunity::getName,communityName);
  291. IdenCommunity idenCommunity = this.idenCommunityService.getOne(queryWrapper);
  292. if(idenCommunity != null){
  293. obj.setCommunityId(idenCommunity.getId());
  294. }
  295. obj.setType(type);
  296. obj.setCreateTime(new Date());
  297. listDb.add(obj);
  298. }
  299. return this.idenCameraService.saveBatch(listDb);
  300. }
  301. return false;
  302. }
  303. }