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

import me.zhengjie.modules.dm.dmtsCollection.domain.DmTsCollection;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.dmtsCollection.repository.DmTsCollectionRepository;
import me.zhengjie.modules.dm.dmtsCollection.service.DmTsCollectionService;
import me.zhengjie.modules.dm.dmtsCollection.service.dto.DmTsCollectionDto;
import me.zhengjie.modules.dm.dmtsCollection.service.dto.DmTsCollectionQueryCriteria;
import me.zhengjie.modules.dm.dmtsCollection.service.mapstruct.DmTsCollectionMapper;
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-01
**/
@Service
@RequiredArgsConstructor
public class DmTsCollectionServiceImpl implements DmTsCollectionService {

    private final DmTsCollectionRepository dmTsCollectionRepository;
    private final DmTsCollectionMapper dmTsCollectionMapper;

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

    @Override
    public List<DmTsCollectionDto> queryAll(DmTsCollectionQueryCriteria criteria){
        return dmTsCollectionMapper.toDto(dmTsCollectionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmTsCollectionDto findById(String collectionSin) {
        DmTsCollection dmTsCollection = dmTsCollectionRepository.findById(collectionSin).orElseGet(DmTsCollection::new);
        ValidationUtil.isNull(dmTsCollection.getCollectionSin(),"DmTsCollection","collectionSin",collectionSin);
        return dmTsCollectionMapper.toDto(dmTsCollection);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmTsCollectionDto create(DmTsCollection resources) {
        resources.setCollectionSin(IdUtil.simpleUUID()); 
        return dmTsCollectionMapper.toDto(dmTsCollectionRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DmTsCollection resources) {
        DmTsCollection dmTsCollection = dmTsCollectionRepository.findById(resources.getCollectionSin()).orElseGet(DmTsCollection::new);
        ValidationUtil.isNull( dmTsCollection.getCollectionSin(),"DmTsCollection","id",resources.getCollectionSin());
        dmTsCollection.copy(resources);
        dmTsCollectionRepository.save(dmTsCollection);
    }

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

    @Override
    public void download(List<DmTsCollectionDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmTsCollectionDto dmTsCollection : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("图书id（可以重复  一对多）", dmTsCollection.getTsId());
            map.put("图书标识", dmTsCollection.getTsLoginNum());
            map.put("图书存放位置", dmTsCollection.getTsCollectionAddress());
            map.put("图书存放时间", dmTsCollection.getCreateTime());
            map.put("图书更新时间", dmTsCollection.getUpdateTime());
            map.put("分配更新者", dmTsCollection.getUpdateBy());
            map.put("分配创建者", dmTsCollection.getCreateBy());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}