package com.seer.teach.mp.admin.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.seer.teach.common.PageListBean;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.mp.admin.controller.req.AgentActivityParticipantQueryReq;
import com.seer.teach.mp.admin.controller.resp.AdminAgentActivityParticipantResp;
import com.seer.teach.mp.admin.convert.AdminAgentActivityParticipantConvert;
import com.seer.teach.mp.admin.service.IAdminAgentActivityParticipantService;
import com.seer.teach.mp.entity.MpAgentActivityParticipantEntity;
import com.seer.teach.mp.service.IMpAgentActivityParticipantService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 代理商活动参与记录表 管理端服务实现类
 * </p>
 *
 * @author
 * @since 2025-12-30
 */
@Service
@RequiredArgsConstructor
public class AdminAgentActivityParticipantServiceImpl implements IAdminAgentActivityParticipantService {

    private final IMpAgentActivityParticipantService mpAgentActivityParticipantService;

    @Override
    public PageListBean<AdminAgentActivityParticipantResp> pageList(AgentActivityParticipantQueryReq query) {
        Page<MpAgentActivityParticipantEntity> page = new Page<>(query.getPageNum(), query.getPageSize());

        LambdaQueryWrapper<MpAgentActivityParticipantEntity> wrapper = new LambdaQueryWrapper<>();
        
        // 根据活动ID查询
        if (query.getActivityId() != null) {
            wrapper.eq(MpAgentActivityParticipantEntity::getActivityId, query.getActivityId());
        }
        
        // 根据代理商ID查询
        if (query.getAgentId() != null) {
            wrapper.eq(MpAgentActivityParticipantEntity::getAgentId, query.getAgentId());
        }
        IPage<MpAgentActivityParticipantEntity> result = mpAgentActivityParticipantService.page(page, wrapper);

        return PageConverterUtils.convertPageListBean(result, AdminAgentActivityParticipantConvert.INSTANCE::convertToRespList);
    }

    @Override
    public boolean saveParticipant(MpAgentActivityParticipantEntity entity) {
        // 检查是否已存在相同的活动、代理商和家长记录
        LambdaQueryWrapper<MpAgentActivityParticipantEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MpAgentActivityParticipantEntity::getActivityId, entity.getActivityId())
                .eq(MpAgentActivityParticipantEntity::getAgentId, entity.getAgentId());

        long count = mpAgentActivityParticipantService.count(wrapper);
        if (count > 0) {
            throw new RuntimeException("该代理商活动参与记录已存在");
        }

        return mpAgentActivityParticipantService.save(entity);
    }

    @Override
    public boolean updateParticipant(MpAgentActivityParticipantEntity entity) {
        // 检查是否存在相同的活动、代理商和家长记录（排除当前记录）
        LambdaQueryWrapper<MpAgentActivityParticipantEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MpAgentActivityParticipantEntity::getActivityId, entity.getActivityId())
                .eq(MpAgentActivityParticipantEntity::getAgentId, entity.getAgentId())
                .ne(MpAgentActivityParticipantEntity::getId, entity.getId());

        long count = mpAgentActivityParticipantService.count(wrapper);
        if (count > 0) {
            throw new RuntimeException("该代理商活动参与记录已存在");
        }

        return mpAgentActivityParticipantService.updateById(entity);
    }

    @Override
    public boolean deleteParticipant(Integer id) {
        return mpAgentActivityParticipantService.removeById(id);
    }

    @Override
    public AdminAgentActivityParticipantResp getById(Integer id) {
        MpAgentActivityParticipantEntity entity = mpAgentActivityParticipantService.getById(id);
        return AdminAgentActivityParticipantConvert.INSTANCE.convertToResp(entity);
    }

}