dzx 1 рік тому
батько
коміт
af2900bae4

+ 12 - 1
src/main/java/com/xjrsoft/module/teacher/controller/TeacherbaseManagerController.java

@@ -4,6 +4,7 @@ import cn.dev33.satoken.annotation.SaCheckPermission;
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
+import com.alibaba.excel.EasyExcel;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@@ -11,6 +12,7 @@ import com.github.yulichang.toolkit.MPJWrappers;
 import com.github.yulichang.wrapper.MPJLambdaWrapper;
 import com.xjrsoft.common.enums.GenderDictionaryEnum;
 import com.xjrsoft.common.model.result.R;
+import com.xjrsoft.common.model.result.RT;
 import com.xjrsoft.common.page.ConventPage;
 import com.xjrsoft.common.page.PageOutput;
 import com.xjrsoft.common.utils.VoToColumnUtil;
@@ -40,9 +42,13 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.validation.Valid;
+import java.io.IOException;
+import java.text.ParseException;
 import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
 
 /**
@@ -134,7 +140,6 @@ public class TeacherbaseManagerController {
     @SaCheckPermission("teacherbasemanager:add")
     public R add(@Valid @RequestBody AddXjrUserDto dto){
         return R.ok(teacherbaseManagerService.add(dto));
-
     }
 
     @PutMapping
@@ -154,7 +159,13 @@ public class TeacherbaseManagerController {
         DataUtil dataUtil = new DataUtil();
         dataUtil.deletePerson(ids);
         return R.ok(teacherbaseManagerService.delete(ids));
+    }
 
+    @PostMapping("/import")
+    @ApiOperation(value = "导入教职工信息")
+    public RT<Boolean> importData(@RequestParam("file") MultipartFile file) throws IOException, ParseException {
+        List<Map<Integer, Object>> excelDataList = EasyExcel.read(file.getInputStream()).sheet().headRowNumber(3).doReadSync();
+        return RT.ok(teacherbaseManagerService.importData(excelDataList));
     }
 
 }

+ 3 - 0
src/main/java/com/xjrsoft/module/teacher/service/ITeacherbaseManagerService.java

@@ -6,6 +6,7 @@ import com.xjrsoft.module.teacher.dto.UpdateXjrUserDto;
 import com.xjrsoft.module.teacher.entity.XjrUser;
 
 import java.util.List;
+import java.util.Map;
 
 /**
 * @title: service
@@ -38,4 +39,6 @@ public interface ITeacherbaseManagerService extends MPJBaseService<XjrUser> {
     * @return
     */
     Boolean delete(List<Long> ids);
+
+    Boolean importData( List<Map<Integer, Object>> excelDataList);
 }

+ 177 - 0
src/main/java/com/xjrsoft/module/teacher/service/impl/TeacherbaseManagerServiceImpl.java

@@ -5,22 +5,33 @@ import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.convert.Convert;
 import cn.hutool.core.util.StrUtil;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.github.yulichang.base.MPJBaseServiceImpl;
+import com.github.yulichang.wrapper.MPJLambdaWrapper;
 import com.xjrsoft.common.constant.GlobalConstant;
+import com.xjrsoft.common.enums.DeleteMark;
+import com.xjrsoft.common.enums.GenderDictionaryEnum;
 import com.xjrsoft.common.enums.RoleEnum;
 import com.xjrsoft.common.utils.RedisUtil;
+import com.xjrsoft.common.utils.VoToColumnUtil;
 import com.xjrsoft.config.CommonPropertiesConfig;
+import com.xjrsoft.module.organization.entity.Department;
+import com.xjrsoft.module.organization.entity.Post;
 import com.xjrsoft.module.organization.entity.User;
 import com.xjrsoft.module.organization.entity.UserDeptRelation;
 import com.xjrsoft.module.organization.entity.UserPostRelation;
 import com.xjrsoft.module.organization.entity.UserRoleRelation;
 import com.xjrsoft.module.organization.mapper.UserDeptRelationMapper;
 import com.xjrsoft.module.organization.mapper.UserRoleRelationMapper;
+import com.xjrsoft.module.organization.service.IDepartmentService;
+import com.xjrsoft.module.organization.service.IPostService;
 import com.xjrsoft.module.organization.service.IUserDeptRelationService;
 import com.xjrsoft.module.organization.service.IUserPostRelationService;
 import com.xjrsoft.module.organization.service.IUserService;
+import com.xjrsoft.module.system.entity.DictionaryDetail;
+import com.xjrsoft.module.system.service.IDictionarydetailService;
 import com.xjrsoft.module.teacher.dto.AddXjrUserDto;
 import com.xjrsoft.module.teacher.dto.UpdateXjrUserDto;
 import com.xjrsoft.module.teacher.entity.BaseTeacher;
