import com.alibaba.fastjson.JSON;
import com.neusoft.smart.pos.ServiceOrgApplication;
import com.neusoft.smart.pos.dto.org.request.TableWithDishesRequest;
import com.neusoft.smart.pos.dto.printer.redis.PrinterBean;
import com.neusoft.smart.pos.dto.printer.redis.PrinterTaskBean;
import com.neusoft.smart.pos.framework.utils.DateUtils;
import com.neusoft.smart.pos.org.service.PrintService;
import com.neusoft.smart.pos.printer.service.PrintQueueHandleService;
import com.neusoft.smart.pos.printer.service.PrinterRedisService;
import com.neusoft.smart.pos.redis.bean.TablePrintBean;
import org.junit.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.SpringJUnit4ClassRunner;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by 姜涛 on 2017/3/22.
 */
//@RunWith(SpringJUnit4ClassRunner.class)
//@SpringBootTest(classes = {ServiceOrgApplication.class})
public class RedisTest {
    @Autowired
    private PrintService printService;
    
    @Autowired
    private PrintQueueHandleService printQueueHandleService;
    
    @Test
    public void getItemTest() throws InterruptedException {
//		for (int i = 0; i < 10; i++) {
//			List<TableWithDishesRequest.Menu> menus = new ArrayList<>();
//			for (int j = 100; j < 110; j++) {
//				TableWithDishesRequest.Menu menu = new TableWithDishesRequest.Menu();
//				menu.setName("菜" + i + "--" + j);
//				menu.setCount(1);
//				menus.add(menu);
//			}
//			printService.insertToQueue(i+"", menus);
//
//			if ((i - 1) % 3 == 0) {
//				List<TableWithDishesRequest.Menu> set = printService.popMenus(""+(i - 1));
//				set.forEach(item -> System.out.println(item.getName()));
//			}
//		}
        
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                List<TableWithDishesRequest.Menu> menus = new ArrayList<>();
                for (int j = 100; j < 110; j++) {
                    TableWithDishesRequest.Menu menu = new TableWithDishesRequest.Menu();
                    menu.setName(i + "菜" + j);
                    menu.setCount(1);
                    menus.add(menu);
                }
                printService.insertToQueue(305, menus);
                System.out.println("thread1存数据了");
            }
        });
        
        Thread thread2 = new Thread(() -> {
            while (true) {
                List<TablePrintBean> set = printService.popMenus(78);
                set.forEach(item -> System.out.println(item.getTableName()));
            }
        });
        thread1.start();
        thread2.start();
        
        Thread.sleep(100000000);
    }
    
    
    @Test
    public void createTest() {
        PrinterTaskBean printerTaskBean = new PrinterTaskBean();
        printerTaskBean.setOrderNumber(DateUtils.getTimeString());
        printerTaskBean.setOrderType("1");
        printerTaskBean.setPrinterJson("{}");
        printerTaskBean.setPrinterType("2");
        printerTaskBean.setPrinterId(2);
        printQueueHandleService.pushTask(true, printerTaskBean);
    }
    
    @Test
    public void peekTest() {
        LinkedList<PrinterTaskBean> list = printQueueHandleService.peekTasks(1, 10);
        System.out.println(JSON.toJSONString(list));
    }
    
    @Test
    public void removeTest() {
        printQueueHandleService.removeTask(1, 2);
    }
    
    @Autowired
    PrinterRedisService printerRedisService;
    
    @Test
    public void createPrinterRedisTest() {
        PrinterBean printerBean = new PrinterBean();
        printerBean.setId(1l);
        printerBean.setDeviceType("1");
        PrinterBean printerBean2 = new PrinterBean();
        printerBean2.setId(2l);
        printerBean2.setPrinterName("2");
        printerBean.getLinkedPrinters().add(printerBean2);
        PrinterBean printerBean3 = new PrinterBean();
        printerBean3.setId(3l);
        printerBean3.setPrinterName("3");
        printerBean.getLinkedPrinters().add(printerBean3);
        printerRedisService.insertOrUpdate(printerBean);
    }
}
