/**
 * @ProjectName: 智慧评标-主观分助手
 * @Package: com.zhpb.gdyd_zhpb_zgf.service.impl
 * @ClassName: UserServiceImpl
 * @Description: 用户服务实现类，实现用户查询、创建、更新等业务逻辑
 * @Author: 张志锋
 * @Date: 2025-10-31
 * @Version: 1.0
 * @Copyright: © 2025 智慧评标-主观分助手
 */
package com.zhpb.gdyd_zhpb_zgf.service.impl;

import com.zhpb.gdyd_zhpb_zgf.entity.mysql.User;
import com.zhpb.gdyd_zhpb_zgf.repository.mysql.UserRepository;
import com.zhpb.gdyd_zhpb_zgf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserRepository userRepository;
    
    @Override
    public User findByUserId(String userId) {
        return userRepository.findByUserId(userId);
    }
    
    @Override
    @Transactional
    public User createUser(String userId, String name) {
        return createUser(userId, name, null);
    }
    
    @Override
    @Transactional
    public User createUser(String userId, String name, String projectId) {
        User user = new User();
        user.setUserId(userId);
        user.setName(name);
        user.setProjectId(projectId);
        user.setStatus("ACTIVE");
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        
        return userRepository.save(user);
    }
    
    @Override
    @Transactional
    public User updateUser(User user) {
        user.setUpdateTime(LocalDateTime.now());
        return userRepository.save(user);
    }
}

