From 56e93a150b368ef08bf1fc567375f965f96e62bf Mon Sep 17 00:00:00 2001 From: zgn <1215577830@qq.com> Date: Mon, 28 Jun 2021 00:33:10 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 +- .../zgn/fazhi/controller/ExcelController.java | 63 +++++-- .../listener/BeforeExaminationListener.java | 3 +- .../zgn/fazhi/listener/BeforeListener.java | 28 ++-- .../listener/BeforeUserInfoListener.java | 157 ++++++++++++++++++ .../zgn/fazhi/listener/UserInfoListener.java | 81 +++++++++ .../com/zgn/fazhi/mapper/SchoolMapper.java | 12 ++ .../com/zgn/fazhi/pojo/po/Appointment.java | 2 +- .../java/com/zgn/fazhi/pojo/po/Interval.java | 2 +- .../java/com/zgn/fazhi/pojo/po/School.java | 26 +++ .../java/com/zgn/fazhi/pojo/po/UserInfo.java | 4 +- .../com/zgn/fazhi/service/RoleService.java | 7 + .../com/zgn/fazhi/service/SchoolService.java | 15 ++ .../zgn/fazhi/service/UserInfoService.java | 9 + .../fazhi/service/impl/RoleServiceImpl.java | 14 ++ .../fazhi/service/impl/SchoolServiceImpl.java | 26 +++ .../service/impl/UserInfoServiceImpl.java | 41 +++++ 17 files changed, 469 insertions(+), 33 deletions(-) create mode 100644 src/main/java/com/zgn/fazhi/listener/BeforeUserInfoListener.java create mode 100644 src/main/java/com/zgn/fazhi/listener/UserInfoListener.java create mode 100644 src/main/java/com/zgn/fazhi/mapper/SchoolMapper.java create mode 100644 src/main/java/com/zgn/fazhi/pojo/po/School.java create mode 100644 src/main/java/com/zgn/fazhi/service/SchoolService.java create mode 100644 src/main/java/com/zgn/fazhi/service/impl/SchoolServiceImpl.java diff --git a/README.md b/README.md index 0dd908b..b0a775c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,14 @@ -# 工程简介 +# 法治小程序后端模块 +#### 练习模块 +#### 考试模块 +#### 用户模块 +#### 预约模块 +#### 法治文档学习模块 +#### 视频以及图片 +#### 评价模块 +#### 成长等级模块 + + diff --git a/src/main/java/com/zgn/fazhi/controller/ExcelController.java b/src/main/java/com/zgn/fazhi/controller/ExcelController.java index 86e9277..9c72343 100644 --- a/src/main/java/com/zgn/fazhi/controller/ExcelController.java +++ b/src/main/java/com/zgn/fazhi/controller/ExcelController.java @@ -2,19 +2,14 @@ package com.zgn.fazhi.controller; import com.alibaba.excel.EasyExcel; import com.zgn.fazhi.factory.ResultFactory; -import com.zgn.fazhi.listener.BeforeExaminationListener; -import com.zgn.fazhi.listener.BeforeListener; -import com.zgn.fazhi.listener.ExaminationDataListener; -import com.zgn.fazhi.listener.PracticeDataListener; +import com.zgn.fazhi.listener.*; import com.zgn.fazhi.mapper.PracticeAnswerMapper; import com.zgn.fazhi.mapper.PracticeMapper; import com.zgn.fazhi.pojo.po.Examination; +import com.zgn.fazhi.pojo.po.School; import com.zgn.fazhi.pojo.po.SubjectType; import com.zgn.fazhi.result.Result; -import com.zgn.fazhi.service.ExaminationService; -import com.zgn.fazhi.service.GradeService; -import com.zgn.fazhi.service.PracticeService; -import com.zgn.fazhi.service.SubjectTypeService; +import com.zgn.fazhi.service.*; import com.zgn.fazhi.utils.RedisUtil; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; @@ -61,13 +56,20 @@ public class ExcelController { @Autowired private RedisTemplate redisTemplate; + @Autowired + private RoleService roleService; + + @Autowired + private SchoolService schoolService; + + @Autowired + private UserInfoService userInfoService; + @PostMapping(value = "/api/uploadPractice", consumes = "multipart/*", headers = "content-type=multipart/form-data") @ApiOperation("上传练习题excel") @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType = "_file", name = "上传文件", required = true)}) public Result practiceFile(@ApiParam(value = "上传练习题", required = true) @RequestPart("file") MultipartFile files) throws IOException { - - // 判断文件是否为空 if (files.isEmpty()) { System.out.println("文件为空"); @@ -84,7 +86,7 @@ public class ExcelController { if (isError()) { try { return ResultFactory.buildResult(402, "数据有误", redisUtil.lGet("testList", 0, -1)); - }finally { + } finally { if (redisUtil.hasKey("testList")) { System.out.println("清除Redis缓存"); redisUtil.del("testList"); @@ -122,7 +124,7 @@ public class ExcelController { if (isError()) { try { return ResultFactory.buildResult(402, "数据有误", redisUtil.lGet("testList", 0, -1)); - }finally { + } finally { if (redisUtil.hasKey("testList")) { System.out.println("清除Redis缓存"); redisUtil.del("testList"); @@ -137,6 +139,43 @@ public class ExcelController { } } + @ApiOperation("上传用户excel") + @PostMapping(value = "/api/uploadUser", consumes = "multipart/*", headers = "content-type=multipart/form-data") + @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType = "_file", name = "上传用户", required = true)}) + public Result userFile(@ApiParam(value = "上传用户", required = true) @RequestPart("file") MultipartFile file) throws IOException { + // 判断文件是否为空 + if (file.isEmpty()) { + System.out.println("文件为空"); + return ResultFactory.buildFailResult("文件为空"); + } else { + try { + System.out.println("执行redis"); + redisTemplate.opsForValue().get("skr"); + } catch (RedisConnectionFailureException e) { + LOGGER.error("插入练习题出现连接异常: " + e.toString()); + return ResultFactory.buildFailResult("连接redis出现异常,请联系运维!"); + } + // 校验用户数据 + EasyExcel.read(file.getInputStream(), new BeforeUserInfoListener(schoolService, roleService, redisUtil)).headRowNumber(1).sheet().doRead(); + if (isError()) { + try { + return ResultFactory.buildResult(402, "数据有误", redisUtil.lGet("testList", 0, -1)); + } finally { + if (redisUtil.hasKey("testList")) { + System.out.println("清除Redis缓存"); + redisUtil.del("testList"); + } + } + } + // 如果数据无误,写入数据库 + EasyExcel.read(file.getInputStream(), new UserInfoListener(userInfoService)).headRowNumber(1).sheet().doRead(); + + System.out.println("文件不为空"); + return ResultFactory.buildSuccessResult("数据无误,导入成功"); + } + } + + private boolean isError() { return redisUtil.lGetListSize("testList") != 0; } diff --git a/src/main/java/com/zgn/fazhi/listener/BeforeExaminationListener.java b/src/main/java/com/zgn/fazhi/listener/BeforeExaminationListener.java index a8ad400..943528b 100644 --- a/src/main/java/com/zgn/fazhi/listener/BeforeExaminationListener.java +++ b/src/main/java/com/zgn/fazhi/listener/BeforeExaminationListener.java @@ -56,14 +56,13 @@ public class BeforeExaminationListener extends AnalysisEventListener data, AnalysisContext context) { LOGGER.info("解析到一条数据:{}", data); + list.add(data); // 每5条记录校验一次合法性 if (list.size() >= BATCH_COUNT) { checkList(list); // 存储完成清理 list list.clear(); } - list.add(data); - } @SneakyThrows diff --git a/src/main/java/com/zgn/fazhi/listener/BeforeListener.java b/src/main/java/com/zgn/fazhi/listener/BeforeListener.java index 9d7b2e1..8aa1a1b 100644 --- a/src/main/java/com/zgn/fazhi/listener/BeforeListener.java +++ b/src/main/java/com/zgn/fazhi/listener/BeforeListener.java @@ -62,6 +62,7 @@ public class BeforeListener extends AnalysisEventListener> @Override public void invoke(Map data, AnalysisContext context) { LOGGER.info("解析到一条数据:{}", data); + list.add(data); // 每5条记录校验一次合法性 if (list.size() >= BATCH_COUNT) { checkList(list); @@ -69,7 +70,6 @@ public class BeforeListener extends AnalysisEventListener> list.clear(); } - list.add(data); } @@ -103,19 +103,19 @@ public class BeforeListener extends AnalysisEventListener> for (Map map : list) { int isNull = 0; -// nullOut: -// for (int i = 0; i < map.size(); i++) { -// if (map.get(i) == null) { -// isNull = 1; -// continue nullOut; -// } -// } -// // 如果map中为null则校验异常 -// if (isNull == 1){ -// redisUtil.lSet("testList", map); -// LOGGER.info("缓存已有: " + redisUtil.lGet("testList", 0, -1)); -// continue out; -// } + nullOut: + for (int i = 0; i < map.size(); i++) { + if (map.get(i) == null) { + isNull = 1; + continue nullOut; + } + } + // 如果map中为null则校验异常 + if (isNull == 1){ + redisUtil.lSet("testList", map); + LOGGER.info("缓存已有: " + redisUtil.lGet("testList", 0, -1)); + continue out; + } // 如果为判断题且长度大于7则不合法 if (Integer.valueOf(map.get(1)).equals(pdid)) { diff --git a/src/main/java/com/zgn/fazhi/listener/BeforeUserInfoListener.java b/src/main/java/com/zgn/fazhi/listener/BeforeUserInfoListener.java new file mode 100644 index 0000000..2d28c21 --- /dev/null +++ b/src/main/java/com/zgn/fazhi/listener/BeforeUserInfoListener.java @@ -0,0 +1,157 @@ +package com.zgn.fazhi.listener; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; +import com.zgn.fazhi.pojo.po.Grade; +import com.zgn.fazhi.pojo.po.Role; +import com.zgn.fazhi.pojo.po.School; +import com.zgn.fazhi.pojo.po.SubjectType; +import com.zgn.fazhi.service.GradeService; +import com.zgn.fazhi.service.RoleService; +import com.zgn.fazhi.service.SchoolService; +import com.zgn.fazhi.service.SubjectTypeService; +import com.zgn.fazhi.utils.RedisUtil; +import lombok.SneakyThrows; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.StringUtils; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 检测excel表格是否正确 + * + * @author ydw + */ + +public class BeforeUserInfoListener extends AnalysisEventListener> { + + + + private static final Logger LOGGER = LoggerFactory.getLogger(BeforeUserInfoListener.class); + /** + * 每隔10条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 + */ + private static final int BATCH_COUNT = 10; + Map headMap = new HashMap<>(); + List> list = new ArrayList>(); + private SchoolService schoolService; + private RoleService roleService; + private RedisUtil redisUtil; + + + public BeforeUserInfoListener(SchoolService schoolService, RoleService roleService, RedisUtil redisUtil) { + this.schoolService = schoolService; + this.roleService = roleService; + this.redisUtil = redisUtil; + } + + + /** + * 检查每一条数据的合法性 + */ + @SneakyThrows + @Override + public void invoke(Map data, AnalysisContext context) { + LOGGER.info("解析到一条数据:{}", data); + // 每10条记录校验一次合法性 + list.add(data); + if (list.size() >= BATCH_COUNT) { + checkList(list); + // 存储完成清理 list + list.clear(); + } + } + + @SneakyThrows + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + checkList(list); + System.out.println("打印头" + headMap); + LOGGER.info("所有的数据校验完毕!" + list.size()); + } + + /** + * 检查数据合法性 + * + * @param list 数据数组 + */ + private void checkList(List> list) { + LOGGER.info("执行checkList校验用户数据中 : " + list); + // 从数据库中查询所有的角色和学校,校验是否合法 + // 角色只能为数据库中的角色、学校要么为null要么为学校列表中的数据 + List allRole = roleService.getAllRole(); + List allSchool = schoolService.getAllSchool(); + + // 遍历该10条数据 + out: + for (Map map : list) { + int isNull = 0; + + int isRole = 0; + // 判断是否是数据库已有该角色 + for (Role role : allRole) { + if (!(StringUtils.isEmpty(role) && map.isEmpty())) { + if (role.getNameZh().equals(map.get(4))) { + // 如果在角色范围内 + isRole = 1; + break; + } + } + } + if (isRole == 0) { + // 如果不在角色范围,将该数据放入redis缓存中,用list + string + LOGGER.info("当前数据不合法 : " + map.toString()); + redisUtil.lSet("testList", map); + LOGGER.info("缓存已有: " + redisUtil.lGet("testList", 0, -1)); + continue out; + } + + String schoolName = map.get(5); + LOGGER.info("school : " + schoolName); + + if (map.get(5) == null){ + continue out; + } + + int isSchool = 0; + // 判断是否在数据库中有该学校或为null + for (School school : allSchool) { + if (!(StringUtils.isEmpty(school) && map.isEmpty())) { + if (school.getName().equals(map.get(5))) { + // 如果在学校范围内或为null + isSchool = 1; + break; + } + } + } + if (isSchool == 0) { + // 如果不在学校范围,将该数据放入redis缓存中,用list + string + LOGGER.info("当前数据不合法 : " + map.toString()); + redisUtil.lSet("testList", map); + LOGGER.info("缓存已有: " + redisUtil.lGet("testList", 0, -1)); + continue out; + } + + + } + + + } + + /** + * 读取表头数据 + * 这里会一行行的返回头 + * 监听器只需要重写这个方法就可以读取到头信息 + * + * @param headMap 表头map + */ + @Override + public void invokeHeadMap(Map headMap, AnalysisContext context) { + this.headMap = headMap; + } + +} diff --git a/src/main/java/com/zgn/fazhi/listener/UserInfoListener.java b/src/main/java/com/zgn/fazhi/listener/UserInfoListener.java new file mode 100644 index 0000000..7b5b007 --- /dev/null +++ b/src/main/java/com/zgn/fazhi/listener/UserInfoListener.java @@ -0,0 +1,81 @@ +package com.zgn.fazhi.listener; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.zgn.fazhi.pojo.vo.PracticeExcels; +import com.zgn.fazhi.service.PracticeService; +import com.zgn.fazhi.service.UserInfoService; +import lombok.SneakyThrows; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * 直接用map接收数据 + * + * @author ydw + */ +public class UserInfoListener extends AnalysisEventListener> { + private static final Logger LOGGER = LoggerFactory.getLogger(UserInfoListener.class); + + private UserInfoService userInfoService; + + /** + * 每隔10条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 + */ + private static final int BATCH_COUNT = 10; + Map headMap = new HashMap<>(); + List> list = new ArrayList>(); + + @SneakyThrows + @Override + public void invoke(Map data, AnalysisContext context) { + LOGGER.info("解析到一条数据:{}", data); + // 每10条记录刷新一次 + if (list.size() >= BATCH_COUNT) { + saveData(list); + // 存储完成清理 list + list.clear(); + } + list.add(data); + } + + public UserInfoListener(UserInfoService userInfoService) { + this.userInfoService = userInfoService; + } + + @SneakyThrows + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + saveData(list); + System.out.println("打印头"+headMap); + LOGGER.info("所有的数据存储完毕!"+list.size()); + + } + + /** + * 加上存储数据库 + */ + private void saveData(List> list) { + LOGGER.info("{}条数据,开始存储数据库!长度为", list.size()); + userInfoService.addUserList(list); + } + + /** + * 读取表头数据 + * 这里会一行行的返回头 + * 监听器只需要重写这个方法就可以读取到头信息 + * + * @param headMap 表头map + */ + @Override + public void invokeHeadMap(Map headMap, AnalysisContext context) { + this.headMap = headMap; + } + +} diff --git a/src/main/java/com/zgn/fazhi/mapper/SchoolMapper.java b/src/main/java/com/zgn/fazhi/mapper/SchoolMapper.java new file mode 100644 index 0000000..1f2d837 --- /dev/null +++ b/src/main/java/com/zgn/fazhi/mapper/SchoolMapper.java @@ -0,0 +1,12 @@ +package com.zgn.fazhi.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.zgn.fazhi.pojo.po.School; +import org.springframework.stereotype.Repository; + +/** + * @author ydw + */ +@Repository +public interface SchoolMapper extends BaseMapper { +} diff --git a/src/main/java/com/zgn/fazhi/pojo/po/Appointment.java b/src/main/java/com/zgn/fazhi/pojo/po/Appointment.java index 5a6c5c5..4eae6ba 100644 --- a/src/main/java/com/zgn/fazhi/pojo/po/Appointment.java +++ b/src/main/java/com/zgn/fazhi/pojo/po/Appointment.java @@ -32,7 +32,7 @@ public class Appointment implements Serializable { private Integer id; @ApiModelProperty(value = "预约日期") - private LocalDateTime time; + private LocalDateTime goDate; @ApiModelProperty(value = "预约时段id,比如1为上午,2为中午...") private Integer intervalId; diff --git a/src/main/java/com/zgn/fazhi/pojo/po/Interval.java b/src/main/java/com/zgn/fazhi/pojo/po/Interval.java index eea05a8..b075a9b 100644 --- a/src/main/java/com/zgn/fazhi/pojo/po/Interval.java +++ b/src/main/java/com/zgn/fazhi/pojo/po/Interval.java @@ -26,7 +26,7 @@ public class Interval implements Serializable { private Integer id; @ApiModelProperty(value = "时间段描述") - private String describe; + private String info; } diff --git a/src/main/java/com/zgn/fazhi/pojo/po/School.java b/src/main/java/com/zgn/fazhi/pojo/po/School.java new file mode 100644 index 0000000..f512559 --- /dev/null +++ b/src/main/java/com/zgn/fazhi/pojo/po/School.java @@ -0,0 +1,26 @@ +package com.zgn.fazhi.pojo.po; + + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = false) +@ApiModel(value="学校", description="") +public class School { + + private static final long serialVersionUID = 1L; + + @ApiModelProperty(value = "学校id") + @TableId(value = "id", type = IdType.AUTO) + private Integer id; + + @ApiModelProperty("学校名称") + private String schoolName; + + +} diff --git a/src/main/java/com/zgn/fazhi/pojo/po/UserInfo.java b/src/main/java/com/zgn/fazhi/pojo/po/UserInfo.java index 014300d..7022034 100644 --- a/src/main/java/com/zgn/fazhi/pojo/po/UserInfo.java +++ b/src/main/java/com/zgn/fazhi/pojo/po/UserInfo.java @@ -49,8 +49,8 @@ public class UserInfo implements Serializable { @ApiModelProperty(value = "角色id") private Integer roleId; - @ApiModelProperty(value = "学校名称") - private String school; + @ApiModelProperty(value = "学校id") + private Integer schoolId; @ApiModelProperty(value = "年级") private Integer grade; diff --git a/src/main/java/com/zgn/fazhi/service/RoleService.java b/src/main/java/com/zgn/fazhi/service/RoleService.java index e7442e2..6162d5b 100644 --- a/src/main/java/com/zgn/fazhi/service/RoleService.java +++ b/src/main/java/com/zgn/fazhi/service/RoleService.java @@ -3,6 +3,8 @@ package com.zgn.fazhi.service; import com.zgn.fazhi.pojo.po.Role; import com.baomidou.mybatisplus.extension.service.IService; +import java.util.List; + /** *

