package com.rf.psychological.user.service.impl;

import cn.hutool.core.date.DateUtil;
import com.rf.psychological.user.dao.model.UserFileEntity;
import com.rf.psychological.user.dao.repository.UserFileRepository;
import com.rf.psychological.user.service.UserFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:用户头像接口
 * @Author: zsf
 * @Date: 2022/12/26
 */
@Service
public class UserFileServiceImpl implements UserFileService {

    @Autowired
    private UserFileRepository fileRepository;

    @Override
    public UserFileEntity saveOrUpdate(UserFileEntity entity) {
        String now = DateUtil.now();
        UserFileEntity fileEntity = fileRepository.findByUserId(entity.getUserId());
        if (fileEntity == null){
            entity.setCreateTime(now);
            entity = fileRepository.save(entity);
        }else {
            fileEntity.setUpdateTime(now);
            fileEntity.setPath(entity.getPath());
            entity = fileRepository.save(fileEntity);
        }
        return entity;
    }

    @Override
    public UserFileEntity findByUserId(String userId) {
        return fileRepository.findByUserId(userId);
    }
}
