Ver código fonte

导入导出开发

anderx 2 anos atrás
pai
commit
ad109c312e

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

@@ -115,6 +115,36 @@ public class ProjectTaskController extends BaseController {
         return getDataTable(list);
     }
 
+
+    /**
+     * 导出项目列表
+     * @param response
+     */
+    @PostMapping("/listRecord/export")
+    public void listRecordExport(HttpServletResponse response,@RequestBody ProjectStaffRecordInput in)
+    {
+        List<ProjectStaffRecordOut> list=projectStaffRecordService.listRecord(in);
+        ExcelUtil<ProjectStaffRecordOut> util = new ExcelUtil<>(ProjectStaffRecordOut.class);
+        util.exportExcel(response, list, "项目研发日志列表");
+    }
+
+    /**
+     * 导入数据
+     * @param file
+     * @param isUpdateSupport
+     * @return
+     * @throws Exception
+     */
+    @PostMapping("/listRecord/importData")
+    public AjaxResult listRecordImportData(MultipartFile file, boolean isUpdateSupport) throws Exception
+    {
+        ExcelUtil<ProjectStaffRecordOut> util = new ExcelUtil<>(ProjectStaffRecordOut.class);
+        List<ProjectStaffRecordOut> list = util.importExcel(file.getInputStream());
+        String operName = getUsername();
+        String message = projectStaffRecordService.importProject(list, isUpdateSupport, operName);
+        return success(message);
+    }
+
     /**
      * 删除项目成员
      * @param in

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

@@ -15,4 +15,6 @@ public interface ProjectStaffRecordService {
     AjaxResult examineRecord(ProjectStaffRecord in);
 
     List<ProjectStaffRecordOut> listRecord(ProjectStaffRecordInput in);
+
+    String importProject(List<ProjectStaffRecordOut> list, boolean isUpdateSupport, String operName);
 }

+ 65 - 0
ruoyi-system/src/main/java/com/ruoyi/project/service/impl/ProjectStaffRecordServiceImpl.java

@@ -3,24 +3,34 @@ 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.BeanValidators;
 import com.ruoyi.project.bo.ProjectStaffRecordInput;
 import com.ruoyi.project.bo.ProjectStaffRecordOut;
+import com.ruoyi.project.bo.ProjectTaskListOut;
 import com.ruoyi.project.domain.ProjectStaffRecord;
 import com.ruoyi.project.domain.ProjectStaffRecordLog;
+import com.ruoyi.project.domain.ProjectTask;
 import com.ruoyi.project.mapper.ProjectStaffRecordLogMapper;
 import com.ruoyi.project.mapper.ProjectStaffRecordMapper;
 import com.ruoyi.project.mapper.ProjectTaskMapper;
 import com.ruoyi.project.service.ProjectStaffRecordService;
 import com.ruoyi.system.mapper.SysUserMapper;
+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.Date;
 import java.util.List;
 
 @Service
 public class ProjectStaffRecordServiceImpl implements ProjectStaffRecordService {
+
+    private static final Logger log = LoggerFactory.getLogger(ProjectStaffRecordServiceImpl.class);
     @Autowired
     private ProjectStaffRecordMapper projectStaffRecordMapper;
     @Autowired
@@ -29,6 +39,8 @@ public class ProjectStaffRecordServiceImpl implements ProjectStaffRecordService
     private SysUserMapper sysUserMapper;
     @Autowired
     private ProjectTaskMapper projectTaskMapper;
+    @Autowired
+    protected Validator validator;
 
     @Override
     public AjaxResult add(ProjectStaffRecord in) {
@@ -119,4 +131,57 @@ public class ProjectStaffRecordServiceImpl implements ProjectStaffRecordService
         }
         return projectStaffRecordMapper.selectList(in);
     }
+
+    @Override
+    public String importProject(List<ProjectStaffRecordOut> 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 (ProjectStaffRecordOut p : list)
+        {
+            try
+            {
+                // 验证是否存在
+                ProjectStaffRecord projectStaffRecord = projectStaffRecordMapper.selectByPrimaryKey(p.getId());
+                if (StringUtils.isNull(projectStaffRecord)){
+                    BeanValidators.validateWithException(validator, projectStaffRecord);
+                    projectStaffRecordMapper.insertSelective(p);
+                    successNum++;
+                    successMsg.append("<br/>" + successNum + "、项目: " + p.getName() + " 导入成功");
+                }else if (isUpdateSupport){
+                    BeanValidators.validateWithException(validator, p);
+                    projectStaffRecordMapper.updateByPrimaryKeySelective(p);
+                    successNum++;
+                    successMsg.append("<br/>" + successNum + "、项目: " + p.getName() + " 更新成功");
+                }
+                else
+                {
+                    failureNum++;
+                    failureMsg.append("<br/>" + failureNum + "、项目: " + p.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();
+    }
 }

+ 4 - 12
ruoyi-system/src/main/java/com/ruoyi/project/service/impl/ProjectTaskServiceImpl.java

@@ -81,21 +81,13 @@ public class ProjectTaskServiceImpl   implements ProjectTaskService {
 
     private void pushRoleType(ProjectListInput in) {
         if (in.getRoleType()==null)in.setRoleType(0);
-        if (in.getRoleType()==1)in.setRoleType(getRoleType());
-    }
-
-    private Integer getRoleType() {
-        if (SecurityUtils.hashRoles(UserRolesType.MANAGE.getCode())){
-            return 1;
-        }else if (SecurityUtils.hashRoles(UserRolesType.INSPECTORS.getCode())){
-            return 2;
-        }else if (SecurityUtils.hashRoles(UserRolesType.CEO.getCode())){
-            return 3;
-        }else {
-            return 0;
+        if (SecurityUtils.hashRoles(UserRolesType.CEO.getCode())){
+            in.setRoleType(1);
         }
     }
 
+
+
     @Override
     public String importProject(List<ProjectTaskListOut> list, boolean isUpdateSupport, String operName) {
         if (StringUtils.isNull(list) || list.size() == 0)