package edu.neusoft.project.model.service;

import edu.neusoft.project.model.dao.LikeSongDao;
import edu.neusoft.project.model.mapper.ListMapper;
import edu.neusoft.project.model.dao.SongDao;
import edu.neusoft.project.model.vo.Person;
import edu.neusoft.project.model.vo.Song;
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 SongService {
    @Autowired
    private SongDao songDao;
    @Autowired
    private LikeSongDao likeSongDao;

    private ListMapper myList;

    private SongDao newSongDao= new SongDao();
    //    定义了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();
        }

    }


    public Song selBySongName(String s) {
        Song song = songDao.selectByName(s);
        return  song;
    }

    public List<Song> likeSongs(){
        List<Song> list = likeSongDao.likeSongs();
        return list;

    }

    public Song selBySongId(String searchText) {
        Song song = songDao.selectById(searchText);
        return  song;
    }
    public Song selectByIdPrecise(String searchText) {
        Song song = songDao.selectByIdPrecise(searchText);
        return  song;
    }

    public void addListened(Person person) {
        songDao.addListened(person);
    }

    public List<Song> mySong(String personCode) {
        List<Song> mylist = myList.mySong(personCode);
        System.out.println("---------mySongService");
        return mylist;

    }

    public int likeAct(String id, String personCode) {
        int ret= songDao.likeAct(id,personCode);

        return ret;
    }

    public int unlikeAct(String id, String personCode) {
        int ret= songDao.unlikeAct(id,personCode);
        return ret;
    }

    public int insSong(int songid, String musicName, String musician, int commentCount) {
        int ret;
        ret =newSongDao.insSong(songid,musicName,musician,commentCount);
        return  ret;
    }
}
