package com.yonge.cooleshow.biz.dal.service.impl;

import com.dayaedu.cbs.common.enums.EClientType;
import com.dayaedu.cbs.openfeign.service.CbsUserService;
import com.dayaedu.cbs.openfeign.wrapper.user.CbsUserWrapper;
import com.yonge.cooleshow.biz.dal.dao.TeacherDao;
import com.yonge.cooleshow.biz.dal.dto.BasicUserInfo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;

@Service
public class CbsUserServiceImpl implements CbsUserService {

    @Resource
    private TeacherDao teacherDao;

    @Override
    public List<CbsUserWrapper.User> list(CbsUserWrapper.UserQuery query) {
        if (query == null || CollectionUtils.isEmpty(query.getUserIds())) {
            return new ArrayList<>();
        }
        List<BasicUserInfo> basicUserInfos = teacherDao.findBasicUserInfo(new HashSet<>(query.getUserIds()));
        List<CbsUserWrapper.User> result = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(basicUserInfos)){
            for (BasicUserInfo simpleUserDto : basicUserInfos) {
                CbsUserWrapper.User user = new CbsUserWrapper.User();
                user.setId(simpleUserDto.getUserId());
                user.setUsername(query.getClientType() == null || query.getClientType() == EClientType.STUDENT ?
                        simpleUserDto.getUsername() : Optional.ofNullable(simpleUserDto.getRealName()).orElse(simpleUserDto.getUsername()));
                user.setPhone(simpleUserDto.getPhone());
                result.add(user);
            }
        }
        return result;
    }

}
