package com.muyu.enterpise.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.domain.MessageValue;
import com.muyu.domain.req.MessageValueAddReq;
import com.muyu.domain.req.MessageValueReq;
import com.muyu.domain.resp.MessageValueListResp;
import com.muyu.enterprise.cache.MessageValueCacheService;
import com.muyu.enterpise.service.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.web.bind.annotation.*;

import java.util.List;

/**
 * 报文数据控制层
 *  * @ClassName MessageValueController
 *  * @Description TODO
 *  * @Author Li HD
 *  * @Date 2024/9/19
 */
@Log4j2
@RestController
@RequestMapping("/messageValue")
@Tag(name = "报文数据控制层", description = "进行报文数据操作")
public class MessageValueController extends BaseController {

    @Autowired
    private MessageValueService messageValueService;

    @Autowired
    private MessageValueCacheService enterpriseCacheService;

    /**
     * 报文数据列表查询
     * @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);
    }

    /**
     * 添加报文数据
     * @param messageValueAddReq
     * @return
     */
    @PostMapping("/")
    @Operation(summary = "添加报文数据", description = "新增报文数据")
    public Result<String> save(@RequestBody MessageValueAddReq messageValueAddReq){
        messageValueService.save(MessageValue.addBuild(messageValueAddReq));
//        enterpiseCacheService.put(String.valueOf(messageValueAddReq.getTemplateId()),MessageValue.addBuild(messageValueAddReq));
        return Result.success("添加成功");
    }

    /**
     * 删除报文数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除报文数据", description = "根据id删除报文数据")
    public Result<String> delete(@PathVariable("id") String id){
        //messageValueService.delete(id);
        return Result.success("删除成功");
    }

    /**
     * 测试方法
     */
    @PostMapping("/test")
    public void test(String str){
        messageValueService.test(str);
    }


    @PostMapping({"/findByTemplateId/{stringVin}"})
    @Operation(
            summary = "根据车辆类型查询报文模版ID",
            description = "根据车辆类型查询报文模版ID"
    )
    public Result<Long> findByTemplateId(@PathVariable("stringVin") String stringVin) {
        return Result.success(this.messageValueService.findByTemplateId(stringVin), "查询成功");
    }
}
