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

import me.zhengjie.modules.dm.dmReadingRoomConfig.domain.DmReadingRoomConfig;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.dmReadingRoomConfig.repository.DmReadingRoomConfigRepository;
import me.zhengjie.modules.dm.dmReadingRoomConfig.service.DmReadingRoomConfigService;
import me.zhengjie.modules.dm.dmReadingRoomConfig.service.dto.DmReadingRoomConfigDto;
import me.zhengjie.modules.dm.dmReadingRoomConfig.service.dto.DmReadingRoomConfigQueryCriteria;
import me.zhengjie.modules.dm.dmReadingRoomConfig.service.mapstruct.DmReadingRoomConfigMapper;
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-02-24
**/
@Service
@RequiredArgsConstructor
public class DmReadingRoomConfigServiceImpl implements DmReadingRoomConfigService {

    private final DmReadingRoomConfigRepository dmReadingRoomConfigRepository;
    private final DmReadingRoomConfigMapper dmReadingRoomConfigMapper;

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

    @Override
    public List<DmReadingRoomConfigDto> queryAll(DmReadingRoomConfigQueryCriteria criteria){
        return dmReadingRoomConfigMapper.toDto(dmReadingRoomConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmReadingRoomConfigDto findById(String borrowConfigId) {
        DmReadingRoomConfig dmReadingRoomConfig = dmReadingRoomConfigRepository.findById(borrowConfigId).orElseGet(DmReadingRoomConfig::new);
        ValidationUtil.isNull(dmReadingRoomConfig.getBorrowConfigId(),"DmReadingRoomConfig","borrowConfigId",borrowConfigId);
        return dmReadingRoomConfigMapper.toDto(dmReadingRoomConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmReadingRoomConfigDto create(DmReadingRoomConfig resources) {
        resources.setBorrowConfigId(IdUtil.simpleUUID()); 
        return dmReadingRoomConfigMapper.toDto(dmReadingRoomConfigRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DmReadingRoomConfig resources) {
        DmReadingRoomConfig dmReadingRoomConfig = dmReadingRoomConfigRepository.findById(resources.getBorrowConfigId()).orElseGet(DmReadingRoomConfig::new);
        ValidationUtil.isNull( dmReadingRoomConfig.getBorrowConfigId(),"DmReadingRoomConfig","id",resources.getBorrowConfigId());
        dmReadingRoomConfig.copy(resources);
        dmReadingRoomConfigRepository.save(dmReadingRoomConfig);
    }

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

    @Override
    public void download(List<DmReadingRoomConfigDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmReadingRoomConfigDto dmReadingRoomConfig : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("00 邮箱  01手机  02 短信", dmReadingRoomConfig.getBorrowMsgType());
            map.put("逾期金额", dmReadingRoomConfig.getOverdueAmount());
            map.put("扣费上限", dmReadingRoomConfig.getOverdueAmountMax());
            map.put("逾期时间设置提醒", dmReadingRoomConfig.getOverdueTimeMsg());
            map.put("创建人", dmReadingRoomConfig.getCreateBy());
            map.put("修改人", dmReadingRoomConfig.getUpdateBy());
            map.put("创建时间", dmReadingRoomConfig.getCreateTime());
            map.put("修改时间", dmReadingRoomConfig.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}