
package com.sonic.bear.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sonic.bear.domain.entity.User;
import com.sonic.bear.domain.entity.UserThird;
import com.sonic.bear.lib.output.BaseUserInfoOutput;
import com.sonic.bear.lib.output.UserInfoListOutput;
import com.sonic.bear.service.UserSearchService;
import com.sonic.bear.service.UserService;
import com.sonic.bear.service.UserThirdService;
import com.sonic.bear.utils.BeanConver;
import com.sonic.lion.lib.client.SubscribeClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author code
 */
@Slf4j
@Service
public class UserSearchServiceImpl implements UserSearchService {

    @Autowired
    private UserService userService;
    @Autowired
    private UserThirdService userThirdService;
    @Autowired
    private SubscribeClient subscribeClient;

    @Override
    public List<UserInfoListOutput> batchGetUserInfo(List<Long> userIdList) {
        List<User> list = userService.list(Wrappers.<User>lambdaQuery()
                .select(User::getUserId, User::getIdCard, User::getHeadImage, User::getNickname, User::getSex)
                .in(User::getUserId, userIdList));
        return BeanConver.copeList(list, UserInfoListOutput.class);
    }

    @Override
    public BaseUserInfoOutput baseUserInfo(Long userId) {
        User user = userService.getOne(Wrappers.<User>lambdaQuery()
                .select(User::getId, User::getUserId, User::getIdCard, User::getHeadImage, User::getNickname, User::getSex, User::getBirthday, User::getLastAccessTime)
                .eq(User::getUserId, userId));
        //根据最后一次访问时间的间隔来判断是否需要更新最后访问时间(每5分钟更新一次，跨天的话首次访问时同步更新)
        if(user.getLastAccessTime() == null
                || user.getLastAccessTime().plusMinutes(5).isBefore(LocalDateTime.now())
                || user.getLastAccessTime().getDayOfMonth() != LocalDateTime.now().getDayOfMonth()) {
            userService.update(Wrappers.<User>lambdaUpdate().set(User::getLastAccessTime, LocalDateTime.now()).eq(User::getId, user.getId()));
        }
        BaseUserInfoOutput  output = BeanConver.copeBean(user, BaseUserInfoOutput.class);
        output.setCpUserInfo(user.getSex() == null || StringUtils.isEmpty(user.getNickname()) || user.getBirthday() == null);
        //查询三方信息
        UserThird userThird = userThirdService.getOne(Wrappers.<UserThird>lambdaQuery()
                .select(UserThird::getThirdType, UserThird::getNickname, UserThird::getEmail)
                .eq(UserThird::getUserId, userId));
        output.setThirdType(userThird == null ? null : userThird.getThirdType());
        output.setThirdNickname(userThird == null ? null : userThird.getNickname());
        output.setThirdEmail(userThird == null ? null : userThird.getEmail());
        output.setIsMember(subscribeClient.queryUserIsSubscribe(userId));
        return output;
    }


}
