package com.bonait.bnframework.common.db.util;

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

import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_GOODPROPERTY;
import com.bonait.bnframework.common.db.mode.BPA_GOODS;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPE;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPENAME;
import com.bonait.bnframework.common.db.mode.BPA_GOODSTYPE;
import com.bonait.bnframework.common.db.mode.BPA_MATERIAL;
import com.bonait.bnframework.common.helper.Tools;

import java.util.List;
import java.util.Map;

/**
 * @author: liup
 * @description:
 * @date: 2024/5/24 16:20.
 */
public class ListAddUtil {
    /**
     * 批量新增
     * @param c
     * @param list
     * @return
     */
    public static boolean AddGOODSTYPEList(Class c, List<BPA_GOODSTYPE> list) {
        if(list.isEmpty()){
            return false;
        }
        QueryDB.lock.lock();
        boolean isSucess = false;
        SQLiteDatabase db = QueryDB.helper.getWritableDatabase();
        try {
            long insert=-1;
            db.beginTransaction();
            for(int i = 0; i < list.size(); i++){
                ContentValues cv = new ContentValues();

                Map<String, Object> map = Tools.getObjValue(list.get(i));
                if (map.get("id").toString().isEmpty())
                    return false;
                for (String key : map.keySet()) {
                    Object value = map.get(key);
                    if (value instanceof String) {
                        cv.put(key, (String) value);
                    } else if (value instanceof Integer) {
                        cv.put(key, ((Integer) value).intValue());
                    } else if (value instanceof Double) {
                        cv.put(key, ((Double) value).doubleValue());
                    } else if (value instanceof Float) {
                        cv.put(key, ((Float) value).floatValue());
                    } else if (value instanceof Long) {
                        cv.put(key, ((Long) value).longValue());
                    } else if (value instanceof Boolean) {
                        cv.put(key, ((Boolean) value).booleanValue());
                    }
                }
                insert = db.insertOrThrow(c.getSimpleName(), null, cv);
                if (insert == -1) {
                    throw new Exception("Failed to insert data at index " + i);
                }
            }

            db.setTransactionSuccessful();

            isSucess = insert > 0;
        } catch (Exception e) {
//            db.close();
            isSucess = false;
        } finally {
            db.endTransaction();
            db.close();
            QueryDB.lock.unlock();
        }
        return isSucess;
    }

