Browse Source

1.补充获取studentCard逻辑;
2.新增访客记录导出接口;

codingliang 1 year ago
parent
commit
005081db04

+ 8 - 3
src/main/java/com/template/api/WelcomeVisitorControllerAPI.java

@@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 
 
 import java.util.List;
 import java.util.List;
 
 
@@ -28,15 +29,15 @@ public interface WelcomeVisitorControllerAPI {
 
 
     @PostMapping
     @PostMapping
     @ApiOperation(value = "新增访客记录")
     @ApiOperation(value = "新增访客记录")
-    CommonResult addVisitor(@Validated @RequestBody WelcomeVisitorRequest visitorRequest);
+    CommonResult addVisitor(@RequestParam String studentCard, @Validated @RequestBody WelcomeVisitorRequest visitorRequest);
 
 
     @GetMapping
     @GetMapping
     @ApiOperation(value = "获取当前用户访客记录")
     @ApiOperation(value = "获取当前用户访客记录")
-    CommonResult<WelcomeVisitorVO> getByCurUser();
+    CommonResult<WelcomeVisitorVO> getByCurUser(@RequestParam String studentCard);
 
 
     @DeleteMapping
     @DeleteMapping
     @ApiOperation(value = "删除当前用户的访客记录")
     @ApiOperation(value = "删除当前用户的访客记录")
-    CommonResult deleteByCurUser();
+    CommonResult deleteByCurUser(@RequestParam String studentCard);
 
 
     @GetMapping("admin/page")
     @GetMapping("admin/page")
     @ApiOperation(value = "访客记录分页")
     @ApiOperation(value = "访客记录分页")
@@ -53,4 +54,8 @@ public interface WelcomeVisitorControllerAPI {
     @DeleteMapping("admin/batch-del")
     @DeleteMapping("admin/batch-del")
     @ApiOperation(value = "删除当前用户的所有访客记录")
     @ApiOperation(value = "删除当前用户的所有访客记录")
     CommonResult deleteByIds(@RequestBody List<Long> ids);
     CommonResult deleteByIds(@RequestBody List<Long> ids);
+
+    @GetMapping("admin/export")
+    @ApiOperation(value = "删除当前用户的所有访客记录")
+    void export();
 }
 }

+ 12 - 7
src/main/java/com/template/controller/WelcomeVisitorController.java

@@ -28,14 +28,14 @@ public class WelcomeVisitorController implements WelcomeVisitorControllerAPI {
 
 
 
 
     @Override
     @Override
-    public CommonResult addVisitor(WelcomeVisitorRequest visitorRequest) {
-        welcomeVisitorService.addVisitor(visitorRequest);
+    public CommonResult addVisitor(String studentCard, WelcomeVisitorRequest visitorRequest) {
+        welcomeVisitorService.addVisitor(studentCard, visitorRequest);
         return CommonResult.ok();
         return CommonResult.ok();
     }
     }
 
 
     @Override
     @Override
-    public CommonResult<WelcomeVisitorVO> getByCurUser() {
-        WelcomeVisitorVO vo = welcomeVisitorService.getByCurUser();
+    public CommonResult<WelcomeVisitorVO> getByCurUser(String studentCard) {
+        WelcomeVisitorVO vo = welcomeVisitorService.getByCurUser(studentCard);
         return CommonResult.ok(vo);
         return CommonResult.ok(vo);
     }
     }
 
 
@@ -46,8 +46,8 @@ public class WelcomeVisitorController implements WelcomeVisitorControllerAPI {
     }
     }
 
 
     @Override
     @Override
-    public CommonResult deleteByCurUser() {
-        welcomeVisitorService.deleteByCurUser();
+    public CommonResult deleteByCurUser(String studentCard) {
+        welcomeVisitorService.deleteByCurUser(studentCard);
         return CommonResult.ok();
         return CommonResult.ok();
     }
     }
 
 
