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

import me.zhengjie.modules.dm.dmtsInfo.domain.DmTsInfo;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.dmtsInfo.repository.DmTsInfoRepository;
import me.zhengjie.modules.dm.dmtsInfo.service.DmTsInfoService;
import me.zhengjie.modules.dm.dmtsInfo.service.dto.DmTsInfoDto;
import me.zhengjie.modules.dm.dmtsInfo.service.dto.DmTsInfoQueryCriteria;
import me.zhengjie.modules.dm.dmtsInfo.service.mapstruct.DmTsInfoMapper;
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-03-01
**/
@Service
@RequiredArgsConstructor
public class DmTsInfoServiceImpl implements DmTsInfoService {

    private final DmTsInfoRepository dmTsInfoRepository;
    private final DmTsInfoMapper dmTsInfoMapper;

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

    @Override
    public List<DmTsInfoDto> queryAll(DmTsInfoQueryCriteria criteria){
        return dmTsInfoMapper.toDto(dmTsInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmTsInfoDto findById(Long tsId) {
        DmTsInfo dmTsInfo = dmTsInfoRepository.findById(tsId).orElseGet(DmTsInfo::new);
        ValidationUtil.isNull(dmTsInfo.getTsId(),"DmTsInfo","tsId",tsId);
        return dmTsInfoMapper.toDto(dmTsInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmTsInfoDto create(DmTsInfo resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setTsId(snowflake.nextId()); 
        return dmTsInfoMapper.toDto(dmTsInfoRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DmTsInfo resources) {
        DmTsInfo dmTsInfo = dmTsInfoRepository.findById(resources.getTsId()).orElseGet(DmTsInfo::new);
        ValidationUtil.isNull( dmTsInfo.getTsId(),"DmTsInfo","id",resources.getTsId());
        dmTsInfo.copy(resources);
        dmTsInfoRepository.save(dmTsInfo);
    }

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

    @Override
    public void download(List<DmTsInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmTsInfoDto dmTsInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("图书位置索引(分类号/书此号）", dmTsInfo.getTsTypeNum());
            map.put("图书名称", dmTsInfo.getTsName());
            map.put("图书作者", dmTsInfo.getTsAuthor());
            map.put("图书出版公司或者出版人名称", dmTsInfo.getTsPublishName());
            map.put("图书出版地址", dmTsInfo.getTsPublishAddress());
            map.put("出版日期", dmTsInfo.getTsPublishTime());
            map.put("图书ISBN编号", dmTsInfo.getTsNum());
            map.put("图书页数", dmTsInfo.getTsTotalPage());
            map.put("图书单价", dmTsInfo.getTsPrice());
            map.put("图书获得方式", dmTsInfo.getTsAcquireType());
            map.put("图书创建人", dmTsInfo.getCreateBy());
            map.put("更新人", dmTsInfo.getUpdateBy());
            map.put("创建时间", dmTsInfo.getCreateTime());
            map.put("更新时间", dmTsInfo.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<Map<String, Object>> getTsTypeAll() {
        return dmTsInfoRepository.selectTsTypeAll();
    }
}