/* * Copyright 2019-2020 Zheng Jie * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package me.zhengjie.modules.capitalFlow.service.impl; import me.zhengjie.modules.capital.service.dto.CapitalAccountCompanyDto; import me.zhengjie.modules.capital.service.dto.CapitalAccountMoneyDto; import me.zhengjie.modules.capital.service.impl.CapitalAccountCompanyServiceImpl; import me.zhengjie.modules.capital.service.impl.CapitalAccountMoneyServiceImpl; import me.zhengjie.modules.capitalFlow.domain.CapitalFlowExpend; import me.zhengjie.modules.capitalFlow.service.dto.CapitalFlowDto; import me.zhengjie.utils.*; import lombok.RequiredArgsConstructor; import me.zhengjie.modules.capitalFlow.repository.CapitalFlowExpendRepository; import me.zhengjie.modules.capitalFlow.service.CapitalFlowExpendService; import me.zhengjie.modules.capitalFlow.service.dto.CapitalFlowExpendDto; import me.zhengjie.modules.capitalFlow.service.dto.CapitalFlowExpendQueryCriteria; import me.zhengjie.modules.capitalFlow.service.mapstruct.CapitalFlowExpendMapper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.*; import java.io.IOException; import javax.servlet.http.HttpServletResponse; import java.util.function.Function; /** * @website https://eladmin.vip * @description 服务实现 * @author 赵士淞 * @date 2023-02-13 **/ @Service @RequiredArgsConstructor public class CapitalFlowExpendServiceImpl implements CapitalFlowExpendService { private final CapitalFlowExpendRepository capitalFlowExpendRepository; private final CapitalFlowExpendMapper capitalFlowExpendMapper; private final CapitalFlowServiceImpl capitalFlowServiceImpl; private final CapitalAccountMoneyServiceImpl capitalAccountMoneyServiceImpl; private final CapitalAccountCompanyServiceImpl capitalAccountCompanyServiceImpl; @Override public Map queryAll(CapitalFlowExpendQueryCriteria criteria, Pageable pageable){ Page page = capitalFlowExpendRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); // 将实体类转换为DTO(重写) Function func = new Function(){ @Override public CapitalFlowExpendDto apply(CapitalFlowExpend t) { // 将实体类转换成DTO CapitalFlowExpendDto capitalFlowExpendDto = capitalFlowExpendMapper.toDto(t); // 流水名称 capitalFlowExpendDto.setFlowName(flowName(t)); // 返回处理好的DTO return capitalFlowExpendDto; } }; return PageUtil.toPage(page.map(func)); } @Override public List queryAll(CapitalFlowExpendQueryCriteria criteria){ return capitalFlowExpendMapper.toDto(capitalFlowExpendRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); } @Override @Transactional public CapitalFlowExpendDto findById(Long id) { CapitalFlowExpend capitalFlowExpend = capitalFlowExpendRepository.findById(id).orElseGet(CapitalFlowExpend::new); ValidationUtil.isNull(capitalFlowExpend.getId(),"CapitalFlowExpend","id",id); return capitalFlowExpendMapper.toDto(capitalFlowExpend); } @Override @Transactional(rollbackFor = Exception.class) public CapitalFlowExpendDto create(CapitalFlowExpend resources) { return capitalFlowExpendMapper.toDto(capitalFlowExpendRepository.save(resources)); } @Override @Transactional(rollbackFor = Exception.class) public void update(CapitalFlowExpend resources) { CapitalFlowExpend capitalFlowExpend = capitalFlowExpendRepository.findById(resources.getId()).orElseGet(CapitalFlowExpend::new); ValidationUtil.isNull( capitalFlowExpend.getId(),"CapitalFlowExpend","id",resources.getId()); capitalFlowExpend.copy(resources); capitalFlowExpendRepository.save(capitalFlowExpend); } @Override public void deleteAll(Long[] ids) { for (Long id : ids) { capitalFlowExpendRepository.deleteById(id); } } @Override public void download(List all, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); for (CapitalFlowExpendDto capitalFlowExpend : all) { String flowName = flowName(capitalFlowExpendMapper.toEntity(capitalFlowExpend)); Map map = new LinkedHashMap<>(); map.put("所属流水", flowName); map.put("标题", capitalFlowExpend.getTitle()); map.put("金额", capitalFlowExpend.getAmount()); map.put("备注", capitalFlowExpend.getRemarks()); map.put("创建者", capitalFlowExpend.getCreateBy()); map.put("创建时间", capitalFlowExpend.getCreateTime()); map.put("更新者", capitalFlowExpend.getUpdateBy()); map.put("更新时间", capitalFlowExpend.getUpdateTime()); list.add(map); } FileUtil.downloadExcel(list, response); } /** * 流水名称 * @param resources * @return */ private String flowName(CapitalFlowExpend resources) { // 查询资金流水数据 CapitalFlowDto capitalFlowDto = capitalFlowServiceImpl.findById(resources.getFlowId()); // 查询年度账号数据 CapitalAccountCompanyDto capitalAccountCompanyDto = capitalAccountCompanyServiceImpl.findById(capitalFlowDto.getAccountCompanyId()); return new SimpleDateFormat("yyyy-MM").format(capitalFlowDto.getFlowYearMonth()) + " - " + capitalAccountCompanyDto.getAccountName(); } @Override @Transactional(rollbackFor = Exception.class) public void flowableCreate(String accountType, String title, BigDecimal amount) { // 关联年度资金现金流账户 CapitalAccountMoneyDto capitalAccountMoneyDto = capitalAccountMoneyServiceImpl.relevanceCapitalAccountMoney(); // 关联年度资金现金流账户(减少金额) capitalAccountMoneyServiceImpl.relevanceCapitalAccountMoneySubtract(capitalAccountMoneyDto, amount); // 关联年度资金公司流账户 CapitalAccountCompanyDto capitalAccountCompanyDto = capitalAccountCompanyServiceImpl.relevanceCapitalAccountCompany(accountType); // 关联年度资金公司流账户(减少金额) capitalAccountCompanyServiceImpl.relevanceCapitalAccountCompanySubtract(capitalAccountCompanyDto, amount); // 关联月度资金公司流账户 CapitalFlowDto capitalFlowDto = capitalFlowServiceImpl.relevanceCapitalFlow(capitalAccountCompanyDto.getId()); // 关联资金流水支出(增加金额) relevanceCapitalFlowExpendAdd(capitalFlowDto.getId(), title, amount); } @Override @Transactional(rollbackFor = Exception.class) public void flowableDelete(String accountType, String title, BigDecimal amount) { // 关联年度资金现金流账户 CapitalAccountMoneyDto capitalAccountMoneyDto = capitalAccountMoneyServiceImpl.relevanceCapitalAccountMoney(); // 关联年度资金现金流账户(增加金额) capitalAccountMoneyServiceImpl.relevanceCapitalAccountMoneyAdd(capitalAccountMoneyDto, amount); // 关联年度资金公司流账户 CapitalAccountCompanyDto capitalAccountCompanyDto = capitalAccountCompanyServiceImpl.relevanceCapitalAccountCompany(accountType); // 关联年度资金公司流账户(增加金额) capitalAccountCompanyServiceImpl.relevanceCapitalAccountCompanyAdd(capitalAccountCompanyDto, amount); // 关联月度资金公司流账户 CapitalFlowDto capitalFlowDto = capitalFlowServiceImpl.relevanceCapitalFlow(capitalAccountCompanyDto.getId()); // 关联资金流水支出(减少金额) relevanceCapitalFlowExpendSubtract(capitalFlowDto.getId(), title, amount); } /** * 关联资金流水支出(增加金额) * @param capitalFlowId * @param title * @param amount */ public void relevanceCapitalFlowExpendAdd(Long capitalFlowId, String title, BigDecimal amount) { // 临时金额 BigDecimal tempAmount = new BigDecimal(0); tempAmount = tempAmount.add(amount); // 流水支出实体类 CapitalFlowExpend capitalFlowExpend = new CapitalFlowExpend(); capitalFlowExpend.setFlowId(capitalFlowId); capitalFlowExpend.setTitle(title); capitalFlowExpend.setAmount(tempAmount); // 保存 capitalFlowExpendRepository.save(capitalFlowExpend); } /** * 关联资金流水支出(减少金额) * @param capitalFlowId * @param title * @param amount */ public void relevanceCapitalFlowExpendSubtract(Long capitalFlowId, String title, BigDecimal amount) { // 临时金额 BigDecimal tempAmount = new BigDecimal(0); tempAmount = tempAmount.subtract(amount); // 流水支出实体类 CapitalFlowExpend capitalFlowExpend = new CapitalFlowExpend(); capitalFlowExpend.setFlowId(capitalFlowId); capitalFlowExpend.setTitle(title); capitalFlowExpend.setAmount(tempAmount); // 保存 capitalFlowExpendRepository.save(capitalFlowExpend); } }