package com.seer.teach.mp.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.mp.admin.controller.req.MpMessageGroupReq;
import com.seer.teach.mp.admin.controller.req.MpMessageGroupQueryReq;
import com.seer.teach.mp.admin.controller.req.MpMessageGroupUpdateReq;
import com.seer.teach.mp.admin.controller.resp.MpMessageGroupResp;
import com.seer.teach.mp.admin.convert.MpMessageGroupConvert;
import com.seer.teach.mp.entity.MpMessageGroupEntity;
import com.seer.teach.mp.service.IMqMessageGroupService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "管理端 - 消息组")
@RequiredArgsConstructor
@RestController
@RequestMapping("/mp/message-group")
public class AdminMessageGroupController {

    private final IMqMessageGroupService mqMessageGroupService;
    
    /**
     * 创建消息组
     */
    @PostMapping
    @Operation(summary = "创建消息组")
    @SaCheckPermission("admin:mp:message-group:create")
    public ResultBean<Boolean> create(@RequestBody @Validated MpMessageGroupReq params) {
        return ResultBean.success(mqMessageGroupService.crateMesGroup(MpMessageGroupConvert.INSTANCE.convertOne(params))) ;
    }
    
    /**
     * 根据ID查询消息组
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询消息组")
    @SaCheckPermission("admin:mp:message-group:getById")
    public ResultBean<MpMessageGroupResp> getById(@PathVariable Integer id) {
        MpMessageGroupResp mesGroup = MpMessageGroupConvert.INSTANCE.convertOne(mqMessageGroupService.getMesGroupById(id));
        return ResultBean.success(mesGroup);
    }
    
    /**
     * 分页查询消息组
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询消息组")
    @SaCheckPermission("admin:mp:message-group:query")
    public ResultBean<PageListBean<MpMessageGroupResp>> page(@RequestBody @Validated MpMessageGroupQueryReq params) {
        Page<MpMessageGroupEntity> pageParam = new Page<>(params.getPageNo(), params.getPageSize());
        MpMessageGroupEntity queryParam = MpMessageGroupConvert.INSTANCE.convertQueryOne(params);
        IPage<MpMessageGroupEntity> pageList = mqMessageGroupService.pageList(pageParam, queryParam);
        PageListBean<MpMessageGroupResp> resultBean =  PageConverterUtils.convertPageListBean(pageList, MpMessageGroupConvert.INSTANCE::convertRespList);
        return ResultBean.success(resultBean);
    }
    
    /**
     * 更新消息组
     */
    @PostMapping("/{id}")
    @Operation(summary = "更新消息组")
    @SaCheckPermission("admin:mp:message-group:update")
    public ResultBean<Boolean> update(@RequestBody @Validated MpMessageGroupUpdateReq params,
                             @PathVariable Integer id) {
        MpMessageGroupEntity entity = MpMessageGroupConvert.INSTANCE.convertUpdateOne( params);
        return ResultBean.success(mqMessageGroupService.updateMesGroup(entity, id));
    }
    
    /**
     * 根据ID删除消息组
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "根据ID删除消息组")
    @SaCheckPermission("admin:mp:message-group:delete")
    public ResultBean<Boolean> delete(@PathVariable Integer id) {
        return ResultBean.success(mqMessageGroupService.deleteById(id));
    }
}
