package com.example.bpa.db;

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

import com.example.bpa.config.ConfigName;
import com.example.bpa.db.file.DBHelper;
import com.example.bpa.db.mode.BPA_ALERTLOG;
import com.example.bpa.db.mode.BPA_GOODS;
import com.example.bpa.db.mode.BPA_GOODSRECIPE;
import com.example.bpa.db.mode.BPA_LOG;
import com.example.bpa.db.mode.BPA_MATERIAL;
import com.example.bpa.db.mode.BPA_MENU;
import com.example.bpa.db.mode.BPA_MENUANDUSER;
import com.example.bpa.db.mode.BPA_ORDER;
import com.example.bpa.db.mode.BPA_ORDERLOG;
import com.example.bpa.db.mode.BPA_ORDERLOGDESC;
import com.example.bpa.db.mode.BPA_SILOS;
import com.example.bpa.db.mode.BPA_SILOSANDMATERIAL;
import com.example.bpa.db.mode.BPA_SUBORDER;
import com.example.bpa.db.mode.BPA_SYSTEMSET;
import com.example.bpa.db.mode.BPA_USER;
import com.example.bpa.db.mode.ModeBase;
import com.example.bpa.helper.Tools;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 数据库数据访问类库
 * add fengyoufu 20230413
 */
public class QueryDB {

    //region 成员变量
    /**
     * 数据库链接
     */
    private static DBHelper helper=DBHelper.getInstance(ConfigName.getInstance().dishesCon);

    /**
     * 时间倒序
     */
    public static String Desc_Time_Down="createTime desc";
    /**
     * 时间正序
     */
    public static String Desc_Time_Up="createTime";
    /**
     * 排序、优先顺序
     */
    public static String Desc_Sort_Up="Sort";
    //endregion

