package com.sf.mapperTest;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sf.entity.Tea;
import com.sf.mapper.TeaMapper;
import com.sf.util.SqlSessionFactoryUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author WanJl
 * @version 1.0
 * @title TeaMapperTest
 * @description
 * @create 2025/10/26
 */
public class TeaMapperTest {

    //获取Sql会话
    SqlSession session = SqlSessionFactoryUtils.getSqlSession(true);
    TeaMapper teaMapper = session.getMapper(TeaMapper.class);

    @Test
    public void testSelectAll(){
        //调用查询所有的方法
        List<Tea> teaList = teaMapper.selectAll();
        //遍历结果
        for (Tea tea : teaList) {
            System.out.println(tea);
        }
    }

    @Test
    public void testInsert(){
        Tea tea = new Tea(null,"冰绿茶",1,100,"冰绿茶..",1);
        Integer result = teaMapper.insert(tea);
        System.out.println("新增了"+result+"条茶叶信息");
    }

    @Test
    public void testUpdate(){
        Tea tea = new Tea(24,"666冰绿茶",1,100,"冰绿茶..",1);
        Integer result = teaMapper.update(tea);
        System.out.println("修改了"+result+"条茶叶信息");
    }
    @Test
    public void testDelete(){
        Integer result = teaMapper.delete(24);
        System.out.println("删除了"+result+"条茶叶信息");
    }

    @Test
    public void testSelectById(){
        Tea tea = teaMapper.selectById(1);
        System.out.println(tea);
    }
    @Test
    public void testSelectByCondition(){
        Tea tea = new Tea();
        tea.setTeaName("金骏眉");
        List<Tea> teaList = teaMapper.selectByCondition(tea);
        System.out.println(teaList);
    }
    @Test
    public void testSelectByIds(){
        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(3);
        ids.add(5);
        ids.add(7);
        ids.add(8);
        ids.add(9);
        ids.add(10);

        List<Tea> teaList = teaMapper.selectByIds(ids);

        teaList.forEach(System.out::println);
    }

    @Test
    public void testSelectAllByPage(){
        //获取Sql会话
        SqlSession session = SqlSessionFactoryUtils.getSqlSession(true);
        TeaMapper teaMapper = session.getMapper(TeaMapper.class);
        //设置分页参数
        PageHelper.startPage(3,10);
        List<Tea> teaList = teaMapper.selectAll();
        for (Tea tea : teaList) {
            System.out.println(tea);
        }

        //PageHelper提供了一个专门用来存分页信息的对象PageInfo
        PageInfo<Tea> pageInfo = new PageInfo<>(teaList);
        System.out.println("当前页的页码："+pageInfo.getPageNum());
        System.out.println("每页的数量："+pageInfo.getPageSize());
        System.out.println("当前页的数量："+pageInfo.getSize());
        System.out.println("当前页面第一个元素在数据库中的行号："+pageInfo.getStartRow());
        System.out.println("当前页面最后一个元素在数据库中的行号："+pageInfo.getEndRow());
        System.out.println("总记录数："+pageInfo.getTotal());
        System.out.println("总页数："+pageInfo.getPages());
        System.out.println("上一页（页面链接使用）："+pageInfo.getPrePage());
        System.out.println("下一页（页面链接使用）："+pageInfo.getNextPage());
        System.out.println("是否为第一页："+pageInfo.isIsFirstPage());
        System.out.println("是否为最后一页："+pageInfo.isIsLastPage());
        System.out.println("是否有前一页："+pageInfo.isHasPreviousPage());
        System.out.println("是否有下一页："+pageInfo.isHasNextPage());
        System.out.println("导航页码数(就是总共有多少页)："+pageInfo.getNavigatePages());
        System.out.println("所有导航页号(就是总共有多少页),可以用来遍历："+ Arrays.toString(pageInfo.getNavigatepageNums()));
        System.out.println("导航条上的第一页："+pageInfo.getNavigateFirstPage());
        System.out.println("导航条上的最后一页："+pageInfo.getNavigateLastPage());
    }
}
