package com.YuyeTech.HeartRate.service.impl;

import com.YuyeTech.HeartRate.dataobject.UserWallets;
import com.YuyeTech.HeartRate.enums.ResultEnum;
import com.YuyeTech.HeartRate.exception.UserException;
import com.YuyeTech.HeartRate.repository.UserWalletsRepository;
import com.YuyeTech.HeartRate.service.UserWalletsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 用户信息
 *
 * @author:slambb
 * @date:2021/8/5
 */
@Service
public class UserWalletsServiceImpl implements UserWalletsService {

    @Autowired
    private UserWalletsRepository userWalletsRepository;

    @Value("${userWallets.defaultGold}")
    private Integer defaultGold;
    @Value("${userWallets.defaultDiamond}")
    private Integer defaultDiamond;

    @Override
    @Transactional
    public UserWallets findUserWalletsByUserId(String userId) throws UserException {

        try {
            UserWallets userWallets = userWalletsRepository.findByUserId(userId).orElse(null);
            if (userWallets == null) {
                //如果不存在用户数据，默认给一定钻石，具体在yml里面设置
                userWallets = new UserWallets();
                userWallets.setUserId(userId);
                userWallets.setGold(defaultGold);
                userWallets.setDiamond(defaultDiamond);
                userWallets.setCreateTime(new Date());
                userWalletsRepository.save(userWallets);
            }
            return userWallets;
        } catch (Exception e) {
//            e.printStackTrace();
            throw new UserException(ResultEnum.WALLETS_GET_ERROR.getCode(), ResultEnum.WALLETS_GET_ERROR.getMessage());
        }

    }


    @Override
    @Transactional
    public UserWallets modifyGoldAndDiamondInUserWallets(String userId, Integer consumeGold, Integer consumeDiamond, Boolean isAdd) {

        UserWallets userWallets = userWalletsRepository.findByUserId(userId).orElse(null);
        //这里有一种情况会null，就是玩家pk时候，对方没有钱包数据。直接设定一个初始值
        //当然这时候的userId 就是 好友的id
        if (userWallets == null) {
            //如果不存在用户数据，默认给一定钻石，具体在yml里面设置
            userWallets = new UserWallets();
            userWallets.setUserId(userId);
            userWallets.setGold(defaultGold);
            userWallets.setDiamond(defaultDiamond);
            userWallets.setCreateTime(new Date());
        }
        //计算金币操作
        Integer _gold = isAdd ? userWallets.getGold() + consumeGold : userWallets.getGold() - consumeGold;
        Integer _diamond = isAdd ? userWallets.getDiamond() + consumeDiamond : userWallets.getDiamond() - consumeDiamond;
        if (_gold < 0) {
            throw new UserException(ResultEnum.WALLETS_GOLD_INSUFFICIENT.getCode(), ResultEnum.WALLETS_GOLD_INSUFFICIENT.getMessage());
        }
        if (_diamond < 0) {
            throw new UserException(ResultEnum.WALLETS_DIAMOND_INSUFFICIENT.getCode(), ResultEnum.WALLETS_DIAMOND_INSUFFICIENT.getMessage());
        }
        userWallets.setGold(_gold);
        userWallets.setDiamond(_diamond);
        userWalletsRepository.save(userWallets);
        return userWallets;

    }
}

