package com.poyee;

import com.tzy.PoyiApplication;
import com.tzy.framework.util.RedisTools;
import com.tzy.framework.util.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.teasoft.bee.osql.PreparedSql;
import org.teasoft.bee.osql.SuidRich;

import java.util.concurrent.TimeUnit;


@Slf4j
@SpringBootTest(classes = PoyiApplication.class)
@RunWith(SpringRunner.class)
class ExamTest {


	@Autowired
	private SuidRich suidRich;
	@Autowired
	private PreparedSql preparedSql;


	@Test
	public void testBatchInsert(){
		//List<TestData> list = new ArrayList<>();
		//for(int i = 0; i <5;i++){
		//	list.add(new TestData(null,"name"+i));
		//}
		//suidRich.insert(list);
		//long id = suidRich.insertAndReturnId(new TestData(null, "name2022"), IncludeType.INCLUDE_EMPTY);
		//System.out.println(id);
	}

	@Test
	public void testUpdate(){
		String sql="update sku set sku_stock=sku_stock-? where id =?";
		preparedSql.modify(sql,new Object[]{1,2});
	}


	@Test
	public void testQuerySqlRoom() {

		int i=0;
		while (true) {
			i++;
			new Thread(){
				@Override
				public void run() {
					lock();
				}
			}.start();

			if(i>1){
				break;
			}
		}
		System.out.println("-------end-----");
	}

	private void lock(){
		RedisUtils redisUtils = RedisTools.getRedisUtils();
			boolean isLock;
			try {
				isLock = redisUtils.tryLock("REDLOCK_KEY", TimeUnit.MILLISECONDS,15L );
				if (isLock) {
					Thread.sleep(16000);
					log.info("-----------111------22222"+Thread.currentThread().getName());
					redisUtils.unlock("REDLOCK_KEY");
				}
			} catch (Exception e) {
				log.info("异常:{}",e);
			} finally {
				// 无论如何, 最后都要解锁
				try {
					redisUtils.unlock("REDLOCK_KEY");
				}catch (Exception e){
					log.info("异常:{}",e);
				}
				System.out.println("释放锁");
			}
		}






}
