package com.ruoyi.ss.recordBalance.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ss.recordBalance.domain.enums.RecordBalanceBstType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ss.recordBalance.mapper.RecordBalanceMapper;
import com.ruoyi.ss.recordBalance.domain.RecordBalance;
import com.ruoyi.ss.recordBalance.domain.RecordBalanceVO;
import com.ruoyi.ss.recordBalance.domain.RecordBalanceQuery;
import com.ruoyi.ss.recordBalance.service.RecordBalanceService;

/**
 * 余额变动记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-22
 */
@Service
public class RecordBalanceServiceImpl implements RecordBalanceService
{
    @Autowired
    private RecordBalanceMapper recordBalanceMapper;

    /**
     * 查询余额变动记录
     *
     * @param id 余额变动记录主键
     * @return 余额变动记录
     */
    @Override
    public RecordBalanceVO selectRecordBalanceById(Long id)
    {
        return recordBalanceMapper.selectRecordBalanceById(id);
    }

    /**
     * 查询余额变动记录列表
     *
     * @param recordBalance 余额变动记录
     * @return 余额变动记录
     */
    @Override
    public List<RecordBalanceVO> selectRecordBalanceList(RecordBalanceQuery recordBalance)
    {
        return recordBalanceMapper.selectRecordBalanceList(recordBalance);
    }

    /**
     * 新增余额变动记录
     *
     * @param recordBalance 余额变动记录
     * @return 结果
     */
    @Override
    public int insertRecordBalance(RecordBalance recordBalance)
    {
        recordBalance.setCreateTime(DateUtils.getNowDate());
        return recordBalanceMapper.insertRecordBalance(recordBalance);
    }

    /**
     * 修改余额变动记录
     *
     * @param recordBalance 余额变动记录
     * @return 结果
     */
    @Override
    public int updateRecordBalance(RecordBalance recordBalance)
    {
        return recordBalanceMapper.updateRecordBalance(recordBalance);
    }

    /**
     * 批量删除余额变动记录
     *
     * @param ids 需要删除的余额变动记录主键
     * @return 结果
     */
    @Override
    public int deleteRecordBalanceByIds(Long[] ids)
    {
        return recordBalanceMapper.deleteRecordBalanceByIds(ids);
    }

    /**
     * 删除余额变动记录信息
     *
     * @param id 余额变动记录主键
     * @return 结果
     */
    @Override
    public int deleteRecordBalanceById(Long id)
    {
        return recordBalanceMapper.deleteRecordBalanceById(id);
    }

    @Override
    public int record(Long userId, BigDecimal beforeBalance, BigDecimal amount, String reason, RecordBalanceBstType bstType, Long bstId) {
        if (userId == null || beforeBalance == null || amount == null || StringUtils.isBlank(reason)) {
            return 0;
        }
        RecordBalance record = new RecordBalance();
        record.setUserId(userId);
        record.setBeforeBalance(beforeBalance);
        record.setAmount(amount);
        record.setAfterBalance(beforeBalance.add(amount));
        record.setReason(reason);
        record.setBstType(bstType.getType());
        record.setBstId(bstId);

        return this.insertRecordBalance(record);
    }

    @Override
    public RecordBalanceVO selectRecordBalanceById(Long id, Long userId) {
        if (id == null || userId == null) {
            return null;
        }
        RecordBalanceQuery query = new RecordBalanceQuery();
        query.setUserId(userId);
        query.setId(id);
        return this.selectOne(query);
    }

    @Override
    public RecordBalanceVO selectOne(RecordBalanceQuery query) {
        return recordBalanceMapper.selectOne(query);
    }
}
