package com.seer.teach.mp.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.req.AgentActivityParticipantReq;
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.MpActivityEntity;
import com.seer.teach.mp.entity.MpAgentActivityParticipantEntity;
import com.seer.teach.mp.entity.MpAgentEntity;
import com.seer.teach.mp.service.IMpActivityService;
import com.seer.teach.mp.service.IMpAgentActivityParticipantService;
import com.seer.teach.mp.service.IMpAgentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

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

    private final IMpAgentActivityParticipantService mpAgentActivityParticipantService;

    private final IMpAgentService mpAgentService;

    private final IMpActivityService mpActivityService;

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

        LambdaQueryWrapper<MpAgentActivityParticipantEntity> wrapper = new LambdaQueryWrapper<>();
        // 根据活动ID查询
        if (query.getActivityName() != null) {
            wrapper.like(MpAgentActivityParticipantEntity::getActivityName, query.getActivityName());
        }
        // 根据代理商ID查询
        if (query.getAgentName() != null) {
            wrapper.like(MpAgentActivityParticipantEntity::getAgentName, query.getAgentName());
        }
        Page<MpAgentActivityParticipantEntity> result = mpAgentActivityParticipantService.page(page, wrapper);
        return PageConverterUtils.convertPageListBean(result, AdminAgentActivityParticipantConvert.INSTANCE::convertToRespList);
    }


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

        long count = mpAgentActivityParticipantService.count(wrapper);
        if (count > 0) {
            throw new RuntimeException("该代理商活动参与记录已存在");
        }
        Integer agentId = mpAgentService.lambdaQuery().eq(MpAgentEntity::getAgentName, entity.getAgentName()).one().getId();
        Integer activityId = mpActivityService.lambdaQuery().eq(MpActivityEntity::getActivityName, entity.getActivityName()).one().getId();
        MpAgentActivityParticipantEntity result = AdminAgentActivityParticipantConvert.INSTANCE.convertToEntity(entity);
        result.setActivityId(activityId);
        result.setAgentId(agentId);
        return mpAgentActivityParticipantService.save(result);
    }

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

        long count = mpAgentActivityParticipantService.count(wrapper);
        if (count > 0) {
            throw new RuntimeException("该代理商活动参与记录已存在");
        }
        Integer agentId = mpAgentService.lambdaQuery().eq(MpAgentEntity::getAgentName, entity.getAgentName()).one().getId();
        Integer activityId = mpActivityService.lambdaQuery().eq(MpActivityEntity::getActivityName, entity.getActivityName()).one().getId();
        entity.setActivityId(activityId);
        entity.setAgentId(agentId);
        return mpAgentActivityParticipantService.updateById(entity);
    }

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

    @Override
    public Boolean deleteById(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        return mpAgentActivityParticipantService.removeByIds(ids);
    }

}