/*
*  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.AgvScene;
import com.youchain.utils.FileUtil;
import com.youchain.utils.PageUtil;
import com.youchain.utils.QueryHelp;
import com.youchain.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import com.youchain.basicdata.repository.AgvSceneRepository;
import com.youchain.basicdata.service.AgvSceneService;
import com.youchain.basicdata.service.dto.AgvSceneDto;
import com.youchain.basicdata.service.dto.AgvSceneQueryCriteria;
import com.youchain.basicdata.service.mapstruct.AgvSceneMapper;
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 DengJiangKun
* @date 2023-11-02
**/
@Service
@RequiredArgsConstructor
public class AgvSceneServiceImpl implements AgvSceneService {

    private final AgvSceneRepository agvSceneRepository;
    private final AgvSceneMapper agvSceneMapper;

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

    @Override
    public List<AgvSceneDto> queryAll(AgvSceneQueryCriteria criteria){
        return agvSceneMapper.toDto(agvSceneRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AgvSceneDto findById(Long id) {
        AgvScene agvScene = agvSceneRepository.findById(id).orElseGet(AgvScene::new);
        ValidationUtil.isNull(agvScene.getId(),"AgvScene","id",id);
        return agvSceneMapper.toDto(agvScene);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AgvSceneDto create(AgvScene resources) {
        return agvSceneMapper.toDto(agvSceneRepository.save(resources));
    }

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

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

    @Override
    public void download(List<AgvSceneDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AgvSceneDto agvScene : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("仓库ID", agvScene.getDept());
            map.put("代码", agvScene.getCode());
            map.put("名称", agvScene.getName());
            map.put("备注", agvScene.getRemark());
            map.put("创建人", agvScene.getCreateBy());
            map.put("修改人", agvScene.getUpdateBy());
            map.put("创建时间", agvScene.getCreateTime());
            map.put("修改时间", agvScene.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}