Bladeren bron

批量导入 文件异常修改

yurk 3 jaren geleden
bovenliggende
commit
b66ecfe042

+ 28 - 4
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/CustomerInfoController.java

@@ -20,10 +20,8 @@ import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -105,6 +103,32 @@ public class CustomerInfoController {
         }
         return msg;
     }
+    @RequestMapping("/batchDelete")
+    public HttpRespMsg batchDelete(String batchIds) {
+        HttpRespMsg msg = new HttpRespMsg();
+        if(!batchIds.equals("")&&!StringUtils.isEmpty(batchIds)){
+            String[] split = batchIds.split(",");
+            List<String> list = Arrays.asList(split);
+            String token = request.getHeader("TOKEN");
+            User user = userMapper.selectById(token);
+            List<Project> projectList = projectMapper.selectList(new QueryWrapper<Project>().in("customer_id", list).eq("company_id", user.getCompanyId()));
+            System.out.println(projectList);
+            if(projectList.size()>0){
+                List<String> collect = projectList.stream().map(pro -> pro.getCustomerName()).collect(Collectors.toList());
+                String s = String.valueOf(collect);
+                msg.setError("客户"+s+"已被使用");
+                return msg;
+            }
+            int r = customerInfoMapper.delete(new QueryWrapper<CustomerInfo>().eq("id", list.get(0)).eq("company_id", user.getCompanyId()));
+            if (r <= 0) {
+                msg.setError("无权删除");
+                return msg;
+            }
+            customerInfoMapper.deleteBatchIds(list);
+        }
+        return msg;
+    }
+
 
     @RequestMapping("/list")
     public HttpRespMsg list(@RequestParam Integer pageIndex, @RequestParam Integer pageSize, String keyword) {

+ 154 - 7
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/ProjectRequirementController.java

@@ -2,26 +2,35 @@ package com.management.platform.controller;
 
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.management.platform.constant.Constant;
-import com.management.platform.entity.*;
+import com.management.platform.entity.Project;
+import com.management.platform.entity.ProjectRequirement;
+import com.management.platform.entity.TaskGroup;
 import com.management.platform.mapper.ProjectMapper;
 import com.management.platform.mapper.ProjectRequirementMapper;
 import com.management.platform.mapper.TaskGroupMapper;
 import com.management.platform.mapper.UserMapper;
 import com.management.platform.service.ProjectRequirementService;
+import com.management.platform.util.ExcelUtil;
 import com.management.platform.util.HttpRespMsg;
+import org.apache.poi.EncryptedDocumentException;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.xssf.usermodel.XSSFCell;
+import org.apache.poi.xssf.usermodel.XSSFRow;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.RequestMapping;
-
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
+import java.io.*;
 import java.time.LocalDate;
 import java.time.format.DateTimeFormatter;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -35,6 +44,8 @@ import java.util.stream.Collectors;
 @RestController
 @RequestMapping("/project-requirement")
 public class ProjectRequirementController {
+    @Value(value = "${upload.path}")
+    private String path;
     @Resource
     private HttpServletRequest request;
     @Resource
@@ -65,16 +76,19 @@ public class ProjectRequirementController {
         HttpRespMsg msg = new HttpRespMsg();
         Integer companyId = userMapper.selectById(request.getHeader("TOKEN")).getCompanyId();
         int startIndex = (pageIndex-1)*pageSize;
-        long total = projectMapper.selectCount(new QueryWrapper<Project>().eq("company_id", companyId));
         LocalDate now = LocalDate.now();
         now = now.minusDays(7);
         String startDate = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(now);
         List<Integer> projectIds = new ArrayList<>();
+        long total;
         if (!StringUtils.isEmpty(groupName)) {
             List<Project> projectList = projectMapper.selectList(new QueryWrapper<Project>().eq("company_id", companyId));
             List<Integer> collect = projectList.stream().map(Project::getId).collect(Collectors.toList());
             List<TaskGroup> taskGroups = taskGroupMapper.selectList(new QueryWrapper<TaskGroup>().eq("name", groupName).in("project_id", collect));
             projectIds = taskGroups.stream().map(TaskGroup::getProjectId).collect(Collectors.toList());
+            total=projectIds.size();
+        }else{
+            total = projectMapper.selectCount(new QueryWrapper<Project>().eq("company_id", companyId));
         }
         List<ProjectRequirement> projectRequirements = projectRequirementMapper.customSelect(companyId, startDate, projectId, projectIds.size()==0?null:projectIds, startIndex, pageSize);
         HashMap map = new HashMap();
@@ -84,6 +98,139 @@ public class ProjectRequirementController {
 
         return msg;
     }
+    @RequestMapping("/exportData")
+    public HttpRespMsg exportData(){
+        HttpRespMsg httpRespMsg=new HttpRespMsg();
+        Integer companyId = userMapper.selectById(request.getHeader("TOKEN")).getCompanyId();
+        LocalDate now = LocalDate.now();
+        now = now.minusDays(7);
+        String startDate = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(now);
+        List<ProjectRequirement> projectRequirementList = projectRequirementMapper.customSelect(companyId,startDate, null, null, null, null);
+        List<List<String>> dataList=new ArrayList<>();
+        String[] titleString={"项目编号","项目名称","近七日活跃人员","人员需求","任务需求"};
+        List<String> nameList = Arrays.asList(titleString);
+        dataList.add(nameList);
+        projectRequirementList.forEach(pr->{
+            List<String> resultData=new ArrayList<>();
+            resultData.add(pr.getProjectCode());
+            resultData.add(pr.getProjectName());
+            resultData.add(pr.getActiveUsers());
+            resultData.add(pr.getMembReq());
+            resultData.add(pr.getTaskReq());
+            dataList.add(resultData);
+        });
+        //生成excel文件导出
+        String fileName = "资源需求统计报表_"+System.currentTimeMillis();
+        String resp = ExcelUtil.exportGeneralExcelByTitleAndList(fileName , dataList, path);
+        httpRespMsg.data=resp;
+        return httpRespMsg;
+    }
+    @RequestMapping("/importData")
+    public HttpRespMsg importData(MultipartFile multipartFile){
+        Integer companyId = userMapper.selectById(request.getHeader("token")).getCompanyId();
+        HttpRespMsg msg=new HttpRespMsg();
+        //然后处理文件
+        String fileName = multipartFile.getOriginalFilename();
+        File file = new File(fileName == null ? "file" : fileName);
+        InputStream inputStream = null;
+        OutputStream outputStream = null;
+        try {
+            inputStream = multipartFile.getInputStream();
+            outputStream = new FileOutputStream(file);
+            byte[] buffer = new byte[4096];
+            int temp = 0;
+            while ((temp = inputStream.read(buffer, 0, 4096)) != -1) {
+                outputStream.write(buffer, 0, temp);
+            }
+            inputStream.close();
+            outputStream.close();
+            //然后解析表格
+            XSSFWorkbook workbook = new XSSFWorkbook(file);
+            //我们只需要第一个sheet
+            XSSFSheet sheet = workbook.getSheetAt(0);
+            //由于第一行需要指明列对应的标题
+            int rowNum = sheet.getLastRowNum();
+            List<Project> projectList = projectMapper.selectList(new QueryWrapper<Project>().eq("company_id", companyId));
+            List<ProjectRequirement> projectRequirementList = projectRequirementMapper.selectList(null);
+            List<ProjectRequirement> projectRequirements=new ArrayList<>();
+            for (int rowIndex = 0; rowIndex <= rowNum; rowIndex++) {
+                XSSFRow row = sheet.getRow(rowIndex);
+                if (row == null) {
+                    continue;
+                }
+                //跳过空行
+                if (ExcelUtil.isRowEmpty(row)) {
+                    continue;
+                }
+                //项目编号	任务需求  人员需求
+                XSSFCell codeCell = row.getCell(0);
+                XSSFCell nameCell = row.getCell(1);
+                XSSFCell taskReqCell = row.getCell(2);
+                XSSFCell membReqCell = row.getCell(3);
+                if(nameCell==null&&codeCell==null){
+                    throw new Exception("项目编号以及名称不能同时不存在");
+                }
+                if (codeCell != null)codeCell.setCellType(CellType.STRING);
+                if (nameCell != null)nameCell.setCellType(CellType.STRING);
+                if (taskReqCell != null)taskReqCell.setCellType(CellType.STRING);
+                if (membReqCell != null)membReqCell.setCellType(CellType.STRING);
+                ProjectRequirement projectRequirement=new ProjectRequirement();
+                List<Project> list = projectList.stream().filter(project ->
+                        (StringUtils.isEmpty(project.getProjectCode())?"":project.getProjectCode()).equals(codeCell.getStringCellValue())
+                        || (StringUtils.isEmpty(project.getProjectName())?"":project.getProjectName()).equals(nameCell.getStringCellValue())
+                ).collect(Collectors.toList());
+                if(StringUtils.isEmpty(list)){
+                    msg.setError("项目编号/名称"+codeCell.getStringCellValue()+"/"+nameCell.getStringCellValue()+"不存在");
+                }else{
+                    list.forEach(li->{
+                        projectRequirement.setProjectId(li.getId());
+                        projectRequirement.setTaskReq(StringUtils.isEmpty(taskReqCell.getStringCellValue())?"":taskReqCell.getStringCellValue());
+                        projectRequirement.setMembReq(StringUtils.isEmpty(membReqCell.getStringCellValue())?"":membReqCell.getStringCellValue());
+                        boolean flag = projectRequirementList.stream().anyMatch(pr -> pr.getProjectId().equals(li.getId()));
+                        if(flag){
+                            Optional<ProjectRequirement> first = projectRequirementList.stream().filter(pr -> pr.getProjectId().equals(li.getId())).findFirst();
+                            projectRequirement.setId(first.get().getId());
+                        }
+                        projectRequirements.add(projectRequirement);
+                    });
+                }
+            }
+            projectRequirementService.saveOrUpdateBatch(projectRequirements);
+        } catch (IOException e) {
+            e.printStackTrace();
+            msg.setError("文件处理出错");
+            return msg;
+        } catch (NullPointerException e) {
+            e.printStackTrace();
+            msg.setError("数据格式有误或存在空数据 导入失败");
+            return msg;
+        }catch (InvalidFormatException e) {
+            e.printStackTrace();
+            msg.setError("文件格式错误,如果安装了加密软件需要先解密再上传");
+        }catch (EncryptedDocumentException e) {
+            e.printStackTrace();
+            msg.setError("文件加密状态,需要先解除加密状态再上传");
+            return msg;
+        } catch (Exception e) {
+            e.printStackTrace();
+            msg.setError("上传失败:"+e.getMessage());
+            return msg;
+        } finally {
+            //关闭流
+            try {
+                if (outputStream != null && inputStream != null) {
+                    outputStream.close();
+                    inputStream.close();
+                    System.out.println("流已关闭");
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+//            file.deleteOnExit();//程序退出时删除临时文件
+            System.out.println(file.delete());
+        }
+        return msg;
+    }
 
     @RequestMapping("/delete")
     public HttpRespMsg delete(Integer id) {

+ 34 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/ReportExtraDegreeController.java

@@ -14,6 +14,7 @@ import com.management.platform.service.ProjectService;
 import com.management.platform.service.ReportExtraDegreeService;
 import com.management.platform.util.HttpRespMsg;
 import org.apache.poi.util.StringUtil;
+import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
@@ -22,6 +23,7 @@ import org.springframework.web.multipart.MultipartFile;
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -142,6 +144,38 @@ public class ReportExtraDegreeController {
         return msg;
     }
 
+    @RequestMapping("/batchDelete")
+    public HttpRespMsg batchDelete(String batchIds) {
+        HttpRespMsg msg = new HttpRespMsg();
+        if(!StringUtils.isEmpty(batchIds)&&!batchIds.equals("")){
+            String[] split = batchIds.split(",");
+            List<String> list = Arrays.asList(split);
+            String token = request.getHeader("TOKEN");
+            User user = userMapper.selectById(token);
+            //检查是否已经被使用
+            QueryWrapper<Project> queryWrapper=new QueryWrapper<>();
+            queryWrapper.in("associate_degrees", list);
+            list.forEach(li->{
+                queryWrapper.or().like("associate_degrees", li+",%")
+                        .or().like("associate_degrees", "%,"+li+",%")
+                        .or().like("associate_degrees", "%,"+li);
+            });
+            List<Project> existsProjects = projectMapper.selectList(queryWrapper);
+            if (existsProjects.size() > 0) {
+                String names = existsProjects.stream().map(Project::getProjectName).collect(Collectors.joining(","));
+                msg.setError("该数据已经被以下项目关联,无法删除:"+names);
+                return msg;
+            }
+            int r = reportExtraDegreeMapper.delete(new QueryWrapper<ReportExtraDegree>().eq("id", list.get(0)).eq("company_id", user.getCompanyId()));
+            if (r <= 0) {
+                msg.setError("无权删除");
+                return msg;
+            }
+            reportExtraDegreeMapper.deleteBatchIds(list);
+        }
+        return msg;
+    }
+
     /**
      * 获取全部getAll
      * @return

+ 7 - 4
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/impl/ProjectServiceImpl.java

@@ -2265,24 +2265,27 @@ public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> impl
             TimeType timeType = timeTypeMapper.selectById(companyId);
             List<Map<String, Object>> list = projectMapper.getCustomDataSum(companyId, startDate, endDate, projectId, userId);
             List<String> headList = new ArrayList<String>();
+            headList.add("序号");
+            headList.add("姓名");
+            headList.add("工作日期");
             headList.add("项目编号");
             headList.add("项目名称");
-            headList.add("人员");
-            headList.add("部门");
             headList.add(timeType.getCustomDataName());
             List<List<String>> allList = new ArrayList<List<String>>();
             allList.add(headList);
             double totalCostTime = 0;
+            int seq=1;
             List<Map<String, Object>> membList = projectMapper.getProjectCusDataSumItem(companyId, startDate, endDate, projectId, userId);
             for (Map<String, Object> membMap : membList) {
                 List<String> membRowData = new ArrayList<String>();
+                membRowData.add(String.valueOf(seq));
+                membRowData.add((String)membMap.get("name"));
                 membRowData.add((String)membMap.get("projectCode"));
                 membRowData.add((String)membMap.get("projectName"));
-                membRowData.add((String)membMap.get("name"));
-                membRowData.add((String)membMap.get("departmentName"));
                 membRowData.add(((Double)membMap.get("cost")).toString());
                 allList.add(membRowData);
                 totalCostTime += (Double)membMap.get("cost");
+                seq++;
             }
             //合计
             List<String> sumRow = new ArrayList<String>();

+ 4 - 1
fhKeeper/formulahousekeeper/management-platform/src/main/resources/mapper/ProjectRequirementMapper.xml

@@ -39,6 +39,9 @@
                  #{item}
              </foreach>
          </if>
-        order by project.id asc limit #{startIndex}, #{pageSize}
+        order by project.id asc
+        <if test="startIndex!=null and startIndex!='' and pageSize!=null and pageSize!=''">
+            limit #{startIndex}, #{pageSize}
+        </if>
     </select>
 </mapper>