/* * 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.CapitalFlowExpend; import me.zhengjie.modules.capitalFlow.service.dto.CapitalFlowDto; import me.zhengjie.modules.system.service.dto.SysCompanyDto; import me.zhengjie.modules.system.service.impl.SysCompanyServiceImpl; import me.zhengjie.utils.ValidationUtil; import me.zhengjie.utils.FileUtil; 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 me.zhengjie.utils.PageUtil; import me.zhengjie.utils.QueryHelp; import java.sql.Timestamp; import java.text.SimpleDateFormat; 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 CapitalFlowExpendServiceImpl implements CapitalFlowExpendService { private final CapitalFlowExpendRepository capitalFlowExpendRepository; private final CapitalFlowExpendMapper capitalFlowExpendMapper; private final CapitalFlowServiceImpl capitalFlowServiceImpl; private final SysCompanyServiceImpl sysCompanyServiceImpl; @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()); // 查询公司数据 SysCompanyDto sysCompanyDto = sysCompanyServiceImpl.findById(capitalFlowDto.getCompanyId()); return new SimpleDateFormat("yyyy-MM").format(capitalFlowDto.getFlowYearMonth()) + " - " + sysCompanyDto.getLabel(); } }