package com.muyu.enterprise.controller;

import com.muyu.enterprise.cache.AllMessageValueCacheService;
import com.muyu.enterprise.service.MessageValueService;
import com.muyu.common.core.domain.Result;
import com.muyu.domain.MessageValue;
import com.muyu.domain.req.MessageValueAddReq;
import com.muyu.domain.req.MessageValueReq;
import com.muyu.domain.resp.MessageValueListResp;
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;
import java.util.stream.Collectors;

/**
 * @Author: LiDongJia
 * @Package: com.muyu.car.controller
 * @Project: 2112-car-cloud-server
 * @name: MessageValueController
 * @Date: 2024/9/19  15:46
 * @Description: 报文数据控制层
 */
@Log4j2
@RestController
@RequestMapping("/messageValue")
@Tag(name = "报文数据控制层", description = "进行报文数据操作")
public class MessageValueController {

    @Autowired
    private MessageValueService messageValueService;

    //存Redis
    @Autowired
    private AllMessageValueCacheService allMessageValueCacheService;


    /**
     * 报文数据列表查询
     *
     * @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);
//        for (MessageValueListResp messageValueListResp : list) {
//            allMessageValueCacheService.put(String.valueOf(messageValueListResp.getMessageTemplateId()), (List<MessageValueListResp>) messageValueListResp);
//        }
        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
                )
        ).collect(Collectors.toList());
        return Result.success(list);
    }

    /**
     * 添加报文数据
     * @param messageValueAddReq
     * @return
     */
    @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("删除成功");
    }

    /**
     * 根据报文模版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);
        allMessageValueCacheService.put(String.valueOf(templateId), list);
        return Result.success(list);
    }


}
