|
@@ -0,0 +1,135 @@
|
|
|
|
+package com.iden.bms.service;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
|
+
|
|
|
|
+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.IdenSysLog;
|
|
|
|
+
|
|
|
|
+import com.iden.common.service.IdenSysLogService;
|
|
|
|
+import com.iden.common.service.IdenCommunityService;
|
|
|
|
+import com.iden.common.vo.SysLogVO;
|
|
|
|
+import com.iden.common.vo.PageReqVO;
|
|
|
|
+import com.iden.common.vo.UserLoginedConvertVO;
|
|
|
|
+
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ *
|
|
|
|
+ * @author makejava
|
|
|
|
+ * @since 2021-05-21 00:08:38
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+public class SysLogService {
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private IdenSysLogService idenSysLogService;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 查询摄像头列表 username,logType,createTime,
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public IPage<SysLogVO> listSysLog(String username, String logType, String createTime, UserLoginedConvertVO loginUser, PageReqVO pageReqVo) {
|
|
|
|
+
|
|
|
|
+ IPage<IdenSysLog> page = new Page<>(pageReqVo.getCurrent(), pageReqVo.getPageSize());
|
|
|
|
+ QueryWrapper<IdenSysLog> queryWrapper = new QueryWrapper<>();
|
|
|
|
+ queryWrapper.lambda().eq(StrUtil.isNotEmpty(username),IdenSysLog::getUsername,username)
|
|
|
|
+ .eq(StrUtil.isNotEmpty(logType),IdenSysLog::getLogType,logType)
|
|
|
|
+ .orderByDesc(IdenSysLog::getCreateTime);
|
|
|
|
+ if(!StringUtils.isEmpty(createTime)) {
|
|
|
|
+ queryWrapper.apply("DATE_FORMAT(create_time,'%Y-%m-%d') = {0}", createTime);
|
|
|
|
+ }
|
|
|
|
+ IPage<IdenSysLog> pageRes = this.idenSysLogService.page(page, queryWrapper);
|
|
|
|
+ IPage<SysLogVO> results = new Page<>(pageRes.getCurrent(),pageRes.getSize(),pageRes.getTotal());
|
|
|
|
+ if(CollUtil.isNotEmpty(pageRes.getRecords())){
|
|
|
|
+ List<SysLogVO> list = new ArrayList<>();
|
|
|
|
+ pageRes.getRecords().forEach(item -> {
|
|
|
|
+ SysLogVO resVO = new SysLogVO();
|
|
|
|
+ BeanUtils.copyProperties(item,resVO);
|
|
|
|
+ list.add(resVO);
|
|
|
|
+ });
|
|
|
|
+ results.setRecords(list);
|
|
|
|
+ }
|
|
|
|
+ return results;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void exportToExcel(String username, String logType, String createTime, UserLoginedConvertVO loginUser, HttpServletResponse response) throws Exception {
|
|
|
|
+
|
|
|
|
+ QueryWrapper<IdenSysLog> queryWrapper = new QueryWrapper<>();
|
|
|
|
+ queryWrapper.lambda().eq(StrUtil.isNotEmpty(username),IdenSysLog::getUsername,username)
|
|
|
|
+ .eq(StrUtil.isNotEmpty(logType),IdenSysLog::getLogType,logType)
|
|
|
|
+ .orderByDesc(IdenSysLog::getCreateTime);
|
|
|
|
+ if(!StringUtils.isEmpty(createTime)) {
|
|
|
|
+ queryWrapper.apply("DATE_FORMAT(create_time,'%Y-%m-%d') = {0}", createTime);
|
|
|
|
+ }
|
|
|
|
+ List<IdenSysLog> list = this.idenSysLogService.list(queryWrapper);
|
|
|
|
+ List<SysLogVO> records = new ArrayList<>();
|
|
|
|
+ if(CollUtil.isNotEmpty(list)) {
|
|
|
|
+ list.forEach(item -> {
|
|
|
|
+ SysLogVO resVO = new SysLogVO();
|
|
|
|
+ BeanUtils.copyProperties(item, resVO);
|
|
|
|
+ records.add(resVO);
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ 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("SysLog", "UTF-8");
|
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename=" + fileName +"_"+ System.currentTimeMillis() + ".xlsx");
|
|
|
|
+
|
|
|
|
+ // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
|
|
|
|
+ // {} 代表普通变量 {.} 代表是list的变量
|
|
|
|
+
|
|
|
|
+ excelTemplateIs = this.getClass().getResourceAsStream("/static/template/export/IdenSysLogExportTemplate.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();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|