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

import com.seer.teach.mp.app.controller.resp.AppMpAgentResp;
import com.seer.teach.mp.app.service.IAppAgentService;
import com.seer.teach.mp.entity.MpAgentEmployeeRelationEntity;
import com.seer.teach.mp.entity.MpAgentEntity;
import com.seer.teach.mp.service.IMpAgentEmployeeRelationService;
import com.seer.teach.mp.service.IMpAgentService;
import lombok.RequiredArgsConstructor;
import com.seer.teach.mp.app.convert.AppAgentConvert;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * App端代理商服务实现类
 */
@Service
@RequiredArgsConstructor
public class AppAgentServiceImpl implements IAppAgentService {

    private final IMpAgentService mpAgentService;
    private final IMpAgentEmployeeRelationService mpAgentEmployeeRelationService;

    @Override
    public AppMpAgentResp getAgentRespByUserId(Integer userId) {
        MpAgentEmployeeRelationEntity mpAgentEmployeeRelationEntity = mpAgentEmployeeRelationService.getOneByUserId(userId);
        if (mpAgentEmployeeRelationEntity == null) {
            return null;
        }
        MpAgentEntity agentEntity = mpAgentService.getAgentById(mpAgentEmployeeRelationEntity.getAgentId());
        if (Objects.isNull(agentEntity)) {
            return null;
        }
        AppMpAgentResp appMpAgentResp = AppAgentConvert.INSTANCE.entityToResp(agentEntity);
        Boolean isContactPerson = Objects.nonNull(agentEntity.getContactUserId()) && agentEntity.getContactUserId().equals(userId);
        appMpAgentResp.setIsContactPerson(isContactPerson);
        return appMpAgentResp;
    }

    @Override
    public Integer getAgentIdByUserId(Integer userId) {
        MpAgentEmployeeRelationEntity mpAgentEmployeeRelationEntity = mpAgentEmployeeRelationService.getOneByUserId(userId);
        if (mpAgentEmployeeRelationEntity == null) {
            return null;
        }
        return mpAgentEmployeeRelationEntity.getAgentId();
    }
}