package com.leromro.core.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leromro.core.domain.vo.AccountChangeTotalMoney;
import com.leromro.core.domain.vo.ClientAccountChangeVO;
import com.leromro.core.mapper.ClientAccountChangeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.leromro.core.domain.ClientAccountChange;
import com.leromro.core.service.IClientAccountChangeService;

/**
 * 用户钱包余额变更记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-07
 */
@Service
public class ClientAccountChangeServiceImpl extends ServiceImpl<ClientAccountChangeMapper, ClientAccountChange> implements IClientAccountChangeService
{
    @Autowired
    private ClientAccountChangeMapper clientAccountChangeMapper;

    /**
     * 查询用户钱包余额变更记录
     * 
     * @param clientAccountChangeId 用户钱包余额变更记录主键
     * @return 用户钱包余额变更记录
     */
    @Override
    public ClientAccountChange selectLClientAccountChangeByClientAccountChangeId(Long clientAccountChangeId)
    {
        return clientAccountChangeMapper.selectLClientAccountChangeByClientAccountChangeId(clientAccountChangeId);
    }

    /**
     * 查询用户钱包余额变更记录列表
     * 
     * @param clientAccountChange 用户钱包余额变更记录
     * @return 用户钱包余额变更记录
     */
    @Override
    public List<ClientAccountChange> selectLClientAccountChangeList(ClientAccountChange clientAccountChange)
    {
        return clientAccountChangeMapper.selectLClientAccountChangeList(clientAccountChange);
    }

    /**
     * 新增用户钱包余额变更记录
     * 
     * @param clientAccountChange 用户钱包余额变更记录
     * @return 结果
     */
    @Override
    public Boolean insertLClientAccountChange(ClientAccountChange clientAccountChange)
    {
        return this.save(clientAccountChange);
    }

    /**
     * 修改用户钱包余额变更记录
     * 
     * @param clientAccountChange 用户钱包余额变更记录
     * @return 结果
     */
    @Override
    public Boolean updateLClientAccountChange(ClientAccountChange clientAccountChange)
    {
        return this.updateById(clientAccountChange);
    }

    /**
     * 批量删除用户钱包余额变更记录
     * 
     * @param clientAccountChangeIds 需要删除的用户钱包余额变更记录主键
     * @return 结果
     */
    @Override
    public int deleteLClientAccountChangeByClientAccountChangeIds(Long[] clientAccountChangeIds)
    {
        return clientAccountChangeMapper.deleteLClientAccountChangeByClientAccountChangeIds(clientAccountChangeIds);
    }

    /**
     * 删除用户钱包余额变更记录信息
     * 
     * @param clientAccountChangeId 用户钱包余额变更记录主键
     * @return 结果
     */
    @Override
    public int deleteLClientAccountChangeByClientAccountChangeId(Long clientAccountChangeId)
    {
        return clientAccountChangeMapper.deleteLClientAccountChangeByClientAccountChangeId(clientAccountChangeId);
    }

    /**
     * @param clientAccountChange
     * @return
     */
    @Override
    public List<ClientAccountChangeVO> selectAccountChangeByuserId(ClientAccountChange clientAccountChange) {
        return clientAccountChangeMapper.selectClientAccountChangebyUserId(clientAccountChange);
    }

    /**
     * @param clientAccountChange
     * @return
     */
    @Override
    public AccountChangeTotalMoney selectTotalHistory(ClientAccountChange clientAccountChange) {
        AccountChangeTotalMoney totalMoney = new AccountChangeTotalMoney();
        List<ClientAccountChangeVO> list = clientAccountChangeMapper.selectClientAccountChangebyUserId(clientAccountChange);
        //算出总收入以及总支出
        BigDecimal totalEarning = list.stream()
                .filter(vo -> "1".equals(vo.getChangeType()))
                .map(ClientAccountChangeVO::getChangeMoney)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal totalExpend = list.stream()
                .filter(vo -> "0".equals(vo.getChangeType()))
                .map(ClientAccountChangeVO::getChangeMoney)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        totalMoney.setTotalExpend(totalExpend);
        totalMoney.setTotalEarning(totalEarning);
        return totalMoney;
    }
}
