package com.muyu.enterprise.controller.car;



import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.enterprise.domain.car.MessageTemplate;
import com.muyu.enterprise.domain.req.car.MessageTemplateAddReq;
import com.muyu.enterprise.domain.resp.car.MessageTemplateListResp;
import com.muyu.enterprise.service.car.MessageTemplateService;
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 javax.annotation.Resource;
import java.util.List;


/**
 * 报文模版控制层
 * @Author：李庆帅
 * @Package：com.muyu.car.controller
 * @Project：cloud-server
 * @name：MessageTemplateController
 * @Date：2024/9/26 22:08
 * @Description: 报文模版控制层
 */
@Log4j2
@RestController
@RequestMapping("messageTemplate")
@Tag(name = "报文模版控制层", description = "进行报文模版操作")
public class MessageTemplateController extends BaseController
{
    @Autowired
    private MessageTemplateService messageTemplateService;


    /**
     * 报文模版列表查询
     * @return 返回结果
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @Operation(summary = "报文模版列表查询", description = "报文模版列表查询")
    public Result<List<MessageTemplateListResp>> findAll() {
        List<MessageTemplate> list = messageTemplateService.list();
        List<MessageTemplateListResp> messageTemplateListRespList = list.stream()
                .map(template -> MessageTemplateListResp.messageTemplateBuild(
                                template
                        )
                )
                .toList();
        return Result.success(messageTemplateListRespList);
    }

    /**
     * 新增添加报文模版
     * @param messageTemplateAddReq 请求对象
     * @return 返回结果
     */
    @Transactional
    @PostMapping("/")
    @Operation(summary = "新增添加报文模版", description = "新增添加报文模版")
    public Result<String> save(@RequestBody MessageTemplateAddReq messageTemplateAddReq) {
        messageTemplateService.save(MessageTemplate.addBuild(messageTemplateAddReq));
        return Result.success("添加成功");
    }

    /**
     * 删除报文模版
     * @param messageTemplateId 请求对象
     * @return 返回结果
     */
    @Transactional
    @DeleteMapping("/{messageTemplateId}")
    @Operation(summary = "删除报文模版", description = "删除报文模版")
    public Result<String> delete(@PathVariable("messageTemplateId") Long messageTemplateId) {
        messageTemplateService.removeById(messageTemplateId);
        return Result.success("删除成功");
    }


}
