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 hcj on 18-7-16
 */
@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 = Context.builder().username("hcj").password("123")
                .context("hello world").enabled(true).build();
        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 = Context.builder().username("hcj").password("123")
                .context("hello world").enabled(true).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);
    }

//    public static void main(String[] args) {
//        User build = User.builder().username("hcj").password("123")
//                .context("hello world").enabled(true).build();
//        String s = JSONObject.toJSONString(build);
//        System.out.println(s);
//
//    }

}
