package com.hai.config;

import com.alibaba.fastjson.JSONObject;
import org.springframework.context.annotation.Configuration;

import java.util.Objects;

/**
 * @serviceName DataUtilConfig.java
 * @author Sum1Dream
 * @version 1.0.0
 * @Description // 数据工具类
 * @createTime 16:07 2023/4/4
 **/
@Configuration
public class DataUtilConfig {

    /**
     * @Author Sum1Dream
     * @Name dataFindByKfc
     * @Description // 根据产品id 查询对应内容
     * @Date 16:09 2023/4/4
     *  @Param object 数据内容
     * @Param productId 产品id
     * @Return com.alibaba.fastjson.JSONObject
     */
    public static JSONObject dataFindByKfc(JSONObject kfcObject , String productId) {
        if (kfcObject.getJSONArray("products") != null) {
            for (Object products : kfcObject.getJSONArray("products")) {
                JSONObject productsObject = (JSONObject) products;
                if (Objects.equals(productsObject.getString("productId"), productId)) {
                    return productsObject;
                }

                if (productsObject.getJSONArray("specGroups") != null) {
                    for (Object specGroups : productsObject.getJSONArray("specGroups")) {
                        JSONObject specGroupsObject = (JSONObject) specGroups;
                        if (Objects.equals(specGroupsObject.getString("productId"), productId)) {
                            return specGroupsObject;
                        }

                    }
                }
            }
        }

        return null;
    }

}
