浏览代码

增加员工信息导入、下载导入模板接口,以及常规参数校验

jzp 2 月之前
父节点
当前提交
0f57a3ed08

+ 30 - 0
fs-company/src/main/java/com/fs/company/controller/company/CompanyUserController.java

@@ -31,6 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.util.Arrays;
 import java.util.Date;
@@ -62,6 +63,35 @@ public class CompanyUserController extends BaseController
     @Autowired
     private ICompanyService companyService;
 
+
+    /**
+     * 下载导入员工信息模板
+     */
+    @GetMapping("/importTemplate")
+    public AjaxResult importVisitTemplate()
+    {
+        ExcelUtil<CompanyUser> util = new ExcelUtil<CompanyUser>(CompanyUser.class);
+        return util.importTemplateExcel("用户数据");
+    }
+
+    @Log(title = "用户导入", businessType = BusinessType.IMPORT)
+    @PostMapping("/importData")
+    public AjaxResult importUserData(MultipartFile file,String deptId) throws Exception
+    {
+        ExcelUtil<CompanyUser> util = new ExcelUtil<>(CompanyUser.class);
+        List<CompanyUser> list = util.importExcel(file.getInputStream());
+        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
+        Long companyId = loginUser.getCompany().getCompanyId();
+        Long newDeptId = 0L;
+        if(deptId == null ||"null".equals(deptId) || "".equals(deptId)){
+            newDeptId = loginUser.getUser().getDeptId();
+        }else{
+            newDeptId = Long.parseLong(deptId);
+        }
+        String message = companyUserService.importUserInfoService(list,newDeptId,companyId);
+        return AjaxResult.success(message);
+    }
+
     /**
      * 获取用户列表
      */

+ 3 - 0
fs-service/src/main/java/com/fs/company/mapper/CompanyUserMapper.java

@@ -231,4 +231,7 @@ public interface CompanyUserMapper
 
     @Select("select domain from company_user where user_id = #{userId}")
     String selectDomainByUserId(Long userId);
+
+    @Select("select user_name userName from company_user  where user_name = #{userName}")
+    String selectCompanyUserByUserName(String userName);
 }

+ 2 - 0
fs-service/src/main/java/com/fs/company/service/ICompanyUserService.java

@@ -134,4 +134,6 @@ public interface ICompanyUserService {
      * @return  list
      */
     List<CompanyUser> selectCompanyUserByIds(Set<Long> ids);
+
+    String importUserInfoService(List<CompanyUser> list,Long deptId,Long companyId);
 }

+ 77 - 0
fs-service/src/main/java/com/fs/company/service/impl/CompanyUserServiceImpl.java

@@ -2,7 +2,9 @@ package com.fs.company.service.impl;
 
 import com.fs.common.annotation.DataScope;
 import com.fs.common.core.domain.R;
+import com.fs.common.exception.ServiceException;
 import com.fs.common.utils.DateUtils;
+import com.fs.common.utils.SecurityUtils;
 import com.fs.common.utils.StringUtils;
 import com.fs.company.domain.*;
 import com.fs.company.mapper.*;
@@ -18,6 +20,7 @@ import com.fs.his.vo.CitysAreaVO;
 import com.fs.qw.mapper.QwUserMapper;
 import com.fs.qw.vo.CompanyUserQwVO;
 import com.fs.qw.vo.QwUserVO;
+import com.fs.system.service.ISysConfigService;
 import com.fs.wxUser.domain.CompanyWxUser;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -399,4 +402,78 @@ public class CompanyUserServiceImpl implements ICompanyUserService
     public List<CompanyUser> selectCompanyUserByIds(Set<Long> ids) {
         return companyUserMapper.selectCompanyUserByIds(ids);
     }
+
+    @Override
+    public String importUserInfoService(List<CompanyUser> userList,Long deptId,Long companyId) {
+        if (StringUtils.isNull(userList) || userList.size() == 0)
+        {
+            throw new ServiceException("导入用户数据不能为空!");
+        }
+        int successNum = 0;
+        int failureNum = 0;
+        StringBuilder successMsg = new StringBuilder();
+        StringBuilder failureMsg = new StringBuilder();
+
+        for (CompanyUser user : userList) {
+            if (user.getUserName() == null || "".equals(user.getUserName()))
+            {
+                throw new ServiceException("用户账号不能为空!");
+            }
+            if (user.getNickName() == null || "".equals(user.getNickName()))
+            {
+                throw new ServiceException("用户昵称不能为空!");
+            }
+            if (user.getPhonenumber() == null || "".equals(user.getPhonenumber()))
+            {
+                throw new ServiceException("手机号码不能为空!");
+            }
+            if (user.getSex() == null || "".equals(user.getSex()))
+            {
+                throw new ServiceException("用户性别不能为空!");
+            }
+            if (user.getPassword() == null || "".equals(user.getPassword()))
+            {
+                throw new ServiceException("密码不能为空!");
+            }
+            //通过账号校验账号是否存在
+            String userName = companyUserMapper.selectCompanyUserByUserName(user.getUserName());
+            if(userName != null){
+                throw new ServiceException(userName + ",账号已存在!");
+            }
+        }
+
+        for (CompanyUser user : userList)
+        {
+            try
+            {
+                user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
+                user.setToken("".equals(user.getToken())?null:user.getToken());
+                user.setIdCard("".equals(user.getIdCard())?null:user.getIdCard());
+                user.setCompanyId(companyId);
+                user.setDeptId(deptId);
+                user.setStatus("0");
+                user.setCreateTime(new Date());
+                this.insertUser(user);
+                successNum++;
+                successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
+            }
+            catch (Exception e)
+            {
+                failureNum++;
+                String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败:";
+                failureMsg.append(msg + e.getMessage());
+            }
+        }
+
+        if (failureNum > 0)
+        {
+            failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
+            throw new ServiceException(failureMsg.toString());
+        }
+        else
+        {
+            successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
+        }
+        return successMsg.toString();
+    }
 }