package com.Service;

import com.Domain.Context;
import com.Domain.User;
import com.Mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * cglib 代理 Created by lqt
 */
@Service
public class UserService {
  @Autowired
  private UserMapper userMapper;

  // 脏读
  @Transactional(isolation = Isolation.READ_UNCOMMITTED)
  public void insert() {
    User user = new User();
    user.setUsername("hch");
    user.setPassword("123");
    user.setEnabled(true);
    Context build = new Context();
    build.setUsername("hcj");
    build.setPassword("123");
    build.setContext("hello world");
    build.setEnabled(true);
    user.setContext(build);
    userMapper.insert(user);
    try {
      Thread.sleep(10000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    throw new RuntimeException();
  }

  public void insertUser(User user) {
    user.setEnabled(true);
    Context build = new Context();
    build.setUsername("hcj");
    build.setPassword("123");
    build.setContext("hello world");
    build.setEnabled(true);
    user.setContext(build);
    user.setContext(build);
    userMapper.insert(user);
  }

//  @Transactional(readOnly = true)
  public List<User> findUser() {
    return userMapper.selectAll();
  }

  public User selectUser(String username, String password) {
    return userMapper.selectUser(username, password);
  }

  public List<User> selectUserByUsername(String username) {
    return userMapper.selectUserByUsername(username);
  }

  public void insertUserNoObject(User user) {
    user.setEnabled(true);
    userMapper.insert(user);
  }
}
