package com.muyu.enterprise.controller;

import com.muyu.enterprise.service.MiddleService;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.domain.Middle;
import com.muyu.domain.req.MiddleReq;
import groovy.util.logging.Log4j2;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Log4j2
@RequestMapping("/middle")
@RestController
@Tag(name = "中间表", description = "中间表")
public class MiddleController extends BaseController {

    @Autowired
    private MiddleService middleService;


    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @Operation(description = "删除信息")
    public Result<String> deleteMiddle(@PathVariable Integer id) {
        middleService.removeById(id);
        logger.info("删除id为{}的中间表信息", id);
        return Result.success(null,"操作成功");
    }

    /**
     * 修改
     */
    @PutMapping("/{id}")
    @Operation(description = "修改信息")
    public Result<String> updateMiddle(
            @PathVariable Integer id,
            @Validated @RequestBody MiddleReq req) {
        middleService.updateById(req);
        logger.info("修改id为{}的中间表信息", id);
        return Result.success(null,"操作成功");
    }

    /**
     * 添加
     */
    @PostMapping("/addMiddle")
    @Operation(description = "添加")
    public Result<String> addMiddle(@Validated @RequestBody MiddleReq req) {
        middleService.save(Middle.build(req));
        return Result.success(null,"操作成功");
    }

}
