/*
*  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.DesignList;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.basicdata.repository.DesignListRepository;
import com.youchain.basicdata.service.DesignListService;
import com.youchain.basicdata.service.dto.DesignListDto;
import com.youchain.basicdata.service.dto.DesignListQueryCriteria;
import com.youchain.basicdata.service.mapstruct.DesignListMapper;
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 LY
* @date 2024-04-26
**/
@Service
@RequiredArgsConstructor
public class DesignListServiceImpl implements DesignListService {

    private final DesignListRepository designListRepository;
    private final DesignListMapper designListMapper;

    @Override
    public Map<String,Object> queryAll(DesignListQueryCriteria criteria, Pageable pageable){
        Page<DesignList> page = designListRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);

        return PageUtil.toPage(page.map(designListMapper::toDto));
    }

    @Override
    public List<DesignListDto> queryAll(DesignListQueryCriteria criteria){
        return designListMapper.toDto(designListRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DesignListDto findById(Long id) {
        DesignList designList = designListRepository.findById(id).orElseGet(DesignList::new);
        ValidationUtil.isNull(designList.getId(),"DesignList","id",id);
        return designListMapper.toDto(designList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DesignListDto create(DesignList resources) {
        return designListMapper.toDto(designListRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DesignList resources) {
        DesignList designList = designListRepository.exist(resources.getChaseCenter());
        ValidationUtil.isNull( designList.getChaseCenter(),"DesignList","chaseCenter",resources.getChaseCenter());
        designList.copy(resources);
        designListRepository.save(designList);
    }

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

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