/*
*  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.CountMoveDetailRecord;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.businessdata.repository.CountMoveDetailRecordRepository;
import com.youchain.businessdata.service.CountMoveDetailRecordService;
import com.youchain.businessdata.service.dto.CountMoveDetailRecordDto;
import com.youchain.businessdata.service.dto.CountMoveDetailRecordQueryCriteria;
import com.youchain.businessdata.service.mapstruct.CountMoveDetailRecordMapper;
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 CountMoveDetailRecordServiceImpl implements CountMoveDetailRecordService {

    private final CountMoveDetailRecordRepository countMoveDetailRecordRepository;
    private final CountMoveDetailRecordMapper countMoveDetailRecordMapper;

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

    @Override
    public List<CountMoveDetailRecordDto> queryAll(CountMoveDetailRecordQueryCriteria criteria){
        return countMoveDetailRecordMapper.toDto(countMoveDetailRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CountMoveDetailRecordDto findById(Long id) {
        CountMoveDetailRecord countMoveDetailRecord = countMoveDetailRecordRepository.findById(id).orElseGet(CountMoveDetailRecord::new);
        ValidationUtil.isNull(countMoveDetailRecord.getId(),"CountMoveDetailRecord","id",id);
        return countMoveDetailRecordMapper.toDto(countMoveDetailRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CountMoveDetailRecordDto create(CountMoveDetailRecord resources) {
        return countMoveDetailRecordMapper.toDto(countMoveDetailRecordRepository.save(resources));
    }

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

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

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