/* * 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.capital.service.impl; import me.zhengjie.exception.BadRequestException; import me.zhengjie.modules.capital.domain.CapitalAccountMoney; import me.zhengjie.modules.constant.Constant; import me.zhengjie.utils.ValidationUtil; import me.zhengjie.utils.FileUtil; import lombok.RequiredArgsConstructor; import me.zhengjie.modules.capital.repository.CapitalAccountMoneyRepository; import me.zhengjie.modules.capital.service.CapitalAccountMoneyService; import me.zhengjie.modules.capital.service.dto.CapitalAccountMoneyDto; import me.zhengjie.modules.capital.service.dto.CapitalAccountMoneyQueryCriteria; import me.zhengjie.modules.capital.service.mapstruct.CapitalAccountMoneyMapper; 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.util.List; import java.util.Map; import java.io.IOException; import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; import java.util.LinkedHashMap; /** * @website https://eladmin.vip * @description 服务实现 * @author 赵士淞 * @date 2023-04-12 **/ @Service @RequiredArgsConstructor public class CapitalAccountMoneyServiceImpl implements CapitalAccountMoneyService { private final CapitalAccountMoneyRepository capitalAccountMoneyRepository; private final CapitalAccountMoneyMapper capitalAccountMoneyMapper; @Override public Map queryAll(CapitalAccountMoneyQueryCriteria criteria, Pageable pageable){ Page page = capitalAccountMoneyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); return PageUtil.toPage(page.map(capitalAccountMoneyMapper::toDto)); } @Override public List queryAll(CapitalAccountMoneyQueryCriteria criteria){ return capitalAccountMoneyMapper.toDto(capitalAccountMoneyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); } @Override @Transactional public CapitalAccountMoneyDto findById(Long id) { CapitalAccountMoney capitalAccountMoney = capitalAccountMoneyRepository.findById(id).orElseGet(CapitalAccountMoney::new); ValidationUtil.isNull(capitalAccountMoney.getId(),"CapitalAccountMoney","id",id); return capitalAccountMoneyMapper.toDto(capitalAccountMoney); } @Override @Transactional(rollbackFor = Exception.class) public CapitalAccountMoneyDto create(CapitalAccountMoney resources) { // 拷贝后校验 copyAfterCheck(resources); return capitalAccountMoneyMapper.toDto(capitalAccountMoneyRepository.save(resources)); } @Override @Transactional(rollbackFor = Exception.class) public void update(CapitalAccountMoney resources) { CapitalAccountMoney capitalAccountMoney = capitalAccountMoneyRepository.findById(resources.getId()).orElseGet(CapitalAccountMoney::new); ValidationUtil.isNull( capitalAccountMoney.getId(),"CapitalAccountMoney","id",resources.getId()); // 拷贝前校验 copyBeforeCheck(capitalAccountMoney); capitalAccountMoney.copy(resources); // 拷贝后校验 copyAfterCheck(capitalAccountMoney); capitalAccountMoneyRepository.save(capitalAccountMoney); } @Override public void deleteAll(Long[] ids) { for (Long id : ids) { capitalAccountMoneyRepository.deleteById(id); } } @Override public void download(List all, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); for (CapitalAccountMoneyDto capitalAccountMoney : all) { Map map = new LinkedHashMap<>(); map.put("账户名称", capitalAccountMoney.getAccountName()); map.put("账户年度", capitalAccountMoney.getAccountYear()); map.put("期初金额", capitalAccountMoney.getOpeningAmount()); map.put("警戒金额", capitalAccountMoney.getWarnAmount()); map.put("账户金额", capitalAccountMoney.getAccountAmount()); map.put("结算状态", capitalAccountMoney.getSettlementStatus()); map.put("备注", capitalAccountMoney.getRemarks()); map.put("创建者", capitalAccountMoney.getCreateBy()); map.put("创建时间", capitalAccountMoney.getCreateTime()); map.put("更新者", capitalAccountMoney.getUpdateBy()); map.put("更新时间", capitalAccountMoney.getUpdateTime()); list.add(map); } FileUtil.downloadExcel(list, response); } /** * 拷贝前校验 * @param resources */ private void copyBeforeCheck(CapitalAccountMoney resources) { // 判断结算状态 if (Constant.SETTLEMENT_STATUS_SETTLED.equals(resources.getSettlementStatus())) { throw new BadRequestException("已结算的现金流账户无法进行修改!"); } } /** * 拷贝后校验 * @param resources */ private void copyAfterCheck(CapitalAccountMoney resources) { // 根据账户年度查询 List capitalAccountMoneyList = capitalAccountMoneyRepository.findByAccountYear(resources.getAccountYear()); // 判断结果集长度 if (capitalAccountMoneyList.size() > 1) { throw new BadRequestException("每个年度只能有一个现金流账户!"); } else if (capitalAccountMoneyList.size() == 1) { if (resources.getId() == null || capitalAccountMoneyList.get(0).getId().longValue() != resources.getId().longValue()) { throw new BadRequestException("每个年度只能有一个现金流账户!"); } } } }