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.mapper.PersonMapper;
import edu.neusoft.project.model.vo.*;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class PersonService {
    @Autowired
    private PersonDao personDao;

    private ListMapper myList;

    private PersonMapper personMapper;

    //    定义了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 Person login(String usernsme,String token) {
//        调用dao层操作数据库，如果有该用户返回true，没有返回false
//        PersonDao dao=new PersonDao();
        Person per=personDao.selectByNameAndToken(usernsme,token);
        System.out.println("personService.ret--"+per);
        if(per!=null){
            return per;
        }
        return null;
    }
    public Person selUserByName(String username) {
        Person person = new Person();
        person=personDao.selectByName(username);
        return  person;
    }
    public Person selUserByAdminName(String username) {
        Person person = new Person();
        person=personDao.selectByAdminName(username);
        return person;
    }
    public Person adminLogin(String usernsme,String token) {
//        调用dao层操作数据库，如果有该用户返回true，没有返回false
//        PersonDao dao=new PersonDao();
        Person per=personDao.selectByNameAndTokenAdmin(usernsme, token);
        if(per!=null){
            return per;
        }
        return null;
    }
    
    public String getLevel(Person globalPerson){
        String level = "1";
        if (globalPerson.getPersonListened()>=0&&globalPerson.getPersonListened()<=100){
            level = "1";
        }else if (globalPerson.getPersonListened()>100&&globalPerson.getPersonListened()<=800){
            level = "2";
        }else if (globalPerson.getPersonListened()>800&&globalPerson.getPersonListened()<=1600){
            level = "3";
        }else if (globalPerson.getPersonListened()>1600&&globalPerson.getPersonListened()<=2400){
            level = "4";
        }else if (globalPerson.getPersonListened()>2400&&globalPerson.getPersonListened()<=3200){
            level = "5";
        }
        return level;
    }

    public void regist(Person person) {
        personDao.insert(person);
    }

    public boolean selUser(String username) {
        boolean flag;
        String returnUsername=personMapper.selUser(username);
        if (!returnUsername.equals(username)){
            flag = false;
        }else {
            flag= true;
        }
        return flag;
    }

    public int buySong(String id, String personCode) {

        int ret = personDao.buySong(id,personCode);

        return ret;

    }

    public List<Indent> showAllAdminIndent() {
        List<Indent> indents = myList.showAllAdminIndents();

        return indents;
    }

    public List<Person> showAllAdminUser() {
        List<Person> users = myList.showAllAdminUsers();
        return users;
    }

    public void insAckTime(String time) {
        int t = Integer.parseInt(time);
        int ret = personDao.insAckTime(t);
        System.out.println(t);
        System.out.println(ret);

    }

    public List<AckTime> getAckTime() {
        List<AckTime> list = personDao.getAckTime();
        System.out.println("AckList--"+list);
        return list;
    }

    public void insIpAdd(String cip) {
        String getIP= "";

        getIP=personDao.getIpAdd(cip);
        if (getIP.equals("")){
            personDao.insIpAdd(cip);
        }else if(!getIP.equals("")){
            personDao.updIpAdd(cip);
        }else {
            System.out.println("这个IP更改的有点问题，详细见PersonService.insIpAdd()");
        }


    }

    public void insProvince(String cid, String cname) {
        String getProvince = "";
        getProvince= personDao.getProvince(cid,cname);
        System.out.println("查询到："+getProvince);
        if (getProvince.equals("")){
            personDao.insProvince(cid,cname);
        }else if (!getProvince.equals("")){
            personDao.updProvince(cid,cname);
        }else {
            System.out.println("这个Province更改的有点问题，详细见PersonService.insProvince()");
        }

    }

    public void insLineCount() {
        personDao.insLineCount();
    }

    public void remLineCount() {
        personDao.remLineCount();
    }

    public void resetLineCount() {
        personDao.resetLineCount();
    }

    public int readLineCount() {
        return personDao.readLineCount();
    }

    public String getUserSession(String uid) {
        String str= "";
        str=personDao.getUserSession(uid);
        return str;
    }

    public void resetUserSession(String uid) {
        personDao.resetUserSession(uid);


    }

    public void updUserSession(Person loginedPer) {
        personDao.updUserSession(loginedPer);

    }

    public void updUserToken(String personCode, String userToken) {
        personDao.updUserToken(personCode,userToken);
    }

    public String getUserToken(String personCode) {
        String str="";
            str=personDao.getUserTooken(personCode);
        return str;
    }

    public String checkRegistUser(String username) {

        String ret = personDao.checkRegistUser(username);
        return ret;
    }

    public int getTradingCount() {
        int ret = personDao.getTradingCount();
        return  ret;
    }

    public void insProvinceAdd(String cname) {
        String getLikeProvince = "";

        getLikeProvince= personDao.getLikeProvinceAdd(cname);
        System.out.println("查询到："+getLikeProvince+"/"+cname);
        if (getLikeProvince.equals("")){
            personDao.insLikeProvinceAdd(getLikeProvince);
        }else if (!getLikeProvince.equals("")){
            personDao.updLikeProvinceAdd(getLikeProvince);
        }else {
            System.out.println("这个Province更改的有点问题，详细见PersonService.insProvince()");
        }

    }

    public List<ProvinceAdd> getProvinceAddEcharts() {
        List<ProvinceAdd> list = personDao.getProvinceAddEcharts();


        return list;
    }

    public int updPersonPict(String realUrl, String fileName) {
        String doesExsitPivt = "";
        PersonDao persondao = new PersonDao();
        int ret = 1;
//        fileName=fileName.substring(0,13);
        doesExsitPivt= persondao.getPict(fileName);
        System.out.println("查询到PersonPict----upload/DB:::"+realUrl+"/"+doesExsitPivt);
        if ("".equals(doesExsitPivt)){
            persondao.insPersonPict(realUrl);
        }else if (!"".equals(doesExsitPivt)){
            if (!fileName.equals(doesExsitPivt)){
                persondao.updPersonPict(realUrl, doesExsitPivt);
            }
        }else {
            ret=0;
            System.out.println("这个PersonPict更改的有点问题，详细见PersonService.updPersonPict()");
        }
        return ret;
    }


//    public List<Person> showAll() {
//        return personDao.selectAll();
//    }
//
//    public void deleteByName(String username) {
//        personDao.delete(username);
//    }
//
//    public void updateByName(String username, String Nusername) {
//        personDao.update(username,Nusername);
//    }


}
