package com.ruoyi.ss.locationLog.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ss.locationLog.domain.LocationLog;
import com.ruoyi.ss.locationLog.mapper.LocationLogMapper;
import com.ruoyi.ss.locationLog.service.ILocationLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 定位日志Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-20
 */
@Service
public class LocationLogServiceImpl implements ILocationLogService
{
    @Resource
    private LocationLogMapper rlLocationLogMapper;

    /**
     * 查询定位日志
     *
     * @param locationId 定位日志主键
     * @return 定位日志
     */
    @Override
    public LocationLog selectEtLocationLogByLocationId(Long locationId)
    {
        return rlLocationLogMapper.selectEtLocationLogByLocationId(locationId);
    }

    /**
     * 查询定位日志列表
     *
     * @param rlLocationLog 定位日志
     * @return 定位日志
     */
    @Override
    public List<LocationLog> selectEtLocationLogList(LocationLog rlLocationLog)
    {
        return rlLocationLogMapper.selectEtLocationLogList(rlLocationLog);
    }

    /**
     * 新增定位日志
     *
     * @param rlLocationLog 定位日志
     * @return 结果
     */
    @Override
    public int insertEtLocationLog(LocationLog rlLocationLog)
    {
        rlLocationLog.setCreateTime(DateUtils.getNowDate());
        return rlLocationLogMapper.insertEtLocationLog(rlLocationLog);
    }

    /**
     * 修改定位日志
     *
     * @param rlLocationLog 定位日志
     * @return 结果
     */
    @Override
    public int updateEtLocationLog(LocationLog rlLocationLog)
    {
        return rlLocationLogMapper.updateEtLocationLog(rlLocationLog);
    }

    /**
     * 批量删除定位日志
     *
     * @param locationIds 需要删除的定位日志主键
     * @return 结果
     */
    @Override
    public int deleteEtLocationLogByLocationIds(Long[] locationIds)
    {
        return rlLocationLogMapper.deleteEtLocationLogByLocationIds(locationIds);
    }

    /**
     * 删除定位日志信息
     *
     * @param locationId 定位日志主键
     * @return 结果
     */
    @Override
    public int deleteEtLocationLogByLocationId(Long locationId)
    {
        return rlLocationLogMapper.deleteEtLocationLogByLocationId(locationId);
    }

    /**
     * 解析参数
     *
     * @param list
     * @return 结果
     */
    @Override
    public List<LocationLog> analytic(List<LocationLog> list) {
        // 创建 ObjectMapper 实例用于解析 JSON
        ObjectMapper objectMapper = new ObjectMapper();

        for (LocationLog log : list) {
            try {
                // 解析 onenetMsg 字段中的 JSON 字符串
                JsonNode rootNode = objectMapper.readTree(log.getOnenetMsg());
                JsonNode valueNode = rootNode.path("value");

                // 提取各个字段的值并填充到 EtLocationLog 对象中
                if (valueNode.has("status")) {
                    log.setStatus2(valueNode.get("status").asInt());
                }
                if (valueNode.has("bat")) {
                    // 电池电压，例如 bat: 511 表示 51.1V
                    log.setBat(valueNode.get("bat").asInt());
                }
                if (valueNode.has("csq")) {
                    // 信号强度
                    log.setCsq(valueNode.get("csq").asInt());
                }
                if (valueNode.has("s")) {
                    // 卫星数量
                    log.setS(valueNode.get("s").asInt());
                }
                if (valueNode.has("q")) {
                    // 钥匙状态
                    log.setQ(valueNode.get("q").asInt());
                }

            } catch (Exception e) {
                // 捕获并打印解析异常信息
                e.printStackTrace();
            }
        }
        return list;
    }

}
