/*
*  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.BotConfig;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.BotConfigRepository;
import cn.ysk.cashier.service.BotConfigService;
import cn.ysk.cashier.dto.BotConfigDto;
import cn.ysk.cashier.dto.BotConfigQueryCriteria;
import cn.ysk.cashier.mapper.BotConfigMapper;
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 BotConfigServiceImpl implements BotConfigService {

    private final BotConfigRepository botConfigRepository;
    private final BotConfigMapper botConfigMapper;

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

    @Override
    public List<BotConfigDto> queryAll(BotConfigQueryCriteria criteria){
        return botConfigMapper.toDto(botConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public BotConfigDto findById(Integer id) {
        BotConfig botConfig = botConfigRepository.findById(id).orElseGet(BotConfig::new);
        ValidationUtil.isNull(botConfig.getId(),"BotConfig","id",id);
        return botConfigMapper.toDto(botConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BotConfigDto create(BotConfig resources) {
        return botConfigMapper.toDto(botConfigRepository.save(resources));
    }

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

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

    @Override
    public void download(List<BotConfigDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BotConfigDto botConfig : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("元素键值", botConfig.getConfigKey());
            map.put("元素值", botConfig.getConfigValue());
            map.put("描述", botConfig.getRemark());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}