package org.springblade.system.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.core.domain.entity.KyDataCollectData;
import org.springblade.system.mapper.DataCollectDataMapper;
import org.springblade.system.service.DataCollectDataService;
import org.springframework.stereotype.Service;

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

/**
 * @author 马三炮
 * @date 2025/9/10
 */
@Service
@Slf4j
public class DataCollectDataServiceImpl implements DataCollectDataService {

    @Resource
    private DataCollectDataMapper dataCollectDataMapper;

    /**
     * 根据数据类型数据汇集管理列表
     *
     * @param kyDataCollectData
     * @return
     */
    @Override
    public List<KyDataCollectData> selectKyDataCollectDataList(KyDataCollectData kyDataCollectData) {
        return dataCollectDataMapper.selectKyDataCollectDataList(kyDataCollectData);
    }

    @Override
    public Map<String, Object> queryDetailById(KyDataCollectData kyDataCollectData) {
        String jsonData = dataCollectDataMapper.queryDetailById(kyDataCollectData);
        if (jsonData != null) {
            JSONArray objects = JSON.parseArray(jsonData);
            for (Object obj : objects) {
                JSONObject object = (JSONObject) obj;
                if (object != null) {
                    String id = object.getString("id");
                    if (id.equals(kyDataCollectData.getJsonId().toString())) {
                        Map<String, Object> map = (Map<String, Object>) object;
                        return map;
                    }
                }
            }
        }
        return null;
    }
}
