/*
*  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.BotUserFlow;
import cn.ysk.cashier.repository.BotUserFlowRepository;
import cn.ysk.cashier.service.BotUserFlowService;
import cn.ysk.cashier.dto.BotUserFlowDto;
import cn.ysk.cashier.dto.BotUserFlowQueryCriteria;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.mapper.BotUserFlowMapper;
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-30
**/
@Service
@RequiredArgsConstructor
public class BotUserFlowServiceImpl implements BotUserFlowService {

    private final BotUserFlowRepository botUserFlowRepository;
    private final BotUserFlowMapper botUserFlowMapper;

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

    @Override
    public List<BotUserFlowDto> queryAll(BotUserFlowQueryCriteria criteria){
        return botUserFlowMapper.toDto(botUserFlowRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public BotUserFlowDto findById(Integer id) {
        BotUserFlow botUserFlow = botUserFlowRepository.findById(id).orElseGet(BotUserFlow::new);
        ValidationUtil.isNull(botUserFlow.getId(),"BotUserFlow","id",id);
        return botUserFlowMapper.toDto(botUserFlow);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BotUserFlowDto create(BotUserFlow resources) {
        return botUserFlowMapper.toDto(botUserFlowRepository.save(resources));
    }

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

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

    @Override
    public void download(List<BotUserFlowDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BotUserFlowDto botUserFlow : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("电报号", botUserFlow.getUserTelegramId());
            map.put("用户名称", botUserFlow.getUserName());
            map.put("业务代码", botUserFlow.getBizCode());
            map.put("变动金额", botUserFlow.getAmount());
            map.put("变动前金额", botUserFlow.getOldBalance());
            map.put("变动后金额", botUserFlow.getNewBalance());
            map.put("创建时间", botUserFlow.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}