package com.it.rattan.monisuo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.it.rattan.monisuo.entity.ColdWallet;
import com.it.rattan.monisuo.mapper.ColdWalletMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 冷钱包地址服务
 */
@Service
public class ColdWalletService {

    @Autowired
    private ColdWalletMapper coldWalletMapper;

    /**
     * 获取所有钱包列表
     */
    public List<ColdWallet> getList() {
        LambdaQueryWrapper<ColdWallet> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(ColdWallet::getIsDefault)
               .orderByDesc(ColdWallet::getCreateTime);
        return coldWalletMapper.selectList(wrapper);
    }

    /**
     * 获取启用的钱包列表
     */
    public List<ColdWallet> getEnabledList() {
        LambdaQueryWrapper<ColdWallet> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ColdWallet::getStatus, 1)
               .orderByDesc(ColdWallet::getIsDefault)
               .orderByDesc(ColdWallet::getCreateTime);
        return coldWalletMapper.selectList(wrapper);
    }

    /**
     * 获取默认钱包
     */
    public ColdWallet getDefaultWallet() {
        LambdaQueryWrapper<ColdWallet> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ColdWallet::getStatus, 1)
               .eq(ColdWallet::getIsDefault, true);
        return coldWalletMapper.selectOne(wrapper);
    }

    /**
     * 根据ID获取钱包
     */
    public ColdWallet getById(Long id) {
        return coldWalletMapper.selectById(id);
    }

    /**
     * 新增钱包
     */
    @Transactional
    public void create(ColdWallet wallet) {
        wallet.setCreateTime(LocalDateTime.now());
        if (wallet.getStatus() == null) {
            wallet.setStatus(1);
        }
        if (wallet.getIsDefault() == null) {
            wallet.setIsDefault(false);
        }

        // 如果设为默认，先清除其他默认
        if (Boolean.TRUE.equals(wallet.getIsDefault())) {
            coldWalletMapper.clearAllDefault();
        }

        coldWalletMapper.insert(wallet);
    }

    /**
     * 更新钱包
     */
    @Transactional
    public void update(ColdWallet wallet) {
        wallet.setUpdateTime(LocalDateTime.now());

        // 如果设为默认，先清除其他默认
        if (Boolean.TRUE.equals(wallet.getIsDefault())) {
            coldWalletMapper.clearAllDefault();
        }

        coldWalletMapper.updateById(wallet);
    }

    /**
     * 删除钱包
     */
    public void delete(Long id) {
        coldWalletMapper.deleteById(id);
    }

    /**
     * 设置默认钱包
     */
    @Transactional
    public void setDefault(Long id) {
        ColdWallet wallet = coldWalletMapper.selectById(id);
        if (wallet == null) {
            throw new RuntimeException("钱包不存在");
        }
        if (wallet.getStatus() != 1) {
            throw new RuntimeException("禁用的钱包不能设为默认");
        }

        // 清除所有默认
        coldWalletMapper.clearAllDefault();

        // 设置新的默认
        wallet.setIsDefault(true);
        wallet.setUpdateTime(LocalDateTime.now());
        coldWalletMapper.updateById(wallet);
    }

    /**
     * 切换状态
     */
    public void toggleStatus(Long id) {
        ColdWallet wallet = coldWalletMapper.selectById(id);
        if (wallet == null) {
            throw new RuntimeException("钱包不存在");
        }

        // 如果是默认钱包要禁用，需要先取消默认
        if (Boolean.TRUE.equals(wallet.getIsDefault()) && wallet.getStatus() == 1) {
            wallet.setIsDefault(false);
        }

        wallet.setStatus(wallet.getStatus() == 1 ? 0 : 1);
        wallet.setUpdateTime(LocalDateTime.now());
        coldWalletMapper.updateById(wallet);
    }
}
