|
@@ -2,23 +2,43 @@ package com.iden.bms.service;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
import cn.hutool.core.collection.CollUtil;
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
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.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.iden.common.entity.IdenCamera;
|
|
import com.iden.common.entity.IdenCamera;
|
|
|
|
+import com.iden.common.entity.IdenCommunity;
|
|
import com.iden.common.enums.CameraTypeEnum;
|
|
import com.iden.common.enums.CameraTypeEnum;
|
|
import com.iden.common.enums.ConnectStatusEnum;
|
|
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.IdenCameraService;
|
|
|
|
+import com.iden.common.service.IdenCommunityService;
|
|
import com.iden.common.util.MyBeanUtils;
|
|
import com.iden.common.util.MyBeanUtils;
|
|
import com.iden.common.vo.CameraVO;
|
|
import com.iden.common.vo.CameraVO;
|
|
import com.iden.common.vo.PageReqVO;
|
|
import com.iden.common.vo.PageReqVO;
|
|
import com.iden.common.vo.UserLoginedConvertVO;
|
|
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.beans.BeanUtils;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
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.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -33,6 +53,8 @@ public class CameraService {
|
|
|
|
|
|
@Resource
|
|
@Resource
|
|
private IdenCameraService idenCameraService;
|
|
private IdenCameraService idenCameraService;
|
|
|
|
+ @Resource
|
|
|
|
+ private IdenCommunityService idenCommunityService;
|
|
|
|
|
|
public Integer countCamera(String type, String district, String subdistrict, Long communityId, String name, UserLoginedConvertVO loginUser) {
|
|
public Integer countCamera(String type, String district, String subdistrict, Long communityId, String name, UserLoginedConvertVO loginUser) {
|
|
QueryWrapper<IdenCamera> queryWrapper = new QueryWrapper<>();
|
|
QueryWrapper<IdenCamera> queryWrapper = new QueryWrapper<>();
|
|
@@ -68,6 +90,13 @@ public class CameraService {
|
|
BeanUtils.copyProperties(item,resVO);
|
|
BeanUtils.copyProperties(item,resVO);
|
|
resVO.setStatusName(ConnectStatusEnum.getValueToName(resVO.getCode()));
|
|
resVO.setStatusName(ConnectStatusEnum.getValueToName(resVO.getCode()));
|
|
resVO.setTypeName(CameraTypeEnum.getValueToName(resVO.getType()));
|
|
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);
|
|
list.add(resVO);
|
|
});
|
|
});
|
|
results.setRecords(list);
|
|
results.setRecords(list);
|
|
@@ -141,4 +170,116 @@ public class CameraService {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ 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.getCode()));
|
|
|
|
+ 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.setContentType("application/vnd.ms-excel");
|
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
|
+ final String fileName = URLEncoder.encode("摄像头表", "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);
|
|
|
|
+ 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());
|
|
|
|
+ } else {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ obj.setType(type);
|
|
|
|
+ listDb.add(obj);
|
|
|
|
+ }
|
|
|
|
+ return this.idenCameraService.saveBatch(listDb);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
}
|
|
}
|