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

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.apkfuns.logutils.LogUtils;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.file.DBHelper;
import com.bonait.bnframework.common.db.mode.BPA_GOODS;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPENAME;

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

/**
 * @author: liup
 * @description:
 * @date: 2024/6/5 17:37.
 */
public class GoodsRecipeUtil {
    /**
     * 商品ID查询配方关联表
     *
     * @param id
     * @return
     */
    public static ArrayList<BPA_GOODSRECIPENAME> GetGoodsRecipeNameGoodId(String id) {
        try {
            LogUtils.d("GetGoodsRecipeNameGoodId id="+id);
            String orderby = QueryDB.Desc_Time_Up;//出料顺序
            String where = "isDelete=? and goodsID=?";
            String[] args = new String[]{"0", id};
            ArrayList<BPA_GOODSRECIPENAME> data = new ArrayList<>();
            ArrayList<Object> obj = QueryDB.Get(BPA_GOODSRECIPENAME.class, where, args, orderby);
            for (Object k : obj) {
                data.add((BPA_GOODSRECIPENAME) k);
            }
            LogUtils.d("GetGoodsRecipeNameGoodId data="+data.toString());
            return data;
        } catch (Exception e) {
            Log.d("商品ID查询配方信息", "商品ID查询配方信息异常: " + e.getMessage());
            return null;
        }
    }

    public static ArrayList<BPA_GOODSRECIPENAME> GetGoodsRecipeNameLikes(String id, List<String> list) {
        try {
            String orderby = QueryDB.Desc_Time_Up;//出料顺序
            String where = "isDelete=? and goodsID=?";
            if(!list.isEmpty()){
                StringBuilder likes = new StringBuilder();
                for (int i=0;i<list.size();i++){
                    likes.append(" and design like '%").append(list.get(i)).append("%'");
                }
                where = "isDelete=? and goodsID=?"+likes;
            }
            GetGoodsRecipeNameGoodId(id);
            LogUtils.d("GetGoodsRecipeNameLikes list="+list);
            LogUtils.d("GetGoodsRecipeNameLikes goodsID="+id+"; where = " +where);
            String[] args = new String[]{"0", id};
            ArrayList<BPA_GOODSRECIPENAME> data = new ArrayList<>();
            ArrayList<Object> obj = QueryDB.Get(BPA_GOODSRECIPENAME.class, where, args, orderby);
            for (Object k : obj) {
                data.add((BPA_GOODSRECIPENAME) k);
            }
            LogUtils.d("GetGoodsRecipeNameLikes data="+data.toString());
            return data;
        } catch (Exception e) {
            Log.d("商品ID查询配方信息", "商品ID查询配方信息异常: " + e.getMessage());
            return null;
        }
    }

    /**
     * 删除商品下的工序
     *
     * @param goodsID
     * @return 是否成功
     */
    public static boolean deleteGoodsRecipeByGoodId(String goodsID) {
        boolean result = true;
        SQLiteDatabase db = DBHelper.getInstance(ConfigName.getInstance().dishesCon).getWritableDatabase();
        long delete = db.delete(BPA_GOODSRECIPENAME.class.getSimpleName(), "goodsID=?",
                new String[]{goodsID});
        if(delete <= 0){
            result = false;
        }
        db.close();
        LogUtils.d("deleteGoodsByTypeId 删除分类下的商品 result=" + result);
        return result;
    }
}
