/*
*  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 com.youchain.basicdata.service.impl;

import com.youchain.basicdata.domain.ItemLis;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.basicdata.repository.ItemLisRepository;
import com.youchain.basicdata.service.ItemLisService;
import com.youchain.basicdata.service.dto.ItemLisDto;
import com.youchain.basicdata.service.dto.ItemLisQueryCriteria;
import com.youchain.basicdata.service.mapstruct.ItemLisMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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://eladmin.vip
* @description 服务实现
* @author baobinglin
* @date 2024-01-03
**/
@Service
@Slf4j
@RequiredArgsConstructor
public class ItemLisServiceImpl implements ItemLisService {

    private final ItemLisRepository itemLisRepository;
    private final ItemLisMapper itemLisMapper;

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

    @Override
    public List<ItemLisDto> queryAll(ItemLisQueryCriteria criteria){
        return itemLisMapper.toDto(itemLisRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ItemLisDto findById(Long id) {
        ItemLis itemLis = itemLisRepository.findById(id).orElseGet(ItemLis::new);
        ValidationUtil.isNull(itemLis.getId(),"ItemLis","id",id);
        return itemLisMapper.toDto(itemLis);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ItemLisDto create(ItemLis resources) {
        return itemLisMapper.toDto(itemLisRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ItemLis resources) {
        ItemLis itemLis = itemLisRepository.findById(resources.getId()).orElseGet(ItemLis::new);
        ValidationUtil.isNull( itemLis.getId(),"ItemLis","id",resources.getId());
        itemLis.copy(resources);
        itemLisRepository.save(itemLis);
    }

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

    @Override
    public void download(List<ItemLisDto> all, HttpServletResponse response) throws Exception {
        
        List<Map<String, Object>>  list= ExcelDownUtils.CreateMap(all,"ItemLis");
        FileUtil.downloadExcel(list, response);
    }
}