package cn.newfeifan.mall.module.member.api.user;

import cn.newfeifan.mall.module.member.api.user.dto.MemberUserRespDTO;
import cn.newfeifan.mall.module.member.convert.user.MemberUserConvert;
import cn.newfeifan.mall.module.member.dal.dataobject.user.MemberUserDO;
import cn.newfeifan.mall.module.member.dal.mysql.user.MemberUserMapper;
import cn.newfeifan.mall.module.member.service.user.MemberUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员用户的 API 实现类
 *
 * @author 非繁源码
 */
@Service
@Validated
public class MemberUserApiImpl implements MemberUserApi {

    @Resource
    private MemberUserService userService;
    @Resource
    private MemberUserMapper memberUserMapper;

    @Override
    public MemberUserRespDTO getUser(Long id) {
        MemberUserDO user = userService.getUser(id);
        return MemberUserConvert.INSTANCE.convert2(user);
    }

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

    @Override
    public MemberUserRespDTO getUserByMobile(String mobile) {
        return MemberUserConvert.INSTANCE.convert2(userService.getUserByMobile(mobile));
    }

    @Override
    public List<MemberUserRespDTO> getUsersByMobile(String mobile) {
        return MemberUserConvert.INSTANCE.convertList2(userService. selectListByMobile(mobile));
    }

    @Override
    public List<String> getUserRsgIds(Long systemUserId, Long userId) {
        List<MemberUserDO> memberUserDOS;
        if (systemUserId != null) {
            memberUserDOS = memberUserMapper.selectList(new LambdaQueryWrapper<MemberUserDO>()
                    .eq(MemberUserDO::getSystemUsersId, systemUserId)
                    .isNotNull(MemberUserDO::getAndroidRegisterId));
        } else {
            memberUserDOS = memberUserMapper.selectList(new LambdaQueryWrapper<MemberUserDO>()
                    .eq(MemberUserDO::getId, userId)
                    .isNotNull(MemberUserDO::getAndroidRegisterId));

        }
        return memberUserDOS.stream().map(MemberUserDO::getAndroidRegisterId).collect(Collectors.toList());
    }

}
