package com.iden.bms.service; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.fill.FillConfig; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.iden.common.entity.IdenCamera; import com.iden.common.entity.IdenCommunity; import com.iden.common.enums.CameraTypeEnum; import com.iden.common.enums.ConnectStatusEnum; import com.iden.common.exceltool.CommonExcelParse; import com.iden.common.exceltool.ExcelUtil; import com.iden.common.exceltool.IExcelParser; import com.iden.common.exceltool.MultipartFileToFile; import com.iden.common.service.IdenCameraService; import com.iden.common.service.IdenCommunityService; import com.iden.common.util.MyBeanUtils; import com.iden.common.vo.CameraVO; import com.iden.common.vo.PageReqVO; import com.iden.common.vo.UserLoginedConvertVO; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * * @author makejava * @since 2021-05-21 00:08:38 */ @Service public class CameraService { @Resource private IdenCameraService idenCameraService; @Resource private IdenCommunityService idenCommunityService; public Integer countCamera(String type, String district, String subdistrict, Long communityId, String name, UserLoginedConvertVO loginUser) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().like(StrUtil.isNotEmpty(name),IdenCamera::getName,name) .eq(StrUtil.isNotEmpty(district),IdenCamera::getDistrict,district) .eq(StrUtil.isNotEmpty(subdistrict),IdenCamera::getSubdistrict,subdistrict) .eq(communityId != null,IdenCamera::getCommunityId,communityId) .eq(StrUtil.isNotEmpty(type),IdenCamera::getType,type); return this.idenCameraService.count(queryWrapper); } /** * 查询摄像头列表 * @return */ public IPage listCamera(String type, String district, String subdistrict, Long communityId, String name, UserLoginedConvertVO loginUser, PageReqVO pageReqVo) { IPage page = new Page<>(pageReqVo.getCurrent(), pageReqVo.getPageSize()); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().like(StrUtil.isNotEmpty(name),IdenCamera::getName,name) .eq(StrUtil.isNotEmpty(district),IdenCamera::getDistrict,district) .eq(StrUtil.isNotEmpty(subdistrict),IdenCamera::getSubdistrict,subdistrict) .eq(communityId != null,IdenCamera::getCommunityId,communityId) .eq(StrUtil.isNotEmpty(type),IdenCamera::getType,type) .orderByAsc(IdenCamera::getCode); IPage pageRes = this.idenCameraService.page(page, queryWrapper); IPage results = new Page<>(pageRes.getCurrent(),pageRes.getSize(),pageRes.getTotal()); if(CollUtil.isNotEmpty(pageRes.getRecords())){ List list = new ArrayList<>(); pageRes.getRecords().forEach(item -> { CameraVO resVO = new CameraVO(); BeanUtils.copyProperties(item,resVO); resVO.setStatusName(ConnectStatusEnum.getValueToName(resVO.getStatus())); resVO.setTypeName(CameraTypeEnum.getValueToName(resVO.getType())); Long communityId1 = resVO.getCommunityId(); if(communityId1 != null){ IdenCommunity idenCommunity = this.idenCommunityService.getById(communityId1); if(idenCommunity != null ){ resVO.setCommunityName(idenCommunity.getName()); } } list.add(resVO); }); results.setRecords(list); } return results; } /** * 删除摄像头 * @param id * @return */ public boolean deleteById(Long id){ return this.idenCameraService.removeById(id); } /** * 详情 * @param id * @return */ public CameraVO getCameraById(Long id){ IdenCamera idenCamera = this.idenCameraService.getById(id); if (idenCamera!=null){ CameraVO resVO = new CameraVO(); BeanUtil.copyProperties(idenCamera,resVO); resVO.setStatusName(ConnectStatusEnum.getValueToName(resVO.getCode())); resVO.setTypeName(CameraTypeEnum.getValueToName(resVO.getType())); Long communityId1 = resVO.getCommunityId(); if(communityId1 != null){ IdenCommunity idenCommunity = this.idenCommunityService.getById(communityId1); if(idenCommunity != null ){ resVO.setCommunityName(idenCommunity.getName()); } } return resVO; } return null; } /** * 保存摄像头 * @param vo */ @Transactional(rollbackFor = Exception.class) public int createCamera(CameraVO vo, UserLoginedConvertVO loginUser){ QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(IdenCamera::getName,vo.getName()); if(idenCameraService.count(queryWrapper) > 0){ return 1; } QueryWrapper queryWrapper2 = new QueryWrapper<>(); queryWrapper2.lambda().eq(IdenCamera::getCode,vo.getCode()); if(idenCameraService.count(queryWrapper2) > 0){ return 2; } //保存摄像头 IdenCamera idenCamera = new IdenCamera(); BeanUtil.copyProperties(vo,idenCamera); idenCamera.setCreateTime(new Date()); this.idenCameraService.save(idenCamera); return 0; } /** * 修改摄像头 * @param vo */ @Transactional(rollbackFor = Exception.class) public void updateCamera(CameraVO vo){ //保存摄像头 IdenCamera idenCamera = new IdenCamera(); BeanUtil.copyProperties(vo,idenCamera); idenCamera.setModifyTime(new Date()); this.idenCameraService.updateById(idenCamera); } public void exportToExcel(String type, String district, String subdistrict, Long communityId, String name, UserLoginedConvertVO loginUser, HttpServletResponse response) throws Exception { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().like(StrUtil.isNotEmpty(name),IdenCamera::getName,name) .eq(StrUtil.isNotEmpty(district),IdenCamera::getDistrict,district) .eq(StrUtil.isNotEmpty(subdistrict),IdenCamera::getSubdistrict,subdistrict) .eq(communityId != null,IdenCamera::getCommunityId,communityId) .eq(StrUtil.isNotEmpty(type),IdenCamera::getType,type) .orderByAsc(IdenCamera::getCode); List list = this.idenCameraService.list(queryWrapper); List records = new ArrayList<>(); if (CollUtil.isNotEmpty(list)) { list.forEach(item->{ CameraVO vo = new CameraVO(); BeanUtil.copyProperties(item, vo); vo.setStatusName(ConnectStatusEnum.getValueToName(vo.getStatus())); vo.setTypeName(CameraTypeEnum.getValueToName(vo.getType())); Long communityId1 = vo.getCommunityId(); if(communityId1 != null){ IdenCommunity idenCommunity = this.idenCommunityService.getById(communityId1); if(idenCommunity != null ){ vo.setCommunityName(idenCommunity.getName()); } } records.add(vo); }); } InputStream excelTemplateIs = null; try { response.reset(); // 非常重要 response.addHeader("Access-Control-Allow-Origin", "*"); response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); final String fileName = URLEncoder.encode("Camera", "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileName +"_"+ System.currentTimeMillis() + ".xlsx"); // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替 // {} 代表普通变量 {.} 代表是list的变量 excelTemplateIs = this.getClass().getResourceAsStream("/static/template/export/IdenCameraExportTemplate.xlsx"); ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(excelTemplateIs).build(); WriteSheet writeSheet = EasyExcel.writerSheet("摄像头表").build(); // 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。 // forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用 // 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存 // 如果数据量大 list不是最后一行 参照另一个 FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.FALSE).build(); excelWriter.fill(records, fillConfig, writeSheet); excelWriter.finish(); } catch (Exception e) { e.printStackTrace(); } finally { if(excelTemplateIs != null){ try { excelTemplateIs.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } } public void downloadFormwork(UserLoginedConvertVO loginUser, HttpServletResponse response) throws Exception { List records = new ArrayList<>(); InputStream excelTemplateIs = null; try { response.reset(); // 非常重要 response.addHeader("Access-Control-Allow-Origin", "*"); response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); final String fileName = URLEncoder.encode("Camera", "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileName +"_"+ System.currentTimeMillis() + ".xlsx"); // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替 // {} 代表普通变量 {.} 代表是list的变量 excelTemplateIs = this.getClass().getResourceAsStream("/static/template/export/IdenCameraExportTemplate.xlsx"); ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(excelTemplateIs).build(); WriteSheet writeSheet = EasyExcel.writerSheet("摄像头表").build(); // 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。 // forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用 // 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存 // 如果数据量大 list不是最后一行 参照另一个 FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.FALSE).build(); excelWriter.fill(records, fillConfig, writeSheet); excelWriter.finish(); } catch (Exception e) { e.printStackTrace(); } finally { if(excelTemplateIs != null){ try { excelTemplateIs.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } } /** * 导入 * @param multipartFile * @return * @throws Exception */ @Transactional(rollbackFor = Exception.class) public Boolean importWithExcel(String type,MultipartFile multipartFile) throws Exception { if(StringUtils.isEmpty(type)){ return false; } final File excelFile = MultipartFileToFile.multipartFileToFile(multipartFile); final Workbook workbook = ExcelUtil.chooseWorkbook(excelFile); final Sheet sheet = workbook.getSheetAt(0); final IExcelParser excelParser = new CommonExcelParse(); final List listParser = excelParser.parse(sheet, new CameraVO()); if (CollectionUtil.isNotEmpty(listParser)) { List listDb = new ArrayList<>(); MultipartFileToFile.delteTempFile(excelFile); for(CameraVO tmpObj : listParser){ if(StringUtils.isEmpty(tmpObj.getCode()) || StringUtils.isEmpty(tmpObj.getName()) || StringUtils.isEmpty(tmpObj.getDistrict()) || StringUtils.isEmpty(tmpObj.getSubdistrict()) || StringUtils.isEmpty(tmpObj.getCommunityName()) || StringUtils.isEmpty(tmpObj.getLatitude()) || StringUtils.isEmpty(tmpObj.getLongitude())){ continue; } final IdenCamera obj = new IdenCamera(); BeanUtils.copyProperties(tmpObj,obj); obj.setStatus(ConnectStatusEnum.getNameToValue(tmpObj.getName())); String communityName = tmpObj.getCommunityName(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(IdenCommunity::getName,communityName); IdenCommunity idenCommunity = this.idenCommunityService.getOne(queryWrapper); if(idenCommunity != null){ obj.setCommunityId(idenCommunity.getId()); } obj.setType(type); obj.setCreateTime(new Date()); listDb.add(obj); } return this.idenCameraService.saveBatch(listDb); } return false; } }