CameraService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. return resVO;
  119. }
  120. return null;
  121. }
  122. /**
  123. * 保存摄像头
  124. * @param vo
  125. */
  126. @Transactional(rollbackFor = Exception.class)
  127. public int createCamera(CameraVO vo, UserLoginedConvertVO loginUser){
  128. QueryWrapper<IdenCamera> queryWrapper = new QueryWrapper<>();
  129. queryWrapper.lambda().eq(IdenCamera::getName,vo.getName());
  130. if(idenCameraService.count(queryWrapper) > 0){
  131. return 1;
  132. }
  133. QueryWrapper<IdenCamera> queryWrapper2 = new QueryWrapper<>();
  134. queryWrapper2.lambda().eq(IdenCamera::getCode,vo.getCode());
  135. if(idenCameraService.count(queryWrapper2) > 0){
  136. return 2;
  137. }
  138. //保存摄像头
  139. IdenCamera idenCamera = new IdenCamera();
  140. BeanUtil.copyProperties(vo,idenCamera);
  141. idenCamera.setCreateTime(new Date());
  142. this.idenCameraService.save(idenCamera);
  143. return 0;
  144. }
  145. /**
  146. * 修改摄像头
  147. * @param vo
  148. */
  149. @Transactional(rollbackFor = Exception.class)
  150. public void updateCamera(CameraVO vo){
  151. //保存摄像头
  152. IdenCamera idenCamera = new IdenCamera();
  153. BeanUtil.copyProperties(vo,idenCamera);
  154. idenCamera.setModifyTime(new Date());
  155. this.idenCameraService.updateById(idenCamera);
  156. }
  157. public void exportToExcel(String type, String district, String subdistrict, Long communityId, String name, UserLoginedConvertVO loginUser, HttpServletResponse response) throws Exception {
  158. QueryWrapper<IdenCamera> queryWrapper = new QueryWrapper<>();
  159. queryWrapper.lambda().like(StrUtil.isNotEmpty(name),IdenCamera::getName,name)
  160. .eq(StrUtil.isNotEmpty(district),IdenCamera::getDistrict,district)
  161. .eq(StrUtil.isNotEmpty(subdistrict),IdenCamera::getSubdistrict,subdistrict)
  162. .eq(communityId != null,IdenCamera::getCommunityId,communityId)
  163. .eq(StrUtil.isNotEmpty(type),IdenCamera::getType,type)
  164. .orderByAsc(IdenCamera::getCode);
  165. List<IdenCamera> list = this.idenCameraService.list(queryWrapper);
  166. List<CameraVO> records = new ArrayList<>();
  167. if (CollUtil.isNotEmpty(list)) {
  168. list.forEach(item->{
  169. CameraVO vo = new CameraVO();
  170. BeanUtil.copyProperties(item, vo);
  171. vo.setStatusName(ConnectStatusEnum.getValueToName(vo.getStatus()));
  172. vo.setTypeName(CameraTypeEnum.getValueToName(vo.getType()));
  173. Long communityId1 = vo.getCommunityId();
  174. if(communityId1 != null){
  175. IdenCommunity idenCommunity = this.idenCommunityService.getById(communityId1);
  176. if(idenCommunity != null ){
  177. vo.setCommunityName(idenCommunity.getName());
  178. }
  179. }
  180. records.add(vo);
  181. });
  182. }
  183. InputStream excelTemplateIs = null;
  184. try {
  185. response.reset(); // 非常重要
  186. response.setContentType("application/vnd.ms-excel");
  187. response.setCharacterEncoding("utf-8");
  188. final String fileName = URLEncoder.encode("摄像头表", "UTF-8");
  189. response.setHeader("Content-disposition", "attachment;filename=" + fileName + System.currentTimeMillis() + ".xlsx");
  190. // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
  191. // {} 代表普通变量 {.} 代表是list的变量
  192. excelTemplateIs = this.getClass().getResourceAsStream("/static/template/export/IdenCameraExportTemplate.xlsx");
  193. ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(excelTemplateIs).build();
  194. WriteSheet writeSheet = EasyExcel.writerSheet("摄像头表").build();
  195. // 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
  196. // forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
  197. // 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
  198. // 如果数据量大 list不是最后一行 参照另一个
  199. FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.FALSE).build();
  200. excelWriter.fill(records, fillConfig, writeSheet);
  201. excelWriter.finish();
  202. } catch (Exception e) {
  203. e.printStackTrace();
  204. } finally {
  205. if(excelTemplateIs != null){
  206. try {
  207. excelTemplateIs.close();
  208. } catch (IOException ioe) {
  209. ioe.printStackTrace();
  210. }
  211. }
  212. }
  213. }
  214. /**
  215. * 导入
  216. * @param multipartFile
  217. * @return
  218. * @throws Exception
  219. */
  220. @Transactional(rollbackFor = Exception.class)
  221. public Boolean importWithExcel(String type,MultipartFile multipartFile) throws Exception {
  222. if(StringUtils.isEmpty(type)){
  223. return false;
  224. }
  225. final File excelFile = MultipartFileToFile.multipartFileToFile(multipartFile);
  226. final Workbook workbook = ExcelUtil.chooseWorkbook(excelFile);
  227. final Sheet sheet = workbook.getSheetAt(0);
  228. final IExcelParser excelParser = new CommonExcelParse();
  229. final List<CameraVO> listParser = excelParser.parse(sheet, new CameraVO());
  230. if (CollectionUtil.isNotEmpty(listParser)) {
  231. List<IdenCamera> listDb = new ArrayList<>();
  232. MultipartFileToFile.delteTempFile(excelFile);
  233. for(CameraVO tmpObj : listParser){
  234. if(StringUtils.isEmpty(tmpObj.getCode())
  235. || StringUtils.isEmpty(tmpObj.getName())
  236. || StringUtils.isEmpty(tmpObj.getDistrict())
  237. || StringUtils.isEmpty(tmpObj.getSubdistrict())
  238. || StringUtils.isEmpty(tmpObj.getCommunityName())
  239. || StringUtils.isEmpty(tmpObj.getLatitude())
  240. || StringUtils.isEmpty(tmpObj.getLongitude())){
  241. continue;
  242. }
  243. final IdenCamera obj = new IdenCamera();
  244. BeanUtils.copyProperties(tmpObj,obj);
  245. obj.setStatus(ConnectStatusEnum.getNameToValue(tmpObj.getName()));
  246. String communityName = tmpObj.getCommunityName();
  247. QueryWrapper<IdenCommunity> queryWrapper = new QueryWrapper<>();
  248. queryWrapper.lambda().eq(IdenCommunity::getName,communityName);
  249. IdenCommunity idenCommunity = this.idenCommunityService.getOne(queryWrapper);
  250. if(idenCommunity != null){
  251. obj.setCommunityId(idenCommunity.getId());
  252. }
  253. obj.setType(type);
  254. obj.setCreateTime(new Date());
  255. listDb.add(obj);
  256. }
  257. return this.idenCameraService.saveBatch(listDb);
  258. }
  259. return false;
  260. }
  261. }