package com.example.demo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.config.StemAPI;
import com.example.demo.dto.request.AddUserRequest;
import com.example.demo.dto.request.UserRequest;
import com.example.demo.dto.request.exam.AddExamRequest;
import com.example.demo.dto.request.exam.StemAndExamRequest;
import com.example.demo.dto.request.wrongbook.WrongBookRequest;
import com.example.demo.dto.response.feign.FeignResponse;
import com.example.demo.dto.response.StemDto;
import com.example.demo.dto.response.feign.GetStemsResponse;
import com.example.demo.entity.Paper;
import com.example.demo.entity.PaperItem;
import com.example.demo.entity.WrongBook;
import com.example.demo.mapper.PaperItemMapper;
import com.example.demo.mapper.PaperMapper;
import com.example.demo.service.ExamService;
import com.example.demo.service.FeignService;
import com.example.demo.service.UserService;
import com.example.demo.service.WrongBookService;
import feign.Feign;
import feign.jackson.JacksonDecoder;
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.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloWorldApplicationTests {
    @Autowired
    FeignService feignService;
    @Autowired
    PaperMapper paperMapper;
    @Autowired
    PaperItemMapper paperItemMapper;
    @Autowired
    ExamService examService;
    @Autowired
    WrongBookService wrongBookService;
    @Autowired
    UserService userService;

    @Test
    public void testFeign() {
        StemAPI stem = Feign.builder()
                            .decoder(new JacksonDecoder())
                            .target(StemAPI.class,"http://112.74.105.17:20003/question-bank");
        FeignResponse a = feignService.getResponse("755191767824859136");
        StemDto s =  a.getData();
        s.setAns();

        QueryWrapper<Paper> wrapper = new QueryWrapper<>();
        wrapper.eq("groupName","小学")
                .eq("gradeName","一年级")
                .eq("subject","数学");
        List<Paper> papers = paperMapper.selectList(wrapper);
        System.out.println(papers);

        QueryWrapper<PaperItem> wrapper1 = new QueryWrapper<>();
        wrapper.eq("pid","1");
        List<PaperItem> items = paperItemMapper.selectList(wrapper1);
        System.out.println(items);

    }

    @Test
    public void test(){
        AddExamRequest request = new AddExamRequest();
        request.setGroupName("小学");
        request.setGradeName("一年级");
        request.setSubject("数学");
        GetStemsResponse response = feignService.getStems(request);
        System.out.println(response.getData().getData().size());
        System.out.println(response.getData());
    }

    @Test
    public void testPage(){
        StemAndExamRequest request = new StemAndExamRequest();
        QueryWrapper<Paper> wrapper = new QueryWrapper();


        Page<Paper> page = new Page<>(3,2);

        IPage<Paper> paperIPage = paperMapper.selectPage(page, wrapper);

        //IPage<Map<String, Object>> mapIPage = paperMapper.selectMapsPage(page,wrapper);

        System.out.println("总页数"+paperIPage.getPages());
        System.out.println("总记录数"+paperIPage.getTotal());
        System.out.println(paperIPage.getSize());
        System.out.println(paperIPage.getCurrent());
        List<Paper> records = paperIPage.getRecords();
        records.forEach(System.out::println);

    }

    @Test
    public void testAdd(){
        AddExamRequest request = new AddExamRequest("sss","小学","一年级","数学",5);

        System.out.println(examService.addExam(request));

    }

    @Test
    public void testWrongBookItemsCount(){
        WrongBookRequest request = new WrongBookRequest(1);
        request.setGroupName("小学");
        request.setGradeName("一年级");
        request.setSubject("数学");
        System.out.println(wrongBookService.count(request));
    }

    @Test
    public void testUser(){
        UserRequest request = new UserRequest("xiaoming","123456");
        System.out.println(userService.login(request));
        AddUserRequest request1 = new AddUserRequest("xx");
        request1.setUsername("12323gs");
        request1.setPassword("eeee");
        System.out.println(userService.register(request1));
    }
}
