package com.muyu.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.domain.CarMessage;

import com.muyu.domain.resp.CarMessageResp;
import com.muyu.mapper.CarInformationMapper;
import com.muyu.mapper.CarMessageMapper;
import com.muyu.mapper.CarMessagePlusMapper;
import com.muyu.service.CarMessageService;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

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

/**
 * 报文模板展示列表业务实现层
 */
@Log4j2
@Service
public class CarMessageServiceImpl
        extends ServiceImpl<CarMessageMapper, CarMessage>
        implements CarMessageService {
    @Resource
    private CarMessageMapper carMessageMapper;

    @Resource
    private CarMessagePlusMapper carMessagePlusMapper;

    @Resource
    private CarInformationMapper carInformationMapper;


    @Override
    public List<CarMessage> selectCarMessageList(int modelCode) {
        LambdaQueryWrapper<CarMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CarMessage::getCarMessageCartype, modelCode);
        return this.list(queryWrapper);
    }

    @Override
    public List<CarMessage> selectCarMessage(Integer carMessageCartype) {
        return carMessagePlusMapper.selectCarMessageList(carMessageCartype);
    }

    @Override
    public List<CarMessage> selectCarMessageListAll(int i) {
        LambdaQueryWrapper<CarMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CarMessage::getCarMessageCartype, i);
        return this.list(queryWrapper);
    }

    @Override
    public JSONObject inciseCarMessage(String testString) {
        return null;
    }

    //报文处理
//    @Resource
//    private RedisTemplate<String , Objects> redisTemplate;

//    /**
//     * 报文解析
//     * @param testString
//     * @return
//     */
//    @Override
//    public JSONObject inciseCarMessage(String testString) {
//        //根据空格拆分切割数据字符串
//        String[] split = testString.split(" ");
//        StringBuilder stringBuilder = new StringBuilder();
//        for (String conversion : split) {
//            //将16进制字符串转换为对应的10进制
//            int inciseindex = Integer.parseInt(conversion, 16);
//            // 将10进制转换为对应的字符
//            stringBuilder.append((char) inciseindex);
//        }
//        //切取车辆VIN
//        String substring = stringBuilder.substring(1, 18);
//        log.info("车辆的VIN码:" + substring);
//        //根据给定的vehicleVin（车辆VIN号）获取对应的模板车辆分类carMessageCartype
//        String selectcared = carInformationMapper.selectcarMessageCartype(substring);
//        //创建接受数据的数组
//        List<CarMessage> messagesList ;
//
//        try{
//            String redisKey = "carMessageList" + selectcared;
//
//            if (redisTemplate.hasKey(redisKey)){
//                List<Objects> list = redisTemplate.opsForList().range(redisKey ,  0, -1);
//                messagesList = list.stream()
//                        .map(objects -> JSON.parseObject(objects.toString(), CarMessage.class))
//                                .toList();
//                log.info("Redis缓存查询成功");
//            }else {
//                messagesList  = carInformationMapper.selectcarMessageCartype(selectcared);
//
//                messagesList.forEach(
//                        listReq -> redisTemplate.opsForList().rightPushAll(redisKey, JSON.toString(listReq) )
//                );
//                log.info("数据库查询成功");
//            }
//          }catch(Exception e){
//            throw  new RuntimeException("获取报文模板失败");
//          }
//        //判断报文模板 列表 不为空
//        if(messagesList.isEmpty()){
//            throw new RuntimeException("报文模版为空");
//        }
//        //存储报文模板解析后的数据
//        JSONObject jsonObject = new JSONObject();
//        for (CarMessage carMessage : messagesList) {
//            //起始位下标
//            Integer startIndex = carMessage.getCarMessageStartIndex();
//            //结束位下标
//            Integer endIndex = carMessage.getCarMessageEndIndex();
//            //根据报文模板获取保温截取位置
//            String value = stringBuilder.substring(startIndex, endIndex);
//            //存入数据
//            jsonObject.put(carMessage.getMessageTypeName(), value);
//
//        }
//        return jsonObject;
//
//    }



}
