package com.sobot.common.login.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.sobot.common.login.model.ServiceFunctionVoModel;
import com.sobot.common.utils.SobotGlobalContext;

import java.util.List;

/**
 * 权限信息 操作类
 */
public class FunctionManager extends SobotCommonDao<ServiceFunctionVoModel> {
    private FunctionManager(Context context) {
        super(new SobotCommonDBHelper(context));
    }

    //volatile关键字确保多线程下正确处理
    private volatile static FunctionManager mInstance;

    public static FunctionManager getInstance() {
        //第一次判空
        if (mInstance == null) {
            //进入同步区域
            synchronized (FunctionManager.class) {
                //第二次判空
                if (mInstance == null) {
                    mInstance = new FunctionManager(SobotGlobalContext.getAppContext());
                }
            }
        }
        return mInstance;
    }

    @Override
    public String getTableName() {
        return SobotCommonDBHelper.TABLE_LOGIN_USER_FUN_INFO;
    }

    @Override
    public void unInit() {

    }

    @SuppressLint("Range")
    @Override
    public ServiceFunctionVoModel parseCursorToBean(Cursor cursor) {
        ServiceFunctionVoModel model = new ServiceFunctionVoModel();
        model.setId(cursor.getString(cursor.getColumnIndex("id")));
        model.setCheckedCode(cursor.getInt(cursor.getColumnIndex("checkedCode")));
        model.setCode(cursor.getString(cursor.getColumnIndex("code")));
        return model;
    }

    @Override
    public ContentValues getContentValues(ServiceFunctionVoModel functionVoModel) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("id", functionVoModel.getId());
        contentValues.put("code", functionVoModel.getCode());
        contentValues.put("checkedCode", functionVoModel.getCheckedCode());

        return contentValues;
    }

    public void setFunctionVos(List<ServiceFunctionVoModel> list) {
        deleteAll();
        insert(list);
    }

    /**
     * 获取权限信息类
     */
    public List<ServiceFunctionVoModel> getFunctionVos() {
        List<ServiceFunctionVoModel> list = query(null, null, null, null, null, null, null);
        return list;
    }
}
