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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.common.enums.RoleEnum;
import com.seer.teach.common.utils.CommonUtils;
import com.seer.teach.mp.entity.MpAgentEntity;
import com.seer.teach.mp.mapper.MpAgentMapper;
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.springframework.stereotype.Service;

import java.util.List;

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

    private final UserInfoServiceApi userInfoServiceApi;

    @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.getAgentName());
        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 agentEntity) {
        return this.updateById(agentEntity);
    }

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