/* * 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.CapitalAccountCompany; 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.CapitalAccountCompanyRepository; import me.zhengjie.modules.capital.service.CapitalAccountCompanyService; import me.zhengjie.modules.capital.service.dto.CapitalAccountCompanyDto; import me.zhengjie.modules.capital.service.dto.CapitalAccountCompanyQueryCriteria; import me.zhengjie.modules.capital.service.mapstruct.CapitalAccountCompanyMapper; 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 CapitalAccountCompanyServiceImpl implements CapitalAccountCompanyService { private final CapitalAccountCompanyRepository capitalAccountCompanyRepository; private final CapitalAccountCompanyMapper capitalAccountCompanyMapper; @Override public Map queryAll(CapitalAccountCompanyQueryCriteria criteria, Pageable pageable){ Page page = capitalAccountCompanyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); return PageUtil.toPage(page.map(capitalAccountCompanyMapper::toDto)); } @Override public List queryAll(CapitalAccountCompanyQueryCriteria criteria){ return capitalAccountCompanyMapper.toDto(capitalAccountCompanyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); } @Override @Transactional public CapitalAccountCompanyDto findById(Long id) { CapitalAccountCompany capitalAccountCompany = capitalAccountCompanyRepository.findById(id).orElseGet(CapitalAccountCompany::new); ValidationUtil.isNull(capitalAccountCompany.getId(),"CapitalAccountCompany","id",id); return capitalAccountCompanyMapper.toDto(capitalAccountCompany); } @Override @Transactional(rollbackFor = Exception.class) public CapitalAccountCompanyDto create(CapitalAccountCompany resources) { // 拷贝后校验 copyAfterCheck(resources); return capitalAccountCompanyMapper.toDto(capitalAccountCompanyRepository.save(resources)); } @Override @Transactional(rollbackFor = Exception.class) public void update(CapitalAccountCompany resources) { CapitalAccountCompany capitalAccountCompany = capitalAccountCompanyRepository.findById(resources.getId()).orElseGet(CapitalAccountCompany::new); ValidationUtil.isNull( capitalAccountCompany.getId(),"CapitalAccountCompany","id",resources.getId()); // 拷贝前校验 copyBeforeCheck(capitalAccountCompany); capitalAccountCompany.copy(resources); // 拷贝后校验 copyAfterCheck(capitalAccountCompany); capitalAccountCompanyRepository.save(capitalAccountCompany); } @Override public void deleteAll(Long[] ids) { for (Long id : ids) { capitalAccountCompanyRepository.deleteById(id); } } @Override public void download(List all, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); for (CapitalAccountCompanyDto capitalAccountCompany : all) { Map map = new LinkedHashMap<>(); map.put("项目区分", capitalAccountCompany.getProjectDistinguish()); map.put("账户类型", capitalAccountCompany.getAccountType()); map.put("账户名称", capitalAccountCompany.getAccountName()); map.put("账户年度", capitalAccountCompany.getAccountYear()); map.put("期初金额", capitalAccountCompany.getOpeningAmount()); map.put("警戒金额", capitalAccountCompany.getWarnAmount()); map.put("账户金额", capitalAccountCompany.getAccountAmount()); map.put("结算状态", capitalAccountCompany.getSettlementStatus()); map.put("备注", capitalAccountCompany.getRemarks()); map.put("创建者", capitalAccountCompany.getCreateBy()); map.put("创建时间", capitalAccountCompany.getCreateTime()); map.put("更新者", capitalAccountCompany.getUpdateBy()); map.put("更新时间", capitalAccountCompany.getUpdateTime()); list.add(map); } FileUtil.downloadExcel(list, response); } /** * 拷贝前校验 * @param resources */ private void copyBeforeCheck(CapitalAccountCompany resources) { // 判断结算状态 if (Constant.SETTLEMENT_STATUS_SETTLED.equals(resources.getSettlementStatus())) { throw new BadRequestException("已结算的公司流账户无法进行修改!"); } } /** * 拷贝后校验 * @param resources */ private void copyAfterCheck(CapitalAccountCompany resources) { // 根据项目区分、账户类型、账户年度查询 List capitalAccountCompanyList = capitalAccountCompanyRepository.findByProjectDistinguishAndAccountTypeAndAccountYear(resources.getProjectDistinguish(), resources.getAccountType(), resources.getAccountYear()); // 判断结果集长度 if (capitalAccountCompanyList.size() > 1) { throw new BadRequestException("每个年度同类型公司流账户只能有一个!"); } else if (capitalAccountCompanyList.size() == 1) { if (resources.getId() == null || capitalAccountCompanyList.get(0).getId().longValue() != resources.getId().longValue()) { throw new BadRequestException("每个年度同类型公司流账户只能有一个!"); } } } }