package com.YuyeTech.TPlat.game.userinfo.service;

import com.YuyeTech.TPlat.common.service.CommonService;
import com.YuyeTech.TPlat.common.service.CommonServiceImpl;
import com.YuyeTech.TPlat.game.imageInfo.projo.Pictures;
import com.YuyeTech.TPlat.dto.UserDTO;
import com.YuyeTech.TPlat.game.imageInfo.repository.PicturesRepository;
import com.YuyeTech.TPlat.game.userinfo.projo.UserInfo;
import com.YuyeTech.TPlat.game.userinfo.repository.UserInfoRepository;
import com.YuyeTech.TPlat.game.userinfo.service.UserInfoService;
import com.YuyeTech.TPlat.game.userinfo.vo.UserInfoVo;
import com.YuyeTech.TPlat.utils.AliyunOSSUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

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

/**
 * 用户信息
 * @author:slambb
 * @date:2019/12/5
 */
@Service
public class UserInfoServiceImpl extends CommonServiceImpl<UserInfoVo, UserInfo, String> implements UserInfoService {

    @Autowired
    private UserInfoRepository userInfoRepository;

    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    @Autowired
    private PicturesRepository picturesRepository;

    //查找用户信息
    @Override
    public UserInfo findUserInfoById(String userId,Boolean bDomain) {
        UserInfo userInfo = userInfoRepository.findById(userId).orElse(null);
//        if(userInfo ==null){
//            throw  new UserException(UserEnum.USER_NOT_EXIST);
//        }

        if(bDomain&&userInfo!=null&& StringUtils.isNoneBlank(userInfo.getAvatarUrl())){
            //修改url，添加访问域名
            String url = aliyunOSSUtil.addDomainName(userInfo.getAvatarUrl());

            userInfo.setAvatarUrl(url);
        }
        return userInfo;
    }


    @Override
    public UserInfo addUserInfo(UserInfo userInfo) {
        return userInfoRepository.save(userInfo);
    }

    @Override
    public String deleteUserInfoById(String userId) {
        userInfoRepository.deleteById(userId);
        return "delete success";
    }

    @Override
    public List<Pictures> getDefaultAvatar() {
        //默认头像类型type 是 3;
        return picturesRepository.findByPictureType(3);
    }

    @Override
    public List<UserDTO> findUserInfoListByIDs(List<String> userIdList) {
        List<UserDTO> gameDTOList = new ArrayList<>();
        if(userIdList.size() !=0){
            gameDTOList = userInfoRepository.findByUserIdIn(userIdList).stream().map(e -> new UserDTO(
                    e.getUsername(),
                    e.getGender(),
                    e.getSignature(),
                    aliyunOSSUtil.addDomainName(e.getAvatarUrl()),
                    e.getCityCode(),
                    e.getHeight(),
                    e.getWeight(),
                    e.getBirthday()
            )).collect(Collectors.toList());

        }

        return gameDTOList;
    }

    @Override
    public Page<UserDTO> findAllByPageable(Pageable pageable) {

       Page<UserInfo> userInfos =  userInfoRepository.findAll(pageable);
       List<UserDTO> pageList = userInfos.stream().map(e -> new UserDTO(
                e.getUsername(),
                e.getGender(),
                e.getSignature(),
                aliyunOSSUtil.addDomainName(e.getAvatarUrl()),
                e.getCityCode(),
                e.getHeight(),
                e.getWeight(),
                e.getBirthday()
        )).collect(Collectors.toList());
        Page endPage = new PageImpl(pageList, pageable, userInfos.getTotalElements());
        return endPage;
    }

    @Override
    public Page<UserInfo> findAllUserInfoByPageable(Pageable pageable) {
        Page<UserInfo> userInfos =  userInfoRepository.findAll(pageable);
        List<UserInfo> pageList = userInfos.stream().map(e ->{
                 e.setAvatarUrl(aliyunOSSUtil.addDomainName(e.getAvatarUrl()));
                  return e;
         }).collect(Collectors.toList());
        Page endPage = new PageImpl(pageList, pageable, userInfos.getTotalElements());
        return endPage;
    }

    @Override
    public Page<UserInfo> searchByUsername(Pageable pageable, String username) {
        return userInfoRepository.searchByUsername(pageable,username);
    }
}

