package com.it.rattan.monisuo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.rattan.monisuo.entity.Coin;
import com.it.rattan.monisuo.mapper.CoinMapper;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;

/**
 * 币种服务
 */
@Service
public class CoinService extends ServiceImpl<CoinMapper, Coin> {

    /**
     * 获取所有上架币种
     */
    public List<Coin> getActiveCoins() {
        LambdaQueryWrapper<Coin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Coin::getStatus, 1)
                .orderByDesc(Coin::getSort);
        return list(wrapper);
    }

    /**
     * 根据代码获取币种
     */
    public Coin getCoinByCode(String code) {
        LambdaQueryWrapper<Coin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Coin::getCode, code.toUpperCase())
                .eq(Coin::getStatus, 1);
        return getOne(wrapper);
    }

    /**
     * 更新币种价格
     */
    public void updatePrice(String code, BigDecimal price) {
        Coin coin = getCoinByCode(code);
        if (coin != null) {
            coin.setPrice(price);
            coin.setUpdateTime(java.time.LocalDateTime.now());
            updateById(coin);
        }
    }

    /**
     * 获取实时币种列表
     */
    public List<Coin> getRealTimeCoins() {
        LambdaQueryWrapper<Coin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Coin::getPriceType, 1)
                .eq(Coin::getStatus, 1);
        return list(wrapper);
    }
}
