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

import com.YuyeTech.HeartRate.common.service.CommonServiceImpl;
import com.YuyeTech.HeartRate.game.imageInfo.projo.Pictures;
import com.YuyeTech.HeartRate.dto.UserDTO;
import com.YuyeTech.HeartRate.game.imageInfo.repository.PicturesRepository;
import com.YuyeTech.HeartRate.game.userinfo.projo.UserInfo;
import com.YuyeTech.HeartRate.game.userinfo.repository.UserInfoRepository;
import com.YuyeTech.HeartRate.game.userinfo.vo.UserInfoVo;
import com.YuyeTech.HeartRate.utils.AliyunOSSUtil;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
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 org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

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

    @PersistenceContext
    private EntityManager em;

    @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(bDomain&&userInfo!=null&& StringUtils.isNoneBlank(userInfo.getAvatarUrl())){
            //修改url，添加访问域名
            String url = aliyunOSSUtil.addDomainName(userInfo.getAvatarUrl());
            userInfo.setAvatarUrl(url);
            //清楚对象缓存，避免update到数据库
            Session session = em.unwrap(Session.class);
            session.evict(userInfo);
        }

        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);
    }
}

