package cn.iocoder.yudao.module.member.service.amount;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppUserInfoCardVO;
import cn.iocoder.yudao.module.member.dal.dataobject.card.CardDO;
import cn.iocoder.yudao.module.member.dal.dataobject.money.MoneyDO;
import cn.iocoder.yudao.module.member.dal.dataobject.order.DishOrderDO;
import cn.iocoder.yudao.module.member.dal.dataobject.rechargelog.RechargeLogDO;
import cn.iocoder.yudao.module.member.dal.dataobject.refund.RefundDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.card.CardMapper;
import cn.iocoder.yudao.module.member.dal.mysql.order.DishOrderMapper;
import cn.iocoder.yudao.module.member.dal.mysql.rechargelog.RechargeLogMapper;
import cn.iocoder.yudao.module.member.dal.mysql.refund.IntegralRefundMapper;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper;
import cn.iocoder.yudao.module.member.enums.CostTypeEnum;
import cn.iocoder.yudao.module.member.service.appup.AppUpService;
import cn.iocoder.yudao.module.member.service.money.MoneyService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.USER_NOT_EXISTS;


/**
 * 金额操作 Service 实现类
 *
 * @author 开发账号
 */

@Service
@Slf4j
public class AmountServiceImpl implements AmountService {

    @Resource
    private MemberUserMapper userMapper;
    @Resource
    private MoneyService moneyService;

    @Override
    public void operateAmount(Long userId, BigDecimal money, BigDecimal cashAmount, BigDecimal giftAmount, BigDecimal wxAmount) {
        synchronized (userId) {
            MemberUserDO memberUserDO = userMapper.selectById(userId);
            if (memberUserDO != null) {
                memberUserDO.setMoney(memberUserDO.getMoney().add(money));
                memberUserDO.setCashAmount(memberUserDO.getCashAmount().add(cashAmount));
                memberUserDO.setGiftAmount(memberUserDO.getGiftAmount().add(giftAmount));
//                memberUserDO.setWxAmt(memberUserDO.getWxAmt().add(wxAmount));
                userMapper.updateById(memberUserDO);
            }
        }
    }

    @Override
    public BigDecimal getAmount(Long userId,Long storeId) {
        MemberUserDO memberUserDO = userMapper.selectById(userId);
        if (memberUserDO == null) {
            throw exception(USER_NOT_EXISTS);
        }
        return memberUserDO.getMoney();
    }

    @Override
    public BigDecimal getCashAmount(Long userId,Long storeId) {
        MemberUserDO memberUserDO = userMapper.selectById(userId);
        if (memberUserDO == null) {
            return BigDecimal.ZERO;
        }
        return memberUserDO.getCashAmount();
    }
}
