package com.template.service.impl;
import com.template.domain.*;
import com.template.mapper.TemplateMapper;
import com.template.service.CarService;
import com.template.service.MessageTemplateTypeService;
import com.template.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 implements TemplateService{

    @Autowired
    private static TemplateMapper templateMapper;

    @Autowired
    private CarService carService;

    @Autowired
    private MessageTemplateTypeService messageTemplateTypeService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Template> templateList() {
        return templateMapper.templateList();
    }

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

        List<MessageTemplateType> templateList = templateMapper.findTemplateById(1);
        String[] split = templateMessage.split(" ");
        String[] strings = new String[split.length];

        List<CompletableFuture<String>> futures = new ArrayList<>();
        for (MessageTemplateType templateType : templateList) {

            futures.add(CompletableFuture.supplyAsync(() -> {
                int startIndex = Integer.parseInt(String.valueOf(templateType.getStartIndex())) - 1;
                int endIndex = Integer.parseInt(String.valueOf(templateType.getEndIndex()));
                StringBuilder hexBuilder = new StringBuilder();
                for (int j = startIndex; j < endIndex; j++) {
                    hexBuilder.append(split[j]);
                }
                // 创建16进制的对象
                String hex = hexBuilder.toString();
                // 转橙字符数组
                char[] result = new char[hex.length() / 2];
                for (int x = 0; x < hex.length(); x += 2) {
                    // 先转十进制
                    int high = Character.digit(hex.charAt(x), 16);
                    // 转二进制
                    int low = Character.digit(hex.charAt(x + 1), 16);
                    // 转字符
                    result[x / 2] = (char) ((high << 4) + low);
                }
                return new String(result);
            }));
        }

        for (int i = 0; i < futures.size(); i++) {
            strings[i] = futures.get(i).get();
        }

        System.out.println("哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈");
        log.info("结果是:"+strings);

    }

    @Override
    public List<MessageTemplateType> findTemplateById(Integer templateId) {
        return templateMapper.findTemplateById(templateId);
    }


}
