package com.yonge.cooleshow.teacher.controller;


import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.ImGroupResultDto;
import com.yonge.cooleshow.biz.dal.dto.ImGroupSearchDto;
import com.yonge.cooleshow.biz.dal.entity.ImGroup;
import com.yonge.cooleshow.biz.dal.service.ImGroupService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.utils.validator.ValidationKit;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 即时通讯群组(ImGroup)表控制层
 *
 * @author zx
 * @since 2022-03-22 10:45:56
 */
@Api(tags = "即时通讯群组")
@RestController
@RequestMapping("/imGroup")
public class ImGroupController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private ImGroupService imGroupService;
    @Resource
    private SysUserFeignService sysUserFeignService;

    @ApiOperation("创建群聊")
    @PostMapping(value = "/create")
    public HttpResponseResult create(@Valid @RequestBody ImGroup imGroup, BindingResult bindingResult) throws Exception {
        ValidationKit.ignoreFields(bindingResult,"id");
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        imGroup.setCreateBy(sysUser.getId());
        imGroupService.create(imGroup);
        return succeed();
    }

    @ApiOperation("解散群聊")
    @PostMapping(value = "/dismiss/{groupId}")
    public HttpResponseResult dismiss(@ApiParam(value = "群编号", required = true) @PathVariable("groupId") String groupId) throws Exception {
        imGroupService.dismiss(groupId);
        return succeed();
    }

    @ApiOperation("获取群详情")
    @PostMapping(value = "/getDetail/{groupId}")
    public HttpResponseResult<ImGroup> getDetail(@ApiParam(value = "群编号", required = true) @PathVariable("groupId") String groupId) throws Exception {
        return succeed(imGroupService.getById(groupId));
    }

    @ApiOperation("群列表")
    @PostMapping(value = "/queryAll")
    public HttpResponseResult<List<ImGroup>> queryAll(@Valid @RequestBody ImGroupSearchDto imGroupSearchDto, BindingResult bindingResult) throws Exception {
        ValidationKit.ignoreFields(bindingResult,"createUserId");
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        imGroupSearchDto.setUserId(sysUser.getId());
        return succeed(imGroupService.queryAll(imGroupSearchDto));
    }

    @ApiOperation("修改群信息")
    @PostMapping(value = "/update")
    public HttpResponseResult<Boolean> update(@Valid @RequestBody ImGroup imGroup) throws Exception {
        return succeed(imGroupService.updateById(imGroup));
    }
}

