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

import com.youchain.businessdata.domain.CountDetail;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.businessdata.repository.CountDetailRepository;
import com.youchain.businessdata.service.CountDetailService;
import com.youchain.businessdata.service.dto.CountDetailDto;
import com.youchain.businessdata.service.dto.CountDetailQueryCriteria;
import com.youchain.businessdata.service.mapstruct.CountDetailMapper;
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 houjianlan
* @date 2023-10-20
**/
@Service
@RequiredArgsConstructor
public class CountDetailServiceImpl implements CountDetailService {

    private final CountDetailRepository countDetailRepository;
    private final CountDetailMapper countDetailMapper;

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

    @Override
    public List<CountDetailDto> queryAll(CountDetailQueryCriteria criteria){
        return countDetailMapper.toDto(countDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CountDetailDto findById(Long id) {
        CountDetail countDetail = countDetailRepository.findById(id).orElseGet(CountDetail::new);
        ValidationUtil.isNull(countDetail.getId(),"CountDetail","id",id);
        return countDetailMapper.toDto(countDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CountDetailDto create(CountDetail resources) {
        return countDetailMapper.toDto(countDetailRepository.save(resources));
    }

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

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

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