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.service.IAdminAgentEmployeeRelationService;
import com.seer.teach.mp.admin.controller.req.AgentEmployeeRelationQueryReq;
import com.seer.teach.mp.admin.controller.req.AgentEmployeeRelationReq;
import com.seer.teach.mp.admin.controller.resp.AgentEmployeeRelationResp;
import com.seer.teach.mp.admin.convert.AdminAgentEmployeeRelationConvert;
import com.seer.teach.mp.entity.MpAgentEmployeeRelationEntity;
import com.seer.teach.mp.service.IMpAgentEmployeeRelationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 管理端代理商员工关系服务实现类
 * </p>
 *
 * @author Lingma
 * @since 2025-12-30
 */
@Service
@RequiredArgsConstructor
public class AdminAgentEmployeeRelationServiceImpl implements IAdminAgentEmployeeRelationService {

    private final IMpAgentEmployeeRelationService agentEmployeeRelationService;

    @Override
    public PageListBean<AgentEmployeeRelationResp> pageList(AgentEmployeeRelationQueryReq query) {
        Page<MpAgentEmployeeRelationEntity> pageParm = new Page<>(query.getPageNo(), query.getPageSize());
        var pageResult = agentEmployeeRelationService.page(pageParm, new LambdaQueryWrapper<>(MpAgentEmployeeRelationEntity.class)
                .eq(Objects.nonNull(query.getAgentId()), MpAgentEmployeeRelationEntity::getAgentId, query.getAgentId())
                .eq(Objects.nonNull(query.getEmployeeUserId()), MpAgentEmployeeRelationEntity::getEmployeeUserId, query.getEmployeeUserId())
                .eq(Objects.nonNull(query.getStatus()), MpAgentEmployeeRelationEntity::getStatus, query.getStatus()));

        return PageConverterUtils.convertPageListBean(pageResult, AdminAgentEmployeeRelationConvert.INSTANCE::convertToRespList);
    }

    @Override
    public boolean saveOrUpdateRelation(AgentEmployeeRelationReq request) {
        MpAgentEmployeeRelationEntity entity = AdminAgentEmployeeRelationConvert.INSTANCE.convert(request);
        return agentEmployeeRelationService.saveOrUpdateRelation(entity);
    }

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

    @Override
    public AgentEmployeeRelationResp getById(Integer id) {
        MpAgentEmployeeRelationEntity entity = agentEmployeeRelationService.getById(id);
        return AdminAgentEmployeeRelationConvert.INSTANCE.convertToResp(entity);
    }

    @Override
    public List<AgentEmployeeRelationResp> getEmployeeRelationsByAgentId(Integer agentId) {
        var relations = agentEmployeeRelationService.getEmployeeRelationsByAgentId(agentId);
        return relations.stream()
            .map(AdminAgentEmployeeRelationConvert.INSTANCE::convertToResp)
            .toList();
    }
}