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

import me.zhengjie.modules.dm.dmReadingRoomConfigOverdue.domain.DmReadingRoomConfigOverdue;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.dmReadingRoomConfigOverdue.repository.DmReadingRoomConfigOverdueRepository;
import me.zhengjie.modules.dm.dmReadingRoomConfigOverdue.service.DmReadingRoomConfigOverdueService;
import me.zhengjie.modules.dm.dmReadingRoomConfigOverdue.service.dto.DmReadingRoomConfigOverdueDto;
import me.zhengjie.modules.dm.dmReadingRoomConfigOverdue.service.dto.DmReadingRoomConfigOverdueQueryCriteria;
import me.zhengjie.modules.dm.dmReadingRoomConfigOverdue.service.mapstruct.DmReadingRoomConfigOverdueMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
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://el-admin.vip
* @description 服务实现
* @author longda
* @date 2022-03-02
**/
@Service
@RequiredArgsConstructor
public class DmReadingRoomConfigOverdueServiceImpl implements DmReadingRoomConfigOverdueService {

    private final DmReadingRoomConfigOverdueRepository dmReadingRoomConfigOverdueRepository;
    private final DmReadingRoomConfigOverdueMapper dmReadingRoomConfigOverdueMapper;

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

    @Override
    public List<DmReadingRoomConfigOverdueDto> queryAll(DmReadingRoomConfigOverdueQueryCriteria criteria){
        return dmReadingRoomConfigOverdueMapper.toDto(dmReadingRoomConfigOverdueRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmReadingRoomConfigOverdueDto findById(String configOverdueId) {
        DmReadingRoomConfigOverdue dmReadingRoomConfigOverdue = dmReadingRoomConfigOverdueRepository.findById(configOverdueId).orElseGet(DmReadingRoomConfigOverdue::new);
        ValidationUtil.isNull(dmReadingRoomConfigOverdue.getConfigOverdueId(),"DmReadingRoomConfigOverdue","configOverdueId",configOverdueId);
        return dmReadingRoomConfigOverdueMapper.toDto(dmReadingRoomConfigOverdue);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmReadingRoomConfigOverdueDto create(DmReadingRoomConfigOverdue resources) {
        resources.setConfigOverdueId(IdUtil.simpleUUID()); 
        return dmReadingRoomConfigOverdueMapper.toDto(dmReadingRoomConfigOverdueRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DmReadingRoomConfigOverdue resources) {
        DmReadingRoomConfigOverdue dmReadingRoomConfigOverdue = dmReadingRoomConfigOverdueRepository.findById(resources.getConfigOverdueId()).orElseGet(DmReadingRoomConfigOverdue::new);
        ValidationUtil.isNull( dmReadingRoomConfigOverdue.getConfigOverdueId(),"DmReadingRoomConfigOverdue","id",resources.getConfigOverdueId());
        dmReadingRoomConfigOverdue.copy(resources);
        dmReadingRoomConfigOverdueRepository.save(dmReadingRoomConfigOverdue);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String configOverdueId : ids) {
            dmReadingRoomConfigOverdueRepository.deleteById(configOverdueId);
        }
    }

    @Override
    public void download(List<DmReadingRoomConfigOverdueDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmReadingRoomConfigOverdueDto dmReadingRoomConfigOverdue : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("设置逾期时间内容", dmReadingRoomConfigOverdue.getConfigOverdueContent());
            map.put("创建人", dmReadingRoomConfigOverdue.getCreateBy());
            map.put("修改人", dmReadingRoomConfigOverdue.getUpdateBy());
            map.put("创建时间", dmReadingRoomConfigOverdue.getCreateTime());
            map.put("修改时间", dmReadingRoomConfigOverdue.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}