/* * 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.PayMonthlyReimbursement; import me.zhengjie.utils.ValidationUtil; import me.zhengjie.utils.FileUtil; import lombok.RequiredArgsConstructor; import me.zhengjie.modules.salary.repository.PayMonthlyReimbursementRepository; import me.zhengjie.modules.salary.service.PayMonthlyReimbursementService; import me.zhengjie.modules.salary.service.dto.PayMonthlyReimbursementDto; import me.zhengjie.modules.salary.service.dto.PayMonthlyReimbursementQueryCriteria; import me.zhengjie.modules.salary.service.mapstruct.PayMonthlyReimbursementMapper; 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 PayMonthlyReimbursementServiceImpl implements PayMonthlyReimbursementService { private final PayMonthlyReimbursementRepository payMonthlyReimbursementRepository; private final PayMonthlyReimbursementMapper payMonthlyReimbursementMapper; @Override public Map queryAll(PayMonthlyReimbursementQueryCriteria criteria, Pageable pageable){ Page page = payMonthlyReimbursementRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); return PageUtil.toPage(page.map(payMonthlyReimbursementMapper::toDto)); } @Override public List queryAll(PayMonthlyReimbursementQueryCriteria criteria){ return payMonthlyReimbursementMapper.toDto(payMonthlyReimbursementRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); } @Override @Transactional public PayMonthlyReimbursementDto findById(Long id) { PayMonthlyReimbursement payMonthlyReimbursement = payMonthlyReimbursementRepository.findById(id).orElseGet(PayMonthlyReimbursement::new); ValidationUtil.isNull(payMonthlyReimbursement.getId(),"PayMonthlyReimbursement","id",id); return payMonthlyReimbursementMapper.toDto(payMonthlyReimbursement); } @Override @Transactional(rollbackFor = Exception.class) public PayMonthlyReimbursementDto create(PayMonthlyReimbursement resources) { Snowflake snowflake = IdUtil.createSnowflake(1, 1); resources.setId(snowflake.nextId()); return payMonthlyReimbursementMapper.toDto(payMonthlyReimbursementRepository.save(resources)); } @Override @Transactional(rollbackFor = Exception.class) public void update(PayMonthlyReimbursement resources) { PayMonthlyReimbursement payMonthlyReimbursement = payMonthlyReimbursementRepository.findById(resources.getId()).orElseGet(PayMonthlyReimbursement::new); ValidationUtil.isNull( payMonthlyReimbursement.getId(),"PayMonthlyReimbursement","id",resources.getId()); payMonthlyReimbursement.copy(resources); payMonthlyReimbursementRepository.save(payMonthlyReimbursement); } @Override public void deleteAll(Long[] ids) { for (Long id : ids) { payMonthlyReimbursementRepository.deleteById(id); } } @Override public void download(List all, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); for (PayMonthlyReimbursementDto payMonthlyReimbursement : all) { Map map = new LinkedHashMap<>(); map.put("员工ID", payMonthlyReimbursement.getUserId()); map.put("年月日", payMonthlyReimbursement.getDate()); map.put("报销金额", payMonthlyReimbursement.getReimbursementCost()); map.put("备考", payMonthlyReimbursement.getRemarks()); list.add(map); } FileUtil.downloadExcel(list, response); } }