/* * 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.salary.service.impl; import me.zhengjie.modules.salary.domain.PayMonthlySubsidy; import me.zhengjie.utils.ValidationUtil; import me.zhengjie.utils.FileUtil; import lombok.RequiredArgsConstructor; import me.zhengjie.modules.salary.repository.PayMonthlySubsidyRepository; import me.zhengjie.modules.salary.service.PayMonthlySubsidyService; import me.zhengjie.modules.salary.service.dto.PayMonthlySubsidyDto; import me.zhengjie.modules.salary.service.dto.PayMonthlySubsidyQueryCriteria; import me.zhengjie.modules.salary.service.mapstruct.PayMonthlySubsidyMapper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import cn.hutool.core.lang.Snowflake; import cn.hutool.core.util.IdUtil; 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-05-07 **/ @Service @RequiredArgsConstructor public class PayMonthlySubsidyServiceImpl implements PayMonthlySubsidyService { private final PayMonthlySubsidyRepository payMonthlySubsidyRepository; private final PayMonthlySubsidyMapper payMonthlySubsidyMapper; @Override public Map queryAll(PayMonthlySubsidyQueryCriteria criteria, Pageable pageable){ Page page = payMonthlySubsidyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); return PageUtil.toPage(page.map(payMonthlySubsidyMapper::toDto)); } @Override public List queryAll(PayMonthlySubsidyQueryCriteria criteria){ return payMonthlySubsidyMapper.toDto(payMonthlySubsidyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); } @Override @Transactional public PayMonthlySubsidyDto findById(Long id) { PayMonthlySubsidy payMonthlySubsidy = payMonthlySubsidyRepository.findById(id).orElseGet(PayMonthlySubsidy::new); ValidationUtil.isNull(payMonthlySubsidy.getId(),"PayMonthlySubsidy","id",id); return payMonthlySubsidyMapper.toDto(payMonthlySubsidy); } @Override @Transactional(rollbackFor = Exception.class) public PayMonthlySubsidyDto create(PayMonthlySubsidy resources) { Snowflake snowflake = IdUtil.createSnowflake(1, 1); resources.setId(snowflake.nextId()); return payMonthlySubsidyMapper.toDto(payMonthlySubsidyRepository.save(resources)); } @Override @Transactional(rollbackFor = Exception.class) public void update(PayMonthlySubsidy resources) { PayMonthlySubsidy payMonthlySubsidy = payMonthlySubsidyRepository.findById(resources.getId()).orElseGet(PayMonthlySubsidy::new); ValidationUtil.isNull( payMonthlySubsidy.getId(),"PayMonthlySubsidy","id",resources.getId()); payMonthlySubsidy.copy(resources); payMonthlySubsidyRepository.save(payMonthlySubsidy); } @Override public void deleteAll(Long[] ids) { for (Long id : ids) { payMonthlySubsidyRepository.deleteById(id); } } @Override public void download(List all, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); for (PayMonthlySubsidyDto payMonthlySubsidy : all) { Map map = new LinkedHashMap<>(); map.put("员工ID", payMonthlySubsidy.getUserId()); map.put("年月日", payMonthlySubsidy.getDate()); map.put("补助金额", payMonthlySubsidy.getSubsidyCost()); map.put("备考", payMonthlySubsidy.getRemarks()); list.add(map); } FileUtil.downloadExcel(list, response); } }