package jj.tech.paolu.biz.webadmin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.jdbc.lock.JdbcLockRegistry;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jj.tech.paolu.biz.webadmin.component.Hgt8Component;
import jj.tech.paolu.biz.webadmin.vo.IdPageVo;
import jj.tech.paolu.config.enums.Games;
import jj.tech.paolu.repository.mybatis.dao.CurrentG24Mapper;
import jj.tech.paolu.repository.mybatis.dao.HistoryG24Mapper;
import jj.tech.paolu.repository.mybatis.dao.support.HistoryG24DynamicSqlSupport;
import jj.tech.paolu.repository.mybatis.entity.CurrentG24;
import jj.tech.paolu.repository.mybatis.entity.HistoryG24;
import jj.tech.paolu.repository.mybatis.entity.Idc9998AdminInfo;
import jj.tech.paolu.utils.Page;
import jj.tech.paolu.utils.R;

@RestController
@RequestMapping(value = "/op/idc9998",name = "游戏报告列表")
@Tag(name  = "Idc9998Controller", description = "游戏报告列表")
public class Idc9998Controller {
	@Autowired JdbcLockRegistry jdbcLockRegistry;
	
	@Autowired CurrentG24Mapper currentG24Mapper;
	@Autowired HistoryG24Mapper historyG24Mapper;
	
	@Autowired Hgt8Component hgt8Component;
	
	
	@Operation(summary = "获取最新游戏数据", description = "test")
	@PostMapping("/getCurret")
	public Object getCurret() {
		
		CurrentG24 g24 = 
		currentG24Mapper.selectOne(s->s.limit(1)).orElse(null);
		
		HashMap<String,Object> r = new HashMap<String,Object>();
		r.put("g24", g24);
		
		return R.SUCCESS(r);
	}
	
	@Operation(summary = "获取历史游戏数据, G_24幸运飞艇,G_29极速时时彩,G_30极速赛车,G_31极速飞艇,G_35澳洲幸运5,G_37澳洲幸运10", 
			description = "test,目前只有G24")
	@PostMapping("/getHistory")
	public Object getHistory(IdPageVo parame) {
		Page p = new Page(parame.pageNum, parame.pageSize);
		
		if(Games.G_24.getCode().equals(parame.id)) {
			List<HistoryG24> list =
			historyG24Mapper.select(s->s
				.orderBy(HistoryG24DynamicSqlSupport.addTime.descending())
				.limit(p.getPageSize())
				.offset(p.limitStart())
			);
			return R.SUCCESS(list);
		}
		
		return R.FALSE("无法找到该游戏数据");
		
	}
	
//	@Operation(summary = "test login")
//	@PostMapping("/login")
//	public Object login() {
////		Idc9998AdminInfo admin =  idc9998Sevice.getByUserName("bk7897");
//		hgt8Component.getInstance("bk7897").login();
//		
//		return R.FALSE("");
//		
//	}
	
	@Operation(summary = "test lock")
	@PostMapping("/lock")
	public Object lock() throws Exception {
		Lock lock = jdbcLockRegistry.obtain("account-lock");
		if (lock.tryLock()) {
			System.out.println("拿到锁");
        } else {
        	System.out.println("拿不到锁");
        }
		return R.FALSE("");
		
	}
	
	
	
	

}
