package com.ff.common.service.impl;

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

import cn.hutool.core.util.NumberUtil;
import com.ff.base.constant.Constants;
import com.ff.base.utils.DateUtils;
import com.ff.base.utils.MessageUtils;
import com.ff.base.utils.SecurityUtils;
import com.ff.common.domain.TenantGameQuotaFlow;
import com.ff.common.dto.BalanceChangesDTO;
import com.ff.common.mapper.TenantGameQuotaFlowMapper;
import com.ff.common.service.ITenantGameQuotaFlowService;
import com.ff.common.service.ITenantSecretKeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ff.common.mapper.TenantGameQuotaMapper;
import com.ff.common.domain.TenantGameQuota;
import com.ff.common.service.ITenantGameQuotaService;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 租户游戏配额Service业务层处理
 *
 * @author shi
 * @date 2025-02-12
 */
@Service
public class TenantGameQuotaServiceImpl implements ITenantGameQuotaService
{
    @Autowired
    private TenantGameQuotaMapper tenantGameQuotaMapper;


    @Resource
    private ITenantGameQuotaFlowService tenantGameQuotaFlowService;


    /**
     * 查询租户游戏配额
     *
     * @param id 租户游戏配额主键
     * @return 租户游戏配额
     */
    @Override
    public TenantGameQuota selectTenantGameQuotaById(Long id)
    {
        return tenantGameQuotaMapper.selectTenantGameQuotaById(id);
    }

    /**
     * 查询租户游戏配额列表
     *
     * @param tenantGameQuota 租户游戏配额
     * @return 租户游戏配额
     */
    @Override
    public List<TenantGameQuota> selectTenantGameQuotaList(TenantGameQuota tenantGameQuota)
    {
        return tenantGameQuotaMapper.selectTenantGameQuotaList(tenantGameQuota);
    }

    /**
     * 新增租户游戏配额
     *
     * @param tenantGameQuota 租户游戏配额
     * @return 结果
     */
    @Override
    public int insertTenantGameQuota(TenantGameQuota tenantGameQuota)
    {
        tenantGameQuota.setCreateTime(DateUtils.getNowDate());
        return tenantGameQuotaMapper.insertTenantGameQuota(tenantGameQuota);
    }

    /**
     * 修改租户游戏配额
     *
     * @param tenantGameQuota 租户游戏配额
     * @return 结果
     */
    @Override
    public int updateTenantGameQuota(TenantGameQuota tenantGameQuota)
    {
        tenantGameQuota.setUpdateTime(DateUtils.getNowDate());
        return tenantGameQuotaMapper.updateTenantGameQuota(tenantGameQuota);
    }

    /**
     * 批量删除租户游戏配额
     *
     * @param ids 需要删除的租户游戏配额主键
     * @return 结果
     */
    @Override
    public int deleteTenantGameQuotaByIds(Long[] ids)
    {
        return tenantGameQuotaMapper.deleteTenantGameQuotaByIds(ids);
    }

    /**
     * 删除租户游戏配额信息
     *
     * @param id 租户游戏配额主键
     * @return 结果
     */
    @Override
    public int deleteTenantGameQuotaById(Long id)
    {
        return tenantGameQuotaMapper.deleteTenantGameQuotaById(id);
    }

    /**
     * 平衡变化
     *
     * @param balanceChangesDTO 余额更改为
     * @return {@link Boolean }
     */
    @Override
    public Boolean balanceChanges(BalanceChangesDTO balanceChangesDTO) {
        TenantGameQuota tenantGameQuota = tenantGameQuotaMapper.selectTenantGameQuotaByTenantKey(balanceChangesDTO.getTenantKey());
        Assert.isTrue(!ObjectUtils.isEmpty(tenantGameQuota), "余额额度不足");

        BigDecimal balanceBefore = tenantGameQuota.getBalance();
        BigDecimal balance =balanceChangesDTO.getBalance();
        if (BigDecimal.ZERO.compareTo(balance) >= 0) {
            return Boolean.TRUE;
        }
        BigDecimal balanceAfter = tenantGameQuota.getBalance();
        //判断是充值还是扣除
        if (balanceChangesDTO.getIsOut()) {
            balanceAfter = NumberUtil.add(balanceBefore, balance);
        } else {
            balanceAfter = NumberUtil.sub(balanceBefore, balance);
        }
        //判断剩余额度是否大于0
        Assert.isTrue(balanceAfter.compareTo(BigDecimal.ZERO)>=0, "余额额度不足");
        //修改余额
        Boolean changedBalanceResult = tenantGameQuotaMapper.changeBalance(TenantGameQuota.builder()
                .tenantKey(balanceChangesDTO.getTenantKey())
                .balance(balanceAfter)
                .version(tenantGameQuota.getVersion())
                .build());
        Assert.isTrue(changedBalanceResult, "租户游戏额度操作失败");

        //插入流水
        TenantGameQuotaFlow tenantGameQuotaFlow = TenantGameQuotaFlow.builder()
                .tenantKey(balanceChangesDTO.getTenantKey())
                .isOut(balanceChangesDTO.getIsOut())
                .balanceAfter(balanceAfter)
                .memberId(balanceChangesDTO.getMemberId())
                .balance(balance)
                .balanceBefore(balanceBefore)
                .operationType(balanceChangesDTO.getOperationType())
                .build();
        tenantGameQuotaFlow.setRemark(balanceChangesDTO.getRemark());
        tenantGameQuotaFlow.setCreateTime(DateUtils.getNowDate());
        tenantGameQuotaFlow.setCreateBy(Constants.SYSTEM);
        tenantGameQuotaFlowService.insertTenantGameQuotaFlow(tenantGameQuotaFlow);
        return changedBalanceResult;
    }
}