@@ -71,6 +71,11 @@ public class WelcomeVisitorController implements WelcomeVisitorControllerAPI {
     @Override
     @Override
     public CommonResult deleteByIds(List<Long> ids) {
     public CommonResult deleteByIds(List<Long> ids) {
         welcomeVisitorService.removeByIds(ids);
         welcomeVisitorService.removeByIds(ids);
-        return null;
+        return CommonResult.ok();
+    }
+
+    @Override
+    public void export() {
+        welcomeVisitorService.export();
     }
     }
 }
 }

+ 13 - 0
src/main/java/com/template/model/vo/WelcomeVisitorVO.java

@@ -1,5 +1,6 @@
 package com.template.model.vo;
 package com.template.model.vo;
 
 
+import com.alibaba.excel.annotation.ExcelProperty;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 import lombok.Data;
@@ -13,38 +14,50 @@ public class WelcomeVisitorVO implements Serializable {
     private static final long serialVersionUID = 1L;
     private static final long serialVersionUID = 1L;
 
 
     @ApiModelProperty(value = "主键ID")
     @ApiModelProperty(value = "主键ID")
+    @ExcelProperty("id")
     private Integer id;
     private Integer id;
 
 
     @ApiModelProperty(value = "学生身份证号")
     @ApiModelProperty(value = "学生身份证号")
+    @ExcelProperty("学生身份证号")
     private String studentCard;
     private String studentCard;
 
 
     @ApiModelProperty(value = "访客姓名")
     @ApiModelProperty(value = "访客姓名")
+    @ExcelProperty("访客姓名")
     private String name;
     private String name;
 
 
     @ApiModelProperty(value = "访客手机号")
     @ApiModelProperty(value = "访客手机号")
+    @ExcelProperty("访客手机号")
     private String phone;
     private String phone;
 
 
     @ApiModelProperty(value = "证件号")
     @ApiModelProperty(value = "证件号")
+    @ExcelProperty("证件号")
     private String cardId;
     private String cardId;
 
 
     @ApiModelProperty(value = "校区名称")
     @ApiModelProperty(value = "校区名称")
+    @ExcelProperty("校区名称")
     private String school;
     private String school;
 
 
     @ApiModelProperty(value = "到访开始时间")
     @ApiModelProperty(value = "到访开始时间")
+    @ExcelProperty("到访开始时间")
     private String startTime;
     private String startTime;
 
 
     @ApiModelProperty(value = "到访结束时间")
     @ApiModelProperty(value = "到访结束时间")
+    @ExcelProperty("到访结束时间")
     private String endTime;
     private String endTime;
 
 
     @ApiModelProperty(value = "车牌号")
     @ApiModelProperty(value = "车牌号")
+    @ExcelProperty("车牌号")
     private String carNumber;
     private String carNumber;
 
 
     @ApiModelProperty(value = "同行人数")
     @ApiModelProperty(value = "同行人数")
+    @ExcelProperty("同行人数")
     private Integer peerNum;
     private Integer peerNum;
 
 
     @ApiModelProperty(value = "访问事由")
     @ApiModelProperty(value = "访问事由")
+    @ExcelProperty("访问事由")
     private String visitorReason;
     private String visitorReason;
 
 
     @ApiModelProperty(value = "备注")
     @ApiModelProperty(value = "备注")
+    @ExcelProperty("备注")
     private String remark;
     private String remark;
 }
 }

+ 12 - 4
src/main/java/com/template/services/WelcomeVisitorService.java

@@ -1,7 +1,7 @@
 package com.template.services;
 package com.template.services;
 
 
-import com.template.model.pojo.WelcomeVisitor;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.template.model.pojo.WelcomeVisitor;
 import com.template.model.query.WelcomeVisitorQuery;
 import com.template.model.query.WelcomeVisitorQuery;
 import com.template.model.request.WelcomeVisitorRequest;
 import com.template.model.request.WelcomeVisitorRequest;
 import com.template.model.result.PageUtils;
 import com.template.model.result.PageUtils;
