|
@@ -1,7 +1,9 @@
|
|
|
package cn.efunbox.manage.base.service.impl;
|
|
|
|
|
|
-import cn.efunbox.manage.base.entity.Manage;
|
|
|
-import cn.efunbox.manage.base.repository.ManageRepository;
|
|
|
+import cn.efunbox.manage.base.entity.*;
|
|
|
+import cn.efunbox.manage.base.enums.CompanyTypeEnum;
|
|
|
+import cn.efunbox.manage.base.enums.ManageTypeEnum;
|
|
|
+import cn.efunbox.manage.base.repository.*;
|
|
|
import cn.efunbox.manage.base.service.ManageService;
|
|
|
import cn.efunbox.manage.common.result.ApiCode;
|
|
|
import cn.efunbox.manage.common.result.ApiResult;
|
|
@@ -9,14 +11,15 @@ import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.bind.annotation.Mapping;
|
|
|
|
|
|
import javax.persistence.criteria.CriteriaBuilder;
|
|
|
import javax.persistence.criteria.CriteriaQuery;
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
import javax.persistence.criteria.Root;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+import static cn.efunbox.manage.base.enums.CompanyTypeEnum.COMPANY;
|
|
|
|
|
|
/**
|
|
|
* ManageSreviceImpl
|
|
@@ -28,6 +31,21 @@ public class ManageServiceImpl implements ManageService {
|
|
|
@Autowired
|
|
|
ManageRepository manageRepository;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ DepartmentRepository departmentRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ProjectRepository projectRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ProductRepository productRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ CompanyRepository companyRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ CostTypeRepository costTypeRepository;
|
|
|
+
|
|
|
@Override
|
|
|
public ApiResult list(Manage manage) {
|
|
|
Specification querySpecifi = new Specification<Manage>() {
|
|
@@ -47,7 +65,7 @@ public class ManageServiceImpl implements ManageService {
|
|
|
if(Objects.nonNull(manage.getType())){
|
|
|
predicates.add(criteriaBuilder.equal(root.get("type"), manage.getType()));
|
|
|
}
|
|
|
- if(!StringUtils.equals("0", manage.getCostType()) && StringUtils.isNotBlank(manage.getCostType())){
|
|
|
+ if(0 == manage.getCostTypeId() && Objects.nonNull(manage.getCostType())){
|
|
|
predicates.add(criteriaBuilder.equal(root.get("costType"), manage.getCostType()));
|
|
|
}
|
|
|
if(Objects.nonNull(manage.getIncomeStatus())){
|
|
@@ -68,14 +86,99 @@ public class ManageServiceImpl implements ManageService {
|
|
|
}
|
|
|
};
|
|
|
List<Manage> manages = manageRepository.findAll(querySpecifi);
|
|
|
- if(Objects.nonNull(manages)){
|
|
|
- return ApiResult.ok(manages);
|
|
|
+ if(Objects.isNull(manages)){
|
|
|
+ return ApiResult.error(ApiCode.UNKNOWN_ERROR);
|
|
|
+
|
|
|
}
|
|
|
- return ApiResult.error(ApiCode.UNKNOWN_ERROR);
|
|
|
+ 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<>();
|
|
|
+ manages.stream().forEach(m -> {
|
|
|
+ deptIds.add(m.getDeptId());
|
|
|
+ projectIds.add(m.getProjectId());
|
|
|
+ if(Objects.equals(m.getType(), ManageTypeEnum.COST)){
|
|
|
+ switch (m.getCompanyType()){
|
|
|
+ case COMPANY:
|
|
|
+ companyIds.add(m.getCompanyId());
|
|
|
+ break;
|
|
|
+ case PROJECT:
|
|
|
+ projectIds.add(m.getCompanyId());
|
|
|
+ break;
|
|
|
+ case DEPARTMENT:
|
|
|
+ deptIds.add(m.getCompanyId());
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ productIds.add(m.getProductId());
|
|
|
+ }
|
|
|
+ if(Objects.nonNull(m.getCostTypeId())){
|
|
|
+ costTypeIds.add(m.getCostTypeId());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ //部门
|
|
|
+ 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);
|
|
|
+ });
|
|
|
+
|
|
|
+ manages.stream().forEach(m -> {
|
|
|
+ m.setDepartment(departmentMap.get(m.getDeptId()));
|
|
|
+ m.setProject(projectMap.get(m.getProjectId()));
|
|
|
+ m.setProduct(producMap.get(m.getProductId()));
|
|
|
+ if(Objects.nonNull(m.getCostTypeId())){
|
|
|
+ m.setCostType(costTypeMap.get(m.getCostTypeId()));
|
|
|
+ }
|
|
|
+ switch (m.getCompanyType()){
|
|
|
+ case COMPANY:
|
|
|
+ m.setCompany(companyMap.get(m.getCompanyId()));
|
|
|
+ break;
|
|
|
+ case PROJECT:
|
|
|
+ m.setCompany(companyMap.get(m.getProjectId()));
|
|
|
+ break;
|
|
|
+ case DEPARTMENT:
|
|
|
+ m.setCompany(companyMap.get(m.getDeptId()));
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return ApiResult.ok(manages);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResult save(Manage manage) {
|
|
|
+ if(Objects.isNull(manage)){
|
|
|
+ return ApiResult.error(ApiCode.PARAMETER_ERROR);
|
|
|
+ }
|
|
|
manage = manageRepository.save(manage);
|
|
|
if(Objects.nonNull(manage)){
|
|
|
return ApiResult.ok(manage);
|
|
@@ -85,10 +188,38 @@ public class ManageServiceImpl implements ManageService {
|
|
|
|
|
|
@Override
|
|
|
public ApiResult update(Manage manage) {
|
|
|
+ if(Objects.isNull(manage)){
|
|
|
+ return ApiResult.error(ApiCode.PARAMETER_ERROR);
|
|
|
+ }
|
|
|
manage = manageRepository.update(manage);
|
|
|
if(Objects.nonNull(manage)){
|
|
|
return ApiResult.ok(manage);
|
|
|
}
|
|
|
return ApiResult.error(ApiCode.UNKNOWN_ERROR);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ApiResult getById(Long manageId) {
|
|
|
+ if(Objects.isNull(manageId)){
|
|
|
+ return ApiResult.error(ApiCode.PARAMETER_ERROR);
|
|
|
+ }
|
|
|
+ Manage manage = manageRepository.find(manageId);
|
|
|
+ manage.setDepartment(departmentRepository.find(manage.getDeptId()));
|
|
|
+ manage.setProject(projectRepository.find(manage.getProjectId()));
|
|
|
+ manage.setProduct(productRepository.find(manage.getProductId()));
|
|
|
+ switch (manage.getCompanyType()){
|
|
|
+ case COMPANY:
|
|
|
+ manage.setCompany(companyRepository.find(manage.getCompanyId()));
|
|
|
+ break;
|
|
|
+ case PROJECT:
|
|
|
+ manage.setCompany(projectRepository.find(manage.getCompanyId()));
|
|
|
+ break;
|
|
|
+ case DEPARTMENT:
|
|
|
+ manage.setCompany(departmentRepository.find(manage.getDeptId()));
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return ApiResult.error(ApiCode.UNKNOWN_ERROR);
|
|
|
+ }
|
|
|
}
|