package com.muyu.server.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.domain.MessageTemplateType;
import com.muyu.common.domain.SysCar;
import com.muyu.common.domain.Template;
import com.muyu.server.mapper.TemplateMapper;
import com.muyu.server.service.MessageTemplateTypeService;
import com.muyu.server.service.SysCarService;
import com.muyu.server.service.TemplateService;
import lombok.extern.log4j.Log4j2;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
/**
 * @Author：liuxinyue
 * @Package：com.template.service.impl
 * @Project：cloud-server-c
 * @name：TemplateServiceImp
 * @Date：2024/9/20 12:12
 */
@Log4j2
@Service
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template> implements TemplateService {

    @Autowired
    private static TemplateMapper templateMapper;

    @Autowired
    private SysCarService sysCarService;

    @Autowired
    private MessageTemplateTypeService messageTemplateTypeService;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void messageParsing(String templateMessage) throws SQLException, IoTDBConnectionException, ClassNotFoundException, StatementExecutionException, ExecutionException, InterruptedException {

        //给一个JSON对象
        JSONObject jsonObject = new JSONObject();
        //先截取出VIN码 然后根据VIN码查询这个车属于什么类型
        if (templateMessage.length() < 18) {
            throw new RuntimeException("The vehicle message is incorrect");
        }
        //将报文进行切割
        String[] hexArray = templateMessage.split(" ");
        StringBuilder result = new StringBuilder();
        for (String hex : hexArray) {
            int decimal = Integer.parseInt(hex, 16);
            result.append((char) decimal);
        }
        //取出VIN码
        String carVin = result.substring(0, 18 - 1);
        log.info("carVin码为:" + carVin);
        //根据VIN码获取车辆信息
        SysCar carByVin = sysCarService.findCarByVin(carVin);
        //对应车辆所对应的报文模版
        Integer templateId = carByVin.getTemplateId();

        List<MessageTemplateType> templateTypeList;

        //key
        String redisKey = "messageTemplateType" + templateId;

        //key存在
        if (redisTemplate.hasKey(redisKey)) {

            List list = redisTemplate.opsForList().range(redisKey, 0, -1);

            templateTypeList = list.stream().map(o -> JSON.parseObject(o.toString(), MessageTemplateType.class))
                    .toList();

        } else {
            List<MessageTemplateType> templateTypeList1 = messageTemplateTypeService.findTemplateById(templateId);
            templateTypeList = templateTypeList1;
            templateTypeList.forEach(
                    templateType ->
                            redisTemplate.opsForList().rightPush(
                                    redisKey, com.alibaba.fastjson.JSON.toJSONString(templateType)
                            )
            );
        }
        //将模版里面有的配置进行循环
        for (MessageTemplateType messageTemplateType : templateTypeList) {
            //开始位置
            Integer startIndex = messageTemplateType.getStartIndex() - 1;
            //结束位置
            Integer endIndex = messageTemplateType.getEndIndex();
            //将每个解析后的字段都存入到JSON对象中
            jsonObject.put(messageTemplateType.getMessageField(), result.substring(startIndex, endIndex));
        }

        log.info("解析后的报文是:" + jsonObject);


    }



}
