package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.system.mapper.UserBillingMapper;
import com.ruoyi.system.mapper.UserWalletMapper;
import com.ruoyi.system.domain.UserBilling;
import com.ruoyi.system.domain.UserWallet;
import com.ruoyi.system.service.IUserBillingService;

/**
 * BillingService业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-06
 */
@Service
public class UserBillingServiceImpl extends ServiceImpl<BaseMapper<UserBilling>,UserBilling> implements IUserBillingService
{
    @Autowired
    private UserBillingMapper userBillingMapper;

    @Autowired
    private UserWalletMapper userWalletMapper;

    /**
     * 查询Billing
     * 
     * @param id Billing主键
     * @return Billing
     */
    @Override
    public UserBilling selectUserBillingById(Long id)
    {
        return userBillingMapper.selectUserBillingById(id);
    }

    /**
     * 查询Billing列表
     * 
     * @param userBilling Billing
     * @return Billing
     */
    @Override
    public List<UserBilling> selectUserBillingList(UserBilling userBilling)
    {
        return userBillingMapper.selectUserBillingList(userBilling);
    }

    /**
     * 新增Billing
     * 
     * @param userBilling Billing
     * @return 结果
     */
    @Override
    public int insertUserBilling(UserBilling userBilling)
    {
        return userBillingMapper.insertUserBilling(userBilling);
    }

    /**
     * 修改Billing
     * 
     * @param userBilling Billing
     * @return 结果
     */
    @Override
    public int updateUserBilling(UserBilling userBilling)
    {
        return userBillingMapper.updateUserBilling(userBilling);
    }

    /**
     * 批量删除Billing
     * 
     * @param ids 需要删除的Billing主键
     * @return 结果
     */
    @Override
    public int deleteUserBillingByIds(Long[] ids)
    {
        return userBillingMapper.deleteUserBillingByIds(ids);
    }

    /**
     * 删除Billing信息
     * 
     * @param id Billing主键
     * @return 结果
     */
    @Override
    public int deleteUserBillingById(Long id)
    {
        return userBillingMapper.deleteUserBillingById(id);
    }

    @Override
    public BigDecimal getUserBalance(Long userId) {
        UserWallet wallet = userWalletMapper.selectOne(new LambdaQueryWrapper<UserWallet>().eq(UserWallet::getUserId, userId).isNull(UserWallet::getStoreId));
        return wallet == null ? BigDecimal.ZERO : wallet.getBalanceWallet();
    }
}
