package com.landaiqing.service;

import com.landaiqing.entity.AdminUserEntity;
import com.landaiqing.mapper.adminMapper;
import org.apache.ibatis.annotations.Param;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class AdminUserService {
    private adminMapper adminMapper;
    private SqlSession sqlSession;
   public AdminUserService()throws IOException {
       // 通过无参构造方法 初始化mybatis 得到flightMapper
       // mybatis-config.xml 目录位置
       String resource = "mybatis-config.xml";
       // 1.解析mybatis-config.xml 得到数据库相关的配置信息
       InputStream inputStream = Resources.getResourceAsStream(resource);
       //2.创建得到一个sqlSessionFactory
       SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
       //3.获取到sqlSession
       SqlSession sqlSession = sqlSessionFactory.openSession(true);
       adminMapper = sqlSession.getMapper(adminMapper.class);
//        sqlSession.close();
   }
    public List<AdminUserEntity> login(String adminUserName,String adminPassword){
       return adminMapper.login(adminUserName,adminPassword);
    }
    public List<AdminUserEntity> selectAllAdmin(){
       return adminMapper.selectAllAdmin();
    }

    public int OpenAdmin(Integer adminId){
        int openAdmin = adminMapper.OpenAdmin(adminId);
//        sqlSession.commit();
        return openAdmin;
    }
    public int CloseAdmin( Integer adminId){
        int closeAdmin = adminMapper.CloseAdmin(adminId);
//        sqlSession.commit();
        return closeAdmin;
    }
    public AdminUserEntity getAdminByID(@Param("adminId")Integer adminId){
       return adminMapper.getAdminByID(adminId);
    }
    public int updateAdmin(AdminUserEntity adminUserEntity){
        int updateAdmin = adminMapper.updateAdmin(adminUserEntity);
//        sqlSession.commit();
        return updateAdmin;
    }
    public int insertAdmin(AdminUserEntity adminUserEntity){
        int insertAdmin = adminMapper.insertAdmin(adminUserEntity);
//        sqlSession.commit();
        return insertAdmin;
    }
    public int deleteAdmin(Integer adminId){
        int deleteAdmin = adminMapper.deleteAdmin(adminId);
//        sqlSession.commit();
        return deleteAdmin;
    }



}
