/*
*  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 cn.ysk.cashier.service.impl;

import cn.ysk.cashier.pojo.BotButtonConfig;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.BotButtonConfigRepository;
import cn.ysk.cashier.service.BotButtonConfigService;
import cn.ysk.cashier.dto.BotButtonConfigDto;
import cn.ysk.cashier.dto.BotButtonConfigQueryCriteria;
import cn.ysk.cashier.mapper.BotButtonConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import cn.ysk.cashier.utils.PageUtil;
import cn.ysk.cashier.utils.QueryHelp;
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 admin
* @date 2023-10-31
**/
@Service
@RequiredArgsConstructor
public class BotButtonConfigServiceImpl implements BotButtonConfigService {

    private final BotButtonConfigRepository botButtonConfigRepository;
    private final BotButtonConfigMapper botButtonConfigMapper;

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

    @Override
    public List<BotButtonConfigDto> queryAll(BotButtonConfigQueryCriteria criteria){
        return botButtonConfigMapper.toDto(botButtonConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public BotButtonConfigDto findById(Integer id) {
        BotButtonConfig botButtonConfig = botButtonConfigRepository.findById(id).orElseGet(BotButtonConfig::new);
        ValidationUtil.isNull(botButtonConfig.getId(),"BotButtonConfig","id",id);
        return botButtonConfigMapper.toDto(botButtonConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BotButtonConfigDto create(BotButtonConfig resources) {
        return botButtonConfigMapper.toDto(botButtonConfigRepository.save(resources));
    }

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

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

    @Override
    public void download(List<BotButtonConfigDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BotButtonConfigDto botButtonConfig : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("按钮名称", botButtonConfig.getButtonName());
            map.put("按钮值", botButtonConfig.getButtonValue());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}