@@ -14,15 +14,17 @@ public interface WelcomeVisitorService extends IService<WelcomeVisitor> {
 
 
     /**
     /**
      * 新增访客记录
      * 新增访客记录
+     * @param studentCard 学生身份证号
      * @param visitorRequest 访客信息
      * @param visitorRequest 访客信息
      */
      */
-    void addVisitor(WelcomeVisitorRequest visitorRequest);
+    void addVisitor(String studentCard, WelcomeVisitorRequest visitorRequest);
 
 
     /**
     /**
      * 获取当前用户访客记录
      * 获取当前用户访客记录
+     * @param studentCard 学生身份证号
      * @return 访客信息
      * @return 访客信息
      */
      */
-    WelcomeVisitorVO getByCurUser();
+    WelcomeVisitorVO getByCurUser(String studentCard);
 
 
     /**
     /**
      * 根据记录id获取访客记录
      * 根据记录id获取访客记录
@@ -33,8 +35,9 @@ public interface WelcomeVisitorService extends IService<WelcomeVisitor> {
 
 
     /**
     /**
      * 删除当前用户的访客记录
      * 删除当前用户的访客记录
+     * @param studentCard 学生身份证号
      */
      */
-    void deleteByCurUser();
+    void deleteByCurUser(String studentCard);
 
 
     /**
     /**
      * 管理员新增访客记录
      * 管理员新增访客记录
@@ -48,4 +51,9 @@ public interface WelcomeVisitorService extends IService<WelcomeVisitor> {
      * @return 分页结果
      * @return 分页结果
      */
      */
     PageUtils<WelcomeVisitorVO> page(WelcomeVisitorQuery visitorQuery);
     PageUtils<WelcomeVisitorVO> page(WelcomeVisitorQuery visitorQuery);
+
+    /**
+     * 导出访客记录
+     */
+    void export();
 }
 }

+ 52 - 17
src/main/java/com/template/services/impl/WelcomeVisitorServiceImpl.java

@@ -1,7 +1,11 @@
 package com.template.services.impl;
 package com.template.services.impl;
 
 
+import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.core.util.StrUtil;
+import cn.hutool.core.util.URLUtil;
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.converters.longconverter.LongStringConverter;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -19,12 +23,22 @@ import com.template.model.result.PageUtils;
 import com.template.model.vo.WelcomeVisitorVO;
 import com.template.model.vo.WelcomeVisitorVO;
 import com.template.services.WelcomeVisitorService;
 import com.template.services.WelcomeVisitorService;
 import lombok.RequiredArgsConstructor;
 import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 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.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.Date;
 import java.util.List;
 import java.util.List;
 
 
