package com.ruoyi.web.modules.account.service.impl;

import java.math.BigDecimal;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.modules.account.mapper.BizMemberMapper;
import com.ruoyi.web.modules.account.service.IMemberConsumptionService;

/**
 * 按订单表重算消费汇总（O11）
 */
@Service
public class MemberConsumptionServiceImpl implements IMemberConsumptionService
{
    @Autowired
    private BizMemberMapper memberMapper;

    @Override
    public void refreshConsumption(Long memberId)
    {
        if (memberId == null)
        {
            return;
        }
        int orderCount = 0;
        BigDecimal totalAmount = BigDecimal.ZERO;
        try
        {
            Map<String, Object> stats = memberMapper.selectConsumptionFromOrder(memberId);
            if (stats != null)
            {
                Object cnt = stats.get("orderCount");
                Object amt = stats.get("totalAmount");
                if (cnt instanceof Number)
                {
                    orderCount = ((Number) cnt).intValue();
                }
                if (amt != null)
                {
                    totalAmount = new BigDecimal(amt.toString());
                }
            }
        }
        catch (Exception ex)
        {
            orderCount = 0;
            totalAmount = BigDecimal.ZERO;
        }
        memberMapper.updateConsumption(memberId, orderCount, totalAmount);
    }
}
