Parcourir la source

项目模块修改

anderx il y a 2 ans
Parent
commit
89f8a4be32

+ 17 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/project/ProjectTaskController.java

@@ -180,6 +180,23 @@ public class ProjectTaskController extends BaseController {
         util.exportExcel(response, list, "项目列表");
     }
 
+    /**
+     * 导入数据
+     * @param file
+     * @param isUpdateSupport
+     * @return
+     * @throws Exception
+     */
+    @PostMapping("/importData")
+    public AjaxResult importData(MultipartFile file, boolean isUpdateSupport) throws Exception
+    {
+        ExcelUtil<ProjectTaskListOut> util = new ExcelUtil<>(ProjectTaskListOut.class);
+        List<ProjectTaskListOut> list = util.importExcel(file.getInputStream());
+        String operName = getUsername();
+        String message = projectTaskService.importProject(list, isUpdateSupport, operName);
+        return success(message);
+    }
+
 
     /**
      * 上传项目文件

+ 2 - 0
ruoyi-system/src/main/java/com/ruoyi/project/service/ProjectTaskService.java

@@ -15,4 +15,6 @@ public interface ProjectTaskService {
     AjaxResult details(Long id);
 
     List<ProjectTaskListOut> list(ProjectListInput in);
+
+    String importProject(List<ProjectTaskListOut> list, boolean updateSupport, String operName);
 }

+ 64 - 0
ruoyi-system/src/main/java/com/ruoyi/project/service/impl/ProjectTaskServiceImpl.java

@@ -3,8 +3,11 @@ package com.ruoyi.project.service.impl;
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.common.enums.UserRolesType;
+import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.SecurityUtils;
+import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.bean.BeanUtils;
+import com.ruoyi.common.utils.bean.BeanValidators;
 import com.ruoyi.project.bo.ProjectListInput;
 import com.ruoyi.project.bo.ProjectTaskListOut;
 import com.ruoyi.project.bo.ProjectTaskOut;
@@ -14,20 +17,28 @@ import com.ruoyi.project.mapper.ProjectStaffMapper;
 import com.ruoyi.project.mapper.ProjectTaskMapper;
 import com.ruoyi.project.service.ProjectTaskService;
 import com.ruoyi.system.service.ISysDeptService;
+import com.ruoyi.system.service.impl.SysUserServiceImpl;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import javax.validation.Validator;
 import java.util.List;
 import java.util.Set;
 
 @Service
 public class ProjectTaskServiceImpl   implements ProjectTaskService {
+
+    private static final Logger log = LoggerFactory.getLogger(ProjectTaskServiceImpl.class);
     @Autowired
     private ProjectTaskMapper projectTaskMapper;
     @Autowired
     private ProjectStaffMapper projectStaffMapper;
     @Autowired
     private ISysDeptService deptService;
+    @Autowired
+    protected Validator validator;
 
     @Override
     public AjaxResult addProjectTask(ProjectTask projectTask) {
@@ -71,4 +82,57 @@ public class ProjectTaskServiceImpl   implements ProjectTaskService {
         if (in.getEndTime()!=null)in.setEndTime(in.getEndTime()+" 23:59:59");
         return projectTaskMapper.selectList(in);
     }
+
+    @Override
+    public String importProject(List<ProjectTaskListOut> list, boolean isUpdateSupport, String operName) {
+        if (StringUtils.isNull(list) || list.size() == 0)
+        {
+            throw new ServiceException("导入用户数据不能为空!");
+        }
+        int successNum = 0;
+        int failureNum = 0;
+        StringBuilder successMsg = new StringBuilder();
+        StringBuilder failureMsg = new StringBuilder();
+        for (ProjectTaskListOut p : list)
+        {
+            try
+            {
+                // 验证是否存在这个用户
+                ProjectTask projectTask = projectTaskMapper.selectByPrimaryKey(p.getId());
+                if (StringUtils.isNull(projectTask)){
+                    BeanValidators.validateWithException(validator, projectTask);
+                    projectTaskMapper.insertSelective(projectTask);
+                    successNum++;
+                    successMsg.append("<br/>" + successNum + "、项目: " + projectTask.getName() + " 导入成功");
+                }else if (isUpdateSupport){
+                    BeanValidators.validateWithException(validator, p);
+                    projectTaskMapper.updateByPrimaryKeySelective(p);
+                    successNum++;
+                    successMsg.append("<br/>" + successNum + "、项目: " + projectTask.getName() + " 更新成功");
+                }
+                else
+                {
+                    failureNum++;
+                    failureMsg.append("<br/>" + failureNum + "、项目: " + projectTask.getName() + " 已存在");
+                }
+            }
+            catch (Exception e)
+            {
+                failureNum++;
+                String msg = "<br/>" + failureNum + "、项目: " + p.getName() + " 导入失败:";
+                failureMsg.append(msg + e.getMessage());
+                log.error(msg, e);
+            }
+        }
+        if (failureNum > 0)
+        {
+            failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
+            throw new ServiceException(failureMsg.toString());
+        }
+        else
+        {
+            successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
+        }
+        return successMsg.toString();
+    }
 }