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

import me.zhengjie.modules.dm.dmReadingRoom.domain.DmReadingRoom;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.dmReadingRoom.repository.DmReadingRoomRepository;
import me.zhengjie.modules.dm.dmReadingRoom.service.DmReadingRoomService;
import me.zhengjie.modules.dm.dmReadingRoom.service.dto.DmReadingRoomDto;
import me.zhengjie.modules.dm.dmReadingRoom.service.dto.DmReadingRoomQueryCriteria;
import me.zhengjie.modules.dm.dmReadingRoom.service.mapstruct.DmReadingRoomMapper;
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://el-admin.vip
* @description 服务实现
* @author longda
* @date 2022-02-25
**/
@Service
@RequiredArgsConstructor
public class DmReadingRoomServiceImpl implements DmReadingRoomService {

    private final DmReadingRoomRepository dmReadingRoomRepository;
    private final DmReadingRoomMapper dmReadingRoomMapper;

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

    @Override
    public List<DmReadingRoomDto> queryAll(DmReadingRoomQueryCriteria criteria){
        return dmReadingRoomMapper.toDto(dmReadingRoomRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmReadingRoomDto findById(Long readingRoomId) {
        DmReadingRoom dmReadingRoom = dmReadingRoomRepository.findById(readingRoomId).orElseGet(DmReadingRoom::new);
        ValidationUtil.isNull(dmReadingRoom.getReadingRoomId(),"DmReadingRoom","readingRoomId",readingRoomId);
        return dmReadingRoomMapper.toDto(dmReadingRoom);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmReadingRoomDto create(DmReadingRoom resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setReadingRoomId(snowflake.nextId()); 
        return dmReadingRoomMapper.toDto(dmReadingRoomRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DmReadingRoom resources) {
        DmReadingRoom dmReadingRoom = dmReadingRoomRepository.findById(resources.getReadingRoomId()).orElseGet(DmReadingRoom::new);
        ValidationUtil.isNull( dmReadingRoom.getReadingRoomId(),"DmReadingRoom","id",resources.getReadingRoomId());
        dmReadingRoom.copy(resources);
        dmReadingRoomRepository.save(dmReadingRoom);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long readingRoomId : ids) {
            dmReadingRoomRepository.deleteById(readingRoomId);
        }
    }

    @Override
    public void download(List<DmReadingRoomDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmReadingRoomDto dmReadingRoom : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("书籍id", dmReadingRoom.getBookId());
            map.put("书籍名称", dmReadingRoom.getBookName());
            map.put("书籍创建时间", dmReadingRoom.getCreateTime());
            map.put("书籍更新时间", dmReadingRoom.getUpdateTime());
            map.put("更新人", dmReadingRoom.getUpdateBy());
            map.put("借阅起始时间", dmReadingRoom.getReadStartTime());
            map.put("借阅到期时间", dmReadingRoom.getReadEndTime());
            map.put("是否逾期 0 未逾期 1 逾期", dmReadingRoom.getIsOverdue());
            map.put("逾期天数", dmReadingRoom.getOverdueDay());
            map.put("归还时间", dmReadingRoom.getReturnTime());
            map.put("借阅人id", dmReadingRoom.getDmUser().getId());
            map.put("借阅金额", dmReadingRoom.getBorrowMoney());
            map.put("创建人", dmReadingRoom.getCreateBy());
            map.put("借阅人姓名", dmReadingRoom.getBorrowUserName());
            map.put("借书证号码", dmReadingRoom.getDmUser().getUserNumber());
            map.put("图书藏id", dmReadingRoom.getCollectionSin());
            map.put("0  借出   1 归还", dmReadingRoom.getBorrowStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Transactional
    @Override
    public List<DmReadingRoom> findReadRoomByOverdue(Integer overdue,String read_end_time) {
        return dmReadingRoomRepository.findReadRoomByOverdue(overdue,read_end_time);
    }
}