* 服务类 @@ -12,5 +14,10 @@ import com.baomidou.mybatisplus.extension.service.IService; * @since 2021-06-25 */ public interface RoleService extends IService { + /** + * 查找所有角色 + * @return 返回角色集合 + */ + List getAllRole(); } diff --git a/src/main/java/com/zgn/fazhi/service/SchoolService.java b/src/main/java/com/zgn/fazhi/service/SchoolService.java new file mode 100644 index 0000000..35d5562 --- /dev/null +++ b/src/main/java/com/zgn/fazhi/service/SchoolService.java @@ -0,0 +1,15 @@ +package com.zgn.fazhi.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.zgn.fazhi.pojo.po.School; + +import java.util.List; + +public interface SchoolService extends IService { + /** + * 获取所有学校 + * @return 返回所有学校的数组 + */ + List getAllSchool(); + +} diff --git a/src/main/java/com/zgn/fazhi/service/UserInfoService.java b/src/main/java/com/zgn/fazhi/service/UserInfoService.java index 8e7334b..59c58d1 100644 --- a/src/main/java/com/zgn/fazhi/service/UserInfoService.java +++ b/src/main/java/com/zgn/fazhi/service/UserInfoService.java @@ -4,6 +4,9 @@ import com.zgn.fazhi.pojo.po.UserInfo; import com.baomidou.mybatisplus.extension.service.IService; import com.zgn.fazhi.result.Result; +import java.util.List; +import java.util.Map; + /** *

* 服务类 @@ -14,4 +17,10 @@ import com.zgn.fazhi.result.Result; */ public interface UserInfoService extends IService { Result addUser(UserInfo userInfo); + + /** + * 批量添加用户信息 + * @param list 用户信息数组 + */ + void addUserList(List> list); } diff --git a/src/main/java/com/zgn/fazhi/service/impl/RoleServiceImpl.java b/src/main/java/com/zgn/fazhi/service/impl/RoleServiceImpl.java index 8b93b60..1799145 100644 --- a/src/main/java/com/zgn/fazhi/service/impl/RoleServiceImpl.java +++ b/src/main/java/com/zgn/fazhi/service/impl/RoleServiceImpl.java @@ -4,8 +4,11 @@ import com.zgn.fazhi.pojo.po.Role; import com.zgn.fazhi.mapper.RoleMapper; import com.zgn.fazhi.service.RoleService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; + /** *

* 服务实现类 @@ -17,4 +20,15 @@ import org.springframework.stereotype.Service; @Service public class RoleServiceImpl extends ServiceImpl implements RoleService { + @Autowired + private RoleMapper roleMapper; + + /** + * 查找所有角色 + * @return 返回角色集合 + */ + @Override + public List getAllRole() { + return roleMapper.selectList(null); + } } diff --git a/src/main/java/com/zgn/fazhi/service/impl/SchoolServiceImpl.java b/src/main/java/com/zgn/fazhi/service/impl/SchoolServiceImpl.java new file mode 100644 index 0000000..d3009b1 --- /dev/null +++ b/src/main/java/com/zgn/fazhi/service/impl/SchoolServiceImpl.java @@ -0,0 +1,26 @@ +package com.zgn.fazhi.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.zgn.fazhi.mapper.SchoolMapper; +import com.zgn.fazhi.pojo.po.School; +import com.zgn.fazhi.service.SchoolService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class SchoolServiceImpl extends ServiceImpl implements SchoolService { + + @Autowired + private SchoolMapper schoolMapper; + + /** + * 获取所有学校 + * @return 返回所有学校的数组 + */ + @Override + public List getAllSchool() { + return schoolMapper.selectList(null); + } +} diff --git a/src/main/java/com/zgn/fazhi/service/impl/UserInfoServiceImpl.java b/src/main/java/com/zgn/fazhi/service/impl/UserInfoServiceImpl.java index e1a6efa..6c058d4 100644 --- a/src/main/java/com/zgn/fazhi/service/impl/UserInfoServiceImpl.java +++ b/src/main/java/com/zgn/fazhi/service/impl/UserInfoServiceImpl.java @@ -2,6 +2,7 @@ package com.zgn.fazhi.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.zgn.fazhi.factory.ResultFactory; +import com.zgn.fazhi.listener.UserInfoListener; import com.zgn.fazhi.pojo.po.UserInfo; import com.zgn.fazhi.mapper.UserInfoMapper; import com.zgn.fazhi.result.Result; @@ -9,10 +10,15 @@ import com.zgn.fazhi.service.UserInfoService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.shiro.crypto.SecureRandomNumberGenerator; import org.apache.shiro.crypto.hash.SimpleHash; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; +import java.util.List; +import java.util.Map; + /** *

* 服务实现类 @@ -23,6 +29,10 @@ import org.springframework.util.StringUtils; */ @Service public class UserInfoServiceImpl extends ServiceImpl implements UserInfoService { + + private static final Logger LOGGER = LoggerFactory.getLogger(UserInfoServiceImpl.class); + + @Autowired private UserInfoMapper userMapper; @@ -79,4 +89,35 @@ public class UserInfoServiceImpl extends ServiceImpl i } + + /** + * 批量添加用户信息 + * @param list 用户信息数组 + */ + @Override + public void addUserList(List> list) { + LOGGER.info("开始导入用户数据"); + for (Map map : list) { + UserInfo userInfo = new UserInfo(); + // 账号 + userInfo.setUsername(map.get(0)); + // 密码 + userInfo.setPassword(map.get(1)); + // 姓名 + userInfo.setName(map.get(2)); + // 手机号 + if (map.get(3) != null){ + userInfo.setPhone(Integer.valueOf(map.get(3))); + } + + // 角色(需要判断,是角色id还是角色名称) + userInfo.setRoleId(Integer.valueOf(map.get(4))); + // 学校 (需要判断,是学校id还是学校名称) + if (map.get(4) != null){ + userInfo.setSchoolId(Integer.valueOf(map.get(5))); + } + + } + + } } -- Gitee From 0b7f808c70b76bc76663b10909ad2acb10d3a961 Mon Sep 17 00:00:00 2001 From: zgn <1215577830@qq.com> Date: Mon, 28 Jun 2021 11:10:25 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E7=94=A8=E6=88=B7=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../listener/BeforeUserInfoListener.java | 2 +- .../java/com/zgn/fazhi/pojo/po/UserInfo.java | 2 +- .../com/zgn/fazhi/service/RoleService.java | 6 ++ .../com/zgn/fazhi/service/SchoolService.java | 6 ++ .../fazhi/service/impl/RoleServiceImpl.java | 8 +++ .../fazhi/service/impl/SchoolServiceImpl.java | 8 +++ .../service/impl/UserInfoServiceImpl.java | 59 ++++++++++++++++--- 7 files changed, 80 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/zgn/fazhi/listener/BeforeUserInfoListener.java b/src/main/java/com/zgn/fazhi/listener/BeforeUserInfoListener.java index 2d28c21..fd739af 100644 --- a/src/main/java/com/zgn/fazhi/listener/BeforeUserInfoListener.java +++ b/src/main/java/com/zgn/fazhi/listener/BeforeUserInfoListener.java @@ -121,7 +121,7 @@ public class BeforeUserInfoListener extends AnalysisEventListener { */ List getAllRole(); + /** + * 通过角色中文名查找角色 + * @return 返回角色 + */ + Role getRoleByNameZh(String name); + } diff --git a/src/main/java/com/zgn/fazhi/service/SchoolService.java b/src/main/java/com/zgn/fazhi/service/SchoolService.java index 35d5562..0457e77 100644 --- a/src/main/java/com/zgn/fazhi/service/SchoolService.java +++ b/src/main/java/com/zgn/fazhi/service/SchoolService.java @@ -12,4 +12,10 @@ public interface SchoolService extends IService { */ List getAllSchool(); + /** + * 通过name查找学校 + * @return 返回学校 + */ + School findByName(String name); + } diff --git a/src/main/java/com/zgn/fazhi/service/impl/RoleServiceImpl.java b/src/main/java/com/zgn/fazhi/service/impl/RoleServiceImpl.java index 1799145..17b1538 100644 --- a/src/main/java/com/zgn/fazhi/service/impl/RoleServiceImpl.java +++ b/src/main/java/com/zgn/fazhi/service/impl/RoleServiceImpl.java @@ -1,5 +1,6 @@ package com.zgn.fazhi.service.impl; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.zgn.fazhi.pojo.po.Role; import com.zgn.fazhi.mapper.RoleMapper; import com.zgn.fazhi.service.RoleService; @@ -31,4 +32,11 @@ public class RoleServiceImpl extends ServiceImpl implements Ro public List getAllRole() { return roleMapper.selectList(null); } + + @Override + public Role getRoleByNameZh(String name) { + QueryWrapper roleQueryWrapper = new QueryWrapper<>(); + roleQueryWrapper.eq("name_zh",name); + return roleMapper.selectOne(roleQueryWrapper); + } } diff --git a/src/main/java/com/zgn/fazhi/service/impl/SchoolServiceImpl.java b/src/main/java/com/zgn/fazhi/service/impl/SchoolServiceImpl.java index d3009b1..af93a24 100644 --- a/src/main/java/com/zgn/fazhi/service/impl/SchoolServiceImpl.java +++ b/src/main/java/com/zgn/fazhi/service/impl/SchoolServiceImpl.java @@ -1,5 +1,6 @@ package com.zgn.fazhi.service.impl; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.zgn.fazhi.mapper.SchoolMapper; import com.zgn.fazhi.pojo.po.School; @@ -23,4 +24,11 @@ public class SchoolServiceImpl extends ServiceImpl impleme public List getAllSchool() { return schoolMapper.selectList(null); } + + @Override + public School findByName(String name) { + QueryWrapper schoolQueryWrapper = new QueryWrapper<>(); + schoolQueryWrapper.eq("school_name", name); + return schoolMapper.selectOne(schoolQueryWrapper); + } } diff --git a/src/main/java/com/zgn/fazhi/service/impl/UserInfoServiceImpl.java b/src/main/java/com/zgn/fazhi/service/impl/UserInfoServiceImpl.java index 6c058d4..63cf06d 100644 --- a/src/main/java/com/zgn/fazhi/service/impl/UserInfoServiceImpl.java +++ b/src/main/java/com/zgn/fazhi/service/impl/UserInfoServiceImpl.java @@ -3,9 +3,13 @@ package com.zgn.fazhi.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.zgn.fazhi.factory.ResultFactory; import com.zgn.fazhi.listener.UserInfoListener; +import com.zgn.fazhi.pojo.po.Role; +import com.zgn.fazhi.pojo.po.School; import com.zgn.fazhi.pojo.po.UserInfo; import com.zgn.fazhi.mapper.UserInfoMapper; import com.zgn.fazhi.result.Result; +import com.zgn.fazhi.service.RoleService; +import com.zgn.fazhi.service.SchoolService; import com.zgn.fazhi.service.UserInfoService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.shiro.crypto.SecureRandomNumberGenerator; @@ -14,6 +18,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import java.util.List; @@ -21,7 +26,7 @@ import java.util.Map; /** *

- * 服务实现类 + * 服务实现类 *

* * @author wt @@ -32,10 +37,15 @@ public class UserInfoServiceImpl extends ServiceImpl i private static final Logger LOGGER = LoggerFactory.getLogger(UserInfoServiceImpl.class); + @Autowired + private RoleService roleService; @Autowired private UserInfoMapper userMapper; + @Autowired + private SchoolService schoolService; + @Override public Result addUser(UserInfo user) { @@ -92,9 +102,11 @@ public class UserInfoServiceImpl extends ServiceImpl i /** * 批量添加用户信息 + * * @param list 用户信息数组 */ @Override + @Transactional(rollbackFor = Exception.class) public void addUserList(List> list) { LOGGER.info("开始导入用户数据"); for (Map map : list) { @@ -106,18 +118,47 @@ public class UserInfoServiceImpl extends ServiceImpl i // 姓名 userInfo.setName(map.get(2)); // 手机号 - if (map.get(3) != null){ - userInfo.setPhone(Integer.valueOf(map.get(3))); + if (map.get(3) != null) { + userInfo.setPhone(map.get(3)); } - - // 角色(需要判断,是角色id还是角色名称) - userInfo.setRoleId(Integer.valueOf(map.get(4))); - // 学校 (需要判断,是学校id还是学校名称) - if (map.get(4) != null){ - userInfo.setSchoolId(Integer.valueOf(map.get(5))); + // 如果为数字 + if (isNumber(map.get(4))) { + LOGGER.info("当前role为数值,直接插入"); + userInfo.setRoleId(Integer.valueOf(map.get(4))); + }else{ + LOGGER.info("当前role为str,正在前往数据库查找对应角色id"); + Role roleByNameZh = roleService.getRoleByNameZh(map.get(4)); + LOGGER.info(roleByNameZh.toString()); + // 角色(需要判断,是角色id还是角色名称) + userInfo.setRoleId(roleByNameZh.getId()); } + String school = map.get(5); + if (!StringUtils.isEmpty(school)) { + // 如果为数字 + if (isNumber(school)) { + LOGGER.info("当前school为数值,直接插入"); + userInfo.setSchoolId(Integer.valueOf(school)); + } else { + LOGGER.info("当前school为str,正在前往数据库查找对应学校id"); + School school1 = schoolService.findByName(school); + // 学校 (需要判断,是学校id还是学校名称) + userInfo.setSchoolId(school1.getId()); + } + } + userMapper.insert(userInfo); + LOGGER.info("完成插入用户 : " + userInfo); } } + + public boolean isNumber(String str) { + try { + Integer.parseInt(str); + return true; + } catch (NumberFormatException e) { + System.out.println("异常:\"" + str + "\"不是数字/整数..."); + return false; + } + } } -- Gitee