+@Slf4j
 @Service
 @Service
 @RequiredArgsConstructor
 @RequiredArgsConstructor
 public class WelcomeVisitorServiceImpl extends ServiceImpl<WelcomeVisitorMapper, WelcomeVisitor> implements WelcomeVisitorService {
 public class WelcomeVisitorServiceImpl extends ServiceImpl<WelcomeVisitorMapper, WelcomeVisitor> implements WelcomeVisitorService {
@@ -33,13 +47,10 @@ public class WelcomeVisitorServiceImpl extends ServiceImpl<WelcomeVisitorMapper,
 
 
     @Override
     @Override
     @Transactional
     @Transactional
-    public void addVisitor(WelcomeVisitorRequest visitorRequest) {
-        // TODO 获取studyCard
-        String studyCard = "";
-
+    public void addVisitor(String studentCard, WelcomeVisitorRequest visitorRequest) {
         // 查询当前用户是否已有访客记录
         // 查询当前用户是否已有访客记录
         LambdaQueryWrapper<WelcomeVisitor> queryWrapper = new LambdaQueryWrapper<>();
         LambdaQueryWrapper<WelcomeVisitor> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(WelcomeVisitor::getStudentCard, studyCard);
+        queryWrapper.eq(WelcomeVisitor::getStudentCard, studentCard);
         if (this.count(queryWrapper) > 0) {
         if (this.count(queryWrapper) > 0) {
             throw new MyCustomException(ResponseStatusEnum.EXISTS);
             throw new MyCustomException(ResponseStatusEnum.EXISTS);
         }
         }
@@ -49,7 +60,7 @@ public class WelcomeVisitorServiceImpl extends ServiceImpl<WelcomeVisitorMapper,
         // 新增访客记录
         // 新增访客记录
         WelcomeVisitor visitor = new WelcomeVisitor();
         WelcomeVisitor visitor = new WelcomeVisitor();
         BeanUtils.copyProperties(visitorRequest, visitor);
         BeanUtils.copyProperties(visitorRequest, visitor);
-        visitor.setStudentCard(studyCard);
+        visitor.setStudentCard(studentCard);
 
 
         save(visitor);
         save(visitor);
 
 
@@ -58,12 +69,9 @@ public class WelcomeVisitorServiceImpl extends ServiceImpl<WelcomeVisitorMapper,
     }
     }
 
 
     @Override
     @Override
-    public WelcomeVisitorVO getByCurUser() {
-        // TODO 获取studyCard
-        String studyCard = "";
-
+    public WelcomeVisitorVO getByCurUser(String studentCard) {
         LambdaQueryWrapper<WelcomeVisitor> queryWrapper = new LambdaQueryWrapper<>();
         LambdaQueryWrapper<WelcomeVisitor> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(WelcomeVisitor::getStudentCard, studyCard);
+        queryWrapper.eq(WelcomeVisitor::getStudentCard, studentCard);
 
 
         WelcomeVisitor visitor = this.getOne(queryWrapper);
         WelcomeVisitor visitor = this.getOne(queryWrapper);
         if (ObjectUtil.isNull(visitor)) {
         if (ObjectUtil.isNull(visitor)) {
@@ -91,12 +99,9 @@ public class WelcomeVisitorServiceImpl extends ServiceImpl<WelcomeVisitorMapper,
     }
     }
 
 
     @Override
     @Override
-    public void deleteByCurUser() {
-        // TODO 获取studyCard
-        String studyCard = "";
-
+    public void deleteByCurUser(String studentCard) {
         LambdaQueryWrapper<WelcomeVisitor> queryWrapper = new LambdaQueryWrapper<>();
         LambdaQueryWrapper<WelcomeVisitor> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(WelcomeVisitor::getStudentCard, studyCard);
+        queryWrapper.eq(WelcomeVisitor::getStudentCard, studentCard);
 
 
         remove(queryWrapper);
         remove(queryWrapper);
     }
     }
@@ -134,6 +139,36 @@ public class WelcomeVisitorServiceImpl extends ServiceImpl<WelcomeVisitorMapper,
         return new PageUtils<>(welcomeVisitorVOS, (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
         return new PageUtils<>(welcomeVisitorVOS, (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
     }
     }
 
 
+    @Override
+    public void export() {
+        LambdaQueryWrapper<WelcomeVisitor> wrapper = new LambdaQueryWrapper<>();
+        wrapper.orderByDesc(WelcomeVisitor::getId);
+        List<WelcomeVisitor> welcomeVisitors = list(wrapper);
+
+        List<WelcomeVisitorVO> welcomeVisitorVOS = BeanUtil.copyListProperties(welcomeVisitors, WelcomeVisitorVO::new);
+
+        try {
+            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
+            HttpServletResponse response = ((ServletRequestAttributes) requestAttributes).getResponse();
+            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
+            response.setCharacterEncoding("UTF-8");
+
+            String excelName = "访客预约记录_";
+            excelName += DateUtil.format(new Date(), "yyyyMMddHHmmss");
+            String fileName = URLUtil.encode(excelName, StandardCharsets.UTF_8);
+            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
+
+            EasyExcel.write(response.getOutputStream(), WelcomeVisitorVO.class)
+                    .sheet("sheet1")
+                    .registerConverter(new LongStringConverter())
+                    .doWrite(welcomeVisitorVOS);
+        } catch (IOException e) {
+            log.error("访客记录导出失败:{}", e.getMessage());
+            throw new MyCustomException(ResponseStatusEnum.FAILED);
+        }
+    }
+
     /**
     /**
      * 新增车辆信息
      * 新增车辆信息
      * @param visitor  访客信息
      * @param visitor  访客信息