/*
*  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.CountMove;
import com.youchain.businessdata.domain.CountMoveDetail;
import com.youchain.businessdata.service.CountMoveService;
import com.youchain.businessdata.service.dto.CountMoveDto;
import com.youchain.exception.BadRequestException;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.businessdata.repository.CountMoveDetailRepository;
import com.youchain.businessdata.service.CountMoveDetailService;
import com.youchain.businessdata.service.dto.CountMoveDetailDto;
import com.youchain.businessdata.service.dto.CountMoveDetailQueryCriteria;
import com.youchain.businessdata.service.mapstruct.CountMoveDetailMapper;
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 2024-11-21
**/
@Service
@RequiredArgsConstructor
public class CountMoveDetailServiceImpl implements CountMoveDetailService {

    private final CountMoveDetailRepository countMoveDetailRepository;
    private final CountMoveDetailMapper countMoveDetailMapper;

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

    @Override
    public List<CountMoveDetailDto> queryAll(CountMoveDetailQueryCriteria criteria){
        return countMoveDetailMapper.toDto(countMoveDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CountMoveDetailDto findById(Long id) {
        CountMoveDetail countMoveDetail = countMoveDetailRepository.findById(id).orElseGet(CountMoveDetail::new);
        ValidationUtil.isNull(countMoveDetail.getId(),"CountMoveDetail","id",id);
        return countMoveDetailMapper.toDto(countMoveDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CountMoveDetailDto create(CountMoveDetail resources) {
        return countMoveDetailMapper.toDto(countMoveDetailRepository.save(resources));
    }

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

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



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