/*
*  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.BotUser;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.BotUserRepository;
import cn.ysk.cashier.service.BotUserService;
import cn.ysk.cashier.dto.BotUserDto;
import cn.ysk.cashier.dto.BotUserQueryCriteria;
import cn.ysk.cashier.mapper.BotUserMapper;
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 BotUserServiceImpl implements BotUserService {

    private final BotUserRepository botUserRepository;
    private final BotUserMapper botUserMapper;

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

    @Override
    public List<BotUserDto> queryAll(BotUserQueryCriteria criteria){
        return botUserMapper.toDto(botUserRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public BotUserDto findById(Integer id) {
        BotUser botUser = botUserRepository.findById(id).orElseGet(BotUser::new);
        ValidationUtil.isNull(botUser.getId(),"BotUser","id",id);
        return botUserMapper.toDto(botUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BotUserDto create(BotUser resources) {
        return botUserMapper.toDto(botUserRepository.save(resources));
    }

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

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

    @Override
    public void download(List<BotUserDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BotUserDto botUser : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" fatherId",  botUser.getFatherId());
            map.put("父级电报号", botUser.getFatherTelegramId());
            map.put("电报号", botUser.getUserTelegramId());
            map.put("用户名称", botUser.getUserName());
            map.put("组电报号", botUser.getGroupTelegramId());
            map.put("用户代码", botUser.getUserCode());
            map.put(" userPayPass",  botUser.getUserPayPass());
            map.put(" bombStatus",  botUser.getBombStatus());
            map.put("用户状态", botUser.getBotStatus());
            map.put("总充值", botUser.getUsdtRechargeTotal());
            map.put("总提现", botUser.getUsdtWithdrawTotal());
            map.put("总资金", botUser.getBalance());
            map.put("冻结资金", botUser.getFreezeBalance());
            map.put("版本号", botUser.getVersion());
            map.put("创建时间", botUser.getCreateTime());
            map.put("更新时间", botUser.getUpdateTime());
            map.put("语言", botUser.getUserLanguage());
            map.put("质押资金", botUser.getChipBalance());
            map.put("绑定时间", botUser.getFatherBindTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}