package com.hc.web.controller.browser.back;

import com.hc.business.model.MsgsAttachment;
import com.hc.business.service.MsgsAttachmentService;
import com.hc.core.utils.result.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * (MsgsAttachment)表控制层
 *
 * @author makejava
 * @since 2024-02-29 18:52:26
 */

@RestController
@RequestMapping("/new/api/back/msgsAttachment")
public class BackMsgsAttachmentController {

    /**
     * 服务对象
     */
    private final MsgsAttachmentService msgsAttachmentService;

    public BackMsgsAttachmentController(MsgsAttachmentService msgsAttachmentService) {
        this.msgsAttachmentService = msgsAttachmentService;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "依据主键获取 MsgsAttachment", response = Result.class)
    public Result selectDetail(@PathVariable("id") Integer id) {
        return Result.success(this.msgsAttachmentService.queryById(id));
    }


    @PutMapping("/update")
    @ApiOperation(value = "修改 MsgsAttachment", response = Result.class)
    public Result updateMsgsAttachment(@RequestBody MsgsAttachment msgsAttachment) {
        return Result.success(msgsAttachmentService.update(msgsAttachment));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "依据主键删除 MsgsAttachment", response = Result.class)
    public Result deleteMsgsAttachment(@PathVariable("id") Integer id) {
        return Result.success(msgsAttachmentService.deleteById(id));
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取 MsgsAttachment列表(分页)", response = Result.class)
    public Result listMsgsAttachment(@RequestParam(defaultValue = "0") Integer page,
                                     @RequestParam(defaultValue = "0") Integer size) {
        return Result.success(msgsAttachmentService.queryAllByLimit(page, size));
    }

    @PostMapping("/insert")
    @ApiOperation(value = "新增附件", response = Result.class)
    public Result addAttachment(@Valid @RequestBody List<MsgsAttachment> msgsAttachmentList) {
        msgsAttachmentList.forEach(msgsAttachmentService::insert);
        return Result.success(msgsAttachmentList);
    }


    @GetMapping("/queryByMsgId")
    @ApiOperation(value = "根据ID获取他的附件", response = Result.class)
    public Result queryByMsgId(@RequestParam(defaultValue = "0") Integer msgId) {
        return Result.success(msgsAttachmentService.queryByMsgId(msgId));
    }


}
