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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.common.enums.RoleEnum;
import com.seer.teach.mp.entity.MpAgentEmployeeRelationEntity;
import com.seer.teach.mp.entity.MpAgentEntity;
import com.seer.teach.mp.mapper.MpAgentMapper;
import com.seer.teach.mp.service.IMpAgentEmployeeRelationService;
import com.seer.teach.mp.service.IMpAgentService;
import com.seer.teach.user.api.UserInfoServiceApi;
import com.seer.teach.user.api.dto.UserInfoDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 代理商服务实现类
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class MpAgentServiceImpl extends ServiceImpl<MpAgentMapper, MpAgentEntity> implements IMpAgentService {

    private final UserInfoServiceApi userInfoServiceApi;
    private final IMpAgentEmployeeRelationService mpAgentEmployeeRelationService;

    @Override
    public MpAgentEntity getAgentById(Integer id) {
        return this.getById(id);
    }

    @Override
    public Boolean saveAgent(MpAgentEntity agentEntity, String password) {
        UserInfoDTO userInfoDTO = new UserInfoDTO();
        userInfoDTO.setUserName(agentEntity.getContactName());
        userInfoDTO.setPassword(password);
        userInfoDTO.setMobile(agentEntity.getContactPhone());
        userInfoDTO.setRoleCode(RoleEnum.AGENT);
        Integer userId = userInfoServiceApi.addUserInfoAndAssignRole(userInfoDTO);
        log.info("用户id:{}", userId);
        agentEntity.setContactUserId(userId);
        return this.save(agentEntity);
    }

    @Override
    public Boolean updateAgent(MpAgentEntity agent, String password) {
        boolean result = this.updateById(agent);
        if (result) {
            UserInfoDTO userInfoDTO = new UserInfoDTO();
            userInfoDTO.setId(agent.getContactUserId());
            userInfoDTO.setUserName(agent.getContactName());
            userInfoDTO.setMobile(agent.getContactPhone());
            if (StringUtils.isNotBlank(password)) {
                userInfoDTO.setPassword(password);
            }
            boolean updateUserResult = userInfoServiceApi.updateUserInfo(userInfoDTO);
            log.info("更新用户结果: {}", updateUserResult);
        }
        return result;
    }

    @Override
    public List<String> getAgentName() {
        List<MpAgentEntity> list = this.list();
        return list.stream().map(MpAgentEntity::getAgentName).toList();
    }

    @Override
    public long getCountByPhone(String phone) {
        return this.lambdaQuery().eq(MpAgentEntity::getContactPhone, phone).count();
    }

    @Override
    public List<MpAgentEntity> getListByUserId(Integer userId) {
        List<MpAgentEntity> list = this.lambdaQuery().eq(MpAgentEntity::getContactUserId, userId).list();
        if (CollectionUtil.isEmpty(list)) {
            List<MpAgentEmployeeRelationEntity> relationEntities = mpAgentEmployeeRelationService.getListByUserId(userId);
            if (CollectionUtil.isNotEmpty(relationEntities)) {
                Set<Integer> agentIds = relationEntities.stream().map(MpAgentEmployeeRelationEntity::getAgentId).collect(Collectors.toSet());
                return this.lambdaQuery().in(MpAgentEntity::getId, agentIds).list();
            }
            return List.of();
        }
        return list;
    }
}