package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.entity.ActivityRegistration;
import com.zhentao.mapper.ActivityRegistrationMapper;
import com.zhentao.service.ActivityRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 联想
* @description 针对表【activity_registration(活动报名记录表)】的数据库操作Service实现
* @createDate 2025-12-17 09:04:20
*/
@Service
public class ActivityRegistrationServiceImpl extends ServiceImpl<ActivityRegistrationMapper, ActivityRegistration>
    implements ActivityRegistrationService{

    @Autowired
    private ActivityRegistrationMapper activityRegistrationMapper;

    @Override
    public Map<String, Object> getRegistrationList(Integer page, Integer pageSize, 
                                                    Integer activityId, 
                                                    Integer checkInStatus, 
                                                    Integer paymentStatus, 
                                                    String keyword) {
        // 查询所有符合条件的记录
        List<ActivityRegistration> allList = activityRegistrationMapper.selectRegistrationList(
                activityId, checkInStatus, paymentStatus, keyword);
        
        // 手动分页
        int total = allList.size();
        int start = (page - 1) * pageSize;
        int end = Math.min(start + pageSize, total);
        List<ActivityRegistration> pageList;
        if (start < total) {
            pageList = allList.subList(start, end);
        } else {
            pageList = new java.util.ArrayList<>();
        }
        
        // 如果没有支付状态，默认设置为1（已支付）
        for (ActivityRegistration registration : pageList) {
            if (registration.getPaymentStatus() == null) {
                registration.setPaymentStatus(1);
            }
        }
        
        Map<String, Object> result = new HashMap<>();
        result.put("list", pageList);
        result.put("total", total);
        result.put("page", page);
        result.put("pageSize", pageSize);
        
        return result;
    }

    @Override
    public List<ActivityRegistration> getAllRegistrations(Integer activityId, 
                                                           Integer checkInStatus, 
                                                           Integer paymentStatus, 
                                                           String keyword) {
        List<ActivityRegistration> list = activityRegistrationMapper.selectRegistrationList(
                activityId, checkInStatus, paymentStatus, keyword);
        
        // 如果没有支付状态，默认设置为1（已支付）
        for (ActivityRegistration registration : list) {
            if (registration.getPaymentStatus() == null) {
                registration.setPaymentStatus(1);
            }
        }
        
        return list;
    }

    @Override
    public Map<String, Object> getRegistrationStats(Integer activityId) {
        Map<String, Object> stats = new HashMap<>();
        
        if (activityId == null) {
            stats.put("totalCount", 0);
            stats.put("checkedInCount", 0);
            stats.put("notCheckedInCount", 0);
            return stats;
        }
        
        // 报名总数：签到状态为已报名(status=1)的记录数
        List<ActivityRegistration> registeredList = activityRegistrationMapper.selectRegistrationList(
                activityId, 1, null, null);
        int totalCount = registeredList.size();
        
        // 已签到：查询status=3的记录数（这些记录之前是status=1，现在已签到）
        // 注意：已签到的记录现在status=3，所以不会出现在status=1的查询结果中
        List<ActivityRegistration> checkedInList = activityRegistrationMapper.selectRegistrationList(
                activityId, 3, null, null);
        int checkedInCount = checkedInList.size();
        
        // 未签到：签到状态为已报名(status=1)但未签到(status≠3)的记录数
        // 根据用户需求，未签到也是根据签到状态是已报名进行显示的
        // 所以未签到数 = 报名总数（status=1的记录数）
        // 因为status=1的记录就是未签到的
        int notCheckedInCount = totalCount;
        
        stats.put("totalCount", totalCount);
        stats.put("checkedInCount", checkedInCount);
        stats.put("notCheckedInCount", notCheckedInCount);
        
        return stats;
    }
}




