/* * 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.capitalFlow.domain.CapitalFlow; import me.zhengjie.modules.capitalFlow.domain.CapitalFlowExpend; import me.zhengjie.modules.capitalFlow.domain.CapitalFlowIncome; import me.zhengjie.modules.capitalFlow.repository.CapitalFlowExpendRepository; import me.zhengjie.modules.capitalFlow.repository.CapitalFlowIncomeRepository; import me.zhengjie.modules.system.service.dto.SysCompanyDto; import me.zhengjie.modules.system.service.impl.SysCompanyServiceImpl; import me.zhengjie.utils.*; import lombok.RequiredArgsConstructor; import me.zhengjie.modules.capitalFlow.repository.CapitalFlowRepository; import me.zhengjie.modules.capitalFlow.service.CapitalFlowService; import me.zhengjie.modules.capitalFlow.service.dto.CapitalFlowDto; import me.zhengjie.modules.capitalFlow.service.dto.CapitalFlowQueryCriteria; import me.zhengjie.modules.capitalFlow.service.mapstruct.CapitalFlowMapper; 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.util.List; import java.util.Map; import java.io.IOException; import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.function.Function; /** * @website https://eladmin.vip * @description 服务实现 * @author 赵士淞 * @date 2023-02-13 **/ @Service @RequiredArgsConstructor public class CapitalFlowServiceImpl implements CapitalFlowService { private final CapitalFlowRepository capitalFlowRepository; private final CapitalFlowMapper capitalFlowMapper; private final SysCompanyServiceImpl sysCompanyServiceImpl; private final CapitalFlowExpendRepository capitalFlowExpendRepository; private final CapitalFlowIncomeRepository capitalFlowIncomeRepository; @Override public Map queryAll(CapitalFlowQueryCriteria criteria, Pageable pageable){ // 判断资金流水查询类流水年月是否为空 if (criteria.getFlowYearMonth() != null && !"".equals(criteria.getFlowYearMonth())) { // 流水年月:国际时间转中国时间 criteria.setFlowYearMonth(DateUtil.internationalTimeToChinaTime(criteria.getFlowYearMonth())); } Page page = capitalFlowRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); // 将实体类转换为DTO(重写) Function func = new Function(){ @Override public CapitalFlowDto apply(CapitalFlow t) { // 将实体类转换成DTO CapitalFlowDto capitalFlowDto = capitalFlowMapper.toDto(t); // 公司名称 capitalFlowDto.setCompanyName(companyName(t)); // 流水支出 capitalFlowDto.setCapitalFlowExpends(capitalFlowExpendRepository.findByFlowId(t.getId())); // 流水支出金额 capitalFlowDto.setCapitalFlowExpendsAmount(capitalFlowExpendRepository.sumAmountByFlowId(t.getId())); // 流水收入 capitalFlowDto.setCapitalFlowIncomes(capitalFlowIncomeRepository.findByFlowId(t.getId())); // 流水收入金额 capitalFlowDto.setCapitalFlowIncomesAmount(capitalFlowIncomeRepository.sumAmountByFlowId(t.getId())); // 返回处理好的DTO return capitalFlowDto; } }; return PageUtil.toPage(page.map(func)); } @Override public List queryAll(CapitalFlowQueryCriteria criteria){ List capitalFlowDtoList = capitalFlowMapper.toDto(capitalFlowRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); // 循环资金流水列表 for (int i = 0; i < capitalFlowDtoList.size(); i ++) { // 资金流水 CapitalFlowDto capitalFlowDto = capitalFlowDtoList.get(i); // DTO赋值 capitalFlowDto.setCompanyName(companyName(capitalFlowMapper.toEntity(capitalFlowDto))); } return capitalFlowDtoList; } @Override @Transactional public CapitalFlowDto findById(Long id) { CapitalFlow capitalFlow = capitalFlowRepository.findById(id).orElseGet(CapitalFlow::new); ValidationUtil.isNull(capitalFlow.getId(),"CapitalFlow","id",id); return capitalFlowMapper.toDto(capitalFlow); } @Override @Transactional(rollbackFor = Exception.class) public CapitalFlowDto create(CapitalFlow resources) { return capitalFlowMapper.toDto(capitalFlowRepository.save(resources)); } @Override @Transactional(rollbackFor = Exception.class) public void update(CapitalFlow resources) { CapitalFlow capitalFlow = capitalFlowRepository.findById(resources.getId()).orElseGet(CapitalFlow::new); ValidationUtil.isNull( capitalFlow.getId(),"CapitalFlow","id",resources.getId()); capitalFlow.copy(resources); capitalFlowRepository.save(capitalFlow); } @Override public void deleteAll(Long[] ids) { for (Long id : ids) { capitalFlowRepository.deleteById(id); } } @Override public void download(List all, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); for (CapitalFlowDto capitalFlow : all) { String companyName = companyName(capitalFlowMapper.toEntity(capitalFlow)); Map map = new LinkedHashMap<>(); map.put("流水年月", capitalFlow.getFlowYearMonth()); map.put("所属公司", companyName); map.put("备注", capitalFlow.getRemarks()); map.put("创建者", capitalFlow.getCreateBy()); map.put("创建时间", capitalFlow.getCreateTime()); map.put("更新者", capitalFlow.getUpdateBy()); map.put("更新时间", capitalFlow.getUpdateTime()); list.add(map); } FileUtil.downloadExcel(list, response); } /** * 公司名称 * @param resources * @return */ private String companyName(CapitalFlow resources) { // 查询公司数据 SysCompanyDto sysCompanyDto = sysCompanyServiceImpl.findById(resources.getCompanyId()); return sysCompanyDto.getLabel(); } }