package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import shop.alien.entity.store.*;
import shop.alien.entity.store.vo.ActivityConfigVo;
import shop.alien.entity.store.vo.ActivityPeriodVo;
import shop.alien.mapper.*;
import shop.alien.store.service.ActivityConfigService;
import shop.alien.store.service.UserPointService;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 *
 * 签到类型活动服务
 * @author zhangchen
 * @since 2025-9-8
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserPointServiceImpl extends ServiceImpl<UserPointMapper, UserPoint> implements UserPointService {
    private final UserPointMapper userPointMapper;
    private final ActivityInviteLogMapper activityInviteLogMapper;
    private final UserSignInRecordMapper userSignInRecordMapper;

    @Override
    public UserPoint getPoint(Integer userId) {
        LambdaQueryWrapper<UserPoint> userPointLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userPointLambdaQueryWrapper.eq(UserPoint::getUserId, userId);
        userPointLambdaQueryWrapper.eq(UserPoint::getDeleteFlag,0);
        List<UserPoint> userPointList = userPointMapper.selectList(userPointLambdaQueryWrapper);
        UserPoint userPoint = new UserPoint();
        if(!CollectionUtils.isEmpty(userPointList)){
            userPoint = userPointList.get(0);
        } else {
            userPoint.setUserId(userId);
            userPoint.setUserPoint(0);
            userPointMapper.insert(userPoint);
        }
        return userPoint;
    }

    @Override
    public UserPoint addPoint(Integer userId, Integer point) {
        UserPoint userPoint = new UserPoint();
        if(userId!=null && point!=null){
            LambdaQueryWrapper<UserPoint> userPointLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userPointLambdaQueryWrapper.eq(UserPoint::getUserId, userId);
            userPointLambdaQueryWrapper.eq(UserPoint::getDeleteFlag,0);
            List<UserPoint> userPointList = userPointMapper.selectList(userPointLambdaQueryWrapper);
            if(!CollectionUtils.isEmpty(userPointList)){
                userPoint = userPointList.get(0);
                int totalPoint = point + userPoint.getUserPoint();
                userPoint.setUserPoint(totalPoint);
                userPointMapper.updateById(userPoint);
            } else {
                userPoint.setUserId(userId);
                userPoint.setUserPoint(point);
                userPointMapper.insert(userPoint);
            }
        }
        return userPoint;
    }

    @Override
    public UserPoint getUserPointDetails(Integer userId) {
        int point = 0;
        LambdaQueryWrapper<ActivityInviteLog> activityInviteLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        activityInviteLogLambdaQueryWrapper.eq(ActivityInviteLog::getInviteUserId, userId);
        activityInviteLogLambdaQueryWrapper.eq(ActivityInviteLog::getDeleteFlag, 0);
        List<ActivityInviteLog> activityInviteLogList = activityInviteLogMapper.selectList(activityInviteLogLambdaQueryWrapper);
        int invitePoint = 0;
        if(com.alibaba.nacos.common.utils.CollectionUtils.isNotEmpty(activityInviteLogList)){
            for(ActivityInviteLog activityInviteLog : activityInviteLogList){
                int inviteRewardType = activityInviteLog.getInviteRewardType();
                if(inviteRewardType == 2 && activityInviteLog.getInviteRewardPoint() != null){
                    invitePoint = activityInviteLog.getInviteRewardPoint() + invitePoint;
                }
            }
        }
        point = point + invitePoint;

        int totalPoint = 0;
        LambdaQueryWrapper<UserSignInRecord> userSignInRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userSignInRecordLambdaQueryWrapper.eq(UserSignInRecord::getUserId, userId);
        userSignInRecordLambdaQueryWrapper.eq(UserSignInRecord::getDeleteFlag, 0);
        userSignInRecordLambdaQueryWrapper.orderByDesc(UserSignInRecord::getSignInDate);
        List<UserSignInRecord> userSignInRecordList =  userSignInRecordMapper.selectList(userSignInRecordLambdaQueryWrapper);
        if(!CollectionUtils.isEmpty(userSignInRecordList)){
            totalPoint = userSignInRecordList.stream()
                    .mapToInt(UserSignInRecord::getPointsObtained)
                    .sum();
        }
        point = point + totalPoint;

        UserPoint userPoint = new UserPoint();
        userPoint.setUserPoint(point);
        userPoint.setUserId(userId);
        return userPoint;
    }
}
