123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- 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<IdenCamera> 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<CameraVO> listCamera(String type, String district, String subdistrict, Long communityId, String name, UserLoginedConvertVO loginUser, PageReqVO pageReqVo) {
- IPage<IdenCamera> page = new Page<>(pageReqVo.getCurrent(), pageReqVo.getPageSize());
- QueryWrapper<IdenCamera> 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<IdenCamera> pageRes = this.idenCameraService.page(page, queryWrapper);
- IPage<CameraVO> results = new Page<>(pageRes.getCurrent(),pageRes.getSize(),pageRes.getTotal());
- if(CollUtil.isNotEmpty(pageRes.getRecords())){
- List<CameraVO> 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<IdenCamera> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().eq(IdenCamera::getName,vo.getName());
- if(idenCameraService.count(queryWrapper) > 0){
- return 1;
- }
- QueryWrapper<IdenCamera> 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<IdenCamera> 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<IdenCamera> list = this.idenCameraService.list(queryWrapper);
- List<CameraVO> 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<CameraVO> 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<CameraVO> listParser = excelParser.parse(sheet, new CameraVO());
- if (CollectionUtil.isNotEmpty(listParser)) {
- List<IdenCamera> 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<IdenCommunity> 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;
- }
- }
|