    //region BPA_MATERIAL 物料表
    /**
     * 新增物料数据
     * add fengyoufu 20230413
     * @param data 物料数据
     * @return 是否成功
     */
    public static boolean AddMaterial(BPA_MATERIAL data)
    {
        return Add(BPA_MATERIAL.class,data);
    }
    /**
     * 修改物料数据
     * add fengyoufu 20230413
     * @param data
     */
    public static void UpdateMaterial(BPA_MATERIAL data)
    {
        Update(BPA_MATERIAL.class,data);
    }
    /**
     * 删除物料数据
     * add fengyoufu 20230413
     * @param data 物料数据
     * @return 是否成功
     */
    public static boolean DeleteMaterial(BPA_MATERIAL data)
    {
        return Delete(BPA_MATERIAL.class,data.id);
    }
    /**
     * 获取所有物料信息
     * @return
     */
    public static ArrayList<BPA_MATERIAL> GetMaterialALL()
    {
        String orderby=Desc_Time_Up;//出料顺序
        String where="isDelete=?";
        String[] args=new String[] { "0" };
        ArrayList<BPA_MATERIAL> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_MATERIAL.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_MATERIAL)k);
        }
        return data;
    }

    /**
     * 判断物料数据是否存在
     * @param name
     * @return
     */
    public static boolean GetMaterialIs(String name)
    {
        boolean isSucess=false;
        String orderby=Desc_Time_Up;//出料顺序
        String where="isDelete=? and name=?";
        String[] args=new String[] { "0" ,name};
        ArrayList<BPA_MATERIAL> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_MATERIAL.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_MATERIAL)k);
        }
        return data.size()>0;
    }
    //endregion

    //region BPA_SILOS 料仓管理表
    /**
     * 新增料仓管理
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean AddSilos(BPA_SILOS data)
    {
        return Add(BPA_SILOS.class,data);
    }
    /**
     * 修改料仓管理
     * add fengyoufu 20230413
     * @param data
     */
    public static void UpdateSilos(BPA_SILOS data)
    {
        Update(BPA_SILOS.class,data);
    }
    /**
     * 删除料仓管理
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean DeleteSilos(BPA_SILOS data)
    {
        return Delete(BPA_SILOS.class,data.id);
    }
    /**
     * 获取所有料仓管理
     * @return
     */
    public static ArrayList<BPA_SILOS> GetSilosALL()
    {
        String orderby=Desc_Sort_Up+','+Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=?";
        String[] args=new String[] { "0" };
        ArrayList<BPA_SILOS> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_SILOS.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_SILOS)k);
        }
        return data;
    }
    //endregion

    //region BPA_SILOSANDMATERIAL 料仓物料关联表
    /**
     * 新增料仓物料关联
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean AddSilosAndMaterial(BPA_SILOSANDMATERIAL data)
    {
        return Add(BPA_SILOSANDMATERIAL.class,data);
    }
    /**
     * 修改料仓物料关联
     * add fengyoufu 20230413
     * @param data
     */
    public static void UpdateSilosAndMaterial(BPA_SILOSANDMATERIAL data)
    {
        Update(BPA_SILOSANDMATERIAL.class,data);
    }
    /**
     * 删除料仓物料关联
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean DeleteSilosAndMaterial(BPA_SILOSANDMATERIAL data)
    {
        return Delete(BPA_SILOSANDMATERIAL.class,data.id);
    }
    /**
     * 获取所有料仓物料关联
     * @return
     */
    public static ArrayList<BPA_SILOSANDMATERIAL> GetSilosAndMaterialALL()
    {
        String orderby=Desc_Sort_Up+','+Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=?";
        String[] args=new String[] { "0" };
        ArrayList<BPA_SILOSANDMATERIAL> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_SILOSANDMATERIAL.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_SILOSANDMATERIAL)k);
        }
        return data;
    }
    /**
     * 根据料仓id查询物料
     * @param id
     * @return
     */
    public static ArrayList<String> GetMaterialList(String id)
    {
        String orderby=Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=? and silosID=?";
        String[] args=new String[] { "0",id };
        ArrayList<String> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_SILOSANDMATERIAL.class,where,args,orderby);
        for (Object k:obj) {
            data.add(((BPA_SILOSANDMATERIAL)k).materialID);
        }
        return data;
    }
    //endregion

    //region BPA_GOODS 商品表
    /**
     * 新增商品表
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean AddGoods(BPA_GOODS data)
    {
        return Add(BPA_GOODS.class,data);
    }
    /**
     * 修改商品
     * add fengyoufu 20230413
     * @param data
     */
    public static void UpdateGoods(BPA_GOODS data)
    {
        Update(BPA_GOODS.class,data);
    }
    /**
     * 删除商品
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean DeleteGoods(BPA_GOODS data)
    {
        return Delete(BPA_GOODS.class,data.id);
    }
    /**
     * 获取所有商品
     * @return
     */
    public static ArrayList<BPA_GOODS> GetGoodsALL()
    {
        String orderby=Desc_Sort_Up+','+Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=?";
        String[] args=new String[] { "0" };
        ArrayList<BPA_GOODS> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_GOODS.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_GOODS)k);
        }
        return data;
    }
    //endregion

    //region BPA_GOODSRECIPE 商品配方明细表
    /**
     * 新增商品配方明细
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean AddGoodsSrecipe(BPA_GOODSRECIPE data)
    {
        return Add(BPA_GOODSRECIPE.class,data);
    }
    /**
     * 修改商品配方明细
     * add fengyoufu 20230413
     * @param data
     */
    public static void UpdateGoodsSrecipe(BPA_GOODSRECIPE data)
    {
        Update(BPA_GOODSRECIPE.class,data);
    }
    /**
     * 删除商品配方明细
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean DeleteGoodsSrecipe(BPA_GOODSRECIPE data)
    {
        return Delete(BPA_GOODSRECIPE.class,data.id);
    }
    /**
     * 获取所有商品配方明细
     * @return
     */
    public static ArrayList<BPA_GOODSRECIPE> GetGoodsSrecipeALL()
    {
        String orderby=Desc_Sort_Up+','+Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=?";
        String[] args=new String[] { "0" };
        ArrayList<BPA_GOODSRECIPE> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_GOODSRECIPE.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_GOODSRECIPE)k);
        }
        return data;
    }

    /**
     * 根据商品id查询配方信息
     * @param id
     * @return
     */
    public static ArrayList<BPA_GOODSRECIPE> GetGoodsSrecipeList(String id)
    {
        String orderby=Desc_Sort_Up+','+Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=? and goodsID=?";
        String[] args=new String[] { "0",id };
        ArrayList<BPA_GOODSRECIPE> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_GOODSRECIPE.class,where,args,orderby);
        for (Object k:obj) {
            data.add(((BPA_GOODSRECIPE)k));
        }
        return data;
    }
    //endregion

    //region BPA_ORDER 订单表
    /**
     * 新增订单
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean AddOrder(BPA_ORDER data)
    {
        return Add(BPA_ORDER.class,data);
    }
    /**
     * 修改订单
     * add fengyoufu 20230413
     * @param data
     */
    public static void UpdateOrder(BPA_ORDER data)
    {
        Update(BPA_ORDER.class,data);
    }
    /**
     * 删除订单
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean DeleteOrder(BPA_ORDER data)
    {
        return Delete(BPA_ORDER.class,data.id);
    }
    /**
     * 获取所有订单
     * @return
     */
    public static ArrayList<BPA_ORDER> GetOrderALL()
    {
        String orderby=Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=?";
        String[] args=new String[] { "0" };
        ArrayList<BPA_ORDER> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_ORDER.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_ORDER)k);
        }
        return data;
    }
    //endregion

    //region BPA_SUBORDER 订单子表
    /**
     * 新增订单子表
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean AddSubOrder(BPA_SUBORDER data)
    {
        return Add(BPA_SUBORDER.class,data);
    }
    /**
     * 修改订单子表
     * add fengyoufu 20230413
     * @param data
     */
    public static void UpdateSubOrder(BPA_SUBORDER data)
    {
        Update(BPA_SUBORDER.class,data);
    }
    /**
     * 删除订单子表
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean DeleteSubOrder(BPA_SUBORDER data)
    {
        return Delete(BPA_SUBORDER.class,data.id);
    }
    /**
     * 获取所有订单子表
     * @return
     */
    public static ArrayList<BPA_SUBORDER> GetSubOrderALL()
    {
        String orderby=Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=?";
        String[] args=new String[] { "0" };
        ArrayList<BPA_SUBORDER> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_SUBORDER.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_SUBORDER)k);
        }
        return data;
    }
    /**
     * 根据主订单id查询子订单列表
     * @param id
     * @return
     */
    public static ArrayList<BPA_SUBORDER> GetSubOrderList(String id)
    {
        String orderby=Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=? and orderID=?";
        String[] args=new String[] { "0",id };
        ArrayList<BPA_SUBORDER> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_SUBORDER.class,where,args,orderby);
        for (Object k:obj) {
            data.add(((BPA_SUBORDER)k));
        }
        return data;
    }
    //endregion

    //region BPA_ALERTLOG 预警日志表
    /**
     * 新增预警日志
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean AddAlertlog(BPA_ALERTLOG data)
    {
        return Add(BPA_ALERTLOG.class,data);
    }
    /**
     * 修改预警日志
     * add fengyoufu 20230413
     * @param data
     */
    public static void UpdateAlertlog(BPA_ALERTLOG data)
    {
        Update(BPA_ALERTLOG.class,data);
    }
    /**
     * 删除预警日志
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean DeleteAlertlog(BPA_ALERTLOG data)
    {
        return Delete(BPA_ALERTLOG.class,data.id);
    }
    /**
     * 获取所有预警日志
     * @return
     */
    public static ArrayList<BPA_ALERTLOG> GetAlertlogALL()
    {
        String orderby=Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=?";
        String[] args=new String[] { "0" };
        ArrayList<BPA_ALERTLOG> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_ALERTLOG.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_ALERTLOG)k);
        }
        return data;
    }
    /**
     * 获取所有今日预警日志
     * @return
     */
    public static ArrayList<BPA_ALERTLOG> GetAlertlogTodan()
    {
        String orderby=Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=? and createTime>?";
        String[] args=new String[] { "0",new SimpleDateFormat("yyyy-MM-dd").format(new Date())+" 00:00:00" };
        ArrayList<BPA_ALERTLOG> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_ALERTLOG.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_ALERTLOG)k);
        }
        return data;
    }
    //endregion

    //region BPA_LOG 日志表
    /**
     * 新增日志
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean Addlog(BPA_LOG data)
    {
        return Add(BPA_LOG.class,data);
    }
    /**
     * 修改日志
     * add fengyoufu 20230413
     * @param data
     */
    public static void Updatelog(BPA_LOG data)
    {
        Update(BPA_LOG.class,data);
    }
    /**
     * 删除日志
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean Deletelog(BPA_LOG data)
    {
        return Delete(BPA_LOG.class,data.id);
    }
    /**
     * 获取日志
     * @return
     */
    public static ArrayList<BPA_LOG> GetlogALL()
    {
        String orderby=Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=?";
        String[] args=new String[] { "0" };
        ArrayList<BPA_LOG> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_LOG.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_LOG)k);
        }
        return data;
    }
    /**
     * 获取所有今日日志
     * @return
     */
    public static ArrayList<BPA_LOG> GetlogTodan()
    {
        String orderby=Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=? and createTime>?";
        String[] args=new String[] { "0",new SimpleDateFormat("yyyy-MM-dd").format(new Date())+" 00:00:00" };
        ArrayList<BPA_LOG> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_LOG.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_LOG)k);
        }
        return data;
    }
    //endregion

    //region BPA_ORDERLOG 订单制作记录表
    /**
     * 新增订单制作记录
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean AddOrderlog(BPA_ORDERLOG data)
    {
        return Add(BPA_ORDERLOG.class,data);
    }
    /**
     * 修改订单制作记录
     * add fengyoufu 20230413
     * @param data
     */
    public static void UpdateOrderlog(BPA_ORDERLOG data)
    {
        Update(BPA_ORDERLOG.class,data);
    }
    /**
     * 删除订单制作记录
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean DeleteOrderlog(BPA_ORDERLOG data)
    {
        return Delete(BPA_ORDERLOG.class,data.id);
    }
    /**
     * 获取所有订单制作记录
     * @return
     */
    public static ArrayList<BPA_ORDERLOG> GetOrderlogALL()
    {
        String orderby=Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=?";
        String[] args=new String[] { "0" };
        ArrayList<BPA_ORDERLOG> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_ORDERLOG.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_ORDERLOG)k);
        }
        return data;
    }
    /**
     * 根据子订单id查询子订单制作记录
     * @param id
     * @return
     */
    public static ArrayList<BPA_ORDERLOG> GetOrderlogList(String id)
    {
        String orderby=Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=? and suborderID=?";
        String[] args=new String[] { "0",id };
        ArrayList<BPA_ORDERLOG> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_ORDERLOG.class,where,args,orderby);
        for (Object k:obj) {
            data.add(((BPA_ORDERLOG)k));
        }
        return data;
    }
    //endregion

    //region BPA_ORDERLOGDESC 订单制作记录描述表
    /**
     * 新增订单制作记录描述
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean AddOrderlogdesc(BPA_ORDERLOGDESC data)
    {
        return Add(BPA_ORDERLOGDESC.class,data);
    }
    /**
     * 修改订单制作记录描述
     * add fengyoufu 20230413
     * @param data
     */
    public static void UpdateOrderlogdesc(BPA_ORDERLOGDESC data)
    {
        Update(BPA_ORDERLOGDESC.class,data);
    }
    /**
     * 删除订单制作记录描述
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean DeleteOrderlogdesc(BPA_ORDERLOGDESC data)
    {
        return Delete(BPA_ORDERLOGDESC.class,data.id);
    }
    /**
     * 获取订单制作记录描述
     * @return
     */
    public static ArrayList<BPA_ORDERLOGDESC> GetOrderlogdescALL()
    {
        String orderby=Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=?";
        String[] args=new String[] { "0" };
        ArrayList<BPA_ORDERLOGDESC> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_ORDERLOGDESC.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_ORDERLOGDESC)k);
        }
        return data;
    }
    /**
     * 根据子订单id查询子订单订单制作记录描述
     * @param id
     * @return
     */
    public static ArrayList<BPA_ORDERLOGDESC> GetOrderlogdescList(String id)
    {
        String orderby=Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=? and suborderID=?";
        String[] args=new String[] { "0",id };
        ArrayList<BPA_ORDERLOGDESC> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_ORDERLOGDESC.class,where,args,orderby);
        for (Object k:obj) {
            data.add(((BPA_ORDERLOGDESC)k));
        }
        return data;
    }
    //endregion

    //region BPA_SYSTEMSET 系统设置表
    /**
     * 新增系统设置
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean AddSystemset(BPA_SYSTEMSET data)
    {
        return Add(BPA_SYSTEMSET.class,data);
    }
    /**
     * 修改系统设置
     * add fengyoufu 20230413
     * @param data
     */
    public static void UpdateSystemset(BPA_SYSTEMSET data)
    {
        Update(BPA_SYSTEMSET.class,data);
    }
    /**
     * 删除系统设置
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean DeleteSystemset(BPA_SYSTEMSET data)
    {
        return Delete(BPA_SYSTEMSET.class,data.id);
    }
    /**
     * 获取系统设置
     * @return
     */
    public static ArrayList<BPA_SYSTEMSET> GetSystemsetALL()
    {
        String orderby=Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=?";
        String[] args=new String[] { "0" };
        ArrayList<BPA_SYSTEMSET> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_SYSTEMSET.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_SYSTEMSET)k);
        }
        return data;
    }
    //endregion

    //region BPA_USER 账户表
    /**
     * 新增账户
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean AddUser(BPA_USER data)
    {
        return Add(BPA_USER.class,data);
    }
    /**
     * 修改账户
     * add fengyoufu 20230413
     * @param data
     */
    public static void UpdateUser(BPA_USER data)
    {
        Update(BPA_USER.class,data);
    }
    /**
     * 删除账户
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean DeleteUser(BPA_USER data)
    {
        return Delete(BPA_USER.class,data.id);
    }
    /**
     * 获取账户
     * @return
     */
    public static ArrayList<BPA_USER> GetUserALL()
    {
        String orderby=Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=?";
        String[] args=new String[] { "0" };
        ArrayList<BPA_USER> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_USER.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_USER)k);
        }
        return data;
    }
    /**
     * 用户登录
     * @param Name
     * @param Pass
     * @return
     */
    public static BPA_USER UserLogin(String Name,String Pass)
    {
        String orderby=Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=? and (account=? or name=?) and pass=?";
        String[] args=new String[] { "0",Name,Name,Pass };
        ArrayList<BPA_USER> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_USER.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_USER)k);
        }
        if(data.size()<=0)
            return null;
        else
            return data.get(0);
    }
    //endregion

    //region BPA_MENU 菜单表
    /**
     * 新增菜单
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean AddMenu(BPA_MENU data)
    {
        return Add(BPA_MENU.class,data);
    }
    /**
     * 修改菜单
     * add fengyoufu 20230413
     * @param data
     */
    public static void UpdateMenu(BPA_MENU data)
    {
        Update(BPA_MENU.class,data);
    }
    /**
     * 删除菜单
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean DeleteMenu(BPA_MENU data)
    {
        return Delete(BPA_MENU.class,data.id);
    }
    /**
     * 获取菜单
     * @return
     */
    public static ArrayList<BPA_MENU> GetMenuALL()
    {
        String orderby=Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=?";
        String[] args=new String[] { "0" };
        ArrayList<BPA_MENU> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_MENU.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_MENU)k);
        }
        return data;
    }
    //endregion

    //region BPA_MENUANDUSER 菜单用户关联表
    /**
     * 新增菜单用户关联表
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean AddMenuAndUser(BPA_MENUANDUSER data)
    {
        return Add(BPA_MENUANDUSER.class,data);
    }
    /**
     * 修改菜单用户关联表
     * add fengyoufu 20230413
     * @param data
     */
    public static void UpdateMenuAndUser(BPA_MENUANDUSER data)
    {
        Update(BPA_MENUANDUSER.class,data);
    }
    /**
     * 删除菜单用户关联表
     * add fengyoufu 20230413
     * @param data
     * @return 是否成功
     */
    public static boolean DeleteMenuAndUser(BPA_MENUANDUSER data)
    {
        return Delete(BPA_MENUANDUSER.class,data.id);
    }
    /**
     * 获取菜单用户关联表
     * @return
     */
    public static ArrayList<BPA_MENUANDUSER> GetMenuAndUserALL()
    {
        String orderby=Desc_Time_Up;//先按排序 创建时间倒序
        String where="isDelete=?";
        String[] args=new String[] { "0" };
        ArrayList<BPA_MENUANDUSER> data=new ArrayList<>();
        ArrayList<Object> obj=Get(BPA_MENUANDUSER.class,where,args,orderby);
        for (Object k:obj) {
            data.add((BPA_MENUANDUSER)k);
        }
        return data;
    }
    //endregion

    //region 私有
    /**
     * 新增
     * @param c
     * @param data
     * @return
     */
    private static boolean Add(Class c,Object data)
    {
        ContentValues cv = new ContentValues();
        Map<String, Object> map= Tools.getObjValue(data);
        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());
            }
