package com.tashow.cloud.user.api.user;

import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.userapi.api.user.UserMemberApi;
import com.tashow.cloud.userapi.api.user.dto.UserMemberRespDTO;
import com.tashow.cloud.user.convert.user.MemberUserConvert;
import com.tashow.cloud.user.dal.dataobject.user.UserLoginDO;
import com.tashow.cloud.user.service.user.UserLoginService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;

import static com.tashow.cloud.common.exception.util.ServiceExceptionUtil.exception;
import static com.tashow.cloud.common.pojo.CommonResult.success;
import static com.tashow.cloud.userapi.enums.ErrorCodeConstants.USER_MOBILE_NOT_EXISTS;

/**
 * 会员用户的 API 实现类
 *
 * @author 芋道源码
 */
@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class UserMemberApiImpl implements UserMemberApi {

    @Resource
    private UserLoginService userService;

    @Override
    public CommonResult<UserMemberRespDTO> getUser(Long id) {
        UserLoginDO user = userService.getUser(id);
        return success(MemberUserConvert.INSTANCE.convert2(user));
    }

    @Override
    public CommonResult<List<UserMemberRespDTO>> getUserList(Collection<Long> ids) {
        return success(MemberUserConvert.INSTANCE.convertList2(userService.getUserList(ids)));
    }

    @Override
    public CommonResult<List<UserMemberRespDTO>> getUserListByNickname(String nickname) {
        return success(MemberUserConvert.INSTANCE.convertList2(userService.getUserListByNickname(nickname)));
    }

    @Override
    public CommonResult<UserMemberRespDTO> getUserByMobile(String mobile) {
        return success(MemberUserConvert.INSTANCE.convert2(userService.getUserByMobile(mobile)));
    }

    @Override
    public CommonResult<Boolean> validateUser(Long id) {
        UserLoginDO user = userService.getUser(id);
        if (user == null) {
            throw exception(USER_MOBILE_NOT_EXISTS);
        }
        return success(true);
    }

}
