|
@@ -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,14 @@ 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.BufferedOutputStream;
|
|
|
+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 +315,373 @@ 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);
|
|
|
+ if(Objects.nonNull(m.getProduct())){
|
|
|
+ cell.setCellValue(m.getProduct().getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ cell = r.createCell(4);
|
|
|
+ if(Objects.nonNull(m.getProject())){
|
|
|
+ cell.setCellValue(m.getProject().getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ cell = r.createCell(5);
|
|
|
+ if(Objects.nonNull(m.getDepartment())){
|
|
|
+ cell.setCellValue(m.getDepartment().getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ cell = r.createCell(6);
|
|
|
+ if(Objects.nonNull(m.getToDepartment())){
|
|
|
+ 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);
|
|
|
+ if(Objects.nonNull(m.getCostType())){
|
|
|
+ cell.setCellValue(m.getCostType().getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ cell = r.createCell(3);
|
|
|
+ if(Objects.nonNull(m.getDepartment())){
|
|
|
+ cell.setCellValue(m.getDepartment().getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ cell = r.createCell(4);
|
|
|
+ if(Objects.nonNull(m.getProject())){
|
|
|
+ cell.setCellValue(m.getProject().getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ cell = r.createCell(5);
|
|
|
+ if(Objects.nonNull(m.getToDepartment())){
|
|
|
+ cell.setCellValue(m.getToDepartment().getName());
|
|
|
+ }
|
|
|
+
|
|
|
+ cell = r.createCell(6);
|
|
|
+ cell.setCellValue(m.getRemark());
|
|
|
+
|
|
|
+ cell = r.createCell(7);
|
|
|
+ cell.setCellValue(m.getGmtModified().toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //清空response
|
|
|
+ response.reset();
|
|
|
+ //设置response的Header
|
|
|
+ String fileName = "经营数据管理平台数据统计表_" + TimeUtil.getPreDateStr() + ".xls";
|
|
|
+ response.reset();
|
|
|
+ response.setContentType("contentType=application/vnd.ms-excel");
|
|
|
+ response.setHeader("Content-disposition","attachment;filename="+URLEncoder.encode(fileName,"utf-8"));
|
|
|
+ workbook.write(response.getOutputStream());
|
|
|
+ response.flushBuffer();
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|