package com.muyu.server.controller;


import com.muyu.common.core.domain.Result;
import com.muyu.domain.CarMessage;
import com.muyu.server.service.CarMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 报文模板展示列表控制层
 */
@Log4j2
@RestController
@RequestMapping("/carMessage")
@Tag(name = "信息报文模块" )
public class CarMessageController {
    @Resource
    private CarMessageService carMessageService;
    /**
     * 根据所属车类别 解析 车辆报文模板
     * @param
     * @return
     */
    @PostMapping("/selectCarMessageList")
    @Operation(summary = "报文模板展示列表")
    public Result<List<CarMessage>> selectCarMessageList(){

        List<CarMessage> carMessages = carMessageService.selectCarMessageList();
        return Result.success(carMessages);
    }


    /**
     * 添加车辆报文规则
     */
    @PostMapping("/insertCarMessage")
    @Operation(summary = "添加报文信息")
    public Result<Integer> insertCarMessage(@Validated @RequestBody CarMessage carMessage){

        int inserted = carMessageService.insertCarMessage(carMessage);
        return Result.error(402,"添加失败");
    }

    /**
     * 删除车辆报文规则
     *
     */
    @PostMapping("/delectByCarMessageId")
    @Operation(summary = "删除报文信息")
    public Result<Integer> delectByCarMessageId(@RequestParam(name = "carMessageId") Integer carMessageId){
        int deleteByCarMessageId = carMessageService.delectByCarMessageId(carMessageId);

        if (deleteByCarMessageId>0){
            return Result.success(200,"删除成功");
        }
        return  Result.error(402,"删除失败");
    }

    /**
     * 修改车辆报文规则
     */
    @PostMapping("/updateCarMessage")
    @Operation(summary = "修改报文信息")
    public Result<Integer> updateCarMessage(@Validated @RequestBody CarMessage carMessage){

        int updateCarMessage = carMessageService.updateCarMessage(carMessage);
        if(updateCarMessage>0)
        {
            return Result.success(200,"修改成功");
        }
        return Result.error( 402,"修改失败");
    }


    /**
     * 测试分割字符
     */
    public class MessageParser{
        public static void main(String[] args) {
            String message = "01,02,03," +
                    "04,05,06,07,08,09," +
                    "10,11,12,13,14,15,16" +
                    ",17,18,19,20,21,22,23," +
                    "24,25,26,27,28,29,30" +
                    ",31,32,33,34,35,36,37," +
                    "38,39,40,41,42,43,44,45," +
                    "46";


            String[] split = message.split(",");
            for (int i = 0; i < split.length; i++) {
                System.out.println(split[i]);
                if(i == 0){
                    System.out.println("开始");
                }else if(i == split.length - 1){
                    System.out.println("结束");
                }
            }
            System.out.println(split.length);
            System.out.println(message.length());

            System.out.println(message.substring(0,1));
            System.out.println(message.substring(1,2));
            System.out.println(message.substring(2,3));
            System.out.println(message.substring(3,4));
            System.out.println(message.substring(4,5));
            System.out.println(message.substring(5,6));
            System.out.println(message.substring(6,7));
            System.out.println(message.substring(7,8));
            System.out.println(message.substring(8,9));
            System.out.println(message.substring(9,10));
            System.out.println(message.substring(10,11));
            System.out.println(message.substring(11,12));
            System.out.println(message.substring(12,13));
            System.out.println(message.substring(13,14));
            System.out.println(message.substring(14,15));
            System.out.println(message.substring(15,16));




        }


    }



}