@@ -44,9 +55,11 @@ import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Map;
 import java.util.Objects;
 import java.util.stream.Collectors;
 
@@ -76,6 +89,11 @@ public class TeacherbaseManagerServiceImpl extends MPJBaseServiceImpl<XjrUserMap
     private final IUserService userService;
     private final CommonPropertiesConfig propertiesConfig;
     private final IUserPostRelationService userPostRelationService;
+    private final IDictionarydetailService dictionaryService;
+    private final IDepartmentService departmentService;
+    private final IPostService postService;
+
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Boolean add(AddXjrUserDto dto) {
@@ -430,4 +448,163 @@ public class TeacherbaseManagerServiceImpl extends MPJBaseServiceImpl<XjrUserMap
 
         return true;
     }
+
+    /**
+     * 导入,根据第一个工号查询是否有相关信息
+     * 1、批量新增或者修改xjr_user、base_teacher、xjr_user_dept_relation、xjr_user_post_relation
+     * 2、更新redis里面的缓存
+     * @param excelDataList 导入的数据
+     * @return 成功
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public Boolean importData(List<Map<Integer, Object>> excelDataList) {
+        List<User> userList = userService.list(
+                new MPJLambdaWrapper<User>()
+                        .select(User::getId)
+                        .select(User.class, x -> VoToColumnUtil.fieldsToColumns(User.class).contains(x.getProperty()))
+                        .innerJoin(BaseTeacher.class, BaseTeacher::getUserId, User::getId)
+        );
+        Map<String, User> userMap = userList.stream().collect(Collectors.toMap(User::getUserName, x -> x));
+
+        List<BaseTeacher> teacherList = teacherbaseManagerBaseTeacherMapper.selectJoinList(BaseTeacher.class,
+                new MPJLambdaWrapper<BaseTeacher>()
+                        .select(BaseTeacher::getId)
+                        .select(BaseTeacher.class, x -> VoToColumnUtil.fieldsToColumns(BaseTeacher.class).contains(x.getProperty()))
+                        .innerJoin(User.class, User::getId, BaseTeacher::getUserId)
+        );
+        Map<Long, BaseTeacher> teacherMap = teacherList.stream().collect(Collectors.toMap(BaseTeacher::getUserId, x -> x));
+
+        List<Long> itemList = new ArrayList<>();
+        itemList.add(2023000000000000016L);
+        itemList.add(2023000000000000006L);
+
+        List<DictionaryDetail> dictionaryList = dictionaryService.list(
+                new QueryWrapper<DictionaryDetail>().lambda()
+                        .in(DictionaryDetail::getItemId, itemList)
+                        .eq(DictionaryDetail::getDeleteMark, DeleteMark.NODELETE.getCode())
+        );
+        Map<String, String> dictionaryMap = dictionaryList.stream().collect(Collectors.toMap(DictionaryDetail::getName, DictionaryDetail::getCode));
+
+        List<Department> departmentList = departmentService.list(
+                new QueryWrapper<Department>().lambda()
+                        .eq(Department::getDeleteMark, DeleteMark.NODELETE.getCode())
+        );
+        Map<String, Long> departmentMap = departmentList.stream().collect(Collectors.toMap(Department::getName, Department::getId));
+
+        List<UserDeptRelation> userDeptRelations = userDeptRelationMapper.selectList(
+                new MPJLambdaWrapper<UserDeptRelation>()
+                        .select(UserDeptRelation::getId)
+                        .select(UserDeptRelation.class, x -> VoToColumnUtil.fieldsToColumns(UserDeptRelation.class).contains(x.getProperty()))
+                        .innerJoin(User.class, User::getId, UserDeptRelation::getUserId)
+                        .innerJoin(BaseTeacher.class, BaseTeacher::getUserId, User::getId)
+        );
+        Map<Long, UserDeptRelation> userDeptMap = userDeptRelations.stream().collect(Collectors.toMap(UserDeptRelation::getUserId, x -> x));
+
+        List<Post> postList = postService.list(
+                new QueryWrapper<Post>().lambda()
+                        .eq(Post::getDeleteMark, DeleteMark.NODELETE.getCode())
+        );
+        Map<String, Long> postMap = postList.stream().collect(Collectors.toMap(Post::getName, Post::getId));
+
+        List<UserPostRelation> postRelations = userPostRelationService.list(
+                new MPJLambdaWrapper<UserPostRelation>()
+                        .select(UserPostRelation::getId)
+                        .select(UserPostRelation.class, x -> VoToColumnUtil.fieldsToColumns(UserPostRelation.class).contains(x.getProperty()))
+                        .innerJoin(User.class, User::getId, UserPostRelation::getUserId)
+                        .innerJoin(BaseTeacher.class, BaseTeacher::getUserId, User::getId)
+        );
+        Map<Long, UserPostRelation> userPostMap = postRelations.stream().collect(Collectors.toMap(UserPostRelation::getUserId, x -> x));
+
+        List<User> updateUserList = new ArrayList<>();
+        List<BaseTeacher> updateTeacherList = new ArrayList<>();
+        List<UserDeptRelation> updateDeptList = new ArrayList<>();
+        List<UserPostRelation> updatePostList = new ArrayList<>();
+
+        for (Map<Integer, Object> rowData : excelDataList) {
+            String jobCode = rowData.get(0).toString();
+            if(userMap.containsKey(jobCode)){
+                User user = userMap.get(jobCode);
+                user.setName(rowData.get(1).toString());
+                user.setGender(GenderDictionaryEnum.getCode(rowData.get(2).toString()));
+                user.setCredentialType(dictionaryMap.get(rowData.get(3).toString()));
+                user.setCredentialNumber(rowData.get(4).toString());
+                user.setMobile(rowData.get(5).toString());
+                updateUserList.add(user);
+
+                BaseTeacher teacher = teacherMap.get(user.getId());
+                teacher.setEmployType(dictionaryMap.get(rowData.get(6).toString()));
+                updateTeacherList.add(teacher);
+
+                UserDeptRelation userDeptRelation = userDeptMap.get(user.getId());
+                userDeptRelation.setDeptId(departmentMap.get(rowData.get(7).toString()));
+                updateDeptList.add(userDeptRelation);
+
+                UserPostRelation postRelation = userPostMap.get(user.getId());
+                postRelation.setPostId(postMap.get(rowData.get(8).toString()));
+                updatePostList.add(postRelation);
+                continue;
+            }
+            User user = new User();
+            user.setName(rowData.get(1).toString());
+            user.setGender(GenderDictionaryEnum.getCode(rowData.get(2).toString()));
+            user.setCredentialType(dictionaryMap.get(rowData.get(3).toString()));
+            user.setCredentialNumber(rowData.get(4).toString());
+            user.setMobile(rowData.get(5).toString());
+            user.setCreateDate(LocalDateTime.now());
+            userService.save(user);
+
+            BaseTeacher teacher = new BaseTeacher();
+            teacher.setEmployType(dictionaryMap.get(rowData.get(6).toString()));
+            teacher.setUserId(user.getId());
+            teacher.setCreateDate(LocalDateTime.now());
+            teacherbaseManagerBaseTeacherMapper.insert(teacher);
+
+            UserDeptRelation userDeptRelation = new UserDeptRelation();
+            userDeptRelation.setDeptId(departmentMap.get(rowData.get(7).toString()));
+            userDeptRelation.setUserId(user.getId());
+            userDeptRelationMapper.insert(userDeptRelation);
+
+            UserPostRelation postRelation = new UserPostRelation();
+            postRelation.setPostId(postMap.get(rowData.get(8).toString()));
+            postRelation.setUserId(user.getId());
+            userPostRelationService.save(postRelation);
+
+            UserRoleRelation roleRelation = new UserRoleRelation();
+            roleRelation.setUserId(user.getId());
+            roleRelation.setRoleId(RoleEnum.TEACHER.getCode());
+            userRoleRelationMapper.insert(roleRelation);
+        }
+
+        if(!updateUserList.isEmpty()){
+            userService.updateBatchById(updateUserList);
+        }
+        if(!updateDeptList.isEmpty()){
+            for (UserDeptRelation relation : updateDeptList) {
+                userDeptRelationMapper.updateById(relation);
+            }
+        }
+        if(!updatePostList.isEmpty()){
+            userPostRelationService.updateBatchById(updatePostList);
+        }
+        if(!updateTeacherList.isEmpty()){
+            for (BaseTeacher teacher : updateTeacherList) {
+                teacherbaseManagerBaseTeacherMapper.updateById(teacher);
+            }
+        }
+
+        List<User> list = userService.list();
+        redisUtil.set(GlobalConstant.USER_CACHE_KEY, list);
+
+        List<UserDeptRelation> deptRelationList = userDeptRelationMapper.selectList(Wrappers.lambdaQuery(UserDeptRelation.class));
+        redisUtil.set(GlobalConstant.USER_DEPT_RELATION_CACHE_KEY, deptRelationList);
+
+        List<UserRoleRelation> userRoleRelationList = userRoleRelationMapper.selectList(Wrappers.lambdaQuery(UserRoleRelation.class));
+        redisUtil.set(GlobalConstant.USER_ROLE_RELATION_CACHE_KEY, userRoleRelationList);
+
+        List<UserPostRelation> postRelationList = userPostRelationService.list(Wrappers.lambdaQuery(UserPostRelation.class));
+        redisUtil.set(GlobalConstant.USER_POST_RELATION_CACHE_KEY, postRelationList);
+
+        return true;
+    }
 }