package com.seer.teach.mp.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 com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.exception.CommonException;
import com.seer.teach.mp.entity.MpAgentEmployeeRelationEntity;
import com.seer.teach.mp.mapper.MpAgentEmployeeRelationMapper;
import com.seer.teach.mp.service.IMpAgentEmployeeRelationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 代理商员工关联表 服务实现类
 * </p>
 *
 * @author Lingma
 * @since 2025-12-29
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MpAgentEmployeeRelationServiceImpl extends ServiceImpl<MpAgentEmployeeRelationMapper, MpAgentEmployeeRelationEntity> implements IMpAgentEmployeeRelationService {

    @Override
    public IPage<MpAgentEmployeeRelationEntity> pageList(Page<MpAgentEmployeeRelationEntity> pageParm, MpAgentEmployeeRelationEntity query) {
        LambdaQueryWrapper<MpAgentEmployeeRelationEntity> queryWrapper = new LambdaQueryWrapper<>();
        
        if (query != null) {
            if (query.getAgentId() != null) {
                queryWrapper.eq(MpAgentEmployeeRelationEntity::getAgentId, query.getAgentId());
            }
            if (query.getEmployeeUserId() != null) {
                queryWrapper.eq(MpAgentEmployeeRelationEntity::getEmployeeUserId, query.getEmployeeUserId());
            }
            if (query.getStatus() != null) {
                queryWrapper.eq(MpAgentEmployeeRelationEntity::getStatus, query.getStatus());
            }
        }
        
        queryWrapper.orderByDesc(MpAgentEmployeeRelationEntity::getCreateTime);
        
        return baseMapper.selectPage(pageParm, queryWrapper);
    }

    @Override
    public boolean saveOrUpdateRelation(MpAgentEmployeeRelationEntity entity) {
        if (entity.getId() == null) {
            LambdaQueryWrapper<MpAgentEmployeeRelationEntity> checkWrapper =
                new LambdaQueryWrapper<MpAgentEmployeeRelationEntity>()
                    .eq(MpAgentEmployeeRelationEntity::getAgentId, entity.getAgentId())
                    .eq(MpAgentEmployeeRelationEntity::getEmployeeUserId, entity.getEmployeeUserId());
                    
            long count = super.count(checkWrapper);
            if (count > 0) {
                throw new CommonException(ResultCodeEnum.AGENT_EMPLOYEE_ALREADY_EXISTS);
            }
            entity.setCreateTime(LocalDateTime.now());
        }
        entity.setUpdateTime(LocalDateTime.now());
        return super.saveOrUpdate(entity);
    }

    @Override
    public boolean deleteRelation(Integer id) {
        return this.removeById(id);
    }

    @Override
    public List<MpAgentEmployeeRelationEntity> getEmployeeRelationsByAgentId(Integer agentId) {
        LambdaQueryWrapper<MpAgentEmployeeRelationEntity> queryWrapper = 
            new LambdaQueryWrapper<MpAgentEmployeeRelationEntity>()
                .eq(MpAgentEmployeeRelationEntity::getAgentId, agentId)
                .orderByDesc(MpAgentEmployeeRelationEntity::getCreateTime);
                
        return super.list(queryWrapper);
    }

    @Override
    public boolean isEmployeeOfAgent(Integer agentId, Integer employeeUserId) {
        LambdaQueryWrapper<MpAgentEmployeeRelationEntity> queryWrapper = 
            new LambdaQueryWrapper<MpAgentEmployeeRelationEntity>()
                .eq(MpAgentEmployeeRelationEntity::getAgentId, agentId)
                .eq(MpAgentEmployeeRelationEntity::getEmployeeUserId, employeeUserId)
                .eq(MpAgentEmployeeRelationEntity::getStatus, 1); // 只检查启用状态的关联
                
        return super.count(queryWrapper) > 0;
    }

    @Override
    public MpAgentEmployeeRelationEntity getOneByUserId(Integer userId) {
        return super.getOne(new LambdaQueryWrapper<>(MpAgentEmployeeRelationEntity.class).eq(MpAgentEmployeeRelationEntity::getEmployeeUserId, userId));
    }
}