package com.izouma.nineth.service.rewardpoints;

import com.anji.captcha.util.StringUtils;
import com.izouma.nineth.config.RedisKeys;
import com.izouma.nineth.domain.UserIntegral;
import com.izouma.nineth.domain.UserIntegralChangeLog;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.enums.IntegralChangeType;
import com.izouma.nineth.enums.IntegralFlag;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.lock.RedisLockable;
import com.izouma.nineth.repo.UserRepo;
import com.izouma.nineth.repo.rewardpoints.UserIntegralChangeLogRepo;
import com.izouma.nineth.utils.JpaUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 *
 * 用户积分变动服务
 *
 */
@Slf4j
@Service
@AllArgsConstructor
public class UserIntegralChangeLogService {

    private UserRepo userRepo;

    private UserIntegralChangeLogRepo userIntegralChangeLogRepo;

    private UserIntegralService userIntegralService;

    public Page<UserIntegralChangeLog> all(PageQuery pageQuery) {
        return userIntegralChangeLogRepo.findAll(JpaUtils.toSpecification(pageQuery, UserIntegralChangeLog.class), JpaUtils.toPageRequest(pageQuery));
    }

    /**
     * 抽奖
     * @return
     */
    @RedisLockable(prefix= RedisKeys.INTEGRAL_CHANGE_LOCK, key="#userId", isWaiting = true, retryCount = 100)
    @Transactional
    public UserIntegralChangeLog createByDrawLottery(Long userId, Long integral){
        UserIntegralChangeLog.UserIntegralChangeLogBuilder builder = UserIntegralChangeLog.builder();
        builder.userId(userId)
                .changeType(IntegralChangeType.DRAW_LOTTERY)
                .description(IntegralChangeType.DRAW_LOTTERY.getDescription())
                .flag(IntegralFlag.DEBIT)
                .integral(-integral);
        UserIntegralChangeLog changeLog = userIntegralChangeLogRepo.saveAndFlush(builder.build());
        UserIntegral userIntegral = userIntegralService.changeIntegral(changeLog);
        changeLog.setRestIntegral(userIntegral.getTotalIntegral());
        userIntegralChangeLogRepo.saveAndFlush(changeLog);
        return changeLog;
    }


    /**
     * 通用送积分方法
     * @return
     */
    @RedisLockable(prefix= RedisKeys.INTEGRAL_CHANGE_LOCK, key="#userId", isWaiting = true, retryCount = 100)
    @Transactional
    public UserIntegralChangeLog createByChangeType(IntegralChangeType changeType, Long userId, Long integral){
        IntegralFlag flag = IntegralFlag.CREDIT;
        if(changeType == IntegralChangeType.DRAW_LOTTERY){
            flag = IntegralFlag.DEBIT;
            integral = -integral;
        }

        UserIntegralChangeLog.UserIntegralChangeLogBuilder builder = UserIntegralChangeLog.builder();
        builder.userId(userId)
                .changeType(changeType)
                .description(changeType.getDescription())
                .flag(flag)
                .integral(integral);
        UserIntegralChangeLog changeLog = userIntegralChangeLogRepo.saveAndFlush(builder.build());
        UserIntegral userIntegral = userIntegralService.changeIntegral(changeLog);
        changeLog.setRestIntegral(userIntegral.getTotalIntegral());
        userIntegralChangeLogRepo.saveAndFlush(changeLog);

        return changeLog;
    }








}
