Explorar el Código

修复用户档案上传功能

Simon hace 1 año
padre
commit
ce6c6b5ebc

+ 40 - 34
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/TUserProfileController.java

@@ -28,14 +28,13 @@ import org.springframework.web.multipart.MultipartFile;
 
 /**
  * 用户档案Controller
- * 
+ *
  * @author zhengjie
  * @date 2023-08-25
  */
 @RestController
 @RequestMapping("/system/profile")
-public class TUserProfileController extends BaseController
-{
+public class TUserProfileController extends BaseController {
     @Autowired
     private ITUserProfileService tUserProfileService;
     @Autowired
@@ -43,16 +42,15 @@ public class TUserProfileController extends BaseController
     @Autowired
     private ISysDeptfileService sysDeptfileService;
 
-    Map<Object,Integer> map = new HashMap<>();
-    Map<String,String> mapUrl = new HashMap<>();
+    Map<Object, Integer> map = new HashMap<>();
+    Map<String, String> mapUrl = new HashMap<>();
 
     /**
      * 查询用户档案列表
      */
     @PreAuthorize("@ss.hasPermi('system:profile:list')")
     @GetMapping("/list")
-    public TableDataInfo list(TUserProfile tUserProfile)
-    {
+    public TableDataInfo list(TUserProfile tUserProfile) {
         startPage();
         List<TUserProfile> list = tUserProfileService.selectTUserProfileList(tUserProfile);
         return getDataTable(list);
@@ -62,8 +60,7 @@ public class TUserProfileController extends BaseController
      * 查询用户档案列表
      */
     @GetMapping("/listDevice")
-    public TableDataInfo listDevice(TUserProfileDto tUserProfileDto)
-    {
+    public TableDataInfo listDevice(TUserProfileDto tUserProfileDto) {
         startPage();
         List<TUserProfileDto> list = tUserProfileService.selectTUserProfileListByDevice(tUserProfileDto);
         return getDataTable(list);
@@ -75,8 +72,7 @@ public class TUserProfileController extends BaseController
     @PreAuthorize("@ss.hasPermi('system:profile:export')")
     @Log(title = "用户档案", businessType = BusinessType.EXPORT)
     @PostMapping("/export")
-    public void export(HttpServletResponse response, TUserProfile tUserProfile)
-    {
+    public void export(HttpServletResponse response, TUserProfile tUserProfile) {
         List<TUserProfile> list = tUserProfileService.selectTUserProfileList(tUserProfile);
         ExcelUtil<TUserProfile> util = new ExcelUtil<TUserProfile>(TUserProfile.class);
         util.exportExcel(response, list, "用户档案数据");
@@ -87,8 +83,7 @@ public class TUserProfileController extends BaseController
      */
     @PreAuthorize("@ss.hasPermi('system:profile:query')")
     @GetMapping(value = "/{id}")
-    public AjaxResult getInfo(@PathVariable("id") Integer id)
-    {
+    public AjaxResult getInfo(@PathVariable("id") Integer id) {
         return success(tUserProfileService.selectTUserProfileById(id));
     }
 
@@ -98,9 +93,8 @@ public class TUserProfileController extends BaseController
     @PreAuthorize("@ss.hasPermi('system:profile:add')")
     @Log(title = "用户档案", businessType = BusinessType.INSERT)
     @PostMapping
-    public AjaxResult add(@RequestBody TUserProfile tUserProfile)
-    {
-        if (StringUtils.isNotNull(mapUrl)){
+    public AjaxResult add(@RequestBody TUserProfile tUserProfile) {
+        if (StringUtils.isNotNull(mapUrl)) {
             tUserProfile.setPhoto(mapUrl.get("photoUrl"));
             tUserProfile.setOtherPhoto(mapUrl.get("otherPhotoUrl"));
         }
@@ -109,6 +103,7 @@ public class TUserProfileController extends BaseController
 
     /**
      * 资料上传请求(单个)
+     *
      * @return 集合
      */
     @PostMapping("/photoUpload")
@@ -139,16 +134,16 @@ public class TUserProfileController extends BaseController
             sysDeptfileService.insertSysDeptfile(sysDeptfile);
             //再添加到数据库后,得到了文件的id
             Integer photoId = sysDeptfile.getId();
-            map.put("photoId",photoId);
-            mapUrl.put("photoUrl",url);
+            map.put("photoId", photoId);
+            mapUrl.put("photoUrl", url);
             //创建一个ajax结果,这个ajax继承map集合,相当于一个map集合
             AjaxResult ajax = new AjaxResult();
-            ajax.put("url",url);
+            ajax.put("url", url);
             ajax.put("name", fname);
 //            ajax.put("businessId",businessId);
 //            ajax.put("businessFileName", fileName);
 
-            return AjaxResult.success("插入成功",ajax);
+            return AjaxResult.success("插入成功", ajax);
         } catch (Exception e) {
             return AjaxResult.error(e.getMessage());
         }
@@ -156,6 +151,7 @@ public class TUserProfileController extends BaseController
 
     /**
      * 资料上传请求(单个)
+     *
      * @return 集合
      */
     @PostMapping("/otherPhotoUpload")
@@ -186,16 +182,16 @@ public class TUserProfileController extends BaseController
             sysDeptfileService.insertSysDeptfile(sysDeptfile);
             //再添加到数据库后,得到了文件的id
             Integer otherPhotoId = sysDeptfile.getId();
-            map.put("otherPhotoId",otherPhotoId);
-            mapUrl.put("otherPhotoUrl",url);
+            map.put("otherPhotoId", otherPhotoId);
+            mapUrl.put("otherPhotoUrl", url);
             //创建一个ajax结果,这个ajax继承map集合,相当于一个map集合
             AjaxResult ajax = new AjaxResult();
-            ajax.put("url",url);
+            ajax.put("url", url);
             ajax.put("name", fname);
 //            ajax.put("businessId",businessId);
 //            ajax.put("businessFileName", fileName);
 
-            return AjaxResult.success("插入成功",ajax);
+            return AjaxResult.success("插入成功", ajax);
         } catch (Exception e) {
             return AjaxResult.error(e.getMessage());
         }
@@ -207,8 +203,7 @@ public class TUserProfileController extends BaseController
     @PreAuthorize("@ss.hasPermi('system:profile:edit')")
     @Log(title = "用户档案", businessType = BusinessType.UPDATE)
     @PutMapping
-    public AjaxResult edit(@RequestBody TUserProfile tUserProfile)
-    {
+    public AjaxResult edit(@RequestBody TUserProfile tUserProfile) {
         return toAjax(tUserProfileService.updateTUserProfile(tUserProfile));
     }
 
@@ -217,9 +212,8 @@ public class TUserProfileController extends BaseController
      */
     @PreAuthorize("@ss.hasPermi('system:profile:remove')")
     @Log(title = "用户档案", businessType = BusinessType.DELETE)
-	@DeleteMapping("/{ids}")
-    public AjaxResult remove(@PathVariable Integer[] ids)
-    {
+    @DeleteMapping("/{ids}")
+    public AjaxResult remove(@PathVariable Integer[] ids) {
         return toAjax(tUserProfileService.deleteTUserProfileByIds(ids));
     }
 
@@ -228,23 +222,35 @@ public class TUserProfileController extends BaseController
      * 下载模板
      */
     @PostMapping("/importTemplate")
-    public void importTemplate(HttpServletResponse response)
-    {
+    public void importTemplate(HttpServletResponse response) {
         ExcelUtil<TUserProfile> util = new ExcelUtil<TUserProfile>(TUserProfile.class);
         util.importTemplateExcel(response, "用户档案模板");
     }
 
+    /**
+     * 导入
+     */
+//    @Log(title = "用户档案", businessType = BusinessType.IMPORT)
+//    @PostMapping("/importData")
+//    @ResponseBody
+//    public AjaxResult importData(MultipartFile file) throws Exception
+//    {
+//        ExcelUtil<TUserProfile> util = new ExcelUtil<TUserProfile>(TUserProfile.class);
+//        List<TUserProfile> tUserProfileList = util.importExcel(file.getInputStream());
+//        String message = tUserProfileService.importTUserProfile(tUserProfileList);
+//        return AjaxResult.success(message);
+//    }
+
     /**
      * 导入
      */
     @Log(title = "用户档案", businessType = BusinessType.IMPORT)
     @PostMapping("/importData")
     @ResponseBody
-    public AjaxResult importData(MultipartFile file) throws Exception
-    {
+    public AjaxResult importDataV2(MultipartFile file) throws Exception {
         ExcelUtil<TUserProfile> util = new ExcelUtil<TUserProfile>(TUserProfile.class);
         List<TUserProfile> tUserProfileList = util.importExcel(file.getInputStream());
-        String message = tUserProfileService.importTUserProfile(tUserProfileList);
+        String message = tUserProfileService.importTUserProfileV2(tUserProfileList);
         return AjaxResult.success(message);
     }
 }

+ 3 - 3
ruoyi-system/src/main/java/com/ruoyi/system/domain/vo/SearchLocationInfoVo.java

@@ -81,7 +81,7 @@ public class SearchLocationInfoVo implements Serializable {
      * 查询到的会员性别"
      */
     @Excel(name = "查询到的会员性别")
-    private Integer userTel1;
+    private String userTel1;
     /**
      * 查询到的紧急联系人"
      */
@@ -210,11 +210,11 @@ public class SearchLocationInfoVo implements Serializable {
         this.userGender = userGender;
     }
 
-    public Integer getUserTel1() {
+    public String getUserTel1() {
         return userTel1;
     }
 
-    public void setUserTel1(Integer userTel1) {
+    public void setUserTel1(String userTel1) {
         this.userTel1 = userTel1;
     }
 

+ 17 - 9
ruoyi-system/src/main/java/com/ruoyi/system/service/ITUserProfileService.java

@@ -7,18 +7,18 @@ import com.ruoyi.system.domain.TUserProfile;
 import com.ruoyi.system.domain.dto.AddUserDto;
 import com.ruoyi.system.domain.dto.TUserProfileDto;
 import com.ruoyi.system.domain.dto.UserAgeDto;
+import org.springframework.web.multipart.MultipartFile;
 
 /**
  * 用户档案Service接口
- * 
+ *
  * @author zhengjie
  * @date 2023-08-25
  */
-public interface ITUserProfileService 
-{
+public interface ITUserProfileService {
     /**
      * 查询用户档案
-     * 
+     *
      * @param id 用户档案主键
      * @return 用户档案
      */
@@ -26,7 +26,7 @@ public interface ITUserProfileService
 
     /**
      * 查询用户档案列表
-     * 
+     *
      * @param tUserProfile 用户档案
      * @return 用户档案集合
      */
@@ -43,7 +43,7 @@ public interface ITUserProfileService
 
     /**
      * 新增用户档案
-     * 
+     *
      * @param tUserProfile 用户档案
      * @return 结果
      */
@@ -51,7 +51,7 @@ public interface ITUserProfileService
 
     /**
      * 修改用户档案
-     * 
+     *
      * @param tUserProfile 用户档案
      * @return 结果
      */
@@ -59,7 +59,7 @@ public interface ITUserProfileService
 
     /**
      * 批量删除用户档案
-     * 
+     *
      * @param ids 需要删除的用户档案主键集合
      * @return 结果
      */
@@ -67,7 +67,7 @@ public interface ITUserProfileService
 
     /**
      * 删除用户档案信息
-     * 
+     *
      * @param id 用户档案主键
      * @return 结果
      */
@@ -81,6 +81,14 @@ public interface ITUserProfileService
      */
     public String importTUserProfile(List<TUserProfile> tUserProfileList);
 
+    /**
+     * 导入用户档案数据
+     *
+     * @param tUserProfileList 用户档案列表
+     * @return 结果
+     */
+    public String importTUserProfileV2(List<TUserProfile> tUserProfileList);
+
     /**
      * 用户年龄
      *

+ 78 - 51
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TUserProfileServiceImpl.java

@@ -1,5 +1,11 @@
 package com.ruoyi.system.service.impl;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
 import java.util.List;
 
 import com.ruoyi.common.exception.ServiceException;
@@ -9,25 +15,30 @@ import com.ruoyi.system.domain.dto.AddUserDto;
 import com.ruoyi.system.domain.dto.TUserProfileDto;
 import com.ruoyi.system.domain.dto.UserAgeDto;
 import com.ruoyi.system.mapper.TEmergentPeopleMapper;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+
 import java.util.ArrayList;
+
 import com.ruoyi.common.utils.StringUtils;
 import org.springframework.transaction.annotation.Transactional;
 import com.ruoyi.system.domain.TEmergentPeople;
 import com.ruoyi.system.mapper.TUserProfileMapper;
 import com.ruoyi.system.domain.TUserProfile;
 import com.ruoyi.system.service.ITUserProfileService;
+import org.springframework.web.multipart.MultipartFile;
 
 /**
  * 用户档案Service业务层处理
- * 
+ *
  * @author zhengjie
  * @date 2023-08-25
  */
 @Service
-public class TUserProfileServiceImpl implements ITUserProfileService 
-{
+public class TUserProfileServiceImpl implements ITUserProfileService {
     @Autowired
     private TUserProfileMapper tUserProfileMapper;
     @Autowired
@@ -35,27 +46,25 @@ public class TUserProfileServiceImpl implements ITUserProfileService
 
     /**
      * 查询用户档案
-     * 
+     *
      * @param id 用户档案主键
      * @return 用户档案
      */
     @Override
-    public TUserProfile selectTUserProfileById(Integer id)
-    {
+    public TUserProfile selectTUserProfileById(Integer id) {
         return tUserProfileMapper.selectTUserProfileById(id);
     }
 
     /**
      * 查询用户档案列表
-     * 
+     *
      * @param tUserProfile 用户档案
      * @return 用户档案
      */
     @Override
-    public List<TUserProfile> selectTUserProfileList(TUserProfile tUserProfile)
-    {
+    public List<TUserProfile> selectTUserProfileList(TUserProfile tUserProfile) {
         List<TUserProfile> tUserProfileList = tUserProfileMapper.selectTUserProfileList(tUserProfile);
-        for(TUserProfile list : tUserProfileList){
+        for (TUserProfile list : tUserProfileList) {
             list.setTEmergentPeopleList(tEmergentPeopleMapper.selectTEmergentPeopleByProfileId(list.getId()));
         }
         return tUserProfileList;
@@ -74,14 +83,13 @@ public class TUserProfileServiceImpl implements ITUserProfileService
 
     /**
      * 新增用户档案
-     * 
+     *
      * @param tUserProfile 用户档案
      * @return 结果
      */
     @Transactional
     @Override
-    public int insertTUserProfile(TUserProfile tUserProfile)
-    {
+    public int insertTUserProfile(TUserProfile tUserProfile) {
         tUserProfile.setCreateTime(DateUtils.getNowDate());
         int rows = tUserProfileMapper.insertTUserProfile(tUserProfile);
         insertTEmergentPeople(tUserProfile);
@@ -90,14 +98,13 @@ public class TUserProfileServiceImpl implements ITUserProfileService
 
     /**
      * 修改用户档案
-     * 
+     *
      * @param tUserProfile 用户档案
      * @return 结果
      */
     @Transactional
     @Override
-    public int updateTUserProfile(TUserProfile tUserProfile)
-    {
+    public int updateTUserProfile(TUserProfile tUserProfile) {
         tUserProfileMapper.deleteTEmergentPeopleByProfileId(tUserProfile.getId());
         insertTEmergentPeople(tUserProfile);
         return tUserProfileMapper.updateTUserProfile(tUserProfile);
@@ -105,28 +112,26 @@ public class TUserProfileServiceImpl implements ITUserProfileService
 
     /**
      * 批量删除用户档案
-     * 
+     *
      * @param ids 需要删除的用户档案主键
      * @return 结果
      */
     @Transactional
     @Override
-    public int deleteTUserProfileByIds(Integer[] ids)
-    {
+    public int deleteTUserProfileByIds(Integer[] ids) {
         tUserProfileMapper.deleteTEmergentPeopleByProfileIds(ids);
         return tUserProfileMapper.deleteTUserProfileByIds(ids);
     }
 
     /**
      * 删除用户档案信息
-     * 
+     *
      * @param id 用户档案主键
      * @return 结果
      */
     @Transactional
     @Override
-    public int deleteTUserProfileById(Integer id)
-    {
+    public int deleteTUserProfileById(Integer id) {
         tUserProfileMapper.deleteTEmergentPeopleByProfileId(id);
         return tUserProfileMapper.deleteTUserProfileById(id);
     }
@@ -134,51 +139,77 @@ public class TUserProfileServiceImpl implements ITUserProfileService
     @Override
     public String importTUserProfile(List<TUserProfile> tUserProfileList) {
 
-        if (StringUtils.isNull(tUserProfileList) || tUserProfileList.size() == 0)
-        {
+        if (StringUtils.isNull(tUserProfileList) || tUserProfileList.size() == 0) {
             throw new ServiceException("导入用户档案信息数据不能为空!");
         }
         int successNum = 0;
         int failureNum = 0;
         StringBuilder successMsg = new StringBuilder();
         StringBuilder failureMsg = new StringBuilder();
-        for (TUserProfile tUserProfile : tUserProfileList)
-        {
-            try
-            {
+        for (TUserProfile tUserProfile : tUserProfileList) {
+            try {
                 // 验证是否存在这个人
-                TUserProfile u = tUserProfileMapper.selectTUserProfileBySfzId(tUserProfile.getSfzid() );
-                if (StringUtils.isNull(u))
-                {
+                TUserProfile u = tUserProfileMapper.selectTUserProfileBySfzId(tUserProfile.getSfzid());
+                if (!StringUtils.isNull(u)) {
                     this.insertTUserProfile(tUserProfile);
                     successNum++;
                     successMsg.append("<br/>" + successNum + "用户档案信息" + " 导入成功");
-                }
-                else
-                {
+                } else {
                     failureNum++;
                     failureMsg.append("<br/>" + failureNum + "用户档案信息" + " 信息不完整");
                 }
-            }
-            catch (Exception e)
-            {
+            } catch (Exception e) {
                 failureNum++;
                 String msg = "<br/>" + failureNum + "用户档案信息" + " 导入失败:";
                 failureMsg.append(msg + e.getMessage());
             }
         }
-        if (failureNum > 0)
-        {
+        if (failureNum > 0) {
             failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
             throw new ServiceException(failureMsg.toString());
-        }
-        else
-        {
+        } else {
             successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
         }
         return successMsg.toString();
     }
 
+    @Transactional
+    @Override
+    public String importTUserProfileV2(List<TUserProfile> tUserProfileList) {
+        if (tUserProfileList == null || tUserProfileList.isEmpty()) {
+            throw new ServiceException("导入用户档案信息数据不能为空!");
+        }
+        //批量导入用户数据
+        for (TUserProfile userProfile : tUserProfileList) {
+            //判断提交数据格式是否正确
+            if (userProfile.getSfzid() == null || userProfile.getSfzid().isEmpty()) {
+                throw new ServiceException("导入用户档案信息身份证号格式异常!");
+            }
+            //根据身份证号查询数据库中的用户信息
+            TUserProfile user = tUserProfileMapper.selectTUserProfileBySfzId(userProfile.getSfzid());
+            if (user != null) {
+                //如果表中有该用户则提示
+                StringBuilder stringBuilder = new StringBuilder();
+                stringBuilder.append("导入用户档案信息已存在");
+                //如果有用户名,则提示
+                if (!StringUtils.isEmpty(userProfile.getName())) {
+                    stringBuilder.append(", 用户名:" + userProfile.getName());
+                }
+                //如果有身份证号,则提示
+                if (!StringUtils.isEmpty(userProfile.getSfzid())) {
+                    stringBuilder.append(", 身份证号:" + userProfile.getSfzid());
+                }
+                stringBuilder.append("!");
+                throw new ServiceException(stringBuilder.toString());
+            }
+            insertTUserProfile(userProfile);
+        }
+        StringBuilder successMsg = new StringBuilder();
+        successMsg.append("恭喜您,数据已全部导入成功!共 " + tUserProfileList.size() + " 条,数据如下:");
+        return successMsg.toString();
+    }
+
+
     @Override
     public List<UserAgeDto> selectuserAgeDto() {
         return tUserProfileMapper.selectuserAgeDto();
@@ -196,23 +227,19 @@ public class TUserProfileServiceImpl implements ITUserProfileService
 
     /**
      * 新增紧急联系人信息
-     * 
+     *
      * @param tUserProfile 用户档案对象
      */
-    public void insertTEmergentPeople(TUserProfile tUserProfile)
-    {
+    public void insertTEmergentPeople(TUserProfile tUserProfile) {
         List<TEmergentPeople> tEmergentPeopleList = tUserProfile.getTEmergentPeopleList();
         Integer id = tUserProfile.getId();
-        if (StringUtils.isNotNull(tEmergentPeopleList))
-        {
+        if (StringUtils.isNotNull(tEmergentPeopleList)) {
             List<TEmergentPeople> list = new ArrayList<TEmergentPeople>();
-            for (TEmergentPeople tEmergentPeople : tEmergentPeopleList)
-            {
+            for (TEmergentPeople tEmergentPeople : tEmergentPeopleList) {
                 tEmergentPeople.setProfileId(id);
                 list.add(tEmergentPeople);
             }
-            if (list.size() > 0)
-            {
+            if (list.size() > 0) {
                 tUserProfileMapper.batchTEmergentPeople(list);
             }
         }