package com.muyu.enterprise.controller.car;


import cn.hutool.json.JSONObject;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.enterprise.domain.car.MessageValue;
import com.muyu.enterprise.domain.req.car.MessageValueAddReq;
import com.muyu.enterprise.domain.req.car.MessageValueReq;
import com.muyu.enterprise.domain.resp.car.MessageValueListResp;
import com.muyu.enterprise.service.car.MessageValueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 报文数据控制层
 * @Author：李庆帅
 * @Package：com.muyu.car.controller
 * @Project：cloud-server
 * @name：MessageValueController
 * @Date：2024/9/26 22:11
 * @Description: 报文数据控制层
 */
@Log4j2
@RestController
@RequestMapping("/messageValue")
@Tag(name = "报文数据控制层", description = "进行报文数据操作")
public class MessageValueController extends BaseController
{
    @Autowired
    private MessageValueService messageValueService;

    /**
     * 报文数据列表查询
     * @param messageValueReq 请求对象
     * @return 返回结果
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @Operation(summary = "报文数据列表", description = "根据报文类别, 报文模版筛选报文数据")
    public Result<List<MessageValueListResp>> findAll(@RequestBody MessageValueReq messageValueReq) {
        List<MessageValueListResp> list = messageValueService.findAll(messageValueReq);
        return Result.success(list);
    }

    /**
     * 提供预警和故障使用
     * @return 返回结果
     */
    @GetMapping("/allList")
    @Operation(summary = "报文数据下拉框", description = "全部报文数据下拉框")
    public Result<List<MessageValueListResp>> findAllList() {
        List<MessageValueListResp> list = messageValueService.list().stream().map(
                value -> MessageValueListResp.valueBuild(
                        value
                )
        ).toList();
        return Result.success(list);
    }

    /**
     * 新增添加报文数据
     * @param messageValueAddReq 请求对象
     * @return 返回结果
     */
    @Transactional
    @PostMapping("/add")
    @Operation(summary = "添加报文数据", description = "新增报文数据")
    public Result<String> save(@RequestBody MessageValueAddReq messageValueAddReq) {
        messageValueService.save(MessageValue.addBuild(messageValueAddReq));
        return Result.success("添加成功");
    }

    /**
     * 通过报文id删除数据
     * @param messageId 请求对象
     * @return 返回结果
     */
    @DeleteMapping("/{messageId}")
    @Operation(summary = "删除报文数据", description = "删除报文数据")
    public Result<String> delete(@PathVariable Long messageId) {
        messageValueService.removeById(messageId);
        return Result.success("删除成功");
    }

//7E 56 49 4e 31 32 33 34 35 36 37 38 39 31 32 33 34 35 32 33 35 36 37 38 39 31 32 33 34 35 31 31 36 2e 36 36 34 30 35 33 30 33 39 2e 35 33 31 37 39 31 30 32 30 30 2e 30 30 35 38 36 37 38 33 37 2e 33 32 34 36 33 35 2e 31 32 33 39 2e 34 39 31 36 38 37 39 38 2e 35 36 50 30 39 30 38 31 39 2e 39 39 39 39 2e 38 36 30 39 39 39 39 39 31 30 30 30 31 33 39 2e 34 36 31 34 2e 36 38 31 31 33 36 39 2e 38 39 31 30 30 2e 30 30 31 30 30 2e 30 30 31 30 30 2e 30 30 31 33 31 34 2e 36 35 33 36 30 2e 35 38 34 2e 35 36 33 2e 32 35 31 30 30 2e 30 30 39 38 2e 32 33 30 33 36 2e 32 36 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 76 7E
    /**
     * 报文转换测试
     * @param testStr 请求参数
     * @return 返回结果
     */
    @PostMapping("/analysis/{testStr}")
    @Operation(summary = "测试解析报文", description = "解析报文测试")
    public Result<JSONObject> analysis(@PathVariable("testStr") String testStr){
        JSONObject messageValue = messageValueService.analysis(testStr);
        return Result.success(messageValue);
    }

    /**
     * 根据报文模版id查询报文数据
     * @param templateId 请求对象
     * @return 返回结果
     */
    @GetMapping("/findByTemplateId/{templateId}")
    @Operation(summary = "根据报文模版id查询报文数据", description = "根据报文模版id查询报文数据")
    public Result<List<MessageValueListResp>> findByTemplateId(@PathVariable("templateId") Long templateId){
        List<MessageValueListResp> list  = messageValueService.findByTemplateId(templateId);
        return Result.success(list);
    }





}