//            else if (value instanceof Date)
//            {
//                cv.put(key,  (Date) value);
//            }
        }
        SQLiteDatabase db = helper.getWritableDatabase();
        long insert = db.insert(c.getSimpleName(), null, cv);
        db.close();
        return insert>0;
    }
    /**
     * 修改
     * @param c
     * @param data
     * @return
     */
    private static boolean Update(Class c,Object data)
    {
        ContentValues cv = new ContentValues();
        Map<String, Object> map= Tools.getObjValue(data);
        String id=map.get("id").toString();
        if (id.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());
            }
//            else if (value instanceof Date)
//            {
//                cv.put(key,  (Date) value);
//            }
        }
        SQLiteDatabase db = helper.getWritableDatabase();
        long insert = db.insert(c.getSimpleName(), null, cv);
        db.update(c.getSimpleName(),cv,"id = ?", new String[] { id });
        db.close();
        return true;
    }
    /**
     * 删除
     * @param c
     * @param id
     * @return
     */
    private static boolean Delete(Class c,String id)
    {
        if (id.isEmpty())
            return false;
        SQLiteDatabase db = helper.getWritableDatabase();
        long delete=db.delete(c.getSimpleName(), "id = ?",
                new String[] { id });
        db.close();
        return delete>0;
    }
    /**
     * 获取数据
     * @param c
     * @param whereClause  "id = ? and id=?"  "sys_pre>? and dia_pre<?"  "createTime>?"
     * @param Args     new String[] { id,id }
     * @param orderby  "date("+COLUMN_DATE+") desc"   "TYPE desc";  desc 倒序
     * @return
     */
    private static ArrayList<Object> Get(Class c,String whereClause,String[] Args,String orderby)
    {
        ArrayList<Object> rlts = new ArrayList<Object>();
        SQLiteDatabase db = helper.getReadableDatabase();
        //String orderby = "TYPE desc";
        List<String> col=new ArrayList<>();
        for (Field field : c.getFields()){
            col.add(field.getName());
        }
        String[] columns= (String[]) col.toArray(new String[col.size()]);
        Cursor cursor = db.query(c.getSimpleName(), columns, whereClause, Args, null,
                null, orderby, null);
        Object data=new Object();
        while (cursor.moveToNext()) {
            switch (c.getSimpleName())
            {
                case "BPA_MATERIAL":
                    data = new BPA_MATERIAL();
                    //私有
                    ((BPA_MATERIAL)data).imgUrl=cursor.getString((int)cursor.getColumnIndex("imgUrl"));
                    ((BPA_MATERIAL)data).name=cursor.getString((int)cursor.getColumnIndex("name"));
                    break;
                case "BPA_SILOS":
                    data = new BPA_SILOS();
                    //私有
                    ((BPA_SILOS)data).num=cursor.getInt((int)cursor.getColumnIndex("num"));
                    ((BPA_SILOS)data).name=cursor.getString((int)cursor.getColumnIndex("name"));
                    ((BPA_SILOS)data).warningValue=cursor.getInt((int)cursor.getColumnIndex("warningValue"));
                    ((BPA_SILOS)data).thrsoleValue=cursor.getInt((int)cursor.getColumnIndex("thrsoleValue"));
                    ((BPA_SILOS)data).plcValue=cursor.getString((int)cursor.getColumnIndex("plcValue"));
                    ((BPA_SILOS)data).bValue=cursor.getInt((int)cursor.getColumnIndex("bValue"));
                    ((BPA_SILOS)data).jValue=cursor.getInt((int)cursor.getColumnIndex("jValue"));
                    ((BPA_SILOS)data).status=cursor.getInt((int)cursor.getColumnIndex("status"));
                    ((BPA_SILOS)data).Sort=cursor.getInt((int)cursor.getColumnIndex("Sort"));
                    break;
                case "BPA_SILOSANDMATERIAL":
                    data = new BPA_SILOSANDMATERIAL();
                    //私有
                    ((BPA_SILOSANDMATERIAL)data).silosID=cursor.getString((int)cursor.getColumnIndex("silosID"));
                    ((BPA_SILOSANDMATERIAL)data).materialID=cursor.getString((int)cursor.getColumnIndex("materialID"));
                    break;
                case "BPA_GOODS":
                    data = new BPA_GOODS();
                    //私有
                    ((BPA_GOODS)data).name=cursor.getString((int)cursor.getColumnIndex("name"));
                    ((BPA_GOODS)data).sort=cursor.getInt((int)cursor.getColumnIndex("sort"));
                    ((BPA_GOODS)data).status=cursor.getInt((int)cursor.getColumnIndex("status"));
                    break;
                case "BPA_GOODSRECIPE":
                    data = new BPA_GOODSRECIPE();
                    //私有
                    ((BPA_GOODSRECIPE)data).goodsID=cursor.getString((int)cursor.getColumnIndex("goodsID"));
                    ((BPA_GOODSRECIPE)data).materialID=cursor.getString((int)cursor.getColumnIndex("materialID"));
                    ((BPA_GOODSRECIPE)data).value=cursor.getInt((int)cursor.getColumnIndex("value"));
                    ((BPA_GOODSRECIPE)data).sort=cursor.getInt((int)cursor.getColumnIndex("sort"));
                    break;
                case "BPA_ORDER":
                    data = new BPA_ORDER();
                    //私有
                    ((BPA_ORDER)data).thirdPartyID=cursor.getString((int)cursor.getColumnIndex("thirdPartyID"));
                    ((BPA_ORDER)data).status=cursor.getInt((int)cursor.getColumnIndex("status"));
                    break;
                case "BPA_SUBORDER":
                    data = new BPA_SUBORDER();
                    //私有
                    ((BPA_SUBORDER)data).orderID=cursor.getString((int)cursor.getColumnIndex("orderID"));
                    ((BPA_SUBORDER)data).goodsID=cursor.getInt((int)cursor.getColumnIndex("goodsID"));
                    ((BPA_SUBORDER)data).number=cursor.getString((int)cursor.getColumnIndex("number"));
                    ((BPA_SUBORDER)data).status=cursor.getInt((int)cursor.getColumnIndex("status"));
                    break;
                case "BPA_ALERTLOG":
                    data = new BPA_ALERTLOG();
                    //私有
                    ((BPA_ALERTLOG)data).type=cursor.getInt((int)cursor.getColumnIndex("type"));
                    ((BPA_ALERTLOG)data).text=cursor.getString((int)cursor.getColumnIndex("text"));
                    break;
                case "BPA_LOG":
                    data = new BPA_LOG();
                    //私有
                    ((BPA_LOG)data).type=cursor.getInt((int)cursor.getColumnIndex("type"));
                    ((BPA_LOG)data).text=cursor.getString((int)cursor.getColumnIndex("text"));
                    break;
                case "BPA_ORDERLOG":
                    data = new BPA_ORDERLOG();
                    //私有
                    ((BPA_ORDERLOG)data).suborderID=cursor.getString((int)cursor.getColumnIndex("suborderID"));
                    ((BPA_ORDERLOG)data).prewStatus=cursor.getInt((int)cursor.getColumnIndex("prewStatus"));
                    ((BPA_ORDERLOG)data).currentStatus=cursor.getInt((int)cursor.getColumnIndex("currentStatus"));
                    break;
                case "BPA_ORDERLOGDESC":
                    data = new BPA_ORDERLOGDESC();
                    //私有
                    ((BPA_ORDERLOGDESC)data).suborderID=cursor.getString((int)cursor.getColumnIndex("suborderID"));
                    ((BPA_ORDERLOGDESC)data).text=cursor.getString((int)cursor.getColumnIndex("text"));
                    break;
                case "BPA_SYSTEMSET":
                    data = new BPA_SYSTEMSET();
                    //私有
                    ((BPA_SYSTEMSET)data).type=cursor.getInt((int)cursor.getColumnIndex("type"));
                    ((BPA_SYSTEMSET)data).value=cursor.getString((int)cursor.getColumnIndex("value"));
                    break;
                case "BPA_USER":
                    data = new BPA_USER();
                    //私有
                    ((BPA_USER)data).name=cursor.getString((int)cursor.getColumnIndex("name"));
                    ((BPA_USER)data).account=cursor.getString((int)cursor.getColumnIndex("account"));
                    ((BPA_USER)data).pass=cursor.getString((int)cursor.getColumnIndex("pass"));
                    break;
                case "BPA_MENU":
                    data = new BPA_MENU();
                    //私有
                    ((BPA_MENU)data).name=cursor.getString((int)cursor.getColumnIndex("name"));
                    break;
                case "BPA_MENUANDUSER":
                    data = new BPA_MENUANDUSER();
                    //私有
                    ((BPA_MENUANDUSER)data).menuID=cursor.getString((int)cursor.getColumnIndex("menuID"));
                    break;
            }
            ((ModeBase)data).id=cursor.getString((int)cursor.getColumnIndex("id"));
            ((ModeBase)data).createTime=cursor.getString((int)cursor.getColumnIndex("createTime"));
            ((ModeBase)data).updateTime=cursor.getString((int)cursor.getColumnIndex("updateTime"));
            ((ModeBase)data).deleteTime=cursor.getString((int)cursor.getColumnIndex("deleteTime"));
            ((ModeBase)data).deviceID=cursor.getString((int)cursor.getColumnIndex("deviceID"));
            ((ModeBase)data).userID=cursor.getString((int)cursor.getColumnIndex("userID"));
            ((ModeBase)data).isDelete=cursor.getInt((int)cursor.getColumnIndex("isDelete"));
            ((ModeBase)data).exp=cursor.getString((int)cursor.getColumnIndex("exp"));
            rlts.add(data);
        }
        cursor.close();
        db.close();
        return rlts;
    }
    //endregion
}
