/*
*  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.dm.card.service.impl;

import me.zhengjie.modules.dm.card.domain.DmCard;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.card.repository.DmCardRepository;
import me.zhengjie.modules.dm.card.service.DmCardService;
import me.zhengjie.modules.dm.card.service.dto.DmCardDto;
import me.zhengjie.modules.dm.card.service.dto.DmCardQueryCriteria;
import me.zhengjie.modules.dm.card.service.mapstruct.DmCardMapper;
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://el-admin.vip
* @description 服务实现
* @author Li Rui
* @date 2021-12-31
**/
@Service
@RequiredArgsConstructor
public class DmCardServiceImpl implements DmCardService {

    private final DmCardRepository dmCardRepository;
    private final DmCardMapper dmCardMapper;

    @Override
    public Map<String,Object> queryAll(DmCardQueryCriteria criteria, Pageable pageable){
        Page<DmCard> page = dmCardRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dmCardMapper::toDto));
    }

    @Override
    public List<DmCardDto> queryAll(DmCardQueryCriteria criteria){
        return dmCardMapper.toDto(dmCardRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmCardDto findById(Integer id) {
        DmCard dmCard = dmCardRepository.findById(id).orElseGet(DmCard::new);
        ValidationUtil.isNull(dmCard.getId(),"DmCard","id",id);
        return dmCardMapper.toDto(dmCard);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmCardDto create(DmCard resources) {
        return dmCardMapper.toDto(dmCardRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DmCard resources) {
        DmCard dmCard = dmCardRepository.findById(resources.getId()).orElseGet(DmCard::new);
        ValidationUtil.isNull( dmCard.getId(),"DmCard","id",resources.getId());
        dmCard.copy(resources);
        dmCardRepository.save(dmCard);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            dmCardRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DmCardDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmCardDto dmCard : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}