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

import me.zhengjie.modules.dm.dmReadingRoomApplay.domain.DmReadingRoomApplay;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.dmReadingRoomApplay.repository.DmReadingRoomApplayRepository;
import me.zhengjie.modules.dm.dmReadingRoomApplay.service.DmReadingRoomApplayService;
import me.zhengjie.modules.dm.dmReadingRoomApplay.service.dto.DmReadingRoomApplayDto;
import me.zhengjie.modules.dm.dmReadingRoomApplay.service.dto.DmReadingRoomApplayQueryCriteria;
import me.zhengjie.modules.dm.dmReadingRoomApplay.service.mapstruct.DmReadingRoomApplayMapper;
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.math.BigDecimal;
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-01
**/
@Service
@RequiredArgsConstructor
public class DmReadingRoomApplayServiceImpl implements DmReadingRoomApplayService {

    private final DmReadingRoomApplayRepository dmReadingRoomApplayRepository;
    private final DmReadingRoomApplayMapper dmReadingRoomApplayMapper;

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

    @Override
    public List<DmReadingRoomApplayDto> queryAll(DmReadingRoomApplayQueryCriteria criteria){
        return dmReadingRoomApplayMapper.toDto(dmReadingRoomApplayRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmReadingRoomApplayDto findById(String readomgRoomApplayId) {
        DmReadingRoomApplay dmReadingRoomApplay = dmReadingRoomApplayRepository.findById(readomgRoomApplayId).orElseGet(DmReadingRoomApplay::new);
        ValidationUtil.isNull(dmReadingRoomApplay.getReadomgRoomApplayId(),"DmReadingRoomApplay","readomgRoomApplayId",readomgRoomApplayId);
        return dmReadingRoomApplayMapper.toDto(dmReadingRoomApplay);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmReadingRoomApplayDto create(DmReadingRoomApplay resources) {
        resources.setReadomgRoomApplayId(IdUtil.simpleUUID()); 
        return dmReadingRoomApplayMapper.toDto(dmReadingRoomApplayRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DmReadingRoomApplay resources) {
        DmReadingRoomApplay dmReadingRoomApplay = dmReadingRoomApplayRepository.findById(resources.getReadomgRoomApplayId()).orElseGet(DmReadingRoomApplay::new);
        ValidationUtil.isNull( dmReadingRoomApplay.getReadomgRoomApplayId(),"DmReadingRoomApplay","id",resources.getReadomgRoomApplayId());
        dmReadingRoomApplay.copy(resources);
        dmReadingRoomApplayRepository.save(dmReadingRoomApplay);
    }

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

    @Override
    public void download(List<DmReadingRoomApplayDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmReadingRoomApplayDto dmReadingRoomApplay : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("关联阅览室记录id", dmReadingRoomApplay.getReadingRoomId());
            map.put("消费金额", dmReadingRoomApplay.getReadomgRoomApplayMoney());
            map.put("逾期金额", dmReadingRoomApplay.getOverdueAmount());
            map.put("上限", dmReadingRoomApplay.getOverdueAmountMax());
            map.put("消费类型", dmReadingRoomApplay.getReadomgRoomApplayType());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BigDecimal findApplaySumMoneyByRoomId(Long readingRoomId) {
        return this.dmReadingRoomApplayRepository.findApplaySumMoneyByRoomId(readingRoomId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<DmReadingRoomApplay> findApplayNowByRoomId(Long readingRoomId) {
        return this.dmReadingRoomApplayRepository.findApplayNowByRoomId(readingRoomId);
    }

    @Override
    public List<DmReadingRoomApplay> findApplayByRoomId(Long roomId) {
        return this.dmReadingRoomApplayRepository.findApplayByRoomId(roomId);
    }
}