package com.mq.mylibrary.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.mq.mylibrary.tool.LogTool;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;


/**
 * 创建人 mQ
 * 创建时间 2019/1/7 0007 18:27
 * 文件名称 BaseDao
 * 说明:
 **/
public abstract class BaseDao<T extends BaseEntity> {


    public abstract String getTableName();

    /**
     * 插入数据
     */
    public long insert(SQLiteDatabase db, ContentValues values) {
        long l = db.insert(getTableName(), null, values);
        LogTool.i(getTableName() + "中插入一条数据, 状态：" + l);
        return l;
    }

    public long insert(SQLiteDatabase db, T entity) {
        ContentValues values = new ContentValues();

        Map<String, Object> map = getKeyAndValue(entity);
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String name = entry.getKey();
            String value = (String) entry.getValue();
            values.put(name, value);
            LogTool.i("key=" + name + "  value=" + value);
        }
        return insert(db, values);
    }

    /**
     * 查出表内所有数据
     */
    public Cursor getAll(SQLiteDatabase db) {
        return db.rawQuery("SELECT * FROM " + getTableName(), null);
    }

    /**
     * 获取数据条数
     */
    public int getSize(SQLiteDatabase db) {
        int count;
        Cursor cursor = db.rawQuery("SELECT * FROM " + getTableName(), null);
        count = cursor.getCount();
        cursor.close();
        LogTool.i("getSize: " + count);
        return count;
    }

    /**
     * 清空表内所有数据
     */
    public void deleltAll(SQLiteDatabase db) {
        db.execSQL("delete from " + getTableName());
    }

    /**
     * 获取属性名数组
     */
    public String[] getFiledName(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        for (int i = 0; i < fields.length; i++) {
            System.out.println(fields[i].getType());
            fieldNames[i] = fields[i].getName();
        }
        return fieldNames;
    }

    /**
     * 根据属性名获取属性值
     */
    public Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(o, new Object[]{});
            return value;
        } catch (Exception e) {
            LogTool.i("获取属性值 " + fieldName + "：" + e.getMessage());
            return null;
        }
    }

    public static Map<String, Object> getKeyAndValue(Object obj) {
        Map<String, Object> map = new HashMap<String, Object>();
        // 得到类对象
        Class userCla = (Class) obj.getClass();
        /* 得到类中的所有属性集合 */
        Field[] fs = userCla.getDeclaredFields();
        for (int i = 0; i < fs.length; i++) {
            Field f = fs[i];
            f.setAccessible(true); // 设置些属性是可以访问的
            Object val = new Object();
            try {
                val = f.get(obj);
                // 得到此属性的值
                map.put(f.getName(), val);// 设置键值
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            /*
             * String type = f.getType().toString();//得到此属性的类型 if
             * (type.endsWith("String")) {
             * System.out.println(f.getType()+"\t是String"); f.set(obj,"12") ;
             * //给属性设值 }else if(type.endsWith("int") ||
             * type.endsWith("Integer")){
             * System.out.println(f.getType()+"\t是int"); f.set(obj,12) ; //给属性设值
             * }else{ System.out.println(f.getType()+"\t"); }
             */

        }
//        System.out.println("单个对象的所有键值==反射==" + map.toString());
        return map;
    }

}
