package com.muyu.enterpise.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.domain.MessageTemplate;
import com.muyu.domain.req.MessageTemplateAddReq;
import com.muyu.domain.resp.MessageTemplateListResp;
import com.muyu.enterpise.service.MessageTemplateService;
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 MessageTemplateController
 *  * @Description TODO
 *  * @Author Li HD
 *  * @Date 2024/9/19
 *  * @Version 1.0
 */
@Log4j2
@RestController
@RequestMapping("/messageTemplate")
@Tag(name = "报文模版控制层", description = "报文模版操作")
public class MessageTemplateController {

    @Autowired
    private MessageTemplateService messageTemplateService;

    /**
     * 报文模版列表查询
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    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
     */
    @PostMapping("/")
    public Result<String> save(@RequestBody MessageTemplateAddReq messageTemplateAddReq) {
        messageTemplateService.save(MessageTemplate.addBuild(messageTemplateAddReq));
        return Result.success("添加成功");
    }

    /**
     * 删除报文模版
     * @param messageTemplateId
     * @return
     */
    @DeleteMapping("/{messageTemplateId}")
    public Result<String> delete(@PathVariable("messageTemplateId") Long messageTemplateId) {
        messageTemplateService.removeById(messageTemplateId);
        return Result.success("删除成功");
    }




}
