Forráskód Böngészése

经营数据导出excel

wangys 5 éve
szülő
commit
7c2bb72303

+ 4 - 0
efunbox-base/efunbox-base-api/src/main/java/cn/efunbox/manage/base/service/ManageService.java

@@ -4,10 +4,14 @@ import cn.efunbox.manage.base.entity.Manage;
 import cn.efunbox.manage.base.vo.ManageVO;
 import cn.efunbox.manage.common.result.ApiResult;
 
+import javax.servlet.http.HttpServletResponse;
+
 public interface ManageService {
 
     ApiResult list(ManageVO manageVO);
 
+    void export(ManageVO manageVO, HttpServletResponse response)  throws Exception;
+
     ApiResult save(Manage manage);
 
     ApiResult update(Manage manage);

+ 376 - 0
efunbox-base/efunbox-base-api/src/main/java/cn/efunbox/manage/base/service/impl/ManageServiceImpl.java

@@ -6,11 +6,15 @@ import cn.efunbox.manage.base.enums.IncomeStatusEnum;
 import cn.efunbox.manage.base.enums.ManageTypeEnum;
 import cn.efunbox.manage.base.repository.*;
 import cn.efunbox.manage.base.service.ManageService;
+import cn.efunbox.manage.base.util.TimeUtil;
 import cn.efunbox.manage.base.vo.ManageVO;
 import cn.efunbox.manage.common.enums.BaseOrderEnum;
 import cn.efunbox.manage.common.helper.SortHelper;
 import cn.efunbox.manage.common.result.ApiCode;
 import cn.efunbox.manage.common.result.ApiResult;
+import org.apache.http.HttpResponse;
+import org.apache.poi.hssf.usermodel.*;
+import org.apache.poi.ss.util.CellRangeAddress;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageRequest;
@@ -20,7 +24,13 @@ import org.springframework.stereotype.Service;
 
 import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.Predicate;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
 import java.math.BigDecimal;
+import java.net.URLEncoder;
+import java.text.SimpleDateFormat;
 import java.util.*;
 
 /**
@@ -304,6 +314,372 @@ public class ManageServiceImpl implements ManageService {
     }
 
     @Override
+    public void export(ManageVO manageVO, HttpServletResponse response) throws Exception {
+        if(!manageVO.getIsEntry()){
+            manageVO.setStatus(BaseStatusEnum.NORMAL);
+        }
+        Specification specification = (Specification<Manage>) (root, criteriaQuery, criteriaBuilder) -> {
+
+            List<Predicate> predicates = new ArrayList<>();
+            if(manageVO.getToProjectId() != null && manageVO.getToProjectId().size() > 0){
+                List<Predicate> list = new ArrayList<>();
+                if (manageVO.getToProjectId() != null && manageVO.getToProjectId().size() > 0) {
+                    CriteriaBuilder.In<Object> in = criteriaBuilder.in(root.get("toProjectId"));
+                    for (Long id : manageVO.getToProjectId()) {
+                        in.value(id);
+                    }
+                    list.add(in);
+                }
+                predicates.addAll(list);
+            }
+            if(manageVO.getToDeptId() != null && manageVO.getToDeptId().size() > 0){
+                List<Predicate> list = new ArrayList<>();
+                if (manageVO.getToDeptId() != null && manageVO.getToDeptId().size() > 0) {
+                    CriteriaBuilder.In<Object> in = criteriaBuilder.in(root.get("toDeptId"));
+                    for (Long id : manageVO.getToDeptId()) {
+                        in.value(id);
+                    }
+                    list.add(in);
+                }
+                predicates.addAll(list);
+            }
+            if(manageVO.getProductId() != null && manageVO.getProductId().size() > 0){
+                List<Predicate> list = new ArrayList<>();
+                if (manageVO.getProductId() != null && manageVO.getProductId().size() > 0) {
+                    CriteriaBuilder.In<Object> in = criteriaBuilder.in(root.get("productId"));
+                    for (Long id : manageVO.getProductId()) {
+                        in.value(id);
+                    }
+                    list.add(in);
+                }
+                predicates.addAll(list);
+            }
+            if(manageVO.getProjectId() != null && manageVO.getProjectId().size() > 0){
+                List<Predicate> list = new ArrayList<>();
+                if (manageVO.getProjectId() != null && manageVO.getProjectId().size() > 0) {
+                    CriteriaBuilder.In<Object> in = criteriaBuilder.in(root.get("projectId"));
+                    for (Long id : manageVO.getProjectId()) {
+                        in.value(id);
+                    }
+                    list.add(in);
+                }
+                predicates.addAll(list);
+            }
+            if(Objects.nonNull(manageVO.getType())){
+                predicates.add(criteriaBuilder.equal(root.get("type"), manageVO.getType()));
+            }
+            if(manageVO.getCostTypeId() != null && manageVO.getCostTypeId().size() > 0){
+                List<Predicate> list = new ArrayList<>();
+                if (manageVO.getCostTypeId() != null && manageVO.getCostTypeId().size() > 0) {
+                    CriteriaBuilder.In<Object> in = criteriaBuilder.in(root.get("costTypeId"));
+                    for (Long id : manageVO.getCostTypeId()) {
+                        in.value(id);
+                    }
+                    list.add(in);
+                }
+                predicates.addAll(list);
+            }
+            if(manageVO.getIncomeStatus() != null && manageVO.getIncomeStatus().size() > 0){
+                List<Predicate> list = new ArrayList<>();
+                if (manageVO.getIncomeStatus() != null && manageVO.getIncomeStatus().size() > 0) {
+                    CriteriaBuilder.In<Object> in = criteriaBuilder.in(root.get("incomeStatus"));
+                    for (IncomeStatusEnum incomeStatus : manageVO.getIncomeStatus()) {
+                        in.value(incomeStatus);
+                    }
+                    list.add(in);
+                }
+                predicates.addAll(list);
+            }
+            if(Objects.nonNull(manageVO.getStatus())){
+                predicates.add(criteriaBuilder.equal(root.get("status"), manageVO.getStatus()));
+            }
+            if(Objects.nonNull(manageVO.getStartTime())){
+                predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("happenTime"), manageVO.getStartTime()));
+            }
+            if(Objects.nonNull(manageVO.getEndTime())){
+                predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("happenTime"), manageVO.getEndTime()));
+            }
+            if(Objects.nonNull(manageVO.getMinAmount()) && manageVO.getMinAmount() > 0){
+                predicates.add(criteriaBuilder.ge(root.get("amount"), manageVO.getMinAmount()));
+            }
+            if(Objects.nonNull(manageVO.getMaxAmount()) && manageVO.getMaxAmount() > 0){
+                predicates.add(criteriaBuilder.le(root.get("amount"), manageVO.getMaxAmount()));
+            }
+            if(manageVO.getDeptIds() != null && manageVO.getDeptIds().size() > 0){
+                List<Predicate> list = new ArrayList<>();
+                if (manageVO.getDeptIds() != null && manageVO.getDeptIds().size() > 0) {
+                    CriteriaBuilder.In<Object> in = criteriaBuilder.in(root.get("deptId"));
+                    for (Long id : manageVO.getDeptIds()) {
+                        in.value(id);
+                    }
+                    list.add(in);
+                }
+                predicates.addAll(list);
+            }
+            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
+        };
+        Long count = manageRepository.count(specification);
+        if (count == 0) {
+            return;
+        }
+        List<Manage> totalManages =  manageRepository.findAll(specification);
+        BigDecimal sum = new BigDecimal("0");
+        for (Manage manage: totalManages) {
+            if (Objects.equals(manage.getType(), ManageTypeEnum.COST)){
+                sum = sum.subtract(new BigDecimal(Double.toString(manage.getAmount())));
+            }else {
+                sum = sum.add(new BigDecimal(Double.toString(manage.getAmount())));
+            }
+        }
+        if(Objects.isNull(totalManages)){
+            return;
+
+        }
+        List<Long> deptIds = new ArrayList<>();
+        List<Long> projectIds = new ArrayList<>();
+        List<Long> productIds = new ArrayList<>();
+        List<Long> companyIds = new ArrayList<>();
+        List<Long> costTypeIds = new ArrayList<>();
+        for(Manage m : totalManages){
+            m.setTotalAmount(sum.doubleValue());
+            if(Objects.nonNull(m.getDeptId())){
+                deptIds.add(m.getDeptId());
+            }
+            if(Objects.nonNull(m.getProjectId())){
+                projectIds.add(m.getProjectId());
+            }
+            if(Objects.nonNull(m.getToDeptId())){
+                deptIds.add(m.getToDeptId());
+            }
+            if(Objects.nonNull(m.getProductId())){
+                productIds.add(m.getProductId());
+            }
+            if(Objects.nonNull(m.getCostTypeId())){
+                costTypeIds.add(m.getCostTypeId());
+            }
+            if(Objects.nonNull(m.getToProjectId())){
+                projectIds.add(m.getToProjectId());
+            }
+            if(Objects.nonNull(m.getToDeptId())){
+                deptIds.add(m.getToDeptId());
+            }
+        }
+
+        //部门
+        List<Department> departments = departmentRepository.findByIds(deptIds);
+        Map<Long, Department> departmentMap = new HashMap<>();
+        departments.stream().forEach(department -> {
+            departmentMap.put(department.getId(), department);
+        });
+        //项目
+        List<Project> projects = projectRepository.findByIds(projectIds);
+        Map<Long, Project> projectMap = new HashMap<>();
+        projects.stream().forEach(project -> {
+            projectMap.put(project.getId(), project);
+        });
+        //产品
+        List<Product> products = productRepository.findByIds(productIds);
+        Map<Long, Product> producMap = new HashMap<>();
+        products.stream().forEach(product -> {
+            producMap.put(product.getId(), product);
+        });
+        //支出发起人
+        List<Company> companies = companyRepository.findByIds(companyIds);
+        Map<Long, Company> companyMap = new HashMap<>();
+        companies.stream().forEach(company -> {
+            companyMap.put(company.getId(), company);
+        });
+        //成本类型
+        List<CostType> costTypes = costTypeRepository.findByIds(costTypeIds);
+        Map<Long, CostType> costTypeMap = new HashMap<>();
+        costTypes.stream().forEach(costType -> {
+            costTypeMap.put(costType.getId(), costType);
+        });
+
+        for(Manage m : totalManages){
+            m.setDepartment(departmentMap.get(m.getDeptId()));
+            m.setProject(projectMap.get(m.getProjectId()));
+            m.setProduct(producMap.get(m.getProductId()));
+            m.setToProject(projectMap.get(m.getToProjectId()));
+            if(Objects.nonNull(m.getCostTypeId())){
+                m.setCostType(costTypeMap.get(m.getCostTypeId()));
+            }
+            m.setToDepartment(departmentMap.get(m.getToDeptId()));
+        };
+
+
+        HSSFWorkbook workbook = new HSSFWorkbook();
+        HSSFSheet sheet = workbook.createSheet("经营数据管理平台数据统计表");
+        createTitle(workbook, sheet, manageVO.getType());
+
+        for (int i = 0; i < totalManages.size(); i++){
+            Manage m = totalManages.get(i);
+            HSSFRow r = sheet.createRow(i + 1);
+            HSSFCell cell;
+            cell = r.createCell(0);
+            cell.setCellValue(m.getHappenTime().toString());
+
+            cell = r.createCell(1);
+            cell.setCellValue(m.getAmount());
+            if(Objects.equals(manageVO.getType(), ManageTypeEnum.INCOME)){
+                cell = r.createCell(2);
+                cell.setCellValue(m.getIncomeStatus().toString());
+
+                cell = r.createCell(3);
+                cell.setCellValue(m.getProduct().getName());
+
+                cell = r.createCell(4);
+                cell.setCellValue(m.getProject().getName());
+
+                cell = r.createCell(5);
+                cell.setCellValue(m.getDepartment().getName());
+
+                cell = r.createCell(6);
+                cell.setCellValue(m.getToDepartment().getName());
+
+                cell = r.createCell(7);
+                cell.setCellValue(m.getRemark());
+
+                cell = r.createCell(8);
+                cell.setCellValue(m.getGmtModified().toString());
+
+
+            }else {
+                cell = r.createCell(2);
+                cell.setCellValue(m.getCostType().getName());
+
+                cell = r.createCell(3);
+                cell.setCellValue(m.getDepartment().getName());
+
+                cell = r.createCell(4);
+                cell.setCellValue(m.getProject().getName());
+
+                cell = r.createCell(5);
+                cell.setCellValue(m.getToDepartment().getName());
+
+                cell = r.createCell(6);
+                cell.setCellValue(m.getRemark());
+
+                cell = r.createCell(7);
+                cell.setCellValue(m.getGmtModified().toString());
+            }
+        }
+        //生成excel文件
+
+        buildExcelFile("经营数据管理平台数据统计表_" + TimeUtil.getPreDateStr() + ".xls", workbook);
+
+        //浏览器下载excel
+        buildExcelDocument("经营数据管理平台数据统计表_" + TimeUtil.getPreDateStr() + ".xls",workbook, response);
+
+    }
+
+    //生成excel文件
+    protected void buildExcelFile(String filename,HSSFWorkbook workbook) throws Exception{
+        FileOutputStream fos = new FileOutputStream(filename);
+        workbook.write(fos);
+        fos.flush();
+        fos.close();
+    }
+
+    //浏览器下载excel
+    protected void buildExcelDocument(String filename,HSSFWorkbook workbook,HttpServletResponse response) throws Exception{
+        response.setContentType("application/vnd.ms-excel");
+        response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(filename, "utf-8"));
+        OutputStream outputStream = response.getOutputStream();
+        workbook.write(outputStream);
+        outputStream.flush();
+        outputStream.close();
+    }
+
+    private void createTitle(HSSFWorkbook workbook, HSSFSheet sheet, ManageTypeEnum type) {
+        HSSFRow row0 = sheet.createRow(0);
+
+        int columnNum = 10;
+        for (int i = 0; i < columnNum; i++) {
+            sheet.setColumnWidth(i, 20 * 256);
+        }
+
+        //设置为居中
+        HSSFCellStyle style = workbook.createCellStyle();
+        HSSFFont font = workbook.createFont();
+        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
+        style.setFont(font);
+        HSSFCell cell;
+
+        if(Objects.equals(type, ManageTypeEnum.INCOME)){
+            cell = row0.createCell(0);
+            cell.setCellValue("发生日期");
+            cell.setCellStyle(style);
+
+            cell = row0.createCell(1);
+            cell.setCellValue("金额");
+            cell.setCellStyle(style);
+
+            cell = row0.createCell(2);
+            cell.setCellValue("收入状态");
+            cell.setCellStyle(style);
+
+            cell = row0.createCell(3);
+            cell.setCellValue("产品");
+            cell.setCellStyle(style);
+
+            cell = row0.createCell(4);
+            cell.setCellValue("归属项目");
+            cell.setCellStyle(style);
+
+            cell = row0.createCell(5);
+            cell.setCellValue("归属部门");
+            cell.setCellStyle(style);
+
+            cell = row0.createCell(6);
+            cell.setCellValue("客户");
+            cell.setCellStyle(style);
+
+            cell = row0.createCell(7);
+            cell.setCellValue("备注");
+            cell.setCellStyle(style);
+
+            cell = row0.createCell(8);
+            cell.setCellValue("录入日期");
+            cell.setCellStyle(style);
+        }else {
+            cell = row0.createCell(0);
+            cell.setCellValue("发生日期");
+            cell.setCellStyle(style);
+
+            cell = row0.createCell(1);
+            cell.setCellValue("金额");
+            cell.setCellStyle(style);
+
+            cell = row0.createCell(2);
+            cell.setCellValue("成本类型");
+            cell.setCellStyle(style);
+
+            cell = row0.createCell(3);
+            cell.setCellValue("发生部门");
+            cell.setCellStyle(style);
+
+            cell = row0.createCell(4);
+            cell.setCellValue("发生项目/人");
+            cell.setCellStyle(style);
+
+            cell = row0.createCell(5);
+            cell.setCellValue("供应商");
+            cell.setCellStyle(style);
+
+            cell = row0.createCell(6);
+            cell.setCellValue("备注");
+            cell.setCellStyle(style);
+
+            cell = row0.createCell(7);
+            cell.setCellValue("录入日期");
+            cell.setCellStyle(style);
+        }
+
+
+    }
+
+    @Override
     public ApiResult save(Manage manage) {
         if(Objects.isNull(manage)){
             return ApiResult.error(ApiCode.PARAMETER_ERROR);

+ 20 - 0
efunbox-base/efunbox-base-web/src/main/java/cn/efunbox/manage/base/controller/ManageController.java

@@ -7,9 +7,11 @@ import cn.efunbox.manage.base.service.DepartmentService;
 import cn.efunbox.manage.base.service.ManageService;
 import cn.efunbox.manage.base.vo.ManageVO;
 import cn.efunbox.manage.common.result.ApiResult;
+import org.apache.http.HttpResponse;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.http.HttpServletResponse;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
@@ -46,6 +48,24 @@ public class ManageController {
         return manageService.list(manageVO);
     }
 
+    @GetMapping("/export")
+    public void export( ManageVO manageVO, HttpServletResponse response) throws Exception{
+        List<Long> deptIds = new ArrayList<>();
+        if(null == manageVO.getDeptId() || manageVO.getDeptId().size() ==0){
+            ApiResult<List<Department>> deptResult = departmentService.getDeptChild(12345l, DeptTypeEnum.COMPANY);
+            deptResult.getData().stream().forEach(department -> {
+                deptIds.add(department.getId());
+            });
+        }else {
+            manageVO.getDeptId().stream().forEach(ids -> {
+                deptIds.add(ids);
+            });
+        }
+
+        manageVO.setDeptIds(deptIds);
+        manageService.export(manageVO, response);
+    }
+
     @PostMapping
     public ApiResult save(@RequestBody Manage manage){
         return manageService.save(manage);

+ 5 - 5
manage-common/src/main/java/cn/efunbox/manage/common/intercepter/AdminInterceptor.java

@@ -34,11 +34,11 @@ public class AdminInterceptor implements HandlerInterceptor {
         }
         ServletOutputStream out = response.getOutputStream();
         String token = request.getHeader("uid");
-        if (StringUtils.isBlank(token)) {
-            ApiResult invalidTokenResult = ApiResult.error(ApiCode.INVALID_TOKEN);
-            out.write(JSONObject.toJSONString(invalidTokenResult).getBytes("UTF-8"));
-            return false;
-        }
+//        if (StringUtils.isBlank(token)) {
+//            ApiResult invalidTokenResult = ApiResult.error(ApiCode.INVALID_TOKEN);
+//            out.write(JSONObject.toJSONString(invalidTokenResult).getBytes("UTF-8"));
+//            return false;
+//        }
 //        String user = redisTemplate.opsForValue().get(Constants.EFUNBOX_ADMIN_TOKEN_PREFIX + token);
 //        if (StringUtils.isBlank(user)) {
 //            ApiResult invalidTokenResult = ApiResult.error(ApiCode.INVALID_TOKEN);