Explorar o código

分摊比例导入功能

yurk %!s(int64=3) %!d(string=hai) anos
pai
achega
f0fa412186

+ 7 - 1
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/ProjectPercentageController.java

@@ -6,8 +6,10 @@ import com.management.platform.util.HttpRespMsg;
 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;
 
 /**
  * <p>
@@ -34,6 +36,10 @@ public class ProjectPercentageController {
         return projectPercentageService.getMonthSetting(ymonth);
     }
 
-
+    @RequestMapping("/importData")
+    public HttpRespMsg importData(Integer companyId, Integer withCheckIn,
+                                  MultipartFile file, HttpServletRequest request){
+        return projectPercentageService.importData(companyId,withCheckIn, file, request);
+    }
 }
 

+ 5 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/ProjectPercentageService.java

@@ -3,6 +3,9 @@ package com.management.platform.service;
 import com.management.platform.entity.ProjectPercentage;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.management.platform.util.HttpRespMsg;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.servlet.http.HttpServletRequest;
 
 /**
  * <p>
@@ -17,4 +20,6 @@ public interface ProjectPercentageService extends IService<ProjectPercentage> {
     HttpRespMsg saveMonthSetting(String projectCols, String userSettings, String ymonth);
 
     HttpRespMsg getMonthSetting(String ymonth);
+
+    HttpRespMsg importData(Integer companyId, Integer withCheckIn, MultipartFile file, HttpServletRequest request);
 }

+ 149 - 9
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/impl/ProjectPercentageServiceImpl.java

@@ -1,11 +1,10 @@
 package com.management.platform.service.impl;
 
 import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.management.platform.entity.FinanceProjects;
-import com.management.platform.entity.Project;
-import com.management.platform.entity.ProjectPercentage;
-import com.management.platform.entity.User;
+import com.google.gson.JsonObject;
+import com.management.platform.entity.*;
 import com.management.platform.mapper.ProjectMapper;
 import com.management.platform.mapper.ProjectPercentageMapper;
 import com.management.platform.mapper.ReportMapper;
@@ -13,14 +12,21 @@ import com.management.platform.mapper.UserMapper;
 import com.management.platform.service.FinanceProjectsService;
 import com.management.platform.service.ProjectPercentageService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.management.platform.util.ExcelUtil;
 import com.management.platform.util.HttpRespMsg;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.ss.usermodel.*;
 import org.springframework.stereotype.Service;
+import org.springframework.util.StringUtils;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.io.*;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.time.format.DateTimeFormatter;
+import java.util.*;
 
 /**
  * <p>
@@ -44,7 +50,8 @@ public class ProjectPercentageServiceImpl extends ServiceImpl<ProjectPercentageM
     private ProjectMapper projectMapper;
     @Resource
     private ReportMapper reportMapper;
-
+    @Resource
+    private ProjectPercentageService projectPercentageService;
     @Override
     public HttpRespMsg saveMonthSetting(String projectCols, String userSettings, String ymonth) {
         HttpRespMsg msg = new HttpRespMsg();
@@ -85,7 +92,7 @@ public class ProjectPercentageServiceImpl extends ServiceImpl<ProjectPercentageM
         List<Project> allProjects = projectMapper.selectList(new QueryWrapper<Project>().eq("company_id", companyId));
 
         List<FinanceProjects> flist = financeProjectsService.list(new QueryWrapper<FinanceProjects>().eq("company_id", user.getCompanyId()));
-        ProjectPercentage projectPercentage = projectPercentageMapper.selectOne(new QueryWrapper<ProjectPercentage>().eq("company_id", user.getCompanyId())
+        ProjectPercentage projectPercentage = projectPercentageMapper.selectOne(new QueryWrapper<ProjectPercentage>().eq("company_id",user.getCompanyId())
                 .eq("ymonth", ymonth));
 
         if (flist.size() == 0) {
@@ -112,4 +119,137 @@ public class ProjectPercentageServiceImpl extends ServiceImpl<ProjectPercentageM
         msg.data = map;
         return msg;
     }
+
+    @Override
+    public HttpRespMsg importData(Integer companyId, Integer withCheckIn, MultipartFile multipartFile, HttpServletRequest request) {
+        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();
+
+            //然后解析表格
+            Workbook workbook = WorkbookFactory.create(new FileInputStream(file));
+            DateFormat df = new SimpleDateFormat("yyyy-MM");
+            //获取公司全部成员
+            List<User> allUserList = userMapper.selectList(new QueryWrapper<User>().eq("company_id", companyId));
+            Sheet sheet = workbook.getSheetAt(0);
+            //由于第一行需要指明列对应的标题
+            int rowNum = sheet.getLastRowNum();
+            if (rowNum == 0) {
+                msg.setError("请填写项目数据");
+                return msg;
+            }
+            //保存data数据
+            String dataString="[";
+            String dataStringOne;
+            List<String> projectList = new ArrayList<>();
+            List<Project> allProjectList = projectMapper.selectList(new QueryWrapper<Project>().eq("company_id", companyId));
+            int projectNameStartIndex=1;
+            int dataCount = 0;
+            for (int rowIndex = 0; rowIndex <= rowNum; rowIndex++) {
+                Row row = sheet.getRow(rowIndex);
+                if (row == null) {
+                    continue;
+                }
+                if (ExcelUtil.isRowEmpty(row)) {
+                    continue;
+                }
+                if (rowIndex == 0) {
+                    //第一行是标题,获取项目名称
+                    int pIndex = projectNameStartIndex;
+                    boolean projectNotExists = false;
+                    String neProjectName = null;
+                    while(pIndex < row.getLastCellNum() && row.getCell(pIndex).getCellTypeEnum() != CellType._NONE &&  row.getCell(pIndex).getCellTypeEnum() != CellType.BLANK) {
+                        row.getCell(pIndex).setCellType(CellType.STRING);
+                        String projectName = row.getCell(pIndex).getStringCellValue().trim();
+                        if (!allProjectList.stream().filter(p->p.getProjectName().equals(projectName)).findAny().isPresent()) {
+                            projectNotExists = true;
+                            neProjectName = projectName;
+                            break;
+                        }
+                        //查询对应名称项目 id存到projectList
+                        Project project = projectMapper.selectList(new QueryWrapper<Project>().eq("project_name", projectName)).get(0);
+                        projectList.add(project.getId().toString());
+                        pIndex++;
+                    }
+                    if (projectNotExists) {
+                        //返回错误提示
+                        msg.setError("项目["+neProjectName+"]不存在,请先在项目管理中添加或导入。");
+                        return msg;
+                    }
+                }else {
+                    dataCount++;
+                    //数据行
+                    for (int i=1;i<projectNameStartIndex+projectList.size(); i++) {
+                        if (row.getCell(i) != null) {
+                            row.getCell(i).setCellType(CellType.STRING);
+                        }
+                    }
+                    if (row.getCell(0) == null) {
+                        msg.setError("第"+dataCount+"行缺少员工姓名");
+                        return msg;
+                    }
+                    String username = row.getCell(0).getStringCellValue().trim();
+                    //检查人员是否存在
+                    Optional<User> any = allUserList.stream().filter(u -> u.getName().equals(username)).findAny();
+                    if (!any.isPresent()) {
+                        msg.setError("人员["+username+"]不存在,请先在组织结构中添加或者通过钉钉同步导入");
+                        return msg;
+                    }
+                    //人员存在
+                    User user = userMapper.selectList(new QueryWrapper<User>().eq("name", username)).get(0);
+                    Integer s = 0;
+                    dataStringOne="{"+"\""+"name"+"\""+":"+"\""+username+"\""+","+"\""+"id"+"\""+":"+"\""+user.getId()+"\""+",";
+                    for (int i=projectNameStartIndex; i < projectNameStartIndex + projectList.size(); i++) {
+                        if (row.getCell(i) == null) {
+                            continue;
+                        }
+                        String stringCellValue = row.getCell(i).getStringCellValue();
+                        s+=Integer.parseInt(stringCellValue);
+                        if(i!=projectNameStartIndex + projectList.size()-1){
+                            dataStringOne+="\""+projectList.get(i-1)+"\""+":"+"\""+stringCellValue+"\""+",";
+                        }else{
+                            dataStringOne+="\""+projectList.get(i-1)+"\""+":"+"\""+stringCellValue+"\""+"}";
+                        }
+                    }
+                    if(rowIndex!=rowNum){
+                        dataString+=dataStringOne+",";
+                    }else{
+                        dataString+=dataStringOne+"]";
+                    }
+                    if(s!=100){
+                        msg.setError("分摊比例错误,不满足100%");
+                        return msg;
+                    }
+                }
+            }
+            ProjectPercentage projectPercentage=new ProjectPercentage();
+            projectPercentage.setId(null);
+            projectPercentage.setYmonth(df.format(new Date()));
+            projectPercentage.setData(dataString);
+            projectPercentage.setCompanyId(companyId);
+            System.out.println(projectPercentage);
+            projectPercentageService.save(projectPercentage);
+            msg.data=dataCount;
+            return msg;
+        }catch (IOException e){
+            e.printStackTrace();
+            msg.setError("文件处理出错");
+            return msg;
+        } catch (InvalidFormatException e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
 }