package edu.neusoft.project.model.service;

import edu.neusoft.project.model.dao.PersonDao;
import edu.neusoft.project.model.mapper.ListMapper;
import edu.neusoft.project.model.vo.Indent;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class IndentService {
    private ListMapper myList;
    @Autowired
    private PersonDao personDao;

    //    定义了mapper属性，使用前需要实例化
//    使用代码块实例化，代码块可以给属性赋初始值，代码块调用时机是第一次实例化的
    {
        try {
            InputStream stream= Resources.getResourceAsStream("mybatis.xml");
            SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(stream);
            SqlSession session=factory.openSession();
            myList=session.getMapper(ListMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println();
        }
    }


    public List<Indent> showMyIndentList(String personCode) {
        List<Indent> myIndentList = myList.showMyIndentList(personCode);
        if (myIndentList==null){
            Indent i = new Indent("isNull");
            myIndentList.add(i);

        }
        return myIndentList;
    }

    public int delThisIndent(String adminListPersonCode, String id) {
        int ret = 0;
        ret = personDao.delThisIndent(adminListPersonCode,id);
        return ret;
    }

//    public String indentInfo(String id) {
//        System.out.println(id);
//        String ret = myList.indentInfo(id);
//
//        System.out.println(ret+"--------indentInfoServ");
//        return ret;
//    }
}
