AccountManager.java 6.33 KB
package com.sw.laryngoscope.manager;

import static com.sw.laryngoscope.common.InitParam.USER_TYPE_ADMIN;
import static com.sw.laryngoscope.common.InitParam.USER_TYPE_GUEST;
import static com.sw.laryngoscope.common.InitParam.USER_TYPE_USER;
import static com.sw.laryngoscope.common.InitParam.name_guest;

import com.sw.laryngoscope.db.AccountInfoDB;
import com.sw.laryngoscope.utils.Logger;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Description 用来保存用户在各个模式运动前设置的信息
 * @Author
 * @Time 2019/09/09
 */
public class AccountManager {

    public List<AccountInfoDB> accountInfolist = new ArrayList<>();

    private static AccountManager instance;

    public static String admin_name = "Administrator";
    public static int admin_name_id = 1;

    public static AccountManager getInstance() {
        if (instance == null) {
            synchronized (AccountManager.class) {
                if (instance == null) {
                    instance = new AccountManager();
                }
            }
        }
        return instance;
    }

    public void initList() {
        accountInfolist = LitePal.findAll(AccountInfoDB.class);
        if ( accountInfolist.size() == 0 ) {
            new AccountInfoDB(admin_name, "00000000", USER_TYPE_ADMIN).save();
            //new AccountInfoDB("sdsd", "00000000", USER_TYPE_USER).save();
        }
        clearAllLogin();
        clearAllSel();
    }

    public String findUserPsd( String name ) {
        List<AccountInfoDB> list = LitePal.where("name = ?", name).find( AccountInfoDB.class);
        return list.get(0).getPassword();
    }

    public AccountInfoDB findUserData( String name ) {
        AccountInfoDB mDb = null;
        List<AccountInfoDB> list = LitePal.where("name = ?", name).find( AccountInfoDB.class);
        if ( list.size() > 0 ) {
            mDb = list.get(0);
        }
        return mDb;
    }

    public List<AccountInfoDB> findAll() {
        accountInfolist = LitePal.findAll(AccountInfoDB.class);
        return accountInfolist;
    }

    public void deleteUser( String user ) {
        Iterator<AccountInfoDB> iterator = accountInfolist.iterator();
        while ( iterator.hasNext() ) {
            AccountInfoDB deleteInfo = iterator.next();
            if ( deleteInfo.getName().equals(user) ) {
                iterator.remove();//使用迭代器的删除方法删除
                deleteInfo.delete();
                Logger.d("===deleteFile==1=");
                break;
            }
        }
    }

    public void clearAllLogin() {
        Iterator<AccountInfoDB> iterator = accountInfolist.iterator();
        while ( iterator.hasNext() ) {
            AccountInfoDB deleteInfo = iterator.next();
            if ( deleteInfo.isLogin() ) {
                deleteInfo.setLogin(false);
                deleteInfo.saveOrUpdate("name = ?", deleteInfo.getName());
                Logger.d("===clearAllLogin==1=");
            }
        }
    }

    public void clearAllSel() {
        Iterator<AccountInfoDB> iterator = accountInfolist.iterator();
        while ( iterator.hasNext() ) {
            AccountInfoDB deleteInfo = iterator.next();
            if ( deleteInfo.getIsSelect() == 1 ) {
                deleteInfo.setIsSelect(0);
                deleteInfo.saveOrUpdate("name = ?", deleteInfo.getName());
                Logger.d("===clearAllSel==1=");
            }
        }
        /*AccountInfoDB tmp = accountInfolist.get(accountInfolist.size() - 1);
        tmp.setIsSelect(1);
        tmp.saveOrUpdate("name = ?", tmp.getName());*/
    }

    public boolean isUserDbOrNoLogin() {
        accountInfolist = LitePal.findAll(AccountInfoDB.class);
        boolean isLogin = false;
        Iterator<AccountInfoDB> iterator = accountInfolist.iterator();
        while ( iterator.hasNext() ) {
            AccountInfoDB info = iterator.next();
            isLogin = info.isLogin() || isLogin;
            if ( info.isLogin() && info.getUserType() == USER_TYPE_USER ) {
                return true;
            }
        }
        if ( isLogin ) {
            return false;
        } else {
            return true;
        }
    }

    public AccountInfoDB getLoginAcc() {
        accountInfolist = LitePal.findAll(AccountInfoDB.class);
        AccountInfoDB ret = new AccountInfoDB(-1000, name_guest, "", USER_TYPE_GUEST, 0, true);
        Iterator<AccountInfoDB> iterator = accountInfolist.iterator();
        while ( iterator.hasNext() ) {
            AccountInfoDB info = iterator.next();
            if ( info.isLogin() ) {
                return info;
            }
        }
        return ret;
    }

    public boolean isAdmin( AccountInfoDB tmp ) {
        if ( tmp.getUserType() == USER_TYPE_ADMIN ) {
            return true;
        }
        return false;
    }
    public boolean isAdmin() {
        accountInfolist = LitePal.findAll(AccountInfoDB.class);
        Iterator<AccountInfoDB> iterator = accountInfolist.iterator();
        while ( iterator.hasNext() ) {
            AccountInfoDB info = iterator.next();
            Logger.d("++++++++++++isAdmin+++++++" + info.toString());
            if ( info.isLogin() && info.getUserType() == USER_TYPE_ADMIN ) {
                return true;
            }
        }
        return false;
    }
    public boolean isAdvance( AccountInfoDB tmp ) {
        if ( tmp.getUserType() == USER_TYPE_USER ) {
            return true;
        }
        return false;
    }
    public boolean isUser( AccountInfoDB tmp ) {
        if ( tmp.getUserType() == USER_TYPE_USER ) {
            return true;
        }
        return false;
    }

    public boolean isLogin() {
        accountInfolist = LitePal.findAll(AccountInfoDB.class);
        Iterator<AccountInfoDB> iterator = accountInfolist.iterator();
        while ( iterator.hasNext() ) {
            AccountInfoDB info = iterator.next();
            if ( info.isLogin() ) {
                return true;
            }
        }
        return false;
    }

    public int getListLen() {
        accountInfolist = LitePal.findAll(AccountInfoDB.class);
        if ( accountInfolist != null ) {
            return accountInfolist.size();
        }
        return 0;
    }

}