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

import cn.dev33.satoken.annotation.SaCheckPermission;
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.MpMessageGroupUserReq;
import com.seer.teach.mp.admin.controller.req.MpMessageGroupUserQueryReq;
import com.seer.teach.mp.admin.controller.resp.MpMessageGroupUserResp;
import com.seer.teach.mp.admin.convert.MpMessageGroupUserConvert;
import com.seer.teach.mp.entity.MpMessageGroupUserEntity;
import com.seer.teach.mp.service.IMqMessageGroupUserService;
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/user")
public class AdminMessageGroupUserController {

    private final IMqMessageGroupUserService mqMessageGroupUserService;
    
    /**
     * 添加用户到消息组
     */
    @PostMapping
    @Operation(summary = "添加用户到消息组")
    @SaCheckPermission("admin:mp:message-group:user:create")
    public ResultBean create(@RequestBody @Validated MpMessageGroupUserReq params) {
        mqMessageGroupUserService.create(params.getGroupId(), params.getUserIds());
        return ResultBean.success();
    }
    
    /**
     * 根据ID查询消息组用户关联
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询消息组用户关联")
    @SaCheckPermission("admin:mp:message-group:user:getById")
    public ResultBean<MpMessageGroupUserResp> getById(@PathVariable Integer id) {
        MpMessageGroupUserEntity resp = mqMessageGroupUserService.getMesGroupUserById(id);
        return ResultBean.success(MpMessageGroupUserConvert.INSTANCE.convertOne(resp));
    }
    
    /**
     * 分页查询消息组用户关联列表
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询消息组用户关联列表")
    @SaCheckPermission("admin:mp:message-group:user:query")
    public ResultBean<PageListBean<MpMessageGroupUserResp>> page(@RequestBody @Validated MpMessageGroupUserQueryReq params) {
        Page<MpMessageGroupUserEntity> pageParam = new Page<>(params.getPageNo(), params.getPageSize());
        MpMessageGroupUserEntity queryParam = MpMessageGroupUserConvert.INSTANCE.convertQueryOne(params);
        Page<MpMessageGroupUserEntity> pageList = mqMessageGroupUserService.pageList(pageParam,queryParam);
        PageListBean resultBean =  PageConverterUtils.convertPageListBean(pageList, MpMessageGroupUserConvert.INSTANCE::convertRespList);
        return ResultBean.success(resultBean);
    }
    

    /**
     * 根据ID删除消息组用户关联
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "根据ID删除消息组用户关联")
    @SaCheckPermission("admin:mp:message-group:user:delete")
    public ResultBean delete(@PathVariable Integer id) {
        mqMessageGroupUserService.deleteById(id);
        return ResultBean.success();
    }
}