yaozc
2023-07-07 c752e33fe402e2179e7e6795f567d41c044ca3c6
eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/business/service/impl/BusinessExpenseServiceImpl.java
@@ -18,19 +18,21 @@
import me.zhengjie.modules.business.domain.BusinessExpense;
import me.zhengjie.modules.business.domain.BusinessExpenseReimbursement;
import me.zhengjie.modules.business.repository.BusinessExpenseReimbursementRepository;
import me.zhengjie.modules.business.service.BusinessExpenseReimbursementService;
import me.zhengjie.modules.business.service.BusinessPlanService;
import me.zhengjie.modules.business.service.dto.BusinessExpenseReimbursementDto;
import me.zhengjie.modules.business.service.dto.BusinessPlanDto;
import me.zhengjie.modules.business.service.dto.*;
import me.zhengjie.modules.business.service.mapstruct.BusinessExpenseReimbursementMapper;
import me.zhengjie.modules.capitalFlow.service.CapitalFlowExpendService;
import me.zhengjie.modules.constant.Constant;
import me.zhengjie.modules.project.service.ProjectService;
import me.zhengjie.modules.project.service.dto.ProjectDto;
import me.zhengjie.modules.system.domain.DictDetail;
import me.zhengjie.modules.system.repository.DictDetailRepository;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.business.repository.BusinessExpenseRepository;
import me.zhengjie.modules.business.service.BusinessExpenseService;
import me.zhengjie.modules.business.service.dto.BusinessExpenseDto;
import me.zhengjie.modules.business.service.dto.BusinessExpenseQueryCriteria;
import me.zhengjie.modules.business.service.mapstruct.BusinessExpenseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -64,6 +66,9 @@
    private final ProjectService projectService;
    private final BusinessExpenseReimbursementRepository businessExpenseReimbursementRepository;
    private final BusinessExpenseReimbursementMapper businessExpenseReimbursementMapper;
    private final CapitalFlowExpendService capitalFlowExpendService;
    private final DictDetailRepository dictDetailRepository;
    private final BusinessExpenseReimbursementService businessExpenseReimbursementService;
    @Override
    public Map<String,Object> queryAll(BusinessExpenseQueryCriteria criteria, Pageable pageable){
@@ -117,6 +122,8 @@
        BusinessExpense businessExpense = businessExpenseRepository.save(businessExpenseMapper.toEntity(resources));
        // 保存相关营业费用明细
        saveBusinessExpenseReimbursement(resources.getBusinessExpenseReimbursementDtoList(), businessExpense.getId());
        // 关联新增
        accountCreate(resources);
        return findById(businessExpense.getId());
    }
@@ -125,10 +132,17 @@
    public void update(BusinessExpenseDto resources) {
        BusinessExpense businessExpense = businessExpenseRepository.findById(resources.getId()).orElseGet(BusinessExpense::new);
        ValidationUtil.isNull( businessExpense.getId(),"BusinessExpense","id",resources.getId());
        // 关联删除
        accountDelete(businessExpense);
        businessExpense.copy(businessExpenseMapper.toEntity(resources));
        businessExpenseRepository.save(businessExpense);
        // 保存相关营业费用明细
        saveBusinessExpenseReimbursement(resources.getBusinessExpenseReimbursementDtoList(), businessExpense.getId());
        // 实体类 转 DTO
        BusinessExpenseDto businessExpenseDto = businessExpenseMapper.toDto(businessExpense);
        businessExpenseDto.setBusinessExpenseReimbursementDtoList(resources.getBusinessExpenseReimbursementDtoList());
        // 关联新增
        accountCreate(businessExpenseDto);
    }
    /**
@@ -153,6 +167,10 @@
    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            BusinessExpense businessExpense = businessExpenseRepository.findById(id).orElseGet(BusinessExpense::new);
            ValidationUtil.isNull( businessExpense.getId(),"BusinessExpense","id",id);
            // 关联删除
            accountDelete(businessExpense);
            businessExpenseRepository.deleteById(id);
        }
    }
@@ -174,4 +192,43 @@
        }
        FileUtil.downloadExcel(list, response);
    }
    /**
     * 关联创建
     * @param resources
     */
    private void accountCreate(BusinessExpenseDto resources) {
        // 营业计划
        BusinessPlanDto businessPlanDto = businessPlanService.findById(resources.getBusinessPlanId());
        // 费用类型
        DictDetail dictDetail = dictDetailRepository.findByDictNameAndValue(Constant.EXPENSE_TYPE, resources.getExpenseType());
        // 费用金额
        BigDecimal amount = new BigDecimal(0);
        for (BusinessExpenseReimbursementDto businessExpenseReimbursementDto : resources.getBusinessExpenseReimbursementDtoList()) {
            amount = amount.add(businessExpenseReimbursementDto.getReimbursementAmount());
        }
        // 关联创建
        capitalFlowExpendService.flowableCreate(Constant.ACCOUNT_TYPE_QIYUAN, businessPlanDto.getBusinessContent() + " - " + dictDetail.getLabel(), amount);
    }
    /**
     * 关联删除
     * @param resources
     */
    private void accountDelete(BusinessExpense resources) {
        // 营业计划
        BusinessPlanDto businessPlanDto = businessPlanService.findById(resources.getBusinessPlanId());
        // 费用类型
        DictDetail dictDetail = dictDetailRepository.findByDictNameAndValue(Constant.EXPENSE_TYPE, resources.getExpenseType());
        // 费用金额
        BigDecimal amount = new BigDecimal(0);
        BusinessExpenseReimbursementQueryCriteria businessExpenseReimbursementQueryCriteria = new BusinessExpenseReimbursementQueryCriteria();
        businessExpenseReimbursementQueryCriteria.setBusinessExpenseId(resources.getId());
        List<BusinessExpenseReimbursementDto> businessExpenseReimbursementDtoList = businessExpenseReimbursementService.queryAll(businessExpenseReimbursementQueryCriteria);
        for (BusinessExpenseReimbursementDto businessExpenseReimbursementDto : businessExpenseReimbursementDtoList) {
            amount = amount.add(businessExpenseReimbursementDto.getReimbursementAmount());
        }
        // 关联删除
        capitalFlowExpendService.flowableDelete(Constant.ACCOUNT_TYPE_QIYUAN, businessPlanDto.getBusinessContent() + " - " + dictDetail.getLabel(), amount);
    }
}