    /**
     * 批量新增
     * @param c
     * @param list
     * @return
     */
    public static boolean AddMATERIALList(Class c, List<BPA_MATERIAL> list) {
        if(list.isEmpty()){
            return false;
        }
        QueryDB.lock.lock();
        boolean isSucess = false;
        SQLiteDatabase db = QueryDB.helper.getWritableDatabase();
        try {
            long insert=-1;
            db.beginTransaction();
            for(int i = 0; i < list.size(); i++){
                ContentValues cv = new ContentValues();

                Map<String, Object> map = Tools.getObjValue(list.get(i));
                if (map.get("id").toString().isEmpty())
                    return false;
                for (String key : map.keySet()) {
                    Object value = map.get(key);
                    if (value instanceof String) {
                        cv.put(key, (String) value);
                    } else if (value instanceof Integer) {
                        cv.put(key, ((Integer) value).intValue());
                    } else if (value instanceof Double) {
                        cv.put(key, ((Double) value).doubleValue());
                    } else if (value instanceof Float) {
                        cv.put(key, ((Float) value).floatValue());
                    } else if (value instanceof Long) {
                        cv.put(key, ((Long) value).longValue());
                    } else if (value instanceof Boolean) {
                        cv.put(key, ((Boolean) value).booleanValue());
                    }
                }
                insert = db.insertOrThrow(c.getSimpleName(), null, cv);
                if (insert == -1) {
                    throw new Exception("Failed to insert data at index " + i);
                }
            }

            db.setTransactionSuccessful();

            isSucess = insert > 0;
        } catch (Exception e) {
//            db.close();
            isSucess = false;
        } finally {
            db.endTransaction();
            db.close();
            QueryDB.lock.unlock();
        }
        return isSucess;
    }
    /**
     * 批量新增
     * @param c
     * @param list
     * @return
     */
    public static boolean AddGoodsPropertyList(Class c, List<BPA_GOODPROPERTY> list) {
        if(list.isEmpty()){
            return false;
        }
        QueryDB.lock.lock();
        boolean isSucess = false;
        SQLiteDatabase db = QueryDB.helper.getWritableDatabase();
        try {
            long insert=-1;
            db.beginTransaction();
            for(int i = 0; i < list.size(); i++){
                ContentValues cv = new ContentValues();

                Map<String, Object> map = Tools.getObjValue(list.get(i));
                if (map.get("id").toString().isEmpty())
                    return false;
                for (String key : map.keySet()) {
                    Object value = map.get(key);
                    if (value instanceof String) {
                        cv.put(key, (String) value);
                    } else if (value instanceof Integer) {
                        cv.put(key, ((Integer) value).intValue());
                    } else if (value instanceof Double) {
                        cv.put(key, ((Double) value).doubleValue());
                    } else if (value instanceof Float) {
                        cv.put(key, ((Float) value).floatValue());
                    } else if (value instanceof Long) {
                        cv.put(key, ((Long) value).longValue());
                    } else if (value instanceof Boolean) {
                        cv.put(key, ((Boolean) value).booleanValue());
                    }
                }
                insert = db.insertOrThrow(c.getSimpleName(), null, cv);
                if (insert == -1) {
                    throw new Exception("Failed to insert data at index " + i);
                }
            }

            db.setTransactionSuccessful();

            isSucess = insert > 0;
        } catch (Exception e) {
//            db.close();
            isSucess = false;
        } finally {
            db.endTransaction();
            db.close();
            QueryDB.lock.unlock();
        }
        return isSucess;
    }
    /**
     * 批量新增
     * @param c
     * @param list
     * @return
     */
    public static boolean AddGoodsList(Class c, List<BPA_GOODS> list) {
        if(list.isEmpty()){
            return false;
        }
        QueryDB.lock.lock();
        boolean isSucess = false;
        SQLiteDatabase db = QueryDB.helper.getWritableDatabase();
        try {
            long insert=-1;
            db.beginTransaction();
            for(int i = 0; i < list.size(); i++){
                ContentValues cv = new ContentValues();

                Map<String, Object> map = Tools.getObjValue(list.get(i));
                if (map.get("id").toString().isEmpty())
                    return false;
                for (String key : map.keySet()) {
                    Object value = map.get(key);
                    if (value instanceof String) {
                        cv.put(key, (String) value);
                    } else if (value instanceof Integer) {
                        cv.put(key, ((Integer) value).intValue());
                    } else if (value instanceof Double) {
                        cv.put(key, ((Double) value).doubleValue());
                    } else if (value instanceof Float) {
                        cv.put(key, ((Float) value).floatValue());
                    } else if (value instanceof Long) {
                        cv.put(key, ((Long) value).longValue());
                    } else if (value instanceof Boolean) {
                        cv.put(key, ((Boolean) value).booleanValue());
                    }
                }
                insert = db.insertOrThrow(c.getSimpleName(), null, cv);
                if (insert == -1) {
                    throw new Exception("Failed to insert data at index " + i);
                }
            }

            db.setTransactionSuccessful();

            isSucess = insert > 0;
        } catch (Exception e) {
//            db.close();
            isSucess = false;
        } finally {
            db.endTransaction();
            db.close();
            QueryDB.lock.unlock();
        }
        return isSucess;
    }
    /**
     * 批量新增
     * @param c
     * @param list
     * @return
     */
    public static boolean AddGoodsRecipeNameList(Class c, List<BPA_GOODSRECIPENAME> list) {
        if(list.isEmpty()){
            return false;
        }
        QueryDB.lock.lock();
        boolean isSucess = false;
        SQLiteDatabase db = QueryDB.helper.getWritableDatabase();
        try {
            long insert=-1;
            db.beginTransaction();
            for(int i = 0; i < list.size(); i++){
                ContentValues cv = new ContentValues();

                Map<String, Object> map = Tools.getObjValue(list.get(i));
                if (map.get("id").toString().isEmpty())
                    return false;
                for (String key : map.keySet()) {
                    Object value = map.get(key);
                    if (value instanceof String) {
                        cv.put(key, (String) value);
                    } else if (value instanceof Integer) {
                        cv.put(key, ((Integer) value).intValue());
                    } else if (value instanceof Double) {
                        cv.put(key, ((Double) value).doubleValue());
                    } else if (value instanceof Float) {
                        cv.put(key, ((Float) value).floatValue());
                    } else if (value instanceof Long) {
                        cv.put(key, ((Long) value).longValue());
                    } else if (value instanceof Boolean) {
                        cv.put(key, ((Boolean) value).booleanValue());
                    }
                }
                insert = db.insertOrThrow(c.getSimpleName(), null, cv);
                if (insert == -1) {
                    throw new Exception("Failed to insert data at index " + i);
                }
            }

            db.setTransactionSuccessful();

            isSucess = insert > 0;
        } catch (Exception e) {
//            db.close();
            isSucess = false;
        } finally {
            db.endTransaction();
            db.close();
            QueryDB.lock.unlock();
        }
        return isSucess;
    }
    /**
     * 批量新增
     * @param c
     * @param list
     * @return
     */
    public static boolean AddGoodsSrecipeList(Class c, List<BPA_GOODSRECIPE> list) {
        if(list.isEmpty()){
            return false;
        }
        QueryDB.lock.lock();
        boolean isSucess = false;
        SQLiteDatabase db = QueryDB.helper.getWritableDatabase();
        try {
            long insert=-1;
            db.beginTransaction();
            for(int i = 0; i < list.size(); i++){
                ContentValues cv = new ContentValues();

                Map<String, Object> map = Tools.getObjValue(list.get(i));
                if (map.get("id").toString().isEmpty())
                    return false;
                for (String key : map.keySet()) {
                    Object value = map.get(key);
                    if (value instanceof String) {
                        cv.put(key, (String) value);
                    } else if (value instanceof Integer) {
                        cv.put(key, ((Integer) value).intValue());
                    } else if (value instanceof Double) {
                        cv.put(key, ((Double) value).doubleValue());
                    } else if (value instanceof Float) {
                        cv.put(key, ((Float) value).floatValue());
                    } else if (value instanceof Long) {
                        cv.put(key, ((Long) value).longValue());
                    } else if (value instanceof Boolean) {
                        cv.put(key, ((Boolean) value).booleanValue());
                    }
                }
                insert = db.insertOrThrow(c.getSimpleName(), null, cv);
                if (insert == -1) {
                    throw new Exception("Failed to insert data at index " + i);
                }
            }

            db.setTransactionSuccessful();

            isSucess = insert > 0;
        } catch (Exception e) {
//            db.close();
            isSucess = false;
        } finally {
            db.endTransaction();
            db.close();
            QueryDB.lock.unlock();
        }
        return isSucess;
